Skip to content

Commit fc66c90

Browse files
authored
Merge pull request github#11859 from erik-krogh/moreShell
JS: slightly broaden the regular expression that recognizes bad string-concats used as shell commands
2 parents 21e63a8 + 1189414 commit fc66c90

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

javascript/ql/lib/semmle/javascript/security/dataflow/UnsafeShellCommandConstructionCustomizations.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ module UnsafeShellCommandConstruction {
9292
StringConcatEndingInCommandExecutionSink() {
9393
this = root.getALeaf() and
9494
root = isExecutedAsShellCommand(DataFlow::TypeBackTracker::end(), sys) and
95-
exists(string prev | prev = this.getPreviousLeaf().getStringValue() |
96-
prev.regexpMatch(".* ('|\")?[0-9a-zA-Z/:_-]*")
97-
)
95+
exists(this.getPreviousLeaf().getStringValue()) // looks like a shell command construction that could be done safer, it has a known prefix
9896
}
9997

10098
override string getSinkType() { result = "string concatenation" }

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ nodes
5757
| lib/lib.js:64:41:64:44 | name |
5858
| lib/lib.js:65:22:65:25 | name |
5959
| lib/lib.js:65:22:65:25 | name |
60+
| lib/lib.js:69:27:69:30 | name |
61+
| lib/lib.js:69:27:69:30 | name |
6062
| lib/lib.js:71:28:71:31 | name |
6163
| lib/lib.js:71:28:71:31 | name |
6264
| lib/lib.js:73:21:73:24 | name |
@@ -116,6 +118,7 @@ nodes
116118
| lib/lib.js:181:15:181:52 | "'" + n ... ) + "'" |
117119
| lib/lib.js:181:21:181:24 | name |
118120
| lib/lib.js:181:21:181:46 | name.re ... "'\\''") |
121+
| lib/lib.js:181:21:181:46 | name.re ... "'\\''") |
119122
| lib/lib.js:182:22:182:27 | broken |
120123
| lib/lib.js:182:22:182:27 | broken |
121124
| lib/lib.js:186:34:186:37 | name |
@@ -385,6 +388,10 @@ edges
385388
| lib/lib.js:64:41:64:44 | name | lib/lib.js:65:22:65:25 | name |
386389
| lib/lib.js:64:41:64:44 | name | lib/lib.js:65:22:65:25 | name |
387390
| lib/lib.js:64:41:64:44 | name | lib/lib.js:65:22:65:25 | name |
391+
| lib/lib.js:64:41:64:44 | name | lib/lib.js:69:27:69:30 | name |
392+
| lib/lib.js:64:41:64:44 | name | lib/lib.js:69:27:69:30 | name |
393+
| lib/lib.js:64:41:64:44 | name | lib/lib.js:69:27:69:30 | name |
394+
| lib/lib.js:64:41:64:44 | name | lib/lib.js:69:27:69:30 | name |
388395
| lib/lib.js:64:41:64:44 | name | lib/lib.js:71:28:71:31 | name |
389396
| lib/lib.js:64:41:64:44 | name | lib/lib.js:71:28:71:31 | name |
390397
| lib/lib.js:64:41:64:44 | name | lib/lib.js:71:28:71:31 | name |
@@ -463,6 +470,7 @@ edges
463470
| lib/lib.js:181:6:181:52 | broken | lib/lib.js:182:22:182:27 | broken |
464471
| lib/lib.js:181:15:181:52 | "'" + n ... ) + "'" | lib/lib.js:181:6:181:52 | broken |
465472
| lib/lib.js:181:21:181:24 | name | lib/lib.js:181:21:181:46 | name.re ... "'\\''") |
473+
| lib/lib.js:181:21:181:24 | name | lib/lib.js:181:21:181:46 | name.re ... "'\\''") |
466474
| lib/lib.js:181:21:181:46 | name.re ... "'\\''") | lib/lib.js:181:15:181:52 | "'" + n ... ) + "'" |
467475
| lib/lib.js:186:34:186:37 | name | lib/lib.js:187:22:187:25 | name |
468476
| lib/lib.js:186:34:186:37 | name | lib/lib.js:187:22:187:25 | name |
@@ -724,6 +732,7 @@ edges
724732
| lib/lib.js:54:13:54:28 | "rm -rf " + name | lib/lib.js:53:33:53:36 | name | lib/lib.js:54:25:54:28 | name | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:53:33:53:36 | name | library input | lib/lib.js:55:2:55:14 | cp.exec(cmd1) | shell command |
725733
| lib/lib.js:57:13:57:28 | "rm -rf " + name | lib/lib.js:53:33:53:36 | name | lib/lib.js:57:25:57:28 | name | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:53:33:53:36 | name | library input | lib/lib.js:59:3:59:14 | cp.exec(cmd) | shell command |
726734
| lib/lib.js:65:10:65:25 | "rm -rf " + name | lib/lib.js:64:41:64:44 | name | lib/lib.js:65:22:65:25 | name | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:64:41:64:44 | name | library input | lib/lib.js:65:2:65:26 | cp.exec ... + name) | shell command |
735+
| lib/lib.js:69:10:69:47 | "for fo ... la end" | lib/lib.js:64:41:64:44 | name | lib/lib.js:69:27:69:30 | name | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:64:41:64:44 | name | library input | lib/lib.js:69:2:69:48 | cp.exec ... a end") | shell command |
727736
| lib/lib.js:71:10:71:31 | "cat /f ... + name | lib/lib.js:64:41:64:44 | name | lib/lib.js:71:28:71:31 | name | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:64:41:64:44 | name | library input | lib/lib.js:71:2:71:32 | cp.exec ... + name) | shell command |
728737
| lib/lib.js:73:10:73:31 | "cat \\" ... + "\\"" | lib/lib.js:64:41:64:44 | name | lib/lib.js:73:21:73:24 | name | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:64:41:64:44 | name | library input | lib/lib.js:73:2:73:32 | cp.exec ... + "\\"") | shell command |
729738
| lib/lib.js:75:10:75:29 | "cat '" + name + "'" | lib/lib.js:64:41:64:44 | name | lib/lib.js:75:20:75:23 | name | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:64:41:64:44 | name | library input | lib/lib.js:75:2:75:30 | cp.exec ... + "'") | shell command |
@@ -742,6 +751,7 @@ edges
742751
| lib/lib.js:149:12:149:27 | "rm -rf " + name | lib/lib.js:148:37:148:40 | name | lib/lib.js:149:24:149:27 | name | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:148:37:148:40 | name | library input | lib/lib.js:152:2:152:23 | cp.spaw ... gs, cb) | shell command |
743752
| lib/lib.js:161:13:161:28 | "rm -rf " + name | lib/lib.js:155:38:155:41 | name | lib/lib.js:161:25:161:28 | name | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:155:38:155:41 | name | library input | lib/lib.js:163:2:167:2 | cp.spaw ... t' }\\n\\t) | shell command |
744753
| lib/lib.js:173:10:173:23 | "fo \| " + name | lib/lib.js:170:41:170:44 | name | lib/lib.js:173:20:173:23 | name | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:170:41:170:44 | name | library input | lib/lib.js:173:2:173:24 | cp.exec ... + name) | shell command |
754+
| lib/lib.js:181:15:181:52 | "'" + n ... ) + "'" | lib/lib.js:177:38:177:41 | name | lib/lib.js:181:21:181:46 | name.re ... "'\\''") | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:177:38:177:41 | name | library input | lib/lib.js:182:2:182:28 | cp.exec ... broken) | shell command |
745755
| lib/lib.js:182:10:182:27 | "rm -rf " + broken | lib/lib.js:177:38:177:41 | name | lib/lib.js:182:22:182:27 | broken | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:177:38:177:41 | name | library input | lib/lib.js:182:2:182:28 | cp.exec ... broken) | shell command |
746756
| lib/lib.js:187:10:187:25 | "rm -rf " + name | lib/lib.js:186:34:186:37 | name | lib/lib.js:187:22:187:25 | name | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:186:34:186:37 | name | library input | lib/lib.js:187:2:187:26 | cp.exec ... + name) | shell command |
747757
| lib/lib.js:190:11:190:26 | "rm -rf " + name | lib/lib.js:186:34:186:37 | name | lib/lib.js:190:23:190:26 | name | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:186:34:186:37 | name | library input | lib/lib.js:190:3:190:27 | cp.exec ... + name) | shell command |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ module.exports.stringConcat = function (name) {
6666

6767
cp.exec(name); // OK.
6868

69-
cp.exec("for foo in (" + name + ") do bla end"); // OK.
69+
cp.exec("for foo in (" + name + ") do bla end"); // NOT OK.
7070

7171
cp.exec("cat /foO/BAR/" + name) // NOT OK.
7272

0 commit comments

Comments
 (0)