Skip to content

Commit d765a33

Browse files
committed
add support for "../" prefixes in sanitizer
1 parent 9d61004 commit d765a33

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,13 @@ module TaintedPath {
368368
this = startsWith and
369369
relativeCall = DataFlow::moduleImport("path").getAMemberCall("relative") and
370370
startsWith.getBaseString().getALocalSource() = relativeCall and
371-
exists(DataFlow::Node subString | subString = startsWith.getSubstring() |
372-
subString.mayHaveStringValue("..")
371+
exists(DataFlow::Node subString, string prefix |
372+
subString = startsWith.getSubstring() and
373+
(prefix = ".." or prefix = "../")
374+
|
375+
subString.mayHaveStringValue(prefix)
373376
or
374-
subString.(StringOps::ConcatenationRoot).getFirstLeaf().mayHaveStringValue("..")
377+
subString.(StringOps::ConcatenationRoot).getFirstLeaf().mayHaveStringValue(prefix)
375378
)
376379
}
377380

javascript/ql/test/query-tests/Security/CWE-022/TaintedPath/TaintedPath.expected

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,11 @@ nodes
12871287
| normalizedPaths.js:270:21:270:24 | path |
12881288
| normalizedPaths.js:270:21:270:24 | path |
12891289
| normalizedPaths.js:270:21:270:24 | path |
1290+
| normalizedPaths.js:278:21:278:24 | path |
1291+
| normalizedPaths.js:278:21:278:24 | path |
1292+
| normalizedPaths.js:278:21:278:24 | path |
1293+
| normalizedPaths.js:278:21:278:24 | path |
1294+
| normalizedPaths.js:278:21:278:24 | path |
12901295
| tainted-require.js:7:19:7:37 | req.param("module") |
12911296
| tainted-require.js:7:19:7:37 | req.param("module") |
12921297
| tainted-require.js:7:19:7:37 | req.param("module") |
@@ -3682,6 +3687,14 @@ edges
36823687
| normalizedPaths.js:254:7:254:47 | path | normalizedPaths.js:270:21:270:24 | path |
36833688
| normalizedPaths.js:254:7:254:47 | path | normalizedPaths.js:270:21:270:24 | path |
36843689
| normalizedPaths.js:254:7:254:47 | path | normalizedPaths.js:270:21:270:24 | path |
3690+
| normalizedPaths.js:254:7:254:47 | path | normalizedPaths.js:278:21:278:24 | path |
3691+
| normalizedPaths.js:254:7:254:47 | path | normalizedPaths.js:278:21:278:24 | path |
3692+
| normalizedPaths.js:254:7:254:47 | path | normalizedPaths.js:278:21:278:24 | path |
3693+
| normalizedPaths.js:254:7:254:47 | path | normalizedPaths.js:278:21:278:24 | path |
3694+
| normalizedPaths.js:254:7:254:47 | path | normalizedPaths.js:278:21:278:24 | path |
3695+
| normalizedPaths.js:254:7:254:47 | path | normalizedPaths.js:278:21:278:24 | path |
3696+
| normalizedPaths.js:254:7:254:47 | path | normalizedPaths.js:278:21:278:24 | path |
3697+
| normalizedPaths.js:254:7:254:47 | path | normalizedPaths.js:278:21:278:24 | path |
36853698
| normalizedPaths.js:254:14:254:47 | pathMod ... y.path) | normalizedPaths.js:254:7:254:47 | path |
36863699
| normalizedPaths.js:254:14:254:47 | pathMod ... y.path) | normalizedPaths.js:254:7:254:47 | path |
36873700
| normalizedPaths.js:254:14:254:47 | pathMod ... y.path) | normalizedPaths.js:254:7:254:47 | path |
@@ -4478,6 +4491,7 @@ edges
44784491
| normalizedPaths.js:256:19:256:22 | path | normalizedPaths.js:254:33:254:46 | req.query.path | normalizedPaths.js:256:19:256:22 | path | This path depends on $@. | normalizedPaths.js:254:33:254:46 | req.query.path | a user-provided value |
44794492
| normalizedPaths.js:262:21:262:24 | path | normalizedPaths.js:254:33:254:46 | req.query.path | normalizedPaths.js:262:21:262:24 | path | This path depends on $@. | normalizedPaths.js:254:33:254:46 | req.query.path | a user-provided value |
44804493
| normalizedPaths.js:270:21:270:24 | path | normalizedPaths.js:254:33:254:46 | req.query.path | normalizedPaths.js:270:21:270:24 | path | This path depends on $@. | normalizedPaths.js:254:33:254:46 | req.query.path | a user-provided value |
4494+
| normalizedPaths.js:278:21:278:24 | path | normalizedPaths.js:254:33:254:46 | req.query.path | normalizedPaths.js:278:21:278:24 | path | This path depends on $@. | normalizedPaths.js:254:33:254:46 | req.query.path | a user-provided value |
44814495
| tainted-require.js:7:19:7:37 | req.param("module") | tainted-require.js:7:19:7:37 | req.param("module") | tainted-require.js:7:19:7:37 | req.param("module") | This path depends on $@. | tainted-require.js:7:19:7:37 | req.param("module") | a user-provided value |
44824496
| tainted-sendFile.js:8:16:8:33 | req.param("gimme") | tainted-sendFile.js:8:16:8:33 | req.param("gimme") | tainted-sendFile.js:8:16:8:33 | req.param("gimme") | This path depends on $@. | tainted-sendFile.js:8:16:8:33 | req.param("gimme") | a user-provided value |
44834497
| tainted-sendFile.js:10:16:10:33 | req.param("gimme") | tainted-sendFile.js:10:16:10:33 | req.param("gimme") | tainted-sendFile.js:10:16:10:33 | req.param("gimme") | This path depends on $@. | tainted-sendFile.js:10:16:10:33 | req.param("gimme") | a user-provided value |

javascript/ql/test/query-tests/Security/CWE-022/TaintedPath/normalizedPaths.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,14 @@ app.get('/relative-startswith', (req, res) => {
269269
if (relativePath.indexOf('..' + pathModule.sep) === 0) {
270270
fs.readFileSync(path); // NOT OK!
271271
} else {
272-
fs.readFileSync(newpath); // OK!
272+
fs.readFileSync(newpath); // OK!
273+
}
274+
275+
let newpath = pathModule.normalize(p);
276+
var relativePath = path.relative(path.normalize(workspaceDir), newpath);
277+
if (relativePath.indexOf('../') === 0) {
278+
fs.readFileSync(path); // NOT OK!
279+
} else {
280+
fs.readFileSync(newpath); // OK!
273281
}
274282
});

0 commit comments

Comments
 (0)