From ae2c7ee4c5e4e76eda5107717cb0293eaddb3cbb Mon Sep 17 00:00:00 2001 From: Owen Mansel-Chan Date: Fri, 27 Nov 2026 13:19:11 +0000 Subject: [PATCH] JS: Ignore positional parameters to sh -c in command injection Only treat the command_string operand of sh/bash -c invocations as a command-injection sink when its position can be determined, ignoring subsequent positional parameters ($0, $1, ...). Fall back to existing whole-array behavior when array mutations, spreads, or uncertain flags make the command_string position ambiguous, and preserve Windows cmd.exe /c behavior. Fixes #23274 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../2026-11-27-sh-c-positional-parameters.md | 4 + .../dataflow/IndirectCommandArgument.qll | 187 ++++++++++++++++-- .../CommandInjection.expected | 43 +++- .../CommandInjection/child_process-test.js | 32 ++- .../IndirectCommandInjection.expected | 19 ++ ...ommand-line-parameter-command-injection.js | 7 + ...llCommandInjectionFromEnvironment.expected | 14 +- ...hell-command-injection-from-environment.js | 3 + .../UnsafeShellCommandConstruction.expected | 4 + .../UnsafeShellCommandConstruction/lib/lib.js | 5 + 10 files changed, 289 insertions(+), 29 deletions(-) create mode 100644 javascript/ql/lib/change-notes/2026-11-27-sh-c-positional-parameters.md diff --git a/javascript/ql/lib/change-notes/2026-11-27-sh-c-positional-parameters.md b/javascript/ql/lib/change-notes/2026-11-27-sh-c-positional-parameters.md new file mode 100644 index 000000000000..80cc68c123f0 --- /dev/null +++ b/javascript/ql/lib/change-notes/2026-11-27-sh-c-positional-parameters.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* Fixed false positives in the command injection queries when untrusted data is passed as positional parameters (`$0`, `$1`, ...) to `sh -c` or `bash -c` rather than as the command string itself. diff --git a/javascript/ql/lib/semmle/javascript/security/dataflow/IndirectCommandArgument.qll b/javascript/ql/lib/semmle/javascript/security/dataflow/IndirectCommandArgument.qll index 6d6c9c3d75fa..779fe1587d28 100644 --- a/javascript/ql/lib/semmle/javascript/security/dataflow/IndirectCommandArgument.qll +++ b/javascript/ql/lib/semmle/javascript/security/dataflow/IndirectCommandArgument.qll @@ -5,20 +5,20 @@ import javascript /** - * Holds if `shell arg ` runs `` as a shell command. - * - * That is, either `shell` is a Unix shell (`sh` or similar) and - * `arg` is `"-c"`, or `shell` is `cmd.exe` and `arg` is `"/c"`. + * Holds if `shell arg ` runs `` as a shell command, where `isUnix` indicates whether + * `shell` is a POSIX-style shell (`sh` or `bash`) rather than Windows `cmd.exe`. */ -private predicate shellCmd(Expr shell, string arg) { +private predicate shellCmd(Expr shell, string arg, boolean isUnix) { exists(string s | s = shell.getStringValue() | s = ["sh", "bash", "/bin/sh", "/bin/bash"] and - arg = "-c" + arg = "-c" and + isUnix = true ) or exists(string s | s = shell.getStringValue().toLowerCase() | s = ["cmd", "cmd.exe"] and - arg = ["/c", "/C"] + arg = ["/c", "/C"] and + isUnix = false ) } @@ -84,9 +84,160 @@ private DataFlow::Node argumentListElement(DataFlow::SourceNode args) { } /** - * Holds if `source` contributes to the arguments of an indirect command execution `sys`. + * Gets a data-flow node whose elements form a prefix of the argument list in `sys` + * after a flow summarized by `t`. + * + * Unlike `argumentList`, this only steps through value-preserving flow and `.concat(...)` + * receivers, preserving element indices from index 0 up to the array's length. + */ +private DataFlow::SourceNode argumentListPrefix( + SystemCommandExecution sys, DataFlow::TypeBackTracker t +) { + t.start() and + result = sys.getArgumentList().getALocalSource() + or + exists(DataFlow::TypeBackTracker t2, DataFlow::SourceNode pred | + pred = argumentListPrefix(sys, t2) + | + result = pred.backtrack(t2, t) + or + t = t2.continue() and + pred.(DataFlow::MethodCallNode).getMethodName() = "concat" and + result = pred.(DataFlow::MethodCallNode).getReceiver().getALocalSource() + ) +} + +/** + * Gets a data-flow node whose elements form a prefix of the argument list in `sys`. + */ +private DataFlow::SourceNode argumentListPrefix(SystemCommandExecution sys) { + result = argumentListPrefix(sys, DataFlow::TypeBackTracker::end()) +} + +/** + * Holds if `args` has no mutating operations that could shift or overwrite prefix indices + * dynamically. + */ +private predicate hasUnmodifiedPrefix(DataFlow::ArrayCreationNode args) { + not exists( + args.getAMethodCall([ + "pop", "unshift", "shift", "splice", "reverse", "sort", "copyWithin", "fill" + ]) + ) and + not exists(args.getAPropertyWrite("length")) and + not exists(DataFlow::PropWrite pw | pw = args.getAPropertyWrite() | + exists(pw.getPropertyNameExpr()) and + not exists(pw.getPropertyName()) + ) +} + +/** + * Gets a data-flow node written to non-negative integer index `i` of `args`, provided no spread + * element precedes or occupies index `i`. + */ +private DataFlow::Node arrayElementAt(DataFlow::ArrayCreationNode args, int i) { + args = argumentListPrefix(_) and + not exists(int j | j in [0 .. i] | args.getElement(j).asExpr() instanceof SpreadElement) and + ( + result = args.getElement(i) + or + exists(string prop | + result = args.getAPropertyWrite(prop).getRhs() and + i = prop.toInt() and + i >= 0 and + prop = i.toString() + ) + ) +} + +/** + * Gets the unique string value that element `i` of `args` may have, if all writes to index `i` + * resolve to that single string constant. + */ +private string uniqueElementString(DataFlow::ArrayCreationNode args, int i) { + forex(DataFlow::Node elem | elem = arrayElementAt(args, i) | + exists(string s | elem.mayHaveStringValue(s)) + ) and + result = unique(string s | arrayElementAt(args, i).mayHaveStringValue(s)) +} + +/** + * Gets the number of subsequent arguments consumed as option-arguments by Unix shell option + * token `opt` (for example `1` for `-o`, `-euo`, `-oe`, or `--rcfile`). + */ +bindingset[opt] +private int shellOptionArgCount(string opt) { + if opt = ["--rcfile", "--init-file"] + then result = 1 + else + if opt.regexpMatch("[-+][a-zA-Z]+") + then result = count(int k | opt.charAt(k) = ["o", "O"]) + else result = 0 +} + +/** + * Holds if `i` is the index of a shell option or the first non-option operand in `args`. + */ +private predicate unixOptionScanIndex(DataFlow::ArrayCreationNode args, int i) { + args = argumentListPrefix(_) and + hasUnmodifiedPrefix(args) and + exists(arrayElementAt(args, 0)) and + i = 0 + or + exists(int prev, string opt, int argCount | + unixOptionScanIndex(args, prev) and + opt = uniqueElementString(args, prev) and + opt.charAt(0) = ["-", "+"] and + not opt = ["-", "--"] and + argCount = shellOptionArgCount(opt) and + forall(int k | k in [1 .. argCount] | exists(arrayElementAt(args, prev + k))) and + i = prev + 1 + argCount + ) and + exists(arrayElementAt(args, i)) +} + +/** + * Gets the index of the `command_string` operand for a Unix `sh -c` invocation using `args`, + * if it can be determined unambiguously from `args`. + */ +private int unixCommandStringIndex(DataFlow::ArrayCreationNode args) { + exists(int cIdx | + unixOptionScanIndex(args, cIdx) and + uniqueElementString(args, cIdx).regexpMatch("-[a-zA-Z]*c[a-zA-Z]*") + ) and + ( + exists(int endOpts | + unixOptionScanIndex(args, endOpts) and + uniqueElementString(args, endOpts) = ["-", "--"] and + result = endOpts + 1 and + exists(arrayElementAt(args, result)) + ) + or + unixOptionScanIndex(args, result) and + not any(string s | arrayElementAt(args, result).mayHaveStringValue(s)).charAt(0) = ["-", "+"] + ) +} + +/** + * Holds if every origin of the prefix of `sys`'s argument list is an array creation with a + * known Unix `-c` command-string index. + */ +private predicate prefixFixesUnixCommand(SystemCommandExecution sys) { + forex(DataFlow::SourceNode leaf | + leaf = argumentListPrefix(sys) and + not leaf.(DataFlow::MethodCallNode).getMethodName() = "concat" and + not exists(DataFlow::TypeBackTracker t0 | t0.start() and exists(leaf.backtrack(t0, _))) + | + exists(unixCommandStringIndex(leaf)) + ) +} + +/** + * Holds if `source` contributes to the command executed by an indirect command execution `sys`. * * An indirect command execution is a system execution command that starts with `sh -c`, `cmd.exe /c`, or similar. + * For POSIX shells (`sh -c`, `bash -c`), only the `command_string` operand is interpreted as shell code when its + * position can be determined; subsequent arguments (`$0`, `$1`, ...) are positional parameters rather than commands. * * For example, `getCommand()` is `source`, and the call to `childProcess.spawn` is `sys` in the following example: * @@ -103,15 +254,21 @@ private DataFlow::Node argumentListElement(DataFlow::SourceNode args) { * ``` */ predicate isIndirectCommandArgument(DataFlow::Node source, SystemCommandExecution sys) { - exists(DataFlow::ArrayCreationNode args, DataFlow::Node shell, string dashC | - shellCmd(shell.asExpr(), dashC) and + exists(DataFlow::ArrayCreationNode args, DataFlow::Node shell, string dashC, boolean isUnix | + shellCmd(shell.asExpr(), dashC, isUnix) and shell = commandArgument(sys) and args = argumentList(sys) and - argumentListElement(args).mayHaveStringValue(dashC) and - exists(DataFlow::SourceNode argsSource | argsSource = argumentList(sys) | - if exists(argsSource.getAPropertyWrite()) - then source = argsSource.getAPropertyWrite().getRhs() - else source = argsSource + argumentListElement(args).mayHaveStringValue(dashC) + | + if isUnix = true and args = argumentListPrefix(sys) and exists(unixCommandStringIndex(args)) + then source = arrayElementAt(args, unixCommandStringIndex(args)) + else ( + not (isUnix = true and not args = argumentListPrefix(sys) and prefixFixesUnixCommand(sys)) and + exists(DataFlow::SourceNode argsSource | argsSource = argumentList(sys) | + if exists(argsSource.getAPropertyWrite()) + then source = argsSource.getAPropertyWrite().getRhs() + else source = argsSource + ) ) ) } diff --git a/javascript/ql/test/query-tests/Security/CWE-078/CommandInjection/CommandInjection.expected b/javascript/ql/test/query-tests/Security/CWE-078/CommandInjection/CommandInjection.expected index 2fecf64d818f..565efdace164 100644 --- a/javascript/ql/test/query-tests/Security/CWE-078/CommandInjection/CommandInjection.expected +++ b/javascript/ql/test/query-tests/Security/CWE-078/CommandInjection/CommandInjection.expected @@ -17,7 +17,6 @@ | child_process-test.js:57:5:57:50 | cp.spaw ... t(cmd)) | child_process-test.js:6:25:6:31 | req.url | child_process-test.js:6:15:6:49 | url.par ... ry.path | This command line depends on a $@. | child_process-test.js:6:25:6:31 | req.url | user-provided value | | child_process-test.js:57:5:57:50 | cp.spaw ... t(cmd)) | child_process-test.js:6:25:6:31 | req.url | child_process-test.js:57:25:57:49 | ['/C', ... at(cmd) | This command line depends on a $@. | child_process-test.js:6:25:6:31 | req.url | user-provided value | | child_process-test.js:67:3:67:21 | cp.spawn(cmd, args) | child_process-test.js:6:25:6:31 | req.url | child_process-test.js:48:15:48:17 | cmd | This command line depends on a $@. | child_process-test.js:6:25:6:31 | req.url | user-provided value | -| child_process-test.js:67:3:67:21 | cp.spawn(cmd, args) | child_process-test.js:6:25:6:31 | req.url | child_process-test.js:66:19:66:22 | args | This command line depends on a $@. | child_process-test.js:6:25:6:31 | req.url | user-provided value | | child_process-test.js:75:29:75:31 | cmd | child_process-test.js:73:25:73:31 | req.url | child_process-test.js:75:29:75:31 | cmd | This command line depends on a $@. | child_process-test.js:73:25:73:31 | req.url | user-provided value | | child_process-test.js:83:19:83:36 | req.query.fileName | child_process-test.js:83:19:83:36 | req.query.fileName | child_process-test.js:83:19:83:36 | req.query.fileName | This command line depends on a $@. | child_process-test.js:83:19:83:36 | req.query.fileName | user-provided value | | child_process-test.js:94:11:94:35 | "ping " ... ms.host | child_process-test.js:94:21:94:30 | ctx.params | child_process-test.js:94:11:94:35 | "ping " ... ms.host | This command line depends on a $@. | child_process-test.js:94:21:94:30 | ctx.params | user-provided value | @@ -44,6 +43,14 @@ | child_process-test.js:184:26:184:29 | args | child_process-test.js:98:28:98:34 | req.url | child_process-test.js:184:26:184:29 | args | This command line depends on a $@. | child_process-test.js:98:28:98:34 | req.url | user-provided value | | child_process-test.js:185:29:185:32 | args | child_process-test.js:98:28:98:34 | req.url | child_process-test.js:185:29:185:32 | args | This command line depends on a $@. | child_process-test.js:98:28:98:34 | req.url | user-provided value | | child_process-test.js:191:26:191:31 | [name] | child_process-test.js:98:28:98:34 | req.url | child_process-test.js:191:26:191:31 | [name] | This command line depends on a $@. | child_process-test.js:98:28:98:34 | req.url | user-provided value | +| child_process-test.js:225:5:225:68 | cp.exec ... => {}) | child_process-test.js:207:25:207:31 | req.url | child_process-test.js:225:30:225:42 | "grep " + cmd | This command line depends on a $@. | child_process-test.js:207:25:207:31 | req.url | user-provided value | +| child_process-test.js:226:5:226:37 | cp.spaw ... , cmd]) | child_process-test.js:207:25:207:31 | req.url | child_process-test.js:226:33:226:35 | cmd | This command line depends on a $@. | child_process-test.js:207:25:207:31 | req.url | user-provided value | +| child_process-test.js:227:5:227:37 | cp.spaw ... , cmd]) | child_process-test.js:207:25:207:31 | req.url | child_process-test.js:227:33:227:35 | cmd | This command line depends on a $@. | child_process-test.js:207:25:207:31 | req.url | user-provided value | +| child_process-test.js:228:5:228:37 | cp.spaw ... , cmd]) | child_process-test.js:207:25:207:31 | req.url | child_process-test.js:228:33:228:35 | cmd | This command line depends on a $@. | child_process-test.js:207:25:207:31 | req.url | user-provided value | +| child_process-test.js:229:5:229:53 | cp.spaw ... , cmd]) | child_process-test.js:207:25:207:31 | req.url | child_process-test.js:229:49:229:51 | cmd | This command line depends on a $@. | child_process-test.js:207:25:207:31 | req.url | user-provided value | +| child_process-test.js:230:5:230:38 | cp.spaw ... t(cmd)) | child_process-test.js:207:25:207:31 | req.url | child_process-test.js:207:15:207:49 | url.par ... ry.path | This command line depends on a $@. | child_process-test.js:207:25:207:31 | req.url | user-provided value | +| child_process-test.js:230:5:230:38 | cp.spaw ... t(cmd)) | child_process-test.js:207:25:207:31 | req.url | child_process-test.js:230:20:230:37 | ["-c"].concat(cmd) | This command line depends on a $@. | child_process-test.js:207:25:207:31 | req.url | user-provided value | +| child_process-test.js:233:5:233:31 | cp.spaw ... edArgs) | child_process-test.js:207:25:207:31 | req.url | child_process-test.js:207:15:207:49 | url.par ... ry.path | This command line depends on a $@. | child_process-test.js:207:25:207:31 | req.url | user-provided value | | command-line-libs.js:14:8:14:18 | options.cmd | command-line-libs.js:9:16:9:23 | req.body | command-line-libs.js:14:8:14:18 | options.cmd | This command line depends on a $@. | command-line-libs.js:9:16:9:23 | req.body | user-provided value | | command-line-libs.js:15:8:15:18 | program.cmd | command-line-libs.js:9:16:9:23 | req.body | command-line-libs.js:15:8:15:18 | program.cmd | This command line depends on a $@. | command-line-libs.js:9:16:9:23 | req.body | user-provided value | | command-line-libs.js:21:12:21:17 | script | command-line-libs.js:9:16:9:23 | req.body | command-line-libs.js:21:12:21:17 | script | This command line depends on a $@. | command-line-libs.js:9:16:9:23 | req.body | user-provided value | @@ -153,7 +160,6 @@ edges | child_process-test.js:6:9:6:11 | cmd | child_process-test.js:39:26:39:28 | cmd | provenance | | | child_process-test.js:6:9:6:11 | cmd | child_process-test.js:43:15:43:17 | cmd | provenance | | | child_process-test.js:6:9:6:11 | cmd | child_process-test.js:48:15:48:17 | cmd | provenance | | -| child_process-test.js:6:9:6:11 | cmd | child_process-test.js:48:15:48:17 | cmd | provenance | | | child_process-test.js:6:9:6:11 | cmd | child_process-test.js:53:15:53:17 | cmd | provenance | | | child_process-test.js:6:9:6:11 | cmd | child_process-test.js:56:54:56:56 | cmd | provenance | | | child_process-test.js:6:9:6:11 | cmd | child_process-test.js:56:54:56:56 | cmd | provenance | | @@ -164,9 +170,6 @@ edges | child_process-test.js:6:15:6:49 | url.par ... ry.path | child_process-test.js:6:9:6:11 | cmd | provenance | | | child_process-test.js:6:25:6:31 | req.url | child_process-test.js:6:15:6:38 | url.par ... , true) | provenance | | | child_process-test.js:25:21:25:23 | cmd | child_process-test.js:25:13:25:31 | "foo" + cmd + "bar" | provenance | | -| child_process-test.js:48:5:48:8 | [post update] args [1] | child_process-test.js:49:15:49:18 | args [1] | provenance | | -| child_process-test.js:48:15:48:17 | cmd | child_process-test.js:48:5:48:8 | [post update] args [1] | provenance | | -| child_process-test.js:49:15:49:18 | args [1] | child_process-test.js:66:19:66:22 | args | provenance | | | child_process-test.js:56:46:56:57 | ["bar", cmd] [1] | child_process-test.js:56:25:56:58 | ['/C', ... , cmd]) | provenance | | | child_process-test.js:56:54:56:56 | cmd | child_process-test.js:56:46:56:57 | ["bar", cmd] [1] | provenance | | | child_process-test.js:57:46:57:48 | cmd | child_process-test.js:57:25:57:49 | ['/C', ... at(cmd) | provenance | | @@ -290,6 +293,19 @@ edges | child_process-test.js:189:14:189:27 | runWithOptions [name] | child_process-test.js:193:5:193:18 | runWithOptions [name] | provenance | | | child_process-test.js:191:27:191:30 | name | child_process-test.js:191:26:191:31 | [name] | provenance | | | child_process-test.js:193:5:193:18 | runWithOptions [name] | child_process-test.js:191:27:191:30 | name | provenance | | +| child_process-test.js:207:9:207:11 | cmd | child_process-test.js:225:40:225:42 | cmd | provenance | | +| child_process-test.js:207:9:207:11 | cmd | child_process-test.js:226:33:226:35 | cmd | provenance | | +| child_process-test.js:207:9:207:11 | cmd | child_process-test.js:227:33:227:35 | cmd | provenance | | +| child_process-test.js:207:9:207:11 | cmd | child_process-test.js:228:33:228:35 | cmd | provenance | | +| child_process-test.js:207:9:207:11 | cmd | child_process-test.js:229:49:229:51 | cmd | provenance | | +| child_process-test.js:207:9:207:11 | cmd | child_process-test.js:230:34:230:36 | cmd | provenance | | +| child_process-test.js:207:15:207:38 | url.par ... , true) | child_process-test.js:207:9:207:11 | cmd | provenance | | +| child_process-test.js:207:15:207:38 | url.par ... , true) | child_process-test.js:207:15:207:49 | url.par ... ry.path | provenance | | +| child_process-test.js:207:15:207:38 | url.par ... , true) | child_process-test.js:207:15:207:49 | url.par ... ry.path | provenance | | +| child_process-test.js:207:15:207:49 | url.par ... ry.path | child_process-test.js:207:9:207:11 | cmd | provenance | | +| child_process-test.js:207:25:207:31 | req.url | child_process-test.js:207:15:207:38 | url.par ... , true) | provenance | | +| child_process-test.js:225:40:225:42 | cmd | child_process-test.js:225:30:225:42 | "grep " + cmd | provenance | | +| child_process-test.js:230:34:230:36 | cmd | child_process-test.js:230:20:230:37 | ["-c"].concat(cmd) | provenance | | | command-line-libs.js:9:9:9:12 | args | command-line-libs.js:12:17:12:20 | args | provenance | | | command-line-libs.js:9:9:9:12 | args | command-line-libs.js:23:29:23:32 | args | provenance | | | command-line-libs.js:9:16:9:23 | req.body | command-line-libs.js:9:9:9:12 | args | provenance | | @@ -510,10 +526,7 @@ nodes | child_process-test.js:25:21:25:23 | cmd | semmle.label | cmd | | child_process-test.js:39:26:39:28 | cmd | semmle.label | cmd | | child_process-test.js:43:15:43:17 | cmd | semmle.label | cmd | -| child_process-test.js:48:5:48:8 | [post update] args [1] | semmle.label | [post update] args [1] | -| child_process-test.js:48:15:48:17 | cmd | semmle.label | cmd | | child_process-test.js:48:15:48:17 | cmd | semmle.label | cmd | -| child_process-test.js:49:15:49:18 | args [1] | semmle.label | args [1] | | child_process-test.js:53:15:53:17 | cmd | semmle.label | cmd | | child_process-test.js:56:25:56:58 | ['/C', ... , cmd]) | semmle.label | ['/C', ... , cmd]) | | child_process-test.js:56:46:56:57 | ["bar", cmd] [1] | semmle.label | ["bar", cmd] [1] | @@ -521,7 +534,6 @@ nodes | child_process-test.js:56:54:56:56 | cmd | semmle.label | cmd | | child_process-test.js:57:25:57:49 | ['/C', ... at(cmd) | semmle.label | ['/C', ... at(cmd) | | child_process-test.js:57:46:57:48 | cmd | semmle.label | cmd | -| child_process-test.js:66:19:66:22 | args | semmle.label | args | | child_process-test.js:73:9:73:11 | cmd | semmle.label | cmd | | child_process-test.js:73:15:73:38 | url.par ... , true) | semmle.label | url.par ... , true) | | child_process-test.js:73:25:73:31 | req.url | semmle.label | req.url | @@ -606,6 +618,19 @@ nodes | child_process-test.js:191:26:191:31 | [name] | semmle.label | [name] | | child_process-test.js:191:27:191:30 | name | semmle.label | name | | child_process-test.js:193:5:193:18 | runWithOptions [name] | semmle.label | runWithOptions [name] | +| child_process-test.js:207:9:207:11 | cmd | semmle.label | cmd | +| child_process-test.js:207:15:207:38 | url.par ... , true) | semmle.label | url.par ... , true) | +| child_process-test.js:207:15:207:49 | url.par ... ry.path | semmle.label | url.par ... ry.path | +| child_process-test.js:207:15:207:49 | url.par ... ry.path | semmle.label | url.par ... ry.path | +| child_process-test.js:207:25:207:31 | req.url | semmle.label | req.url | +| child_process-test.js:225:30:225:42 | "grep " + cmd | semmle.label | "grep " + cmd | +| child_process-test.js:225:40:225:42 | cmd | semmle.label | cmd | +| child_process-test.js:226:33:226:35 | cmd | semmle.label | cmd | +| child_process-test.js:227:33:227:35 | cmd | semmle.label | cmd | +| child_process-test.js:228:33:228:35 | cmd | semmle.label | cmd | +| child_process-test.js:229:49:229:51 | cmd | semmle.label | cmd | +| child_process-test.js:230:20:230:37 | ["-c"].concat(cmd) | semmle.label | ["-c"].concat(cmd) | +| child_process-test.js:230:34:230:36 | cmd | semmle.label | cmd | | command-line-libs.js:9:9:9:12 | args | semmle.label | args | | command-line-libs.js:9:16:9:23 | req.body | semmle.label | req.body | | command-line-libs.js:12:17:12:20 | args | semmle.label | args | diff --git a/javascript/ql/test/query-tests/Security/CWE-078/CommandInjection/child_process-test.js b/javascript/ql/test/query-tests/Security/CWE-078/CommandInjection/child_process-test.js index b6d93e14a19d..8917217e1494 100644 --- a/javascript/ql/test/query-tests/Security/CWE-078/CommandInjection/child_process-test.js +++ b/javascript/ql/test/query-tests/Security/CWE-078/CommandInjection/child_process-test.js @@ -63,7 +63,7 @@ var server = http.createServer(function(req, res) { }); -function run(cmd, args) { // $ Sink +function run(cmd, args) { cp.spawn(cmd, args); // $ Alert - but note that the sink is where `args` is build. } @@ -202,3 +202,33 @@ http.createServer(function(req, res) { cp.execFile("echo", options, () => {}); cp.execFileSync("echo", options); }); + +http.createServer(function(req, res) { + let cmd = url.parse(req.url, true).query.path; // $ Sink Source + + // POSIX `sh -c` / `bash -c` positional parameters ($0, $1, ...) are not interpreted as shell code + cp.execFile("sh", ["-c", 'grep -c -- "$1" /var/log/app.log', "sh", cmd], () => {}); + cp.spawn("bash", ["-c", 'wc -l < "$1"', "bash", cmd]); + cp.execFileSync("sh", ["-c", 'for f in "$@"; do wc -c < "$f"; done', "sh"].concat([cmd])); + cp.spawn("sh", ["-c", 'wc -l < "$1"', "sh", ...[cmd]]); + let shArgs = ["-c", 'wc -l < "$1"', "sh"]; + shArgs.push(cmd); + cp.spawn("sh", shArgs); + run("sh", ["-c", 'wc -l < "$1"', "sh", cmd]); + cp.spawn("sh", ["-e", "-c", 'wc -l < "$1"', "sh", cmd]); + cp.spawn("sh", ["-c", "-e", 'wc -l < "$1"', "sh", cmd]); + cp.spawn("sh", ["-c", "--", 'wc -l < "$1"', "sh", cmd]); + cp.spawn("bash", ["-euo", "pipefail", "-c", 'wc -l < "$1"', "bash", cmd]); + cp.execFile("sh", ["-c", 'grep "$@" /var/log/app.log', "sh"].concat(["-c", cmd]), () => {}); + + // Tainted command_string with `sh -c` / `bash -c` must still alert + cp.execFile("sh", ["-c", "grep " + cmd, "sh", "safe"], () => {}); // $ Alert + cp.spawn("sh", ["-e", "-c", cmd]); // $ Alert + cp.spawn("sh", ["-c", "-e", cmd]); // $ Alert + cp.spawn("sh", ["-c", "--", cmd]); // $ Alert + cp.spawn("bash", ["-euo", "pipefail", "-c", cmd]); // $ Alert + cp.spawn("sh", ["-c"].concat(cmd)); // $ Alert + let mutatedArgs = ["-c", "echo ok"]; + mutatedArgs.splice(1, 1, cmd); + cp.spawn("sh", mutatedArgs); // $ Alert +}); diff --git a/javascript/ql/test/query-tests/Security/CWE-078/IndirectCommandInjection/IndirectCommandInjection.expected b/javascript/ql/test/query-tests/Security/CWE-078/IndirectCommandInjection/IndirectCommandInjection.expected index ee906376953a..71d4fa66677b 100644 --- a/javascript/ql/test/query-tests/Security/CWE-078/IndirectCommandInjection/IndirectCommandInjection.expected +++ b/javascript/ql/test/query-tests/Security/CWE-078/IndirectCommandInjection/IndirectCommandInjection.expected @@ -42,6 +42,7 @@ | command-line-parameter-command-injection.js:145:10:145:45 | "cmd.sh ... zzaType | command-line-parameter-command-injection.js:145:22:145:35 | program.opts() | command-line-parameter-command-injection.js:145:10:145:45 | "cmd.sh ... zzaType | This command depends on an unsanitized $@. | command-line-parameter-command-injection.js:145:22:145:35 | program.opts() | command-line argument | | command-line-parameter-command-injection.js:145:10:145:45 | "cmd.sh ... zzaType | command-line-parameter-command-injection.js:145:22:145:45 | program ... zzaType | command-line-parameter-command-injection.js:145:10:145:45 | "cmd.sh ... zzaType | This command depends on an unsanitized $@. | command-line-parameter-command-injection.js:145:22:145:45 | program ... zzaType | command-line argument | | command-line-parameter-command-injection.js:146:10:146:38 | "cmd.sh ... zzaType | command-line-parameter-command-injection.js:146:22:146:38 | program.pizzaType | command-line-parameter-command-injection.js:146:10:146:38 | "cmd.sh ... zzaType | This command depends on an unsanitized $@. | command-line-parameter-command-injection.js:146:22:146:38 | program.pizzaType | command-line argument | +| command-line-parameter-command-injection.js:155:2:155:68 | cp.exec ... used"]) | command-line-parameter-command-injection.js:152:15:152:26 | process.argv | command-line-parameter-command-injection.js:155:31:155:50 | "wc -c < " + argv[0] | This command depends on an unsanitized $@. | command-line-parameter-command-injection.js:152:15:152:26 | process.argv | command-line argument | edges | actions.js:4:6:4:16 | process.env | actions.js:4:6:4:29 | process ... _DATA'] | provenance | | | actions.js:7:15:7:15 | e | actions.js:8:10:8:10 | e | provenance | | @@ -179,6 +180,15 @@ edges | command-line-parameter-command-injection.js:145:22:145:35 | program.opts() | command-line-parameter-command-injection.js:145:10:145:45 | "cmd.sh ... zzaType | provenance | | | command-line-parameter-command-injection.js:145:22:145:45 | program ... zzaType | command-line-parameter-command-injection.js:145:10:145:45 | "cmd.sh ... zzaType | provenance | | | command-line-parameter-command-injection.js:146:22:146:38 | program.pizzaType | command-line-parameter-command-injection.js:146:10:146:38 | "cmd.sh ... zzaType | provenance | | +| command-line-parameter-command-injection.js:152:8:152:11 | argv | command-line-parameter-command-injection.js:155:44:155:47 | argv | provenance | | +| command-line-parameter-command-injection.js:152:8:152:11 | argv [ArrayElement] | command-line-parameter-command-injection.js:155:44:155:47 | argv [ArrayElement] | provenance | | +| command-line-parameter-command-injection.js:152:15:152:26 | process.argv | command-line-parameter-command-injection.js:152:15:152:35 | process ... lice(2) | provenance | | +| command-line-parameter-command-injection.js:152:15:152:26 | process.argv | command-line-parameter-command-injection.js:152:15:152:35 | process ... lice(2) [ArrayElement] | provenance | | +| command-line-parameter-command-injection.js:152:15:152:35 | process ... lice(2) | command-line-parameter-command-injection.js:152:8:152:11 | argv | provenance | | +| command-line-parameter-command-injection.js:152:15:152:35 | process ... lice(2) [ArrayElement] | command-line-parameter-command-injection.js:152:8:152:11 | argv [ArrayElement] | provenance | | +| command-line-parameter-command-injection.js:155:44:155:47 | argv | command-line-parameter-command-injection.js:155:31:155:50 | "wc -c < " + argv[0] | provenance | | +| command-line-parameter-command-injection.js:155:44:155:47 | argv [ArrayElement] | command-line-parameter-command-injection.js:155:44:155:50 | argv[0] | provenance | | +| command-line-parameter-command-injection.js:155:44:155:50 | argv[0] | command-line-parameter-command-injection.js:155:31:155:50 | "wc -c < " + argv[0] | provenance | | nodes | actions.js:4:6:4:16 | process.env | semmle.label | process.env | | actions.js:4:6:4:29 | process ... _DATA'] | semmle.label | process ... _DATA'] | @@ -342,4 +352,13 @@ nodes | command-line-parameter-command-injection.js:145:22:145:45 | program ... zzaType | semmle.label | program ... zzaType | | command-line-parameter-command-injection.js:146:10:146:38 | "cmd.sh ... zzaType | semmle.label | "cmd.sh ... zzaType | | command-line-parameter-command-injection.js:146:22:146:38 | program.pizzaType | semmle.label | program.pizzaType | +| command-line-parameter-command-injection.js:152:8:152:11 | argv | semmle.label | argv | +| command-line-parameter-command-injection.js:152:8:152:11 | argv [ArrayElement] | semmle.label | argv [ArrayElement] | +| command-line-parameter-command-injection.js:152:15:152:26 | process.argv | semmle.label | process.argv | +| command-line-parameter-command-injection.js:152:15:152:35 | process ... lice(2) | semmle.label | process ... lice(2) | +| command-line-parameter-command-injection.js:152:15:152:35 | process ... lice(2) [ArrayElement] | semmle.label | process ... lice(2) [ArrayElement] | +| command-line-parameter-command-injection.js:155:31:155:50 | "wc -c < " + argv[0] | semmle.label | "wc -c < " + argv[0] | +| command-line-parameter-command-injection.js:155:44:155:47 | argv | semmle.label | argv | +| command-line-parameter-command-injection.js:155:44:155:47 | argv [ArrayElement] | semmle.label | argv [ArrayElement] | +| command-line-parameter-command-injection.js:155:44:155:50 | argv[0] | semmle.label | argv[0] | subpaths diff --git a/javascript/ql/test/query-tests/Security/CWE-078/IndirectCommandInjection/command-line-parameter-command-injection.js b/javascript/ql/test/query-tests/Security/CWE-078/IndirectCommandInjection/command-line-parameter-command-injection.js index fe5142a0b3c8..a13d25b8b749 100644 --- a/javascript/ql/test/query-tests/Security/CWE-078/IndirectCommandInjection/command-line-parameter-command-injection.js +++ b/javascript/ql/test/query-tests/Security/CWE-078/IndirectCommandInjection/command-line-parameter-command-injection.js @@ -146,4 +146,11 @@ cp.exec("cmd.sh " + require("optimist").argv.foo); // $ Alert cp.exec("cmd.sh " + program.pizzaType); // $ Alert cp.execFile(program.opts().pizzaType, ["foo", "bar"]); +}); + +(function () { + const argv = process.argv.slice(2); // $ Source + cp.execFileSync("sh", ["-c", 'for f in "$@"; do wc -c < "$f"; done', "sh"].concat(argv)); + cp.execFileSync("sh", ["-c", 'wc -c < "$1"', "sh", argv[0]]); + cp.execFileSync("sh", ["-c", "wc -c < " + argv[0], "sh", "unused"]); // $ Alert }); \ No newline at end of file diff --git a/javascript/ql/test/query-tests/Security/CWE-078/ShellCommandInjectionFromEnvironment/ShellCommandInjectionFromEnvironment.expected b/javascript/ql/test/query-tests/Security/CWE-078/ShellCommandInjectionFromEnvironment/ShellCommandInjectionFromEnvironment.expected index 046d83da0588..5f8cf72758a2 100644 --- a/javascript/ql/test/query-tests/Security/CWE-078/ShellCommandInjectionFromEnvironment/ShellCommandInjectionFromEnvironment.expected +++ b/javascript/ql/test/query-tests/Security/CWE-078/ShellCommandInjectionFromEnvironment/ShellCommandInjectionFromEnvironment.expected @@ -1,3 +1,8 @@ +#select +| tst_shell-command-injection-from-environment.js:6:14:6:53 | 'rm -rf ... "temp") | tst_shell-command-injection-from-environment.js:6:36:6:44 | __dirname | tst_shell-command-injection-from-environment.js:6:14:6:53 | 'rm -rf ... "temp") | This shell command depends on an uncontrolled $@. | tst_shell-command-injection-from-environment.js:6:36:6:44 | __dirname | absolute path | +| tst_shell-command-injection-from-environment.js:8:14:8:53 | 'rm -rf ... "temp") | tst_shell-command-injection-from-environment.js:8:36:8:44 | __dirname | tst_shell-command-injection-from-environment.js:8:14:8:53 | 'rm -rf ... "temp") | This shell command depends on an uncontrolled $@. | tst_shell-command-injection-from-environment.js:8:36:8:44 | __dirname | absolute path | +| tst_shell-command-injection-from-environment.js:9:18:9:57 | 'rm -rf ... "temp") | tst_shell-command-injection-from-environment.js:9:40:9:48 | __dirname | tst_shell-command-injection-from-environment.js:9:18:9:57 | 'rm -rf ... "temp") | This shell command depends on an uncontrolled $@. | tst_shell-command-injection-from-environment.js:9:40:9:48 | __dirname | absolute path | +| tst_shell-command-injection-from-environment.js:15:2:15:88 | cp.exec ... used']) | tst_shell-command-injection-from-environment.js:15:53:15:61 | __dirname | tst_shell-command-injection-from-environment.js:15:31:15:70 | 'rm -rf ... "temp") | This shell command depends on an uncontrolled $@. | tst_shell-command-injection-from-environment.js:15:53:15:61 | __dirname | absolute path | edges | tst_shell-command-injection-from-environment.js:6:26:6:53 | path.jo ... "temp") | tst_shell-command-injection-from-environment.js:6:14:6:53 | 'rm -rf ... "temp") | provenance | | | tst_shell-command-injection-from-environment.js:6:36:6:44 | __dirname | tst_shell-command-injection-from-environment.js:6:26:6:53 | path.jo ... "temp") | provenance | | @@ -5,6 +10,8 @@ edges | tst_shell-command-injection-from-environment.js:8:36:8:44 | __dirname | tst_shell-command-injection-from-environment.js:8:26:8:53 | path.jo ... "temp") | provenance | | | tst_shell-command-injection-from-environment.js:9:30:9:57 | path.jo ... "temp") | tst_shell-command-injection-from-environment.js:9:18:9:57 | 'rm -rf ... "temp") | provenance | | | tst_shell-command-injection-from-environment.js:9:40:9:48 | __dirname | tst_shell-command-injection-from-environment.js:9:30:9:57 | path.jo ... "temp") | provenance | | +| tst_shell-command-injection-from-environment.js:15:43:15:70 | path.jo ... "temp") | tst_shell-command-injection-from-environment.js:15:31:15:70 | 'rm -rf ... "temp") | provenance | | +| tst_shell-command-injection-from-environment.js:15:53:15:61 | __dirname | tst_shell-command-injection-from-environment.js:15:43:15:70 | path.jo ... "temp") | provenance | | nodes | tst_shell-command-injection-from-environment.js:6:14:6:53 | 'rm -rf ... "temp") | semmle.label | 'rm -rf ... "temp") | | tst_shell-command-injection-from-environment.js:6:26:6:53 | path.jo ... "temp") | semmle.label | path.jo ... "temp") | @@ -15,8 +22,7 @@ nodes | tst_shell-command-injection-from-environment.js:9:18:9:57 | 'rm -rf ... "temp") | semmle.label | 'rm -rf ... "temp") | | tst_shell-command-injection-from-environment.js:9:30:9:57 | path.jo ... "temp") | semmle.label | path.jo ... "temp") | | tst_shell-command-injection-from-environment.js:9:40:9:48 | __dirname | semmle.label | __dirname | +| tst_shell-command-injection-from-environment.js:15:31:15:70 | 'rm -rf ... "temp") | semmle.label | 'rm -rf ... "temp") | +| tst_shell-command-injection-from-environment.js:15:43:15:70 | path.jo ... "temp") | semmle.label | path.jo ... "temp") | +| tst_shell-command-injection-from-environment.js:15:53:15:61 | __dirname | semmle.label | __dirname | subpaths -#select -| tst_shell-command-injection-from-environment.js:6:14:6:53 | 'rm -rf ... "temp") | tst_shell-command-injection-from-environment.js:6:36:6:44 | __dirname | tst_shell-command-injection-from-environment.js:6:14:6:53 | 'rm -rf ... "temp") | This shell command depends on an uncontrolled $@. | tst_shell-command-injection-from-environment.js:6:36:6:44 | __dirname | absolute path | -| tst_shell-command-injection-from-environment.js:8:14:8:53 | 'rm -rf ... "temp") | tst_shell-command-injection-from-environment.js:8:36:8:44 | __dirname | tst_shell-command-injection-from-environment.js:8:14:8:53 | 'rm -rf ... "temp") | This shell command depends on an uncontrolled $@. | tst_shell-command-injection-from-environment.js:8:36:8:44 | __dirname | absolute path | -| tst_shell-command-injection-from-environment.js:9:18:9:57 | 'rm -rf ... "temp") | tst_shell-command-injection-from-environment.js:9:40:9:48 | __dirname | tst_shell-command-injection-from-environment.js:9:18:9:57 | 'rm -rf ... "temp") | This shell command depends on an uncontrolled $@. | tst_shell-command-injection-from-environment.js:9:40:9:48 | __dirname | absolute path | diff --git a/javascript/ql/test/query-tests/Security/CWE-078/ShellCommandInjectionFromEnvironment/tst_shell-command-injection-from-environment.js b/javascript/ql/test/query-tests/Security/CWE-078/ShellCommandInjectionFromEnvironment/tst_shell-command-injection-from-environment.js index 0a0edcbd25c7..2d680cb04d21 100644 --- a/javascript/ql/test/query-tests/Security/CWE-078/ShellCommandInjectionFromEnvironment/tst_shell-command-injection-from-environment.js +++ b/javascript/ql/test/query-tests/Security/CWE-078/ShellCommandInjectionFromEnvironment/tst_shell-command-injection-from-environment.js @@ -10,4 +10,7 @@ var cp = require('child_process'), const safe = "\"" + path.join(__dirname, "temp") + "\""; execa.shellSync('rm -rf ' + safe); + + cp.execFileSync('sh', ['-c', 'rm -rf -- "$1"', 'sh', path.join(__dirname, "temp")]); + cp.execFileSync('sh', ['-c', 'rm -rf ' + path.join(__dirname, "temp"), 'sh', 'unused']); // $ Alert }); diff --git a/javascript/ql/test/query-tests/Security/CWE-078/UnsafeShellCommandConstruction/UnsafeShellCommandConstruction.expected b/javascript/ql/test/query-tests/Security/CWE-078/UnsafeShellCommandConstruction/UnsafeShellCommandConstruction.expected index 6717207be3b0..ca86a6680519 100644 --- a/javascript/ql/test/query-tests/Security/CWE-078/UnsafeShellCommandConstruction/UnsafeShellCommandConstruction.expected +++ b/javascript/ql/test/query-tests/Security/CWE-078/UnsafeShellCommandConstruction/UnsafeShellCommandConstruction.expected @@ -121,6 +121,7 @@ | lib/lib.js:736:26:736:31 | [name] | lib/lib.js:643:48:643:51 | name | lib/lib.js:736:26:736:31 | [name] | This shell argument which depends on $@ is later used in a $@. | lib/lib.js:643:48:643:51 | name | library input | lib/lib.js:736:9:736:41 | cp.spaw ... ptions) | shell command | | lib/lib.js:752:11:752:26 | "rm -rf " + name | lib/lib.js:750:48:750:51 | name | lib/lib.js:752:23:752:26 | name | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:750:48:750:51 | name | library input | lib/lib.js:752:3:752:27 | cp.exec ... + name) | shell command | | lib/lib.js:754:11:754:26 | "rm -rf " + name | lib/lib.js:750:48:750:51 | name | lib/lib.js:754:23:754:26 | name | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:750:48:750:51 | name | library input | lib/lib.js:754:3:754:27 | cp.exec ... + name) | shell command | +| lib/lib.js:765:24:765:39 | "rm -rf " + name | lib/lib.js:763:47:763:50 | name | lib/lib.js:765:36:765:39 | name | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:763:47:763:50 | name | library input | lib/lib.js:765:2:765:61 | cp.spaw ... "], cb) | shell command | | 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 | | 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 | | 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 | @@ -383,6 +384,7 @@ edges | lib/lib.js:738:5:738:18 | runWithOptions [name] | lib/lib.js:736:27:736:30 | name | provenance | | | lib/lib.js:750:48:750:51 | name | lib/lib.js:752:23:752:26 | name | provenance | | | lib/lib.js:750:48:750:51 | name | lib/lib.js:754:23:754:26 | name | provenance | | +| lib/lib.js:763:47:763:50 | name | lib/lib.js:765:36:765:39 | name | provenance | | | lib/subLib2/compiled-file.ts:3:26:3:29 | name | lib/subLib2/compiled-file.ts:4:25:4:28 | name | provenance | | | lib/subLib2/special-file.js:3:28:3:31 | name | lib/subLib2/special-file.js:4:22:4:25 | name | provenance | | | lib/subLib3/my-file.ts:3:28:3:31 | name | lib/subLib3/my-file.ts:4:22:4:25 | name | provenance | | @@ -659,6 +661,8 @@ nodes | lib/lib.js:750:48:750:51 | name | semmle.label | name | | lib/lib.js:752:23:752:26 | name | semmle.label | name | | lib/lib.js:754:23:754:26 | name | semmle.label | name | +| lib/lib.js:763:47:763:50 | name | semmle.label | name | +| lib/lib.js:765:36:765:39 | name | semmle.label | name | | lib/subLib2/compiled-file.ts:3:26:3:29 | name | semmle.label | name | | lib/subLib2/compiled-file.ts:4:25:4:28 | name | semmle.label | name | | lib/subLib2/special-file.js:3:28:3:31 | name | semmle.label | name | diff --git a/javascript/ql/test/query-tests/Security/CWE-078/UnsafeShellCommandConstruction/lib/lib.js b/javascript/ql/test/query-tests/Security/CWE-078/UnsafeShellCommandConstruction/lib/lib.js index 15122b75b45e..583c1af4ad0c 100644 --- a/javascript/ql/test/query-tests/Security/CWE-078/UnsafeShellCommandConstruction/lib/lib.js +++ b/javascript/ql/test/query-tests/Security/CWE-078/UnsafeShellCommandConstruction/lib/lib.js @@ -759,3 +759,8 @@ module.exports.sanitizerParseFloat = function (name) { // $ Source cp.exec("rm -rf " + parsed); } } + +module.exports.indirectPositional = function (name) { // $ Source + cp.spawn("sh", ["-c", 'rm -rf -- "$1"', "sh", "prefix/" + name], cb); + cp.spawn("sh", ["-c", "rm -rf " + name, "sh", "unused"], cb); // $ Alert +}