Skip to content

Commit 2c6bead

Browse files
committed
JS: Recognize more forms of scheme checks
1 parent 5034d40 commit 2c6bead

File tree

4 files changed

+108
-9
lines changed

4 files changed

+108
-9
lines changed

change-notes/1.24/analysis-javascript.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
| Useless regular-expression character escape (`js/useless-regexp-character-escape`) | Fewer false positive results | This query now distinguishes escapes in strings and regular expression literals. |
8787
| Identical operands (`js/redundant-operation`) | Fewer results | This query now recognizes cases where the operands change a value using ++/-- expressions. |
8888
| Superfluous trailing arguments (`js/superfluous-trailing-arguments`) | Fewer results | This query now recognizes cases where a function uses the `Function.arguments` value to process a variable number of parameters. |
89+
| Incomplete URL scheme check (`js/incomplete-url-scheme-check`) | More results | This query now recognizes more variations of URL scheme checks. |
8990

9091
## Changes to libraries
9192

javascript/ql/src/Security/CWE-020/IncompleteUrlSchemeCheck.ql

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,50 @@
1212
*/
1313

1414
import javascript
15-
import semmle.javascript.dataflow.internal.AccessPaths
1615

1716
/** A URL scheme that can be used to represent executable code. */
1817
class DangerousScheme extends string {
1918
DangerousScheme() { this = "data:" or this = "javascript:" or this = "vbscript:" }
19+
20+
/** Gets the name of this scheme without the `:`. */
21+
string getWithoutColon() {
22+
this = result + ":"
23+
}
24+
25+
/** Gets the name of this scheme, with or without the `:`. */
26+
string getWithOrWithoutColon() {
27+
result = this or result = getWithoutColon()
28+
}
29+
}
30+
31+
/** Returns a node that refers to the scheme of `url`. */
32+
DataFlow::SourceNode schemeOf(DataFlow::Node url) {
33+
// url.split(":")[0]
34+
exists(DataFlow::MethodCallNode split |
35+
split.getMethodName() = "split" and
36+
split.getArgument(0).getStringValue() = ":" and
37+
result = split.getAPropertyRead("0") and
38+
url = split.getReceiver()
39+
)
40+
or
41+
// url.getScheme(), url.getProtocol(), getScheme(url), getProtocol(url)
42+
exists(DataFlow::CallNode call |
43+
result = call and
44+
(call.getCalleeName() = "getScheme" or call.getCalleeName() = "getProtocol")
45+
|
46+
call.getNumArgument() = 1 and
47+
url = call.getArgument(0)
48+
or
49+
call.getNumArgument() = 0 and
50+
url = call.getReceiver()
51+
)
52+
or
53+
// url.scheme, url.protocol
54+
exists(DataFlow::PropRead prop |
55+
result = prop and
56+
(prop.getPropertyName() = "scheme" or prop.getPropertyName() = "protocol") and
57+
url = prop.getBase()
58+
)
2059
}
2160

2261
/** Gets a data-flow node that checks `nd` against the given `scheme`. */
@@ -27,6 +66,20 @@ DataFlow::Node schemeCheck(DataFlow::Node nd, DangerousScheme scheme) {
2766
sw.getSubstring().mayHaveStringValue(scheme)
2867
)
2968
or
69+
// check of the form `array.includes(getScheme(nd))`
70+
exists(InclusionTest test, DataFlow::ArrayCreationNode array | test = result |
71+
schemeOf(nd).flowsTo(test.getContainedNode()) and
72+
array.flowsTo(test.getContainerNode()) and
73+
array.getAnElement().mayHaveStringValue(scheme.getWithOrWithoutColon())
74+
)
75+
or
76+
// check of the form `getScheme(nd) === scheme`
77+
exists(EqualityTest test, Expr op1, Expr op2 | test.flow() = result |
78+
test.hasOperands(op1, op2) and
79+
schemeOf(nd).flowsToExpr(op1) and
80+
op2.mayHaveStringValue(scheme.getWithOrWithoutColon())
81+
)
82+
or
3083
// propagate through trimming, case conversion, and regexp replace
3184
exists(DataFlow::MethodCallNode stringop |
3285
stringop.getMethodName().matches("trim%") or
@@ -42,14 +95,14 @@ DataFlow::Node schemeCheck(DataFlow::Node nd, DangerousScheme scheme) {
4295
}
4396

4497
/** Gets a data-flow node that checks an instance of `ap` against the given `scheme`. */
45-
DataFlow::Node schemeCheckOn(AccessPath ap, DangerousScheme scheme) {
46-
result = schemeCheck(ap.getAnInstance().flow(), scheme)
98+
DataFlow::Node schemeCheckOn(DataFlow::SourceNode root, string path, DangerousScheme scheme) {
99+
result = schemeCheck(AccessPath::getAReferenceTo(root, path), scheme)
47100
}
48101

49-
from AccessPath ap, int n
102+
from DataFlow::SourceNode root, string path, int n
50103
where
51104
n = strictcount(DangerousScheme s) and
52-
strictcount(DangerousScheme s | exists(schemeCheckOn(ap, s))) < n
53-
select schemeCheckOn(ap, "javascript:"),
105+
strictcount(DangerousScheme s | exists(schemeCheckOn(root, path, s))) < n
106+
select schemeCheckOn(root, path, "javascript:"),
54107
"This check does not consider " +
55-
strictconcat(DangerousScheme s | not exists(schemeCheckOn(ap, s)) | s, " and ") + "."
108+
strictconcat(DangerousScheme s | not exists(schemeCheckOn(root, path, s)) | s, " and ") + "."
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
| IncompleteUrlSchemeCheck.js:3:9:3:35 | u.start ... ript:") | This check does not consider data: and vbscript:. |
1+
| IncompleteUrlSchemeCheck.js:5:9:5:35 | u.start ... ript:") | This check does not consider data: and vbscript:. |
2+
| IncompleteUrlSchemeCheck.js:16:9:16:39 | badProt ... otocol) | This check does not consider vbscript:. |
3+
| IncompleteUrlSchemeCheck.js:23:9:23:43 | badProt ... scheme) | This check does not consider vbscript:. |
4+
| IncompleteUrlSchemeCheck.js:30:9:30:43 | badProt ... scheme) | This check does not consider vbscript:. |
5+
| IncompleteUrlSchemeCheck.js:37:9:37:31 | scheme ... script" | This check does not consider data: and vbscript:. |
Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,47 @@
1+
import * as dummy from 'dummy';
2+
13
function sanitizeUrl(url) {
24
let u = decodeURI(url).trim().toLowerCase();
3-
if (u.startsWith("javascript:"))
5+
if (u.startsWith("javascript:")) // NOT OK
6+
return "about:blank";
7+
return url;
8+
}
9+
10+
let badProtocols = ['javascript:', 'data:'];
11+
let badProtocolNoColon = ['javascript', 'data'];
12+
let badProtocolsGood = ['javascript:', 'data:', 'vbscript:'];
13+
14+
function test2(url) {
15+
let protocol = new URL(url).protocol;
16+
if (badProtocols.includes(protocol)) // NOT OK
17+
return "about:blank";
18+
return url;
19+
}
20+
21+
function test3(url) {
22+
let scheme = goog.uri.utils.getScheme(url);
23+
if (badProtocolNoColon.includes(scheme)) // NOT OK
24+
return "about:blank";
25+
return url;
26+
}
27+
28+
function test4(url) {
29+
let scheme = url.split(':')[0];
30+
if (badProtocolNoColon.includes(scheme)) // NOT OK
31+
return "about:blank";
32+
return url;
33+
}
34+
35+
function test5(url) {
36+
let scheme = url.split(':')[0];
37+
if (scheme === "javascript") // NOT OK
38+
return "about:blank";
39+
return url;
40+
}
41+
42+
function test6(url) {
43+
let protocol = new URL(url).protocol;
44+
if (badProtocolsGood.includes(protocol)) // OK
445
return "about:blank";
546
return url;
647
}

0 commit comments

Comments
 (0)