Skip to content

Commit 5125dc7

Browse files
authored
Merge pull request #2730 from esbena/js/model-path-parse
Approved by asgerf
2 parents c53f801 + 5f1317f commit 5125dc7

File tree

6 files changed

+72
-7
lines changed

6 files changed

+72
-7
lines changed

javascript/ql/src/semmle/javascript/frameworks/NodeJSLib.qll

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,4 +1045,25 @@ module NodeJSLib {
10451045
i = 0 and result = this.getArgument(i)
10461046
}
10471047
}
1048+
1049+
/**
1050+
* Provides predicates for working with the "path" module and its platform-specific instances as a single module.
1051+
*/
1052+
module Path {
1053+
/**
1054+
* Gets a node that imports the "path" module, or one of its platform-specific instances.
1055+
*/
1056+
DataFlow::SourceNode moduleImport() {
1057+
result = DataFlow::moduleImport("path") or
1058+
result = DataFlow::moduleMember("path", "posix") or
1059+
result = DataFlow::moduleMember("path", "win32")
1060+
}
1061+
1062+
/**
1063+
* Gets an access to member `member` of the "path" module, or one of its platform-specific instances.
1064+
*/
1065+
DataFlow::SourceNode moduleMember(string member) {
1066+
result = moduleImport().getAPropertyRead(member)
1067+
}
1068+
}
10481069
}

javascript/ql/src/semmle/javascript/frameworks/UriLibraries.qll

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,4 +401,33 @@ private module ClosureLibraryUri {
401401
succ = uri
402402
}
403403
}
404+
405+
/**
406+
* Provides classes for working with [path](https://nodejs.org/api/path.html) code.
407+
*/
408+
module path {
409+
/**
410+
* A taint step in the path module.
411+
*/
412+
private class Step extends UriLibraryStep, DataFlow::CallNode {
413+
DataFlow::Node src;
414+
415+
Step() {
416+
exists(DataFlow::SourceNode ref |
417+
ref = NodeJSLib::Path::moduleMember("parse") or
418+
// a ponyfill: https://www.npmjs.com/package/path-parse
419+
ref = DataFlow::moduleImport("path-parse") or
420+
ref = DataFlow::moduleMember("path-parse", "posix") or
421+
ref = DataFlow::moduleMember("path-parse", "win32")
422+
|
423+
this = ref.getACall() and
424+
src = getAnArgument()
425+
)
426+
}
427+
428+
override predicate step(DataFlow::Node pred, DataFlow::Node succ) {
429+
pred = src and succ = this
430+
}
431+
}
432+
}
404433
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ module TaintedPath {
142142
or
143143
// path.join()
144144
exists(DataFlow::CallNode join, int n |
145-
join = DataFlow::moduleMember("path", "join").getACall()
145+
join = NodeJSLib::Path::moduleMember("join").getACall()
146146
|
147147
src = join.getArgument(n) and
148148
dst = join and

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ module TaintedPath {
124124
DataFlow::Node output;
125125

126126
NormalizingPathCall() {
127-
this = DataFlow::moduleMember("path", "normalize").getACall() and
127+
this = NodeJSLib::Path::moduleMember("normalize").getACall() and
128128
input = getArgument(0) and
129129
output = this
130130
}
@@ -148,7 +148,7 @@ module TaintedPath {
148148
DataFlow::Node output;
149149

150150
ResolvingPathCall() {
151-
this = DataFlow::moduleMember("path", "resolve").getACall() and
151+
this = NodeJSLib::Path::moduleMember("resolve").getACall() and
152152
input = getAnArgument() and
153153
output = this
154154
or
@@ -180,7 +180,7 @@ module TaintedPath {
180180
DataFlow::Node output;
181181

182182
NormalizingRelativePathCall() {
183-
this = DataFlow::moduleMember("path", "relative").getACall() and
183+
this = NodeJSLib::Path::moduleMember("relative").getACall() and
184184
input = getAnArgument() and
185185
output = this
186186
}
@@ -205,7 +205,7 @@ module TaintedPath {
205205

206206
PreservingPathCall() {
207207
exists(string name | name = "dirname" or name = "toNamespacedPath" |
208-
this = DataFlow::moduleMember("path", name).getACall() and
208+
this = NodeJSLib::Path::moduleMember(name).getACall() and
209209
input = getAnArgument() and
210210
output = this
211211
)
@@ -244,7 +244,7 @@ module TaintedPath {
244244
// ".." + path.sep
245245
exists(StringOps::Concatenation conc | node = conc |
246246
conc.getOperand(0).getStringValue() = ".." and
247-
conc.getOperand(1).getALocalSource() = DataFlow::moduleMember("path", "sep") and
247+
conc.getOperand(1).getALocalSource() = NodeJSLib::Path::moduleMember("sep") and
248248
conc.getNumOperand() = 2
249249
)
250250
}
@@ -311,7 +311,7 @@ module TaintedPath {
311311

312312
IsAbsoluteSanitizer() {
313313
exists(DataFlow::CallNode call | this = call |
314-
call = DataFlow::moduleMember("path", "isAbsolute").getACall() and
314+
call = NodeJSLib::Path::moduleMember("isAbsolute").getACall() and
315315
operand = call.getArgument(0) and
316316
polarity = true and
317317
negatable = true

javascript/ql/test/library-tests/frameworks/UriLibraries/UriLibraryStep.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
| closureUri.js:23:1:23:18 | utils.getPath(uri) | closureUri.js:23:15:23:17 | uri | closureUri.js:23:1:23:18 | utils.getPath(uri) |
2525
| closureUri.js:27:1:27:23 | stringU ... code(x) | closureUri.js:27:22:27:22 | x | closureUri.js:27:1:27:23 | stringU ... code(x) |
2626
| closureUri.js:28:1:28:23 | stringU ... code(x) | closureUri.js:28:22:28:22 | x | closureUri.js:28:1:28:23 | stringU ... code(x) |
27+
| path-parse.js:4:1:4:13 | path.parse(x) | path-parse.js:4:12:4:12 | x | path-parse.js:4:1:4:13 | path.parse(x) |
28+
| path-parse.js:5:1:5:13 | path_parse(x) | path-parse.js:5:12:5:12 | x | path-parse.js:5:1:5:13 | path_parse(x) |
29+
| path-parse.js:6:1:6:19 | path.posix.parse(x) | path-parse.js:6:18:6:18 | x | path-parse.js:6:1:6:19 | path.posix.parse(x) |
30+
| path-parse.js:7:1:7:19 | path_parse.posix(x) | path-parse.js:7:18:7:18 | x | path-parse.js:7:1:7:19 | path_parse.posix(x) |
31+
| path-parse.js:8:1:8:19 | path.win32.parse(x) | path-parse.js:8:18:8:18 | x | path-parse.js:8:1:8:19 | path.win32.parse(x) |
32+
| path-parse.js:9:1:9:19 | path_parse.win32(x) | path-parse.js:9:18:9:18 | x | path-parse.js:9:1:9:19 | path_parse.win32(x) |
2733
| punycode.js:3:9:3:26 | punycode.decode(x) | punycode.js:3:25:3:25 | x | punycode.js:3:9:3:26 | punycode.decode(x) |
2834
| punycode.js:5:5:5:22 | punycode.encode(x) | punycode.js:5:21:5:21 | x | punycode.js:5:5:5:22 | punycode.encode(x) |
2935
| punycode.js:7:5:7:25 | punycod ... code(x) | punycode.js:7:24:7:24 | x | punycode.js:7:5:7:25 | punycod ... code(x) |
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const path = require('path');
2+
const path_parse = require('path-parse');
3+
4+
path.parse(x);
5+
path_parse(x);
6+
path.posix.parse(x);
7+
path_parse.posix(x);
8+
path.win32.parse(x);
9+
path_parse.win32(x);

0 commit comments

Comments
 (0)