Skip to content

Commit ce24215

Browse files
author
Max Schaefer
committed
JavaScript: Improve modelling of Module.prototype._compile sink.
1 parent 70c82c8 commit ce24215

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,19 @@ module CodeInjection {
169169
}
170170

171171
/**
172-
* The first argument to `Module.prototype._compile` from the Node.js built-in module `module`,
173-
* considered as a code-injection sink.
172+
* The first argument to `Module.prototype._compile`, considered as a code-injection sink.
174173
*/
175174
class ModuleCompileSink extends Sink {
176175
ModuleCompileSink() {
176+
// `require('module').prototype._compile`
177177
this =
178178
API::moduleImport("module").getInstance().getMember("_compile").getACall().getArgument(0)
179+
or
180+
// `module.constructor.prototype._compile`
181+
exists(DataFlow::SourceNode moduleConstructor |
182+
moduleConstructor = DataFlow::moduleVarNode(_).getAPropertyRead("constructor") and
183+
this = moduleConstructor.getAnInstantiation().getAMethodCall("_compile").getArgument(0)
184+
)
179185
}
180186
}
181187

javascript/ql/test/query-tests/Security/CWE-094/CodeInjection/CodeInjection.expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ nodes
9797
| module.js:9:16:9:29 | req.query.code |
9898
| module.js:9:16:9:29 | req.query.code |
9999
| module.js:9:16:9:29 | req.query.code |
100+
| module.js:11:17:11:30 | req.query.code |
101+
| module.js:11:17:11:30 | req.query.code |
102+
| module.js:11:17:11:30 | req.query.code |
100103
| react-native.js:7:7:7:33 | tainted |
101104
| react-native.js:7:17:7:33 | req.param("code") |
102105
| react-native.js:7:17:7:33 | req.param("code") |
@@ -221,6 +224,7 @@ edges
221224
| express.js:19:37:19:70 | req.par ... odule") | express.js:19:37:19:70 | req.par ... odule") |
222225
| express.js:21:19:21:48 | req.par ... ntext") | express.js:21:19:21:48 | req.par ... ntext") |
223226
| module.js:9:16:9:29 | req.query.code | module.js:9:16:9:29 | req.query.code |
227+
| module.js:11:17:11:30 | req.query.code | module.js:11:17:11:30 | req.query.code |
224228
| react-native.js:7:7:7:33 | tainted | react-native.js:8:32:8:38 | tainted |
225229
| react-native.js:7:7:7:33 | tainted | react-native.js:8:32:8:38 | tainted |
226230
| react-native.js:7:7:7:33 | tainted | react-native.js:10:23:10:29 | tainted |
@@ -305,6 +309,7 @@ edges
305309
| express.js:19:37:19:70 | req.par ... odule") | express.js:19:37:19:70 | req.par ... odule") | express.js:19:37:19:70 | req.par ... odule") | $@ flows to here and is interpreted as code. | express.js:19:37:19:70 | req.par ... odule") | User-provided value |
306310
| express.js:21:19:21:48 | req.par ... ntext") | express.js:21:19:21:48 | req.par ... ntext") | express.js:21:19:21:48 | req.par ... ntext") | $@ flows to here and is interpreted as code. | express.js:21:19:21:48 | req.par ... ntext") | User-provided value |
307311
| module.js:9:16:9:29 | req.query.code | module.js:9:16:9:29 | req.query.code | module.js:9:16:9:29 | req.query.code | $@ flows to here and is interpreted as code. | module.js:9:16:9:29 | req.query.code | User-provided value |
312+
| module.js:11:17:11:30 | req.query.code | module.js:11:17:11:30 | req.query.code | module.js:11:17:11:30 | req.query.code | $@ flows to here and is interpreted as code. | module.js:11:17:11:30 | req.query.code | User-provided value |
308313
| react-native.js:8:32:8:38 | tainted | react-native.js:7:17:7:33 | req.param("code") | react-native.js:8:32:8:38 | tainted | $@ flows to here and is interpreted as code. | react-native.js:7:17:7:33 | req.param("code") | User-provided value |
309314
| react-native.js:10:23:10:29 | tainted | react-native.js:7:17:7:33 | req.param("code") | react-native.js:10:23:10:29 | tainted | $@ flows to here and is interpreted as code. | react-native.js:7:17:7:33 | req.param("code") | User-provided value |
310315
| react.js:10:56:10:77 | documen ... on.hash | react.js:10:56:10:77 | documen ... on.hash | react.js:10:56:10:77 | documen ... on.hash | $@ flows to here and is interpreted as code. | react.js:10:56:10:77 | documen ... on.hash | User-provided value |

javascript/ql/test/query-tests/Security/CWE-094/CodeInjection/HeuristicSourceCodeInjection.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ nodes
101101
| module.js:9:16:9:29 | req.query.code |
102102
| module.js:9:16:9:29 | req.query.code |
103103
| module.js:9:16:9:29 | req.query.code |
104+
| module.js:11:17:11:30 | req.query.code |
105+
| module.js:11:17:11:30 | req.query.code |
106+
| module.js:11:17:11:30 | req.query.code |
104107
| react-native.js:7:7:7:33 | tainted |
105108
| react-native.js:7:17:7:33 | req.param("code") |
106109
| react-native.js:7:17:7:33 | req.param("code") |
@@ -229,6 +232,7 @@ edges
229232
| express.js:19:37:19:70 | req.par ... odule") | express.js:19:37:19:70 | req.par ... odule") |
230233
| express.js:21:19:21:48 | req.par ... ntext") | express.js:21:19:21:48 | req.par ... ntext") |
231234
| module.js:9:16:9:29 | req.query.code | module.js:9:16:9:29 | req.query.code |
235+
| module.js:11:17:11:30 | req.query.code | module.js:11:17:11:30 | req.query.code |
232236
| react-native.js:7:7:7:33 | tainted | react-native.js:8:32:8:38 | tainted |
233237
| react-native.js:7:7:7:33 | tainted | react-native.js:8:32:8:38 | tainted |
234238
| react-native.js:7:7:7:33 | tainted | react-native.js:10:23:10:29 | tainted |

javascript/ql/test/query-tests/Security/CWE-094/CodeInjection/module.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ app.get('/some/path', function (req, res) {
77
let filename = req.query.filename;
88
var m = new Module(filename, module.parent);
99
m._compile(req.query.code, filename); // NOT OK
10+
var m2 = new module.constructor;
11+
m2._compile(req.query.code, filename); // NOT OK
1012
});

0 commit comments

Comments
 (0)