Skip to content

Commit 254ac7f

Browse files
committed
JS: Fix TypeofCheck
1 parent 0496642 commit 254ac7f

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

javascript/ql/src/semmle/javascript/security/dataflow/PrototypePollutingAssignment.qll

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,18 @@ module PrototypePollutingAssignment {
163163
string value;
164164

165165
TypeofCheck() {
166-
astNode.getLeftOperand().(TypeofExpr).getOperand() = operand and
167-
astNode.getRightOperand().getStringValue() = value
166+
exists(TypeofExpr typeof, Expr str |
167+
astNode.hasOperands(typeof, str) and
168+
typeof.getOperand() = operand and
169+
str.getStringValue() = value
170+
)
168171
}
169172

170173
override predicate sanitizes(boolean outcome, Expr e, DataFlow::FlowLabel label) {
171174
(
172-
value = "object" and outcome = false
175+
value = "object" and outcome = astNode.getPolarity().booleanNot()
173176
or
174-
value != "object" and outcome = true
177+
value != "object" and outcome = astNode.getPolarity()
175178
) and
176179
e = operand and
177180
label instanceof ObjectPrototype

javascript/ql/test/query-tests/Security/CWE-915/PrototypePollutingAssignment/PrototypePollutingAssignment.expected

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ nodes
1717
| tst.js:33:23:33:25 | obj |
1818
| tst.js:34:5:34:7 | obj |
1919
| tst.js:34:5:34:7 | obj |
20-
| tst.js:42:9:42:11 | obj |
21-
| tst.js:42:9:42:11 | obj |
20+
| tst.js:39:9:39:11 | obj |
21+
| tst.js:39:9:39:11 | obj |
22+
| tst.js:45:9:45:11 | obj |
23+
| tst.js:45:9:45:11 | obj |
24+
| tst.js:48:9:48:11 | obj |
25+
| tst.js:48:9:48:11 | obj |
2226
edges
2327
| tst.js:5:9:5:38 | taint | tst.js:8:12:8:16 | taint |
2428
| tst.js:5:9:5:38 | taint | tst.js:9:12:9:16 | taint |
@@ -37,11 +41,17 @@ edges
3741
| tst.js:14:27:14:31 | taint | tst.js:14:5:14:32 | unsafeG ... taint) |
3842
| tst.js:33:23:33:25 | obj | tst.js:34:5:34:7 | obj |
3943
| tst.js:33:23:33:25 | obj | tst.js:34:5:34:7 | obj |
40-
| tst.js:33:23:33:25 | obj | tst.js:42:9:42:11 | obj |
41-
| tst.js:33:23:33:25 | obj | tst.js:42:9:42:11 | obj |
44+
| tst.js:33:23:33:25 | obj | tst.js:39:9:39:11 | obj |
45+
| tst.js:33:23:33:25 | obj | tst.js:39:9:39:11 | obj |
46+
| tst.js:33:23:33:25 | obj | tst.js:45:9:45:11 | obj |
47+
| tst.js:33:23:33:25 | obj | tst.js:45:9:45:11 | obj |
48+
| tst.js:33:23:33:25 | obj | tst.js:48:9:48:11 | obj |
49+
| tst.js:33:23:33:25 | obj | tst.js:48:9:48:11 | obj |
4250
#select
4351
| tst.js:8:5:8:17 | object[taint] | tst.js:5:24:5:37 | req.query.data | tst.js:8:5:8:17 | object[taint] | This assignment may alter Object.prototype if a malicious '__proto__' string is injected from $@. | tst.js:5:24:5:37 | req.query.data | here |
4452
| tst.js:9:5:9:17 | object[taint] | tst.js:5:24:5:37 | req.query.data | tst.js:9:5:9:17 | object[taint] | This assignment may alter Object.prototype if a malicious '__proto__' string is injected from $@. | tst.js:5:24:5:37 | req.query.data | here |
4553
| tst.js:14:5:14:32 | unsafeG ... taint) | tst.js:5:24:5:37 | req.query.data | tst.js:14:5:14:32 | unsafeG ... taint) | This assignment may alter Object.prototype if a malicious '__proto__' string is injected from $@. | tst.js:5:24:5:37 | req.query.data | here |
4654
| tst.js:34:5:34:7 | obj | tst.js:5:24:5:37 | req.query.data | tst.js:34:5:34:7 | obj | This assignment may alter Object.prototype if a malicious '__proto__' string is injected from $@. | tst.js:5:24:5:37 | req.query.data | here |
47-
| tst.js:42:9:42:11 | obj | tst.js:5:24:5:37 | req.query.data | tst.js:42:9:42:11 | obj | This assignment may alter Object.prototype if a malicious '__proto__' string is injected from $@. | tst.js:5:24:5:37 | req.query.data | here |
55+
| tst.js:39:9:39:11 | obj | tst.js:5:24:5:37 | req.query.data | tst.js:39:9:39:11 | obj | This assignment may alter Object.prototype if a malicious '__proto__' string is injected from $@. | tst.js:5:24:5:37 | req.query.data | here |
56+
| tst.js:45:9:45:11 | obj | tst.js:5:24:5:37 | req.query.data | tst.js:45:9:45:11 | obj | This assignment may alter Object.prototype if a malicious '__proto__' string is injected from $@. | tst.js:5:24:5:37 | req.query.data | here |
57+
| tst.js:48:9:48:11 | obj | tst.js:5:24:5:37 | req.query.data | tst.js:48:9:48:11 | obj | This assignment may alter Object.prototype if a malicious '__proto__' string is injected from $@. | tst.js:5:24:5:37 | req.query.data | here |

javascript/ql/test/query-tests/Security/CWE-915/PrototypePollutingAssignment/tst.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,21 @@ function mutateObject(obj, x) {
3535
if (obj instanceof Object) {
3636
obj.foo = x; // OK
3737
}
38+
if (obj != null) {
39+
obj.foo = x; // NOT OK
40+
}
3841
if (typeof obj === 'function') {
3942
obj.foo = x; // OK
4043
}
41-
if (obj != null) {
44+
if (typeof obj !== 'function') {
4245
obj.foo = x; // NOT OK
4346
}
47+
if (typeof obj === 'object') {
48+
obj.foo = x; // NOT OK
49+
}
50+
if (typeof obj !== 'object') {
51+
obj.foo = x; // OK
52+
}
4453
}
4554

4655
function unsafeGetProp(obj, prop) {

0 commit comments

Comments
 (0)