Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1d9b0ce

Browse files
authored
Merge pull request #4460 from max-schaefer/js/unsafe-shell-command-construction-infeasible-paths
Approved by asgerf
2 parents 2b19a48 + 4100ab2 commit 1d9b0ce

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,15 @@ module UnsafeShellCommandConstruction {
3131
guard instanceof PathExistsSanitizerGuard or
3232
guard instanceof TaintTracking::AdHocWhitelistCheckSanitizer
3333
}
34+
35+
// override to require that there is a path without unmatched return steps
36+
override predicate hasFlowPath(DataFlow::SourcePathNode source, DataFlow::SinkPathNode sink) {
37+
super.hasFlowPath(source, sink) and
38+
exists(DataFlow::MidPathNode mid |
39+
source.getASuccessor*() = mid and
40+
sink = mid.getASuccessor() and
41+
mid.getPathSummary().hasReturn() = false
42+
)
43+
}
3444
}
3545
}

javascript/ql/test/query-tests/Security/CWE-078/UnsafeShellCommandConstruction.expected

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,17 @@ nodes
180180
| lib/lib.js:324:40:324:42 | arg |
181181
| lib/lib.js:325:49:325:51 | arg |
182182
| lib/lib.js:325:49:325:51 | arg |
183+
| lib/lib.js:329:13:329:13 | x |
184+
| lib/lib.js:329:13:329:13 | x |
185+
| lib/lib.js:330:9:330:9 | x |
186+
| lib/lib.js:336:22:336:31 | id("test") |
187+
| lib/lib.js:336:22:336:31 | id("test") |
188+
| lib/lib.js:339:39:339:39 | n |
189+
| lib/lib.js:339:39:339:39 | n |
190+
| lib/lib.js:340:22:340:26 | id(n) |
191+
| lib/lib.js:340:22:340:26 | id(n) |
192+
| lib/lib.js:340:22:340:26 | id(n) |
193+
| lib/lib.js:340:25:340:25 | n |
183194
edges
184195
| lib/lib2.js:3:28:3:31 | name | lib/lib2.js:4:22:4:25 | name |
185196
| lib/lib2.js:3:28:3:31 | name | lib/lib2.js:4:22:4:25 | name |
@@ -396,6 +407,16 @@ edges
396407
| lib/lib.js:324:40:324:42 | arg | lib/lib.js:325:49:325:51 | arg |
397408
| lib/lib.js:324:40:324:42 | arg | lib/lib.js:325:49:325:51 | arg |
398409
| lib/lib.js:324:40:324:42 | arg | lib/lib.js:325:49:325:51 | arg |
410+
| lib/lib.js:329:13:329:13 | x | lib/lib.js:330:9:330:9 | x |
411+
| lib/lib.js:329:13:329:13 | x | lib/lib.js:330:9:330:9 | x |
412+
| lib/lib.js:330:9:330:9 | x | lib/lib.js:336:22:336:31 | id("test") |
413+
| lib/lib.js:330:9:330:9 | x | lib/lib.js:336:22:336:31 | id("test") |
414+
| lib/lib.js:330:9:330:9 | x | lib/lib.js:340:22:340:26 | id(n) |
415+
| lib/lib.js:330:9:330:9 | x | lib/lib.js:340:22:340:26 | id(n) |
416+
| lib/lib.js:339:39:339:39 | n | lib/lib.js:340:25:340:25 | n |
417+
| lib/lib.js:339:39:339:39 | n | lib/lib.js:340:25:340:25 | n |
418+
| lib/lib.js:340:25:340:25 | n | lib/lib.js:340:22:340:26 | id(n) |
419+
| lib/lib.js:340:25:340:25 | n | lib/lib.js:340:22:340:26 | id(n) |
399420
#select
400421
| lib/lib2.js:4:10:4:25 | "rm -rf " + name | lib/lib2.js:3:28:3:31 | name | lib/lib2.js:4:22:4:25 | name | $@ based on library input is later used in $@. | lib/lib2.js:4:10:4:25 | "rm -rf " + name | String concatenation | lib/lib2.js:4:2:4:26 | cp.exec ... + name) | shell command |
401422
| lib/lib2.js:8:10:8:25 | "rm -rf " + name | lib/lib2.js:7:32:7:35 | name | lib/lib2.js:8:22:8:25 | name | $@ based on library input is later used in $@. | lib/lib2.js:8:10:8:25 | "rm -rf " + name | String concatenation | lib/lib2.js:8:2:8:26 | cp.exec ... + name) | shell command |
@@ -450,3 +471,4 @@ edges
450471
| lib/lib.js:315:10:315:25 | "rm -rf " + name | lib/lib.js:314:40:314:43 | name | lib/lib.js:315:22:315:25 | name | $@ based on library input is later used in $@. | lib/lib.js:315:10:315:25 | "rm -rf " + name | String concatenation | lib/lib.js:315:2:315:26 | cp.exec ... + name) | shell command |
451472
| lib/lib.js:320:11:320:26 | "rm -rf " + name | lib/lib.js:314:40:314:43 | name | lib/lib.js:320:23:320:26 | name | $@ based on library input is later used in $@. | lib/lib.js:320:11:320:26 | "rm -rf " + name | String concatenation | lib/lib.js:320:3:320:27 | cp.exec ... + name) | shell command |
452473
| lib/lib.js:325:12:325:51 | "MyWind ... " + arg | lib/lib.js:324:40:324:42 | arg | lib/lib.js:325:49:325:51 | arg | $@ based on library input is later used in $@. | lib/lib.js:325:12:325:51 | "MyWind ... " + arg | String concatenation | lib/lib.js:326:2:326:13 | cp.exec(cmd) | shell command |
474+
| lib/lib.js:340:10:340:26 | "rm -rf " + id(n) | lib/lib.js:339:39:339:39 | n | lib/lib.js:340:22:340:26 | id(n) | $@ based on library input is later used in $@. | lib/lib.js:340:10:340:26 | "rm -rf " + id(n) | String concatenation | lib/lib.js:340:2:340:27 | cp.exec ... id(n)) | shell command |

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,18 @@ module.exports.typeofcheck = function (name) {
324324
module.exports.typeofcheck = function (arg) {
325325
var cmd = "MyWindowCommand | findstr /i /c:" + arg; // NOT OK
326326
cp.exec(cmd);
327-
}
327+
}
328+
329+
function id(x) {
330+
return x;
331+
}
332+
333+
module.exports.id = id;
334+
335+
module.exports.unproblematic = function() {
336+
cp.exec("rm -rf " + id("test")); // OK
337+
};
338+
339+
module.exports.problematic = function(n) {
340+
cp.exec("rm -rf " + id(n)); // NOT OK
341+
};

0 commit comments

Comments
 (0)