|
| 1 | +/** |
| 2 | + * Provides classes for working with Angular (also known as Angular 2.x) applications. |
| 3 | + */ |
| 4 | + |
| 5 | +private import javascript |
| 6 | +private import semmle.javascript.security.dataflow.Xss |
| 7 | +private import semmle.javascript.security.dataflow.CodeInjectionCustomizations |
| 8 | +private import semmle.javascript.security.dataflow.ClientSideUrlRedirectCustomizations |
| 9 | +private import semmle.javascript.DynamicPropertyAccess |
| 10 | + |
| 11 | +/** |
| 12 | + * Provides classes for working with Angular (also known as Angular 2.x) applications. |
| 13 | + */ |
| 14 | +module Angular2 { |
| 15 | + /** Gets a reference to a `Router` object. */ |
| 16 | + DataFlow::SourceNode router() { result.hasUnderlyingType("@angular/router", "Router") } |
| 17 | + |
| 18 | + /** Gets a reference to a `RouterState` object. */ |
| 19 | + DataFlow::SourceNode routerState() { |
| 20 | + result.hasUnderlyingType("@angular/router", "RouterState") |
| 21 | + or |
| 22 | + result = router().getAPropertyRead("routerState") |
| 23 | + } |
| 24 | + |
| 25 | + /** Gets a reference to a `RouterStateSnapshot` object. */ |
| 26 | + DataFlow::SourceNode routerStateSnapshot() { |
| 27 | + result.hasUnderlyingType("@angular/router", "RouterStateSnapshot") |
| 28 | + or |
| 29 | + result = routerState().getAPropertyRead("snapshot") |
| 30 | + } |
| 31 | + |
| 32 | + /** Gets a reference to an `ActivatedRoute` object. */ |
| 33 | + DataFlow::SourceNode activatedRoute() { |
| 34 | + result.hasUnderlyingType("@angular/router", "ActivatedRoute") |
| 35 | + } |
| 36 | + |
| 37 | + /** Gets a reference to an `ActivatedRouteSnapshot` object. */ |
| 38 | + DataFlow::SourceNode activatedRouteSnapshot() { |
| 39 | + result.hasUnderlyingType("@angular/router", "ActivatedRouteSnapshot") |
| 40 | + or |
| 41 | + result = activatedRoute().getAPropertyRead("snapshot") |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Gets a data flow node referring to the value of the route property `name`, accessed |
| 46 | + * via one of the following patterns: |
| 47 | + * ```js |
| 48 | + * route.snapshot.name |
| 49 | + * route.snapshot.data.name |
| 50 | + * route.name.subscribe(x => ...) |
| 51 | + * ``` |
| 52 | + */ |
| 53 | + DataFlow::SourceNode activatedRouteProp(string name) { |
| 54 | + // this.route.snapshot.foo |
| 55 | + result = activatedRouteSnapshot().getAPropertyRead(name) |
| 56 | + or |
| 57 | + // this.route.snapshot.data.foo |
| 58 | + result = activatedRouteSnapshot().getAPropertyRead("data").getAPropertyRead(name) |
| 59 | + or |
| 60 | + // this.route.foo.subscribe(foo => { ... }) |
| 61 | + result = |
| 62 | + activatedRoute() |
| 63 | + .getAPropertyRead(name) |
| 64 | + .getAMethodCall("subscribe") |
| 65 | + .getABoundCallbackParameter(0, 0) |
| 66 | + } |
| 67 | + |
| 68 | + /** Gets an array of URL segments matched by some route. */ |
| 69 | + private DataFlow::SourceNode urlSegmentArray() { result = activatedRouteProp("url") } |
| 70 | + |
| 71 | + /** Gets a data flow node referring to a `UrlSegment` object matched by some route. */ |
| 72 | + DataFlow::SourceNode urlSegment() { |
| 73 | + result = getAnEnumeratedArrayElement(urlSegmentArray()) |
| 74 | + or |
| 75 | + result = urlSegmentArray().getAPropertyRead(any(string s | exists(s.toInt()))) |
| 76 | + } |
| 77 | + |
| 78 | + /** Gets a reference to a `ParamMap` object, usually containing values from the URL. */ |
| 79 | + DataFlow::SourceNode paramMap() { |
| 80 | + result.hasUnderlyingType("@angular/router", "ParamMap") |
| 81 | + or |
| 82 | + result = activatedRouteProp(["paramMap", "queryParamMap"]) |
| 83 | + or |
| 84 | + result = urlSegment().getAPropertyRead("parameterMap") |
| 85 | + } |
| 86 | + |
| 87 | + /** Gets a reference to a `Params` object, usually containing values from the URL. */ |
| 88 | + DataFlow::SourceNode paramDictionaryObject() { |
| 89 | + result.hasUnderlyingType("@angular/router", "Params") and |
| 90 | + not result instanceof DataFlow::ObjectLiteralNode // ignore object literals found by contextual typing |
| 91 | + or |
| 92 | + result = activatedRouteProp(["params", "queryParams"]) |
| 93 | + or |
| 94 | + result = paramMap().getAPropertyRead("params") |
| 95 | + or |
| 96 | + result = urlSegment().getAPropertyRead("parameters") |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * A value from `@angular/router` derived from the URL. |
| 101 | + */ |
| 102 | + class AngularSource extends RemoteFlowSource { |
| 103 | + AngularSource() { |
| 104 | + this = paramMap().getAMethodCall(["get", "getAll"]) |
| 105 | + or |
| 106 | + this = paramDictionaryObject() |
| 107 | + or |
| 108 | + this = activatedRouteProp("fragment") |
| 109 | + or |
| 110 | + this = urlSegment().getAPropertyRead("path") |
| 111 | + or |
| 112 | + // Note that Router.url and RouterStateSnapshot.url are strings, not UrlSegment[] |
| 113 | + this = router().getAPropertyRead("url") |
| 114 | + or |
| 115 | + this = routerStateSnapshot().getAPropertyRead("url") |
| 116 | + } |
| 117 | + |
| 118 | + override string getSourceType() { result = "Angular route parameter" } |
| 119 | + } |
| 120 | + |
| 121 | + /** Gets a reference to a `DomSanitizer` object. */ |
| 122 | + DataFlow::SourceNode domSanitizer() { |
| 123 | + result.hasUnderlyingType("@angular/platform-browser", "DomSanitizer") |
| 124 | + } |
| 125 | + |
| 126 | + /** A value that is about to be promoted to a trusted HTML or CSS value. */ |
| 127 | + private class AngularXssSink extends DomBasedXss::Sink { |
| 128 | + AngularXssSink() { |
| 129 | + this = |
| 130 | + domSanitizer() |
| 131 | + .getAMethodCall(["bypassSecurityTrustHtml", "bypassSecurityTrustStyle"]) |
| 132 | + .getArgument(0) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** A value that is about to be promoted to a trusted script value. */ |
| 137 | + private class AngularCodeInjectionSink extends CodeInjection::Sink { |
| 138 | + AngularCodeInjectionSink() { |
| 139 | + this = domSanitizer().getAMethodCall(["bypassSecurityTrustScript"]).getArgument(0) |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * A value that is about to be promoted to a trusted URL or resource URL value. |
| 145 | + */ |
| 146 | + private class AngularUrlSink extends ClientSideUrlRedirect::Sink { |
| 147 | + // We mark this as a client URL redirect sink for precision reasons, though its description can be a bit confusing. |
| 148 | + AngularUrlSink() { |
| 149 | + this = |
| 150 | + domSanitizer() |
| 151 | + .getAMethodCall(["bypassSecurityTrustUrl", "bypassSecurityTrustResourceUrl"]) |
| 152 | + .getArgument(0) |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private predicate taintStep(DataFlow::Node pred, DataFlow::Node succ) { |
| 157 | + exists(DataFlow::CallNode call | |
| 158 | + call = DataFlow::moduleMember("@angular/router", "convertToParamMap").getACall() |
| 159 | + or |
| 160 | + call = router().getAMemberCall(["parseUrl", "serializeUrl"]) |
| 161 | + | |
| 162 | + pred = call.getArgument(0) and |
| 163 | + succ = call |
| 164 | + ) |
| 165 | + } |
| 166 | + |
| 167 | + private class AngularTaintStep extends TaintTracking::AdditionalTaintStep { |
| 168 | + AngularTaintStep() { taintStep(_, this) } |
| 169 | + |
| 170 | + override predicate step(DataFlow::Node pred, DataFlow::Node succ) { taintStep(pred, succ) } |
| 171 | + } |
| 172 | + |
| 173 | + /** Gets a reference to an `HttpClient` object. */ |
| 174 | + DataFlow::SourceNode httpClient() { |
| 175 | + result.hasUnderlyingType("@angular/common/http", "HttpClient") |
| 176 | + } |
| 177 | + |
| 178 | + private class AngularClientRequest extends ClientRequest::Range, DataFlow::MethodCallNode { |
| 179 | + int argumentOffset; |
| 180 | + |
| 181 | + AngularClientRequest() { |
| 182 | + this = httpClient().getAMethodCall("request") and argumentOffset = 1 |
| 183 | + or |
| 184 | + this = httpClient().getAMethodCall() and |
| 185 | + not getMethodName() = "request" and |
| 186 | + argumentOffset = 0 |
| 187 | + } |
| 188 | + |
| 189 | + override DataFlow::Node getUrl() { result = getArgument(argumentOffset) } |
| 190 | + |
| 191 | + override DataFlow::Node getHost() { none() } |
| 192 | + |
| 193 | + override DataFlow::Node getADataNode() { |
| 194 | + getMethodName() = ["patch", "post", "put"] and |
| 195 | + result = getArgument(argumentOffset + 1) |
| 196 | + or |
| 197 | + result = getOptionArgument(argumentOffset + 1, "body") |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + private string getInternalName(string name) { |
| 202 | + exists(Identifier id | |
| 203 | + result = id.getName() and |
| 204 | + name = result.regexpCapture("\\u0275(DomAdapter|getDOM)", 1) |
| 205 | + ) |
| 206 | + } |
| 207 | + |
| 208 | + /** Gets a reference to a `DomAdapter`, which provides acess to raw DOM elements. */ |
| 209 | + private DataFlow::SourceNode domAdapter() { |
| 210 | + // Note: these are internal properties, prefixed with the "latin small letter barred O (U+0275)" character. |
| 211 | + // Despite being internal, some codebases do access them. |
| 212 | + result.hasUnderlyingType("@angular/common", getInternalName("DomAdapter")) |
| 213 | + or |
| 214 | + result = DataFlow::moduleImport("@angular/common").getAMemberCall(getInternalName("getDOM")) |
| 215 | + } |
| 216 | + |
| 217 | + /** A reference to the DOM location obtained through `DomAdapter.getLocation()`. */ |
| 218 | + private class DomAdapterLocation extends DOM::LocationSource::Range { |
| 219 | + DomAdapterLocation() { this = domAdapter().getAMethodCall("getLocation") } |
| 220 | + } |
| 221 | +} |
0 commit comments