Skip to content

Commit 7dd095c

Browse files
authored
Merge pull request #9756 from erik-krogh/greyMatter
JS: add model for the gray-matter library to js/code-injection
2 parents df78b7e + 11be15a commit 7dd095c

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The `gray-matter` library is now modeled as a sink for the `js/code-injection` query.

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ module CodeInjection {
5151
}
5252
}
5353

54+
/** An expression parsed by the `gray-matter` library. */
55+
class GrayMatterSink extends Sink {
56+
GrayMatterSink() {
57+
exists(API::CallNode call |
58+
call = DataFlow::moduleImport("gray-matter").getACall() and
59+
this = call.getArgument(0) and
60+
// if the js/javascript engine is set, then we assume they are set to something safe.
61+
not exists(call.getParameter(1).getMember("engines").getMember(["js", "javascript"]))
62+
)
63+
}
64+
}
65+
5466
/**
5567
* A template tag occurring in JS code, viewed as a code injection sink.
5668
*/

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ nodes
1111
| lib/index.js:13:38:13:41 | data |
1212
| lib/index.js:14:21:14:24 | data |
1313
| lib/index.js:14:21:14:24 | data |
14+
| lib/index.js:19:26:19:29 | data |
15+
| lib/index.js:19:26:19:29 | data |
16+
| lib/index.js:22:7:22:10 | data |
17+
| lib/index.js:22:7:22:10 | data |
1418
edges
1519
| lib/index.js:1:35:1:38 | data | lib/index.js:2:21:2:24 | data |
1620
| lib/index.js:1:35:1:38 | data | lib/index.js:2:21:2:24 | data |
@@ -24,7 +28,12 @@ edges
2428
| lib/index.js:13:38:13:41 | data | lib/index.js:14:21:14:24 | data |
2529
| lib/index.js:13:38:13:41 | data | lib/index.js:14:21:14:24 | data |
2630
| lib/index.js:13:38:13:41 | data | lib/index.js:14:21:14:24 | data |
31+
| lib/index.js:19:26:19:29 | data | lib/index.js:22:7:22:10 | data |
32+
| lib/index.js:19:26:19:29 | data | lib/index.js:22:7:22:10 | data |
33+
| lib/index.js:19:26:19:29 | data | lib/index.js:22:7:22:10 | data |
34+
| lib/index.js:19:26:19:29 | data | lib/index.js:22:7:22:10 | data |
2735
#select
2836
| lib/index.js:2:21:2:24 | data | lib/index.js:1:35:1:38 | data | lib/index.js:2:21:2:24 | data | $@ flows to here and is later $@. | lib/index.js:1:35:1:38 | data | Library input | lib/index.js:2:15:2:30 | "(" + data + ")" | interpreted as code |
2937
| lib/index.js:6:26:6:29 | name | lib/index.js:5:35:5:38 | name | lib/index.js:6:26:6:29 | name | $@ flows to here and is later $@. | lib/index.js:5:35:5:38 | name | Library input | lib/index.js:6:17:6:29 | "obj." + name | interpreted as code |
3038
| lib/index.js:14:21:14:24 | data | lib/index.js:13:38:13:41 | data | lib/index.js:14:21:14:24 | data | $@ flows to here and is later $@. | lib/index.js:13:38:13:41 | data | Library input | lib/index.js:14:15:14:30 | "(" + data + ")" | interpreted as code |
39+
| lib/index.js:22:7:22:10 | data | lib/index.js:19:26:19:29 | data | lib/index.js:22:7:22:10 | data | $@ flows to here and is later $@. | lib/index.js:19:26:19:29 | data | Library input | lib/index.js:25:24:25:26 | str | interpreted as code |

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,24 @@ export function safeAssignment(obj, value) {
1212

1313
global.unsafeDeserialize = function (data) {
1414
return eval("(" + data + ")"); // NOT OK
15-
}
15+
}
16+
17+
const matter = require("gray-matter");
18+
19+
export function greySink(data) {
20+
const str = `
21+
---js
22+
${data}
23+
---
24+
`
25+
const res = matter(str);
26+
console.log(res);
27+
28+
matter(str, { // OK
29+
engines: {
30+
js: function (data) {
31+
console.log("NOPE");
32+
}
33+
}
34+
});
35+
}

0 commit comments

Comments
 (0)