diff --git a/csharp/src/security/CWE-862/WebMethodActions.qll b/csharp/src/security/CWE-862/WebMethodActions.qll new file mode 100644 index 00000000..0f9ac819 --- /dev/null +++ b/csharp/src/security/CWE-862/WebMethodActions.qll @@ -0,0 +1,32 @@ +/** + * Provides a model of ASP.NET web methods (methods annotated with + * `[System.Web.Services.WebMethod]`) as action methods for access control queries. + */ + +import csharp +private import semmle.code.csharp.frameworks.system.web.Services +private import semmle.code.csharp.security.auth.ActionMethods + +/** + * A method annotated with `[System.Web.Services.WebMethod]`, which is remotely invocable + * either as an ASMX web service operation or as an ASP.NET page method. + */ +class WebMethodActionMethod extends ActionMethod { + WebMethodActionMethod() { + this.getAnAttribute().getType() instanceof SystemWebServicesWebMethodAttributeClass + } + + /** + * Holds if this method may represent a stateful action. In addition to the generic + * edit-like names, web methods whose names contain `transfer`, `withdraw`, `deposit`, + * `update` or `remove` as a whole camelCase word are considered stateful. + */ + override predicate isEdit() { + super.isEdit() + or + this.getName() + // separate camelCase words + .regexpReplaceAll("([a-z])([A-Z])", "$1_$2") + .regexpMatch("(?i)(.*_)?(transfer|withdraw|deposit|update|remove)(_.*)?") + } +} diff --git a/csharp/src/security/CWE-862/WebMethodMissingAccessControl.qhelp b/csharp/src/security/CWE-862/WebMethodMissingAccessControl.qhelp new file mode 100644 index 00000000..ca739c60 --- /dev/null +++ b/csharp/src/security/CWE-862/WebMethodMissingAccessControl.qhelp @@ -0,0 +1,95 @@ + + + + +

+A method marked with [System.Web.Services.WebMethod] is an ASMX web service operation +or, when it is a static method of a page, an ASP.NET page method. Any client that can reach the +service can call it. A web method that changes data, such as one that transfers, withdraws, +deposits, updates, removes, edits or deletes something, or that performs an administrative action, +should check that the caller is allowed to do so. +

+

+This query reports such a web method when it has no authorization check in its code, no +authorization attribute that applies to it, and no <location> rule in a +Web.config file that denies access to it. The standard query +cs/web/missing-function-level-access-control does not consider web methods. +

+

+The MVC and Web API Authorize attributes (System.Web.Mvc.AuthorizeAttribute +and System.Web.Http.AuthorizeAttribute) are filters of their own pipelines. They do +not run for a web method, so they do not protect it. +

+ +
+ + +

+Check that the caller is authenticated and authorized before changing data, for example with +Context.User.Identity.IsAuthenticated or Context.User.IsInRole(...). Alternatively, +deny access to unauthorized users with an authorization element in a +<location> element of Web.config that covers the service. +

+ +
+ + +

+In the following example, any caller can transfer funds between arbitrary accounts: +

+ + + +

+The method is fixed by checking that the caller is authenticated (and, ideally, owns the debited +account): +

+ + + +

+The following Web.config file denies anonymous users access to the service: +

+ + + + + + + + + + +]]> + +
+ +
  • OWASP: Broken Access Control.
  • +
  • Microsoft Learn: WebMethodAttribute Class.
  • +
  • Microsoft Learn: Control authorization permissions in an ASP.NET application.
  • +
  • Common Weakness Enumeration: CWE-285.
  • +
  • Common Weakness Enumeration: CWE-284.
  • +
  • Common Weakness Enumeration: CWE-862.
  • +
    +
    diff --git a/csharp/src/security/CWE-862/WebMethodMissingAccessControl.ql b/csharp/src/security/CWE-862/WebMethodMissingAccessControl.ql new file mode 100644 index 00000000..864fa20b --- /dev/null +++ b/csharp/src/security/CWE-862/WebMethodMissingAccessControl.ql @@ -0,0 +1,53 @@ +/** + * @name Missing function level access control on a web method + * @description A web method that changes data or performs an administrative action + * without an authorization check can be invoked by any caller. + * @kind problem + * @problem.severity warning + * @security-severity 7.5 + * @precision medium + * @id githubsecuritylab/cs/web/web-method-missing-access-control + * @tags security + * experimental + * external/cwe/cwe-285 + * external/cwe/cwe-284 + * external/cwe/cwe-862 + */ + +import csharp +import semmle.code.csharp.security.auth.MissingFunctionLevelAccessControlQuery +import WebMethodActions + +private ValueOrRefType getAnUnboundBaseType(ValueOrRefType t) { + result = t.getABaseType().getUnboundDeclaration() +} + +/** + * Holds if `attr` is an MVC or Web API authorization filter. These filters run only in + * their own pipelines, so they do not protect a web method. + */ +private predicate isPipelineAuthorizeAttribute(Attribute attr) { + attr.getType() + .getABaseType*() + .hasFullyQualifiedName(["System.Web.Mvc", "System.Web.Http"], "AuthorizeAttribute") +} + +/** Holds if `m` has an authorization attribute that applies to a web method. */ +private predicate hasWebMethodAuthViaAttribute(WebMethodActionMethod m) { + exists(Attribute attr | + attr.getType().getABaseType*().getName().toLowerCase().matches("%auth%") and + not isPipelineAuthorizeAttribute(attr) + | + attr = m.getOverridee*().getAnAttribute() or + attr = getAnUnboundBaseType*(m.getDeclaringType()).getAnAttribute() + ) +} + +from WebMethodActionMethod m +where + needsAuth(m) and + not hasAuthViaCode(m) and + not hasAuthViaXml(m) and + not hasWebMethodAuthViaAttribute(m) and + exists(m.getBody().getAChildStmt()) // exclude empty methods +select m, "This web method is missing an authorization check." diff --git a/csharp/test/security/CWE-862/WebMethodMissingAccessControl/AccountService.asmx.cs b/csharp/test/security/CWE-862/WebMethodMissingAccessControl/AccountService.asmx.cs new file mode 100644 index 00000000..2ed96730 --- /dev/null +++ b/csharp/test/security/CWE-862/WebMethodMissingAccessControl/AccountService.asmx.cs @@ -0,0 +1,142 @@ +using System.Web.Mvc; +using System.Web.Services; + +namespace Bank.WebServices +{ + public class RequireAuthorizationAttribute : System.Attribute { } + + [WebService(Namespace = "http://example.com/")] + public class AccountService : WebService + { + // BAD: state-changing web method without any authorization check. + [WebMethod] + public string TransferBalance(long from, long to, double amount) + { + Withdraw(from, amount); + Deposit(to, amount); + return "OK"; + } + + // BAD: edit operation exposed as a web method without authorization. + [WebMethod] + public void DeleteAccount(long accountId) + { + Remove(accountId); + } + + // BAD: `update` is a whole camelCase word of the name. + [WebMethod] + public void UpdateEmail(long accountId, string email) + { + System.Console.WriteLine(accountId + email); + } + + // GOOD: checks that the caller is authenticated. + [WebMethod] + public string TransferBalanceChecked(long from, long to, double amount) + { + if (!Context.User.Identity.IsAuthenticated) + return "Unauthorized"; + Withdraw(from, amount); + Deposit(to, amount); + return "OK"; + } + + // GOOD: checks the caller's role. + [WebMethod] + public void DeleteAccountChecked(long accountId) + { + if (Context.User.IsInRole("Admin")) + Remove(accountId); + } + + // GOOD: an authorization attribute that is not an MVC or Web API filter. + [WebMethod] + [RequireAuthorization] + public void WithdrawAll(long accountId) + { + Withdraw(accountId, 0); + } + + // BAD: MVC authorization attributes do not protect ASMX operations. + [WebMethod] + [Authorize] + public string TransferBalanceAuthorized(long from, long to, double amount) + { + Withdraw(from, amount); + Deposit(to, amount); + return "OK"; + } + + // BAD: Web API authorization attributes do not protect ASMX operations. + [WebMethod] + [System.Web.Http.Authorize] + public void DepositAll(long accountId) + { + Deposit(accountId, 0); + } + + // GOOD: read-only operation, the query only targets sensitive (state-changing / admin) actions. + [WebMethod] + public long[] GetUserAccounts(long userId) + { + return new long[] { userId }; + } + + // GOOD: `updated` is not the whole word `update`. + [WebMethod] + public string GetLastUpdated(long accountId) + { + return accountId.ToString(); + } + + // GOOD: not a web method, so not remotely invocable. + public void TransferInternal(long from, long to, double amount) + { + Withdraw(from, amount); + Deposit(to, amount); + } + + // GOOD: empty web method. + [WebMethod] + public void TransferNothing() { } + + private void Withdraw(long account, double amount) { } + + private void Deposit(long account, double amount) { } + + private void Remove(long account) { } + } + + [Authorize] + public class AdminService : WebService + { + // BAD: MVC authorization attributes do not protect ASMX operations. + [WebMethod] + public void DeleteUser(long userId) + { + System.Console.WriteLine(userId); + } + } + + [RequireAuthorization] + public class ProtectedService : WebService + { + // GOOD: the class has an authorization attribute that applies to its web methods. + [WebMethod] + public void RemoveUser(long userId) + { + System.Console.WriteLine(userId); + } + } + + public class TransfersPage : System.Web.UI.Page + { + // BAD: ASP.NET page method (static [WebMethod]) without authorization. + [WebMethod] + public static void TransferFunds(long from, long to, double amount) + { + System.Console.WriteLine(from + to + amount); + } + } +} diff --git a/csharp/test/security/CWE-862/WebMethodMissingAccessControl/App_Code/WebService.cs b/csharp/test/security/CWE-862/WebMethodMissingAccessControl/App_Code/WebService.cs new file mode 100644 index 00000000..d4d11698 --- /dev/null +++ b/csharp/test/security/CWE-862/WebMethodMissingAccessControl/App_Code/WebService.cs @@ -0,0 +1,54 @@ +namespace Bank +{ + using System; + using System.Web.Services; + + public struct TransferResult + { + public bool Success; + public string Message; + } + + // Variant: SOAP service in App_Code with a fully qualified base type and + // `[WebMethod(Description = ...)]` named arguments, taking a request object. + [WebService(Namespace = "http://example.com/bank/ws/", Description = "Core bank services.")] + public class Services : System.Web.Services.WebService + { + public class MoneyTransfer + { + public string debitAccount; + public string creditAccount; + public double transferAmount; + + public bool setAmount(double amt) + { + this.transferAmount = amt; + return true; + } + } + + // GOOD: read-only lookup (not considered sensitive by name). + [WebMethod(Description = "Determine if a UserId is valid and exists.")] + public bool IsValidUser(string userId) + { + return userId.Length > 0; + } + + // GOOD: read-only lookup (not considered sensitive by name). + [WebMethod(Description = "Retrieve accounts by UserId.")] + public int[] GetUserAccounts(int userId) + { + return new int[] { userId }; + } + + // BAD: anonymous callers can move funds between arbitrary accounts. + [WebMethod(Description = "Transfer funds from one account to another.")] + public TransferResult TransferBalance(MoneyTransfer transDetails) + { + TransferResult result = new TransferResult(); + result.Success = transDetails.debitAccount != transDetails.creditAccount; + result.Message = "$" + transDetails.transferAmount + " transferred."; + return result; + } + } +} diff --git a/csharp/test/security/CWE-862/WebMethodMissingAccessControl/Stubs.cs b/csharp/test/security/CWE-862/WebMethodMissingAccessControl/Stubs.cs new file mode 100644 index 00000000..6b690939 --- /dev/null +++ b/csharp/test/security/CWE-862/WebMethodMissingAccessControl/Stubs.cs @@ -0,0 +1,45 @@ +// Minimal stubs for the ASP.NET (System.Web) types used by the tests. +namespace System.Web +{ + public class HttpContext + { + public System.Security.Principal.IPrincipal User => throw null; + } +} + +namespace System.Web.Services +{ + public class WebMethodAttribute : System.Attribute + { + public string Description { get; set; } + } + + public class WebServiceAttribute : System.Attribute + { + public string Namespace { get; set; } + public string Description { get; set; } + } + + public class WebService + { + public System.Web.HttpContext Context => throw null; + } +} + +namespace System.Web.UI +{ + public class Page + { + public System.Web.HttpContext Context => throw null; + } +} + +namespace System.Web.Mvc +{ + public class AuthorizeAttribute : System.Attribute { } +} + +namespace System.Web.Http +{ + public class AuthorizeAttribute : System.Attribute { } +} diff --git a/csharp/test/security/CWE-862/WebMethodMissingAccessControl/WebConfig/ConfiguredService.asmx.cs b/csharp/test/security/CWE-862/WebMethodMissingAccessControl/WebConfig/ConfiguredService.asmx.cs new file mode 100644 index 00000000..e9d80cd7 --- /dev/null +++ b/csharp/test/security/CWE-862/WebMethodMissingAccessControl/WebConfig/ConfiguredService.asmx.cs @@ -0,0 +1,16 @@ +using System.Web.Mvc; +using System.Web.Services; + +namespace Bank.WebServices +{ + [Authorize] + public class ConfiguredService : WebService + { + // GOOD: the `` rule in Web.config denies anonymous users. + [WebMethod] + public void TransferFunds() + { + System.Console.WriteLine("transfer"); + } + } +} diff --git a/csharp/test/security/CWE-862/WebMethodMissingAccessControl/WebConfig/UnconfiguredService.asmx.cs b/csharp/test/security/CWE-862/WebMethodMissingAccessControl/WebConfig/UnconfiguredService.asmx.cs new file mode 100644 index 00000000..fad47c9b --- /dev/null +++ b/csharp/test/security/CWE-862/WebMethodMissingAccessControl/WebConfig/UnconfiguredService.asmx.cs @@ -0,0 +1,14 @@ +using System.Web.Services; + +namespace Bank.WebServices +{ + public class UnconfiguredService : WebService + { + // BAD: no `` rule in Web.config covers this service. + [WebMethod] + public void TransferFunds() + { + System.Console.WriteLine("transfer"); + } + } +} diff --git a/csharp/test/security/CWE-862/WebMethodMissingAccessControl/WebConfig/Web.config b/csharp/test/security/CWE-862/WebMethodMissingAccessControl/WebConfig/Web.config new file mode 100644 index 00000000..973f1d94 --- /dev/null +++ b/csharp/test/security/CWE-862/WebMethodMissingAccessControl/WebConfig/Web.config @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/csharp/test/security/CWE-862/WebMethodMissingAccessControl/WebMethodMissingAccessControl.expected b/csharp/test/security/CWE-862/WebMethodMissingAccessControl/WebMethodMissingAccessControl.expected new file mode 100644 index 00000000..a91c0533 --- /dev/null +++ b/csharp/test/security/CWE-862/WebMethodMissingAccessControl/WebMethodMissingAccessControl.expected @@ -0,0 +1,9 @@ +| AccountService.asmx.cs:13:23:13:37 | TransferBalance | This web method is missing an authorization check. | +| AccountService.asmx.cs:22:21:22:33 | DeleteAccount | This web method is missing an authorization check. | +| AccountService.asmx.cs:29:21:29:31 | UpdateEmail | This web method is missing an authorization check. | +| AccountService.asmx.cs:64:23:64:47 | TransferBalanceAuthorized | This web method is missing an authorization check. | +| AccountService.asmx.cs:74:21:74:30 | DepositAll | This web method is missing an authorization check. | +| AccountService.asmx.cs:116:21:116:30 | DeleteUser | This web method is missing an authorization check. | +| AccountService.asmx.cs:137:28:137:40 | TransferFunds | This web method is missing an authorization check. | +| App_Code/WebService.cs:46:31:46:45 | TransferBalance | This web method is missing an authorization check. | +| WebConfig/UnconfiguredService.asmx.cs:9:21:9:33 | TransferFunds | This web method is missing an authorization check. | diff --git a/csharp/test/security/CWE-862/WebMethodMissingAccessControl/WebMethodMissingAccessControl.qlref b/csharp/test/security/CWE-862/WebMethodMissingAccessControl/WebMethodMissingAccessControl.qlref new file mode 100644 index 00000000..d854f95f --- /dev/null +++ b/csharp/test/security/CWE-862/WebMethodMissingAccessControl/WebMethodMissingAccessControl.qlref @@ -0,0 +1 @@ +query: security/CWE-862/WebMethodMissingAccessControl.ql diff --git a/csharp/test/security/CWE-862/WebMethodMissingAccessControl/options b/csharp/test/security/CWE-862/WebMethodMissingAccessControl/options new file mode 100644 index 00000000..29bc31c8 --- /dev/null +++ b/csharp/test/security/CWE-862/WebMethodMissingAccessControl/options @@ -0,0 +1,2 @@ +semmle-extractor-options: /nostdlib /noconfig +semmle-extractor-options: --load-sources-from-project:${testdir}/../../../../../codeql/csharp/ql/test/resources/stubs/_frameworks/Microsoft.NETCore.App/Microsoft.NETCore.App.csproj