Skip to content

Commit 6e183af

Browse files
committed
ignore test files for the `prototypeLessObject' predicate
1 parent e94b0f5 commit 6e183af

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

javascript/ql/lib/semmle/javascript/security/dataflow/PrototypePollutingAssignmentQuery.qll

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ private import javascript
1111
private import semmle.javascript.DynamicPropertyAccess
1212
private import semmle.javascript.dataflow.InferredTypes
1313
private import PrototypePollutingAssignmentCustomizations::PrototypePollutingAssignment
14+
private import filters.ClassifyFiles as ClassifyFiles
1415

1516
// Materialize flow labels
1617
private class ConcreteObjectPrototype extends ObjectPrototype {
@@ -98,7 +99,8 @@ private DataFlow::SourceNode prototypeLessObject(DataFlow::TypeTracker t) {
9899
t.start() and
99100
// We assume the argument to Object.create is not Object.prototype, since most
100101
// users wouldn't bother to call Object.create in that case.
101-
result = DataFlow::globalVarRef("Object").getAMemberCall("create")
102+
result = DataFlow::globalVarRef("Object").getAMemberCall("create") and
103+
not result.getFile() instanceof TestFile
102104
or
103105
// Allow use of SharedFlowSteps to track a bit further
104106
exists(DataFlow::Node mid |
@@ -109,6 +111,14 @@ private DataFlow::SourceNode prototypeLessObject(DataFlow::TypeTracker t) {
109111
exists(DataFlow::TypeTracker t2 | result = prototypeLessObject(t2).track(t2, t))
110112
}
111113

114+
/**
115+
* A test file.
116+
* Objects created in such files are ignored in the `prototypeLessObject` predicate.
117+
*/
118+
private class TestFile extends File {
119+
TestFile() { ClassifyFiles::classify(this, "test") }
120+
}
121+
112122
/** Holds if `Object.prototype` has a member named `prop`. */
113123
private predicate isPropertyPresentOnObjectPrototype(string prop) {
114124
exists(ExternalInstanceMemberDecl decl |

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ nodes
3232
| lib.js:22:3:22:14 | obj[path[0]] |
3333
| lib.js:22:7:22:10 | path |
3434
| lib.js:22:7:22:13 | path[0] |
35+
| lib.js:25:44:25:47 | path |
36+
| lib.js:25:44:25:47 | path |
37+
| lib.js:26:10:26:21 | obj[path[0]] |
38+
| lib.js:26:10:26:21 | obj[path[0]] |
39+
| lib.js:26:14:26:17 | path |
40+
| lib.js:26:14:26:20 | path[0] |
3541
| tst.js:5:9:5:38 | taint |
3642
| tst.js:5:17:5:38 | String( ... y.data) |
3743
| tst.js:5:24:5:37 | req.query.data |
@@ -104,6 +110,11 @@ edges
104110
| lib.js:22:7:22:10 | path | lib.js:22:7:22:13 | path[0] |
105111
| lib.js:22:7:22:13 | path[0] | lib.js:22:3:22:14 | obj[path[0]] |
106112
| lib.js:22:7:22:13 | path[0] | lib.js:22:3:22:14 | obj[path[0]] |
113+
| lib.js:25:44:25:47 | path | lib.js:26:14:26:17 | path |
114+
| lib.js:25:44:25:47 | path | lib.js:26:14:26:17 | path |
115+
| lib.js:26:14:26:17 | path | lib.js:26:14:26:20 | path[0] |
116+
| lib.js:26:14:26:20 | path[0] | lib.js:26:10:26:21 | obj[path[0]] |
117+
| lib.js:26:14:26:20 | path[0] | lib.js:26:10:26:21 | obj[path[0]] |
107118
| tst.js:5:9:5:38 | taint | tst.js:8:12:8:16 | taint |
108119
| tst.js:5:9:5:38 | taint | tst.js:9:12:9:16 | taint |
109120
| tst.js:5:9:5:38 | taint | tst.js:12:25:12:29 | taint |
@@ -144,6 +155,7 @@ edges
144155
| lib.js:6:7:6:9 | obj | lib.js:1:43:1:46 | path | lib.js:6:7:6:9 | obj | This assignment may alter Object.prototype if a malicious '__proto__' string is injected from $@. | lib.js:1:43:1:46 | path | here |
145156
| lib.js:15:3:15:14 | obj[path[0]] | lib.js:14:38:14:41 | path | lib.js:15:3:15:14 | obj[path[0]] | This assignment may alter Object.prototype if a malicious '__proto__' string is injected from $@. | lib.js:14:38:14:41 | path | here |
146157
| lib.js:22:3:22:14 | obj[path[0]] | lib.js:20:14:20:25 | arguments[1] | lib.js:22:3:22:14 | obj[path[0]] | This assignment may alter Object.prototype if a malicious '__proto__' string is injected from $@. | lib.js:20:14:20:25 | arguments[1] | here |
158+
| lib.js:26:10:26:21 | obj[path[0]] | lib.js:25:44:25:47 | path | lib.js:26:10:26:21 | obj[path[0]] | This assignment may alter Object.prototype if a malicious '__proto__' string is injected from $@. | lib.js:25:44:25:47 | path | here |
147159
| 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 |
148160
| 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 |
149161
| 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 |

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ module.exports.setWithArgs = function() {
2020
var path = arguments[1];
2121
var value = arguments[2];
2222
obj[path[0]][path[1]] = value; // NOT OK
23-
}
23+
}
24+
25+
module.exports.usedInTest = function (obj, path, value) {
26+
return obj[path[0]][path[1]] = value; // NOT OK
27+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const lib = require("./lib");
2+
3+
describe("lib", () => {
4+
it("should work", () => {
5+
const obj = Object.create(null);
6+
7+
lib.usedInTest(obj, "foo", "my-value");
8+
});
9+
});

0 commit comments

Comments
 (0)