Skip to content

Commit faba019

Browse files
author
Max Schaefer
authored
Merge pull request github#1229 from esben-semmle/js/whitelist-unwrappind
JS: whitelilist delimiter unwrapping for js/incomplete-sanitization
2 parents f5ccd3c + fd429ce commit faba019

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

javascript/ql/src/Security/CWE-116/IncompleteSanitization.ql

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,34 @@ predicate allBackslashesEscaped(DataFlow::Node nd) {
101101
allBackslashesEscaped(nd.getAPredecessor())
102102
}
103103

104+
/**
105+
* Holds if `repl` looks like a call to "String.prototype.replace" that deliberately removes the first occurrence of `str`.
106+
*/
107+
predicate removesFirstOccurence(DataFlow::MethodCallNode repl, string str) {
108+
repl.getMethodName() = "replace" and
109+
repl.getArgument(0).getStringValue() = str and
110+
repl.getArgument(1).getStringValue() = ""
111+
}
112+
113+
/**
114+
* Holds if `leftUnwrap` and `rightUnwrap` unwraps a string from a pair of surrounding delimiters.
115+
*/
116+
predicate isDelimiterUnwrapper(
117+
DataFlow::MethodCallNode leftUnwrap, DataFlow::MethodCallNode rightUnwrap
118+
) {
119+
exists(string left, string right |
120+
left = "[" and right = "]"
121+
or
122+
left = "{" and right = "}"
123+
or
124+
left = "(" and right = ")"
125+
|
126+
removesFirstOccurence(leftUnwrap, left) and
127+
removesFirstOccurence(rightUnwrap, right) and
128+
leftUnwrap.getAMethodCall() = rightUnwrap
129+
)
130+
}
131+
104132
from MethodCallExpr repl, Expr old, string msg
105133
where
106134
repl.getMethodName() = "replace" and
@@ -122,7 +150,10 @@ where
122150
)
123151
) and
124152
// don't flag replace operations in a loop
125-
not DataFlow::valueNode(repl.getReceiver()) = DataFlow::valueNode(repl).getASuccessor+()
153+
not DataFlow::valueNode(repl.getReceiver()) = DataFlow::valueNode(repl).getASuccessor+() and
154+
// dont' flag unwrapper
155+
not isDelimiterUnwrapper(repl.flow(), _) and
156+
not isDelimiterUnwrapper(_, repl.flow())
126157
or
127158
exists(RegExpLiteral rel |
128159
isBackslashEscape(repl, rel) and

javascript/ql/test/query-tests/Security/CWE-116/IncompleteSanitization/IncompleteSanitization.expected

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,13 @@
1515
| tst.js:61:10:61:18 | s.replace | This replaces only the first occurrence of "'" + "". |
1616
| tst.js:65:10:65:18 | s.replace | This replaces only the first occurrence of "'". |
1717
| tst.js:69:10:69:18 | s.replace | This replaces only the first occurrence of "'" + "". |
18+
| tst.js:133:2:133:10 | s.replace | This replaces only the first occurrence of '<'. |
19+
| tst.js:133:2:133:27 | s.repla ... replace | This replaces only the first occurrence of '>'. |
20+
| tst.js:135:2:135:10 | s.replace | This replaces only the first occurrence of '['. |
21+
| tst.js:135:2:135:30 | s.repla ... replace | This replaces only the first occurrence of ']'. |
22+
| tst.js:136:2:136:10 | s.replace | This replaces only the first occurrence of '{'. |
23+
| tst.js:136:2:136:30 | s.repla ... replace | This replaces only the first occurrence of '}'. |
24+
| tst.js:140:2:140:10 | s.replace | This replaces only the first occurrence of /{/. |
25+
| tst.js:140:2:140:27 | s.repla ... replace | This replaces only the first occurrence of /}/. |
26+
| tst.js:141:2:141:10 | s.replace | This replaces only the first occurrence of ']'. |
27+
| tst.js:141:2:141:27 | s.repla ... replace | This replaces only the first occurrence of '['. |

javascript/ql/test/query-tests/Security/CWE-116/IncompleteSanitization/tst.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,21 @@ function good11(s) {
126126
return s.replace("%d", "42");
127127
}
128128

129+
function good12(s) {
130+
s.replace('[', '').replace(']', ''); // OK
131+
s.replace('(', '').replace(')', ''); // OK
132+
s.replace('{', '').replace('}', ''); // OK
133+
s.replace('<', '').replace('>', ''); // NOT OK: too common as a bad HTML sanitizer
134+
135+
s.replace('[', '\\[').replace(']', '\\]'); // NOT OK
136+
s.replace('{', '\\{').replace('}', '\\}'); // NOT OK
137+
138+
s = s.replace('[', ''); // OK
139+
s = s.replace(']', ''); // OK
140+
s.replace(/{/, '').replace(/}/, ''); // NOT OK: should have used a string literal if a single replacement was intended
141+
s.replace(']', '').replace('[', ''); // probably OK, but still flagged
142+
}
143+
129144
app.get('/some/path', function(req, res) {
130145
let untrusted = req.param("p");
131146

@@ -162,4 +177,5 @@ app.get('/some/path', function(req, res) {
162177
good10(untrusted);
163178
flowifyComments(untrusted);
164179
good11(untrusted);
180+
good12(untrusted);
165181
});

0 commit comments

Comments
 (0)