|
| 1 | +/** |
| 2 | + * @name Incomplete URL scheme check |
| 3 | + * @description Checking for the "javascript:" URL scheme without also checking for "vbscript:" |
| 4 | + * and "data:" suggests a logic error or even a security vulnerability. |
| 5 | + * @kind problem |
| 6 | + * @problem.severity warning |
| 7 | + * @precision high |
| 8 | + * @id js/incomplete-url-scheme-check |
| 9 | + * @tags security |
| 10 | + * correctness |
| 11 | + * external/cwe/cwe-020 |
| 12 | + */ |
| 13 | + |
| 14 | +import javascript |
| 15 | +import semmle.javascript.dataflow.internal.AccessPaths |
| 16 | + |
| 17 | +/** A URL scheme that can be used to represent executable code. */ |
| 18 | +class DangerousScheme extends string { |
| 19 | + DangerousScheme() { this = "data:" or this = "javascript:" or this = "vbscript:" } |
| 20 | +} |
| 21 | + |
| 22 | +/** Gets a data-flow node that checks `nd` against the given `scheme`. */ |
| 23 | +DataFlow::Node schemeCheck( |
| 24 | + DataFlow::Node nd, DangerousScheme scheme |
| 25 | +) { |
| 26 | + // check of the form `nd.startsWith(scheme)` |
| 27 | + exists(StringOps::StartsWith sw | sw = result | |
| 28 | + sw.getBaseString() = nd and |
| 29 | + sw.getSubstring().mayHaveStringValue(scheme) |
| 30 | + ) |
| 31 | + or |
| 32 | + // propagate through trimming, case conversion, and regexp replace |
| 33 | + exists(DataFlow::MethodCallNode stringop | |
| 34 | + stringop.getMethodName().matches("trim%") or |
| 35 | + stringop.getMethodName().matches("to%Case") or |
| 36 | + stringop.getMethodName() = "replace" |
| 37 | + | |
| 38 | + result = schemeCheck(stringop, scheme) and |
| 39 | + nd = stringop.getReceiver() |
| 40 | + ) |
| 41 | + or |
| 42 | + // propagate through local data flow |
| 43 | + result = schemeCheck(nd.getASuccessor(), scheme) |
| 44 | +} |
| 45 | + |
| 46 | +/** Gets a data-flow node that checks an instance of `ap` against the given `scheme`. */ |
| 47 | +DataFlow::Node schemeCheckOn(AccessPath ap, DangerousScheme scheme) { |
| 48 | + result = schemeCheck(ap.getAnInstance().flow(), scheme) |
| 49 | +} |
| 50 | + |
| 51 | +from AccessPath ap, int n |
| 52 | +where |
| 53 | + n = strictcount(DangerousScheme s) and |
| 54 | + strictcount(DangerousScheme s | exists(schemeCheckOn(ap, s))) < n |
| 55 | +select schemeCheckOn(ap, "javascript:"), |
| 56 | + "This check does not consider " + |
| 57 | + strictconcat(DangerousScheme s | not exists(schemeCheckOn(ap, s)) | s, " and ") + "." |
0 commit comments