|
| 1 | +/** Definitions for the missing function level access control query */ |
| 2 | + |
| 3 | +import csharp |
| 4 | +import semmle.code.csharp.frameworks.microsoft.AspNetCore |
| 5 | +import semmle.code.csharp.frameworks.system.web.UI |
| 6 | +import semmle.code.asp.WebConfig |
| 7 | + |
| 8 | +/** A method representing an action for a web endpoint. */ |
| 9 | +abstract class ActionMethod extends Method { |
| 10 | + /** |
| 11 | + * Gets a string that can indicate what this method does to determine if it should have an auth check; |
| 12 | + * such as its method name, class name, or file path. |
| 13 | + */ |
| 14 | + string getADescription() { |
| 15 | + result = |
| 16 | + [ |
| 17 | + this.getName(), this.getDeclaringType().getBaseClass*().getName(), |
| 18 | + this.getDeclaringType().getFile().getRelativePath() |
| 19 | + ] |
| 20 | + } |
| 21 | + |
| 22 | + /** Holds if this method may need an authorization check. */ |
| 23 | + predicate needsAuth() { |
| 24 | + this.getADescription() |
| 25 | + .regexpReplaceAll("([a-z])([A-Z])", "$1_$2") |
| 26 | + // separate camelCase words |
| 27 | + .toLowerCase() |
| 28 | + .regexpMatch(".*(edit|delete|modify|admin|superuser).*") |
| 29 | + } |
| 30 | + |
| 31 | + /** Gets a callable for which if it contains an auth check, this method should be considered authenticated. */ |
| 32 | + Callable getAnAuthorizingCallable() { result = this } |
| 33 | + |
| 34 | + /** |
| 35 | + * Gets a possible url route that could refer to this action, |
| 36 | + * which would be covered by `<location>` configurations specifying a prefix of it. |
| 37 | + */ |
| 38 | + string getARoute() { result = this.getDeclaringType().getFile().getRelativePath() } |
| 39 | +} |
| 40 | + |
| 41 | +/** An action method in the MVC framework. */ |
| 42 | +private class MvcActionMethod extends ActionMethod { |
| 43 | + MvcActionMethod() { this = any(MicrosoftAspNetCoreMvcController c).getAnActionMethod() } |
| 44 | +} |
| 45 | + |
| 46 | +/** An action method on a subclass of `System.Web.UI.Page`. */ |
| 47 | +private class WebFormActionMethod extends ActionMethod { |
| 48 | + WebFormActionMethod() { |
| 49 | + this.getDeclaringType().getBaseClass+() instanceof SystemWebUIPageClass and |
| 50 | + this.getAParameter().getType().getName().matches("%EventArgs") |
| 51 | + } |
| 52 | + |
| 53 | + override Callable getAnAuthorizingCallable() { |
| 54 | + result = super.getAnAuthorizingCallable() |
| 55 | + or |
| 56 | + result.getDeclaringType() = this.getDeclaringType() and |
| 57 | + result.getName() = "Page_Load" |
| 58 | + } |
| 59 | + |
| 60 | + override string getARoute() { |
| 61 | + exists(string physicalRoute | physicalRoute = super.getARoute() | |
| 62 | + result = physicalRoute |
| 63 | + or |
| 64 | + exists(string absolutePhysical | |
| 65 | + virtualRouteMapping(result, absolutePhysical) and |
| 66 | + physicalRouteMatches(absolutePhysical, physicalRoute) |
| 67 | + ) |
| 68 | + ) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Holds if `virtualRoute` is a URL path |
| 74 | + * that can map to the corresponding `physicalRoute` filepath |
| 75 | + * through a call to `MapPageRoute` |
| 76 | + */ |
| 77 | +private predicate virtualRouteMapping(string virtualRoute, string physicalRoute) { |
| 78 | + exists(MethodCall mapPageRouteCall, StringLiteral virtualLit, StringLiteral physicalLit | |
| 79 | + mapPageRouteCall |
| 80 | + .getTarget() |
| 81 | + .hasQualifiedName("System.Web.Routing", "RouteCollection", "MapPageRoute") and |
| 82 | + virtualLit = mapPageRouteCall.getArgument(1) and |
| 83 | + physicalLit = mapPageRouteCall.getArgument(2) and |
| 84 | + virtualLit.getValue() = virtualRoute and |
| 85 | + physicalLit.getValue() = physicalRoute |
| 86 | + ) |
| 87 | +} |
| 88 | + |
| 89 | +/** Holds if the filepath `route` can refer to `actual` after expanding a '~". */ |
| 90 | +bindingset[route, actual] |
| 91 | +private predicate physicalRouteMatches(string route, string actual) { |
| 92 | + route = actual |
| 93 | + or |
| 94 | + route.charAt(0) = "~" and |
| 95 | + exists(string dir | actual = dir + route.suffix(1) + ".cs") |
| 96 | +} |
| 97 | + |
| 98 | +/** An expression that indicates that some authorization/authentication check is being performed. */ |
| 99 | +class AuthExpr extends Expr { |
| 100 | + AuthExpr() { |
| 101 | + this.(MethodCall) |
| 102 | + .getTarget() |
| 103 | + .hasQualifiedName("System.Security.Principal", "IPrincipal", "IsInRole") |
| 104 | + or |
| 105 | + this.(PropertyAccess) |
| 106 | + .getTarget() |
| 107 | + .hasQualifiedName("System.Security.Principal", "IIdentity", ["IsAuthenticated", "Name"]) |
| 108 | + or |
| 109 | + this.(MethodCall).getTarget().getName().toLowerCase().matches("%auth%") |
| 110 | + or |
| 111 | + this.(PropertyAccess).getTarget().getName().toLowerCase().matches("%auth%") |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/** Holds if `m` is a method that should have an auth check, and does indeed have one. */ |
| 116 | +predicate hasAuthViaCode(ActionMethod m) { |
| 117 | + m.needsAuth() and |
| 118 | + exists(Callable caller, AuthExpr auth | |
| 119 | + m.getAnAuthorizingCallable().calls*(caller) and |
| 120 | + auth.getEnclosingCallable() = caller |
| 121 | + ) |
| 122 | +} |
| 123 | + |
| 124 | +/** An `<authorization>` XML element. */ |
| 125 | +class AuthorizationXmlElement extends XmlElement { |
| 126 | + AuthorizationXmlElement() { |
| 127 | + this.getParent() instanceof SystemWebXmlElement and |
| 128 | + this.getName().toLowerCase() = "authorization" |
| 129 | + } |
| 130 | + |
| 131 | + /** Holds if this element has a `<deny>` element to deny access to a resource. */ |
| 132 | + predicate hasDenyElement() { this.getAChild().getName().toLowerCase() = "deny" } |
| 133 | + |
| 134 | + /** Gets the physical filepath of this element. */ |
| 135 | + string getPhysicalPath() { result = this.getFile().getParentContainer().getRelativePath() } |
| 136 | + |
| 137 | + /** Gets the path specified by a `<location>` tag containing this element, if any. */ |
| 138 | + string getLocationTagPath() { |
| 139 | + exists(LocationXmlElement loc, XmlAttribute path | |
| 140 | + loc = this.getParent().(SystemWebXmlElement).getParent() and |
| 141 | + path = loc.getAnAttribute() and |
| 142 | + path.getName().toLowerCase() = "path" and |
| 143 | + result = path.getValue() |
| 144 | + ) |
| 145 | + } |
| 146 | + |
| 147 | + /** Gets a route prefix that this configuration can refer to. */ |
| 148 | + string getARoute() { |
| 149 | + result = this.getLocationTagPath() |
| 150 | + or |
| 151 | + result = this.getPhysicalPath() + "/" + this.getLocationTagPath() |
| 152 | + or |
| 153 | + not exists(this.getLocationTagPath()) and |
| 154 | + result = this.getPhysicalPath() |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +/** |
| 159 | + * Holds if the given action has an xml `authorization` tag that refers to it. |
| 160 | + */ |
| 161 | +predicate hasAuthViaXml(ActionMethod m) { |
| 162 | + exists(AuthorizationXmlElement el, string rest | |
| 163 | + el.hasDenyElement() and |
| 164 | + m.getARoute() = el.getARoute() + rest |
| 165 | + ) |
| 166 | +} |
| 167 | + |
| 168 | +/** Holds if the given action has an attribute that indications authorization. */ |
| 169 | +predicate hasAuthViaAttribute(ActionMethod m) { |
| 170 | + exists(Attribute attr | attr.getType().getName().toLowerCase().matches("%auth%") | |
| 171 | + attr = m.getAnAttribute() or |
| 172 | + attr = m.getDeclaringType().getABaseType*().getAnAttribute() |
| 173 | + ) |
| 174 | +} |
| 175 | + |
| 176 | +/** Holds if `m` is a method that should have an auth check, but is missing it. */ |
| 177 | +predicate missingAuth(ActionMethod m) { |
| 178 | + m.needsAuth() and |
| 179 | + not hasAuthViaCode(m) and |
| 180 | + not hasAuthViaXml(m) and |
| 181 | + not hasAuthViaAttribute(m) and |
| 182 | + exists(m.getBody().getAChildStmt()) // exclude empty methods |
| 183 | +} |
0 commit comments