Skip to content

Commit 155f1fc

Browse files
committed
JS: Added test cases for unsafe shell command sanitization with RegExpr Object, instead of literal
1 parent 23b18ae commit 155f1fc

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| UnsafeShellCommandConstruction/lib/lib.js:640 | did not expect an alert, but found an alert for UnsafeShellCommandConstruction | OK -- Currently this is flagged as a bad sanitization, but it is not certain that it is bad. | ComandInjection |

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,22 @@ nodes
319319
| lib/lib.js:626:29:626:32 | name |
320320
| lib/lib.js:629:25:629:28 | name |
321321
| lib/lib.js:629:25:629:28 | name |
322+
| lib/lib.js:632:38:632:41 | name |
323+
| lib/lib.js:632:38:632:41 | name |
324+
| lib/lib.js:633:6:633:68 | sanitized |
325+
| lib/lib.js:633:18:633:68 | "'" + n ... ) + "'" |
326+
| lib/lib.js:633:24:633:27 | name |
327+
| lib/lib.js:633:24:633:62 | name.re ... '\\\\''") |
328+
| lib/lib.js:633:24:633:62 | name.re ... '\\\\''") |
329+
| lib/lib.js:634:22:634:30 | sanitized |
330+
| lib/lib.js:634:22:634:30 | sanitized |
331+
| lib/lib.js:639:6:639:84 | sanitized |
332+
| lib/lib.js:639:18:639:84 | "'" + n ... ) + "'" |
333+
| lib/lib.js:639:24:639:27 | name |
334+
| lib/lib.js:639:24:639:78 | name.re ... '\\\\''") |
335+
| lib/lib.js:639:24:639:78 | name.re ... '\\\\''") |
336+
| lib/lib.js:640:22:640:30 | sanitized |
337+
| lib/lib.js:640:22:640:30 | sanitized |
322338
| lib/subLib2/compiled-file.ts:3:26:3:29 | name |
323339
| lib/subLib2/compiled-file.ts:3:26:3:29 | name |
324340
| lib/subLib2/compiled-file.ts:4:25:4:28 | name |
@@ -749,6 +765,22 @@ edges
749765
| lib/lib.js:608:42:608:45 | name | lib/lib.js:629:25:629:28 | name |
750766
| lib/lib.js:608:42:608:45 | name | lib/lib.js:629:25:629:28 | name |
751767
| lib/lib.js:608:42:608:45 | name | lib/lib.js:629:25:629:28 | name |
768+
| lib/lib.js:632:38:632:41 | name | lib/lib.js:633:24:633:27 | name |
769+
| lib/lib.js:632:38:632:41 | name | lib/lib.js:633:24:633:27 | name |
770+
| lib/lib.js:632:38:632:41 | name | lib/lib.js:639:24:639:27 | name |
771+
| lib/lib.js:632:38:632:41 | name | lib/lib.js:639:24:639:27 | name |
772+
| lib/lib.js:633:6:633:68 | sanitized | lib/lib.js:634:22:634:30 | sanitized |
773+
| lib/lib.js:633:6:633:68 | sanitized | lib/lib.js:634:22:634:30 | sanitized |
774+
| lib/lib.js:633:18:633:68 | "'" + n ... ) + "'" | lib/lib.js:633:6:633:68 | sanitized |
775+
| lib/lib.js:633:24:633:27 | name | lib/lib.js:633:24:633:62 | name.re ... '\\\\''") |
776+
| lib/lib.js:633:24:633:27 | name | lib/lib.js:633:24:633:62 | name.re ... '\\\\''") |
777+
| lib/lib.js:633:24:633:62 | name.re ... '\\\\''") | lib/lib.js:633:18:633:68 | "'" + n ... ) + "'" |
778+
| lib/lib.js:639:6:639:84 | sanitized | lib/lib.js:640:22:640:30 | sanitized |
779+
| lib/lib.js:639:6:639:84 | sanitized | lib/lib.js:640:22:640:30 | sanitized |
780+
| lib/lib.js:639:18:639:84 | "'" + n ... ) + "'" | lib/lib.js:639:6:639:84 | sanitized |
781+
| lib/lib.js:639:24:639:27 | name | lib/lib.js:639:24:639:78 | name.re ... '\\\\''") |
782+
| lib/lib.js:639:24:639:27 | name | lib/lib.js:639:24:639:78 | name.re ... '\\\\''") |
783+
| lib/lib.js:639:24:639:78 | name.re ... '\\\\''") | lib/lib.js:639:18:639:84 | "'" + n ... ) + "'" |
752784
| lib/subLib2/compiled-file.ts:3:26:3:29 | name | lib/subLib2/compiled-file.ts:4:25:4:28 | name |
753785
| lib/subLib2/compiled-file.ts:3:26:3:29 | name | lib/subLib2/compiled-file.ts:4:25:4:28 | name |
754786
| lib/subLib2/compiled-file.ts:3:26:3:29 | name | lib/subLib2/compiled-file.ts:4:25:4:28 | name |
@@ -879,6 +911,10 @@ edges
879911
| lib/lib.js:609:10:609:25 | "rm -rf " + name | lib/lib.js:608:42:608:45 | name | lib/lib.js:609:22:609:25 | name | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:608:42:608:45 | name | library input | lib/lib.js:609:2:609:26 | cp.exec ... + name) | shell command |
880912
| lib/lib.js:626:17:626:32 | "rm -rf " + name | lib/lib.js:608:42:608:45 | name | lib/lib.js:626:29:626:32 | name | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:608:42:608:45 | name | library input | lib/lib.js:626:9:626:33 | cp.exec ... + name) | shell command |
881913
| lib/lib.js:629:13:629:28 | "rm -rf " + name | lib/lib.js:608:42:608:45 | name | lib/lib.js:629:25:629:28 | name | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:608:42:608:45 | name | library input | lib/lib.js:629:5:629:29 | cp.exec ... + name) | shell command |
914+
| lib/lib.js:633:18:633:68 | "'" + n ... ) + "'" | lib/lib.js:632:38:632:41 | name | lib/lib.js:633:24:633:62 | name.re ... '\\\\''") | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:632:38:632:41 | name | library input | lib/lib.js:634:2:634:31 | cp.exec ... itized) | shell command |
915+
| lib/lib.js:634:10:634:30 | "rm -rf ... nitized | lib/lib.js:632:38:632:41 | name | lib/lib.js:634:22:634:30 | sanitized | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:632:38:632:41 | name | library input | lib/lib.js:634:2:634:31 | cp.exec ... itized) | shell command |
916+
| lib/lib.js:639:18:639:84 | "'" + n ... ) + "'" | lib/lib.js:632:38:632:41 | name | lib/lib.js:639:24:639:78 | name.re ... '\\\\''") | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:632:38:632:41 | name | library input | lib/lib.js:640:2:640:31 | cp.exec ... itized) | shell command |
917+
| lib/lib.js:640:10:640:30 | "rm -rf ... nitized | lib/lib.js:632:38:632:41 | name | lib/lib.js:640:22:640:30 | sanitized | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:632:38:632:41 | name | library input | lib/lib.js:640:2:640:31 | cp.exec ... itized) | shell command |
882918
| 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 | This string concatenation which depends on $@ is later used in a $@. | 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 |
883919
| 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 | This string concatenation which depends on $@ is later used in a $@. | 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 |
884920
| 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 | This string concatenation which depends on $@ is later used in a $@. | 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/UnsafeShellCommandConstruction/lib/lib.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,3 +628,14 @@ module.exports.veryIndeirect = function (name) {
628628

629629
cp.exec("rm -rf " + name); // NOT OK
630630
}
631+
632+
module.exports.sanitizer = function (name) {
633+
var sanitized = "'" + name.replace(new RegExp("\'"), "'\\''") + "'"
634+
cp.exec("rm -rf " + sanitized); // NOT OK
635+
636+
var sanitized = "'" + name.replace(new RegExp("\'", 'g'), "'\\''") + "'"
637+
cp.exec("rm -rf " + sanitized); // OK
638+
639+
var sanitized = "'" + name.replace(new RegExp("\'", unknownFlags()), "'\\''") + "'"
640+
cp.exec("rm -rf " + sanitized); // OK -- Currently this is flagged as a bad sanitization, but it is not certain that it is bad.
641+
}

0 commit comments

Comments
 (0)