Skip to content

Commit eb56a5a

Browse files
committed
support more patterns that recognize valid numbers
1 parent 5340530 commit eb56a5a

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

javascript/ql/lib/semmle/javascript/dataflow/TaintTracking.qll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,17 @@ module TaintTracking {
11791179
parse = isNaN.getArgument(0) and
11801180
operand = parse.getArgument(0).asExpr()
11811181
)
1182+
or
1183+
exists(UnaryExpr unary | unary.getOperator() = ["+", "-"] |
1184+
unary = isNaN.getArgument(0).asExpr() and
1185+
operand = unary.getOperand()
1186+
)
1187+
or
1188+
exists(BinaryExpr bin | bin.getOperator() = ["+", "-"] |
1189+
bin = isNaN.getArgument(0).asExpr() and
1190+
operand = bin.getAnOperand() and
1191+
bin.getAnOperand() instanceof NumberLiteral
1192+
)
11821193
)
11831194
or
11841195
isTypeofGuard(guard.asExpr(), operand, "number") and

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,16 @@ nodes
262262
| lib/lib.js:513:23:513:26 | name |
263263
| lib/lib.js:519:23:519:26 | name |
264264
| lib/lib.js:519:23:519:26 | name |
265+
| lib/lib.js:525:23:525:26 | name |
266+
| lib/lib.js:525:23:525:26 | name |
267+
| lib/lib.js:531:23:531:26 | name |
268+
| lib/lib.js:531:23:531:26 | name |
269+
| lib/lib.js:537:23:537:26 | name |
270+
| lib/lib.js:537:23:537:26 | name |
271+
| lib/lib.js:543:23:543:26 | name |
272+
| lib/lib.js:543:23:543:26 | name |
273+
| lib/lib.js:545:23:545:26 | name |
274+
| lib/lib.js:545:23:545:26 | name |
265275
| lib/subLib2/compiled-file.ts:3:26:3:29 | name |
266276
| lib/subLib2/compiled-file.ts:3:26:3:29 | name |
267277
| lib/subLib2/compiled-file.ts:4:25:4:28 | name |
@@ -594,6 +604,26 @@ edges
594604
| lib/lib.js:509:39:509:42 | name | lib/lib.js:519:23:519:26 | name |
595605
| lib/lib.js:509:39:509:42 | name | lib/lib.js:519:23:519:26 | name |
596606
| lib/lib.js:509:39:509:42 | name | lib/lib.js:519:23:519:26 | name |
607+
| lib/lib.js:509:39:509:42 | name | lib/lib.js:525:23:525:26 | name |
608+
| lib/lib.js:509:39:509:42 | name | lib/lib.js:525:23:525:26 | name |
609+
| lib/lib.js:509:39:509:42 | name | lib/lib.js:525:23:525:26 | name |
610+
| lib/lib.js:509:39:509:42 | name | lib/lib.js:525:23:525:26 | name |
611+
| lib/lib.js:509:39:509:42 | name | lib/lib.js:531:23:531:26 | name |
612+
| lib/lib.js:509:39:509:42 | name | lib/lib.js:531:23:531:26 | name |
613+
| lib/lib.js:509:39:509:42 | name | lib/lib.js:531:23:531:26 | name |
614+
| lib/lib.js:509:39:509:42 | name | lib/lib.js:531:23:531:26 | name |
615+
| lib/lib.js:509:39:509:42 | name | lib/lib.js:537:23:537:26 | name |
616+
| lib/lib.js:509:39:509:42 | name | lib/lib.js:537:23:537:26 | name |
617+
| lib/lib.js:509:39:509:42 | name | lib/lib.js:537:23:537:26 | name |
618+
| lib/lib.js:509:39:509:42 | name | lib/lib.js:537:23:537:26 | name |
619+
| lib/lib.js:509:39:509:42 | name | lib/lib.js:543:23:543:26 | name |
620+
| lib/lib.js:509:39:509:42 | name | lib/lib.js:543:23:543:26 | name |
621+
| lib/lib.js:509:39:509:42 | name | lib/lib.js:543:23:543:26 | name |
622+
| lib/lib.js:509:39:509:42 | name | lib/lib.js:543:23:543:26 | name |
623+
| lib/lib.js:509:39:509:42 | name | lib/lib.js:545:23:545:26 | name |
624+
| lib/lib.js:509:39:509:42 | name | lib/lib.js:545:23:545:26 | name |
625+
| lib/lib.js:509:39:509:42 | name | lib/lib.js:545:23:545:26 | name |
626+
| lib/lib.js:509:39:509:42 | name | lib/lib.js:545:23:545:26 | name |
597627
| lib/subLib2/compiled-file.ts:3:26:3:29 | name | lib/subLib2/compiled-file.ts:4:25:4:28 | name |
598628
| lib/subLib2/compiled-file.ts:3:26:3:29 | name | lib/subLib2/compiled-file.ts:4:25:4:28 | name |
599629
| lib/subLib2/compiled-file.ts:3:26:3:29 | name | lib/subLib2/compiled-file.ts:4:25:4:28 | name |
@@ -689,6 +719,11 @@ edges
689719
| lib/lib.js:510:10:510:25 | "rm -rf " + name | lib/lib.js:509:39:509:42 | name | lib/lib.js:510:22:510:25 | name | $@ based on $@ is later used in $@. | lib/lib.js:510:10:510:25 | "rm -rf " + name | String concatenation | lib/lib.js:509:39:509:42 | name | library input | lib/lib.js:510:2:510:26 | cp.exec ... + name) | shell command |
690720
| lib/lib.js:513:11:513:26 | "rm -rf " + name | lib/lib.js:509:39:509:42 | name | lib/lib.js:513:23:513:26 | name | $@ based on $@ is later used in $@. | lib/lib.js:513:11:513:26 | "rm -rf " + name | String concatenation | lib/lib.js:509:39:509:42 | name | library input | lib/lib.js:513:3:513:27 | cp.exec ... + name) | shell command |
691721
| lib/lib.js:519:11:519:26 | "rm -rf " + name | lib/lib.js:509:39:509:42 | name | lib/lib.js:519:23:519:26 | name | $@ based on $@ is later used in $@. | lib/lib.js:519:11:519:26 | "rm -rf " + name | String concatenation | lib/lib.js:509:39:509:42 | name | library input | lib/lib.js:519:3:519:27 | cp.exec ... + name) | shell command |
722+
| lib/lib.js:525:11:525:26 | "rm -rf " + name | lib/lib.js:509:39:509:42 | name | lib/lib.js:525:23:525:26 | name | $@ based on $@ is later used in $@. | lib/lib.js:525:11:525:26 | "rm -rf " + name | String concatenation | lib/lib.js:509:39:509:42 | name | library input | lib/lib.js:525:3:525:27 | cp.exec ... + name) | shell command |
723+
| lib/lib.js:531:11:531:26 | "rm -rf " + name | lib/lib.js:509:39:509:42 | name | lib/lib.js:531:23:531:26 | name | $@ based on $@ is later used in $@. | lib/lib.js:531:11:531:26 | "rm -rf " + name | String concatenation | lib/lib.js:509:39:509:42 | name | library input | lib/lib.js:531:3:531:27 | cp.exec ... + name) | shell command |
724+
| lib/lib.js:537:11:537:26 | "rm -rf " + name | lib/lib.js:509:39:509:42 | name | lib/lib.js:537:23:537:26 | name | $@ based on $@ is later used in $@. | lib/lib.js:537:11:537:26 | "rm -rf " + name | String concatenation | lib/lib.js:509:39:509:42 | name | library input | lib/lib.js:537:3:537:27 | cp.exec ... + name) | shell command |
725+
| lib/lib.js:543:11:543:26 | "rm -rf " + name | lib/lib.js:509:39:509:42 | name | lib/lib.js:543:23:543:26 | name | $@ based on $@ is later used in $@. | lib/lib.js:543:11:543:26 | "rm -rf " + name | String concatenation | lib/lib.js:509:39:509:42 | name | library input | lib/lib.js:543:3:543:27 | cp.exec ... + name) | shell command |
726+
| lib/lib.js:545:11:545:26 | "rm -rf " + name | lib/lib.js:509:39:509:42 | name | lib/lib.js:545:23:545:26 | name | $@ based on $@ is later used in $@. | lib/lib.js:545:11:545:26 | "rm -rf " + name | String concatenation | lib/lib.js:509:39:509:42 | name | library input | lib/lib.js:545:3:545:27 | cp.exec ... + name) | shell command |
692727
| lib/subLib2/compiled-file.ts:4:13:4:28 | "rm -rf " + name | lib/subLib2/compiled-file.ts:3:26:3:29 | name | lib/subLib2/compiled-file.ts:4:25:4:28 | name | $@ based on $@ is later used in $@. | lib/subLib2/compiled-file.ts:4:13:4:28 | "rm -rf " + name | String concatenation | lib/subLib2/compiled-file.ts:3:26:3:29 | name | library input | lib/subLib2/compiled-file.ts:4:5:4:29 | cp.exec ... + name) | shell command |
693728
| lib/subLib2/special-file.js:4:10:4:25 | "rm -rf " + name | lib/subLib2/special-file.js:3:28:3:31 | name | lib/subLib2/special-file.js:4:22:4:25 | name | $@ based on $@ is later used in $@. | lib/subLib2/special-file.js:4:10:4:25 | "rm -rf " + name | String concatenation | lib/subLib2/special-file.js:3:28:3:31 | name | library input | lib/subLib2/special-file.js:4:2:4:26 | cp.exec ... + name) | shell command |
694729
| lib/subLib3/my-file.ts:4:10:4:25 | "rm -rf " + name | lib/subLib3/my-file.ts:3:28:3:31 | name | lib/subLib3/my-file.ts:4:22:4:25 | name | $@ based on $@ is later used in $@. | lib/subLib3/my-file.ts:4:10:4:25 | "rm -rf " + name | String concatenation | lib/subLib3/my-file.ts:3:28:3:31 | name | library input | lib/subLib3/my-file.ts:4:2:4:26 | cp.exec ... + name) | shell command |

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,4 +520,28 @@ module.exports.sanitizer4 = function (name) {
520520
} else {
521521
cp.exec("rm -rf " + name); // OK
522522
}
523+
524+
if (isNaN(+name)) {
525+
cp.exec("rm -rf " + name); // NOT OK
526+
} else {
527+
cp.exec("rm -rf " + name); // OK
528+
}
529+
530+
if (isNaN(parseInt(name, 10))) {
531+
cp.exec("rm -rf " + name); // NOT OK
532+
} else {
533+
cp.exec("rm -rf " + name); // OK
534+
}
535+
536+
if (isNaN(name - 0)) {
537+
cp.exec("rm -rf " + name); // NOT OK
538+
} else {
539+
cp.exec("rm -rf " + name); // OK
540+
}
541+
542+
if (isNaN(name | 0)) { // <- not a sanitizer
543+
cp.exec("rm -rf " + name); // NOT OK
544+
} else {
545+
cp.exec("rm -rf " + name); // NOT OK
546+
}
523547
}

0 commit comments

Comments
 (0)