diff --git a/javascript/ql/lib/change-notes/2026-11-24-shell-launcher-command-injection.md b/javascript/ql/lib/change-notes/2026-11-24-shell-launcher-command-injection.md new file mode 100644 index 000000000000..9f12989654ef --- /dev/null +++ b/javascript/ql/lib/change-notes/2026-11-24-shell-launcher-command-injection.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* The `isIndirectCommandArgument` predicate now recognizes shell commands launched through `env` or `nice`. \ No newline at end of file diff --git a/javascript/ql/lib/semmle/javascript/security/dataflow/IndirectCommandArgument.qll b/javascript/ql/lib/semmle/javascript/security/dataflow/IndirectCommandArgument.qll index 6d6c9c3d75fa..d7655a956f7f 100644 --- a/javascript/ql/lib/semmle/javascript/security/dataflow/IndirectCommandArgument.qll +++ b/javascript/ql/lib/semmle/javascript/security/dataflow/IndirectCommandArgument.qll @@ -63,6 +63,16 @@ private DataFlow::SourceNode argumentList(SystemCommandExecution sys) { result = argumentList(sys, DataFlow::TypeBackTracker::end()) } +/** Gets an argument list without following array transformations that can change positions. */ +private DataFlow::SourceNode positionalArgumentList( + SystemCommandExecution sys, DataFlow::TypeBackTracker t +) { + t.start() and + result = sys.getArgumentList().getALocalSource() + or + exists(DataFlow::TypeBackTracker t2 | result = positionalArgumentList(sys, t2).backtrack(t2, t)) +} + /** * Gets a data-flow node whose value ends up being interpreted as an element of the argument list * `args` after a flow summarised by `t`. @@ -83,10 +93,244 @@ private DataFlow::Node argumentListElement(DataFlow::SourceNode args) { result = argumentListElement(args, DataFlow::TypeBackTracker::end()) } +/** Holds if the first `n` elements are recognized `env` options. */ +private predicate envOptions(DataFlow::ArrayCreationNode args, int n) { + n = 0 and args.getSize() >= 0 + or + envOptions(args, n - 1) and + args.getElement(n - 1).getALocalSource().getStringValue() = ["-i", "--ignore-environment"] + or + envOptions(args, n - 1) and + args.getElement(n - 1).getALocalSource().getStringValue().regexpMatch("--unset=.+") + or + envOptions(args, n - 2) and + args.getElement(n - 2).getALocalSource().getStringValue() = ["-u", "--unset"] and + exists(args.getElement(n - 1)) and + not args.getElement(n - 1).asExpr() instanceof SpreadElement +} + +/** Holds if `element` is an environment assignment, possibly with a dynamic value. */ +private predicate isEnvAssignment(DataFlow::Node element) { + exists(string value | + value = element.getStringValue() and + value.regexpMatch("[A-Za-z_][A-Za-z0-9_]*=.*") + ) + or + exists(AddExpr add | + add = element.asExpr() and + add.getLeftOperand().getStringValue().regexpMatch("[A-Za-z_][A-Za-z0-9_]*=") + ) + or + exists(DataFlow::Node prefix | + prefix = StringConcatenation::getFirstOperand(element) and + prefix.getStringValue().regexpMatch("[A-Za-z_][A-Za-z0-9_]*=") + ) +} + +/** Holds if the first `n` elements are recognized `env` options, delimiter, and assignments. */ +private predicate envPrefix(DataFlow::ArrayCreationNode args, int n) { + envOptions(args, n) + or + envOptions(args, n - 1) and + args.getElement(n - 1).getALocalSource().getStringValue() = ["--", "-"] + or + envOptions(args, n - 1) and + ( + isEnvAssignment(args.getElement(n - 1)) + or + isEnvAssignment(args.getElement(n - 1).getALocalSource()) + ) + or + envPrefix(args, n - 1) and + ( + isEnvAssignment(args.getElement(n - 1)) + or + isEnvAssignment(args.getElement(n - 1).getALocalSource()) + ) +} + +/** Holds if the first `n` elements are recognized `nice` options. */ +private predicate nicePrefix(DataFlow::ArrayCreationNode args, int n) { + n = 0 and args.getSize() >= 0 + or + nicePrefix(args, n - 1) and + args.getElement(n - 1) + .getALocalSource() + .getStringValue() + .regexpMatch("(-[0-9]+|-n[+-]?[0-9]+|--adjustment=[+-]?[0-9]+)") + or + nicePrefix(args, n - 2) and + args.getElement(n - 2).getALocalSource().getStringValue() = ["-n", "--adjustment"] and + exists(args.getElement(n - 1)) and + not args.getElement(n - 1).asExpr() instanceof SpreadElement +} + +/** Holds if the first `n` elements after a shell are supported shell options. */ +private predicate shellOptions(DataFlow::ArrayCreationNode args, int shellIndex, int n) { + n = 0 and + exists(args.getElement(shellIndex)) + or + shellOptions(args, shellIndex, n - 1) and + args.getElement(shellIndex + n).getALocalSource().getStringValue() = + ["-a", "-b", "-C", "-e", "-f", "-h", "-k", "-l", "-m", "-n", "-p", "-t", "-u", "-v", "-x"] +} + +/** Holds if a launcher runs the shell at index `i` in its argument array. */ +private predicate launcherShell(SystemCommandExecution sys, DataFlow::ArrayCreationNode args, int i) { + exists(string launcher | launcher = commandArgument(sys).getStringValue() | + launcher = ["env", "/usr/bin/env", "/bin/env"] and envPrefix(args, i) + or + launcher = ["nice", "/usr/bin/nice", "/bin/nice"] and + ( + nicePrefix(args, i) + or + nicePrefix(args, i - 1) and + args.getElement(i - 1).getALocalSource().getStringValue() = "--" + ) + ) +} + +/** Holds if the array can reach the call without its elements being modified. */ +private predicate positionalArrayReaches( + DataFlow::ArrayCreationNode args, ControlFlowNode node, SystemCommandExecution sys +) { + args = positionalArgumentList(sys, DataFlow::TypeBackTracker::end()) and + exists(int i | launcherShell(sys, args, i)) and + ( + node = sys.asExpr() + or + not exists(DataFlow::PropWrite write | + write = args.getAPropertyWrite() and + write.getWriteNode() = node and + ( + not exists(write.getPropertyName()) + or + write.getPropertyName().regexpMatch("([0-9]+|length)") + ) + ) and + not exists(DeleteExpr del, DataFlow::PropRef ref | + del.getOperand().flow() = ref and + args.flowsTo(ref.getBase()) and + del = node and + ( + not exists(ref.getPropertyName()) + or + ref.getPropertyName().regexpMatch("[0-9]+") + ) + ) and + not exists(DataFlow::MethodCallNode call | + call = + args.getAMethodCall([ + "copyWithin", "fill", "pop", "push", "reverse", "shift", "sort", "splice", "unshift" + ]) and + node = call.asExpr() + ) and + positionalArrayReaches(args, node.getASuccessor(), sys) + ) +} + +/** Holds if an array reaches `use` without its positional elements being modified. */ +private predicate positionalArrayReachesUse( + DataFlow::SourceNode args, ControlFlowNode node, ControlFlowNode use +) { + node = use and args.getFile() = use.getFile() + or + not exists(DataFlow::PropWrite write | + write = args.getAPropertyWrite() and + write.getWriteNode() = node and + ( + not exists(write.getPropertyName()) + or + write.getPropertyName().regexpMatch("([0-9]+|length)") + ) + ) and + not exists(DeleteExpr del, DataFlow::PropRef ref | + del.getOperand().flow() = ref and + args.flowsTo(ref.getBase()) and + del = node and + ( + not exists(ref.getPropertyName()) + or + ref.getPropertyName().regexpMatch("[0-9]+") + ) + ) and + not exists(DataFlow::MethodCallNode call | + call = + args.getAMethodCall([ + "copyWithin", "fill", "pop", "push", "reverse", "shift", "sort", "splice", "unshift" + ]) and + node = call.asExpr() + ) and + positionalArrayReachesUse(args, node.getASuccessor(), use) +} + +/** Holds if a launcher call uses a wrapper's array parameter. */ +private predicate positionalWrapperArray( + SystemCommandExecution sys, DataFlow::ArrayCreationNode args, DataFlow::InvokeNode wrapperCall, + Function wrapper +) { + exists(int argsIndex | + sys.asExpr().getContainer() = wrapper and + wrapperCall.getACallee() = wrapper and + args = wrapperCall.getArgument(argsIndex).getALocalSource() and + positionalArrayReachesUse(args, args.asExpr().getASuccessor(), wrapperCall.asExpr()) and + positionalArrayReachesUse(args, wrapper.getStart(), sys.asExpr()) and + exists(DataFlow::ParameterNode argsParam, VarAccess access | + argsParam.getParameter() = wrapper.getParameter(argsIndex) and + access = sys.getArgumentList().asExpr() and + access.getVariable() = argsParam.getParameter().getAVariable() and + positionalArrayReachesUse(argsParam, wrapper.getStart(), sys.asExpr()) + ) + ) +} + +/** Holds if one wrapper call launches `env` or `nice` with `args`. */ +private predicate wrappedLauncherShell( + SystemCommandExecution sys, DataFlow::ArrayCreationNode args, DataFlow::InvokeNode wrapperCall, + Function wrapper, int i +) { + exists( + DataFlow::ParameterNode launcherParam, DataFlow::ParameterNode argsParam, string launcher, + int launcherIndex, int argsIndex + | + wrapperCall.getACallee() = wrapper and + sys.asExpr().getContainer() = wrapper and + launcherParam = sys.getACommandArgument().getALocalSource() and + argsParam = sys.getArgumentList().getALocalSource() and + launcherParam.getParameter() = wrapper.getParameter(launcherIndex) and + argsParam.getParameter() = wrapper.getParameter(argsIndex) and + args = wrapperCall.getArgument(argsIndex).getALocalSource() and + launcher = wrapperCall.getInvokeExpr().getArgument(launcherIndex).getStringValue() + | + launcher = ["env", "/usr/bin/env", "/bin/env"] and envPrefix(args, i) + or + launcher = ["nice", "/usr/bin/nice", "/bin/nice"] and + ( + nicePrefix(args, i) + or + nicePrefix(args, i - 1) and + args.getElement(i - 1).getALocalSource().getStringValue() = "--" + ) + ) + or + exists(string launcher | launcher = sys.getACommandArgument().getStringValue() | + launcher = ["env", "/usr/bin/env", "/bin/env"] and envPrefix(args, i) + or + launcher = ["nice", "/usr/bin/nice", "/bin/nice"] and + ( + nicePrefix(args, i) + or + nicePrefix(args, i - 1) and + args.getElement(i - 1).getALocalSource().getStringValue() = "--" + ) + ) +} + /** * Holds if `source` contributes to the arguments of an indirect command execution `sys`. * - * An indirect command execution is a system execution command that starts with `sh -c`, `cmd.exe /c`, or similar. + * An indirect command execution is a system execution command that starts with `sh -c`, `cmd.exe /c`, or similar, + * optionally through an `env` or `nice` launcher. * * For example, `getCommand()` is `source`, and the call to `childProcess.spawn` is `sys` in the following example: * @@ -114,4 +358,40 @@ predicate isIndirectCommandArgument(DataFlow::Node source, SystemCommandExecutio else source = argsSource ) ) + or + exists(DataFlow::ArrayCreationNode args, int i, string dashC | + args = positionalArgumentList(sys, DataFlow::TypeBackTracker::end()) and + not exists(VarAccess access | + access = sys.getACommandArgument().asExpr() and access.getVariable().isParameter() + ) and + not exists(DataFlow::ParameterNode parameter | + parameter = sys.getACommandArgument().getALocalSource() + ) and + positionalArrayReaches(args, args.asExpr().getASuccessor(), sys) and + launcherShell(sys, args, i) and + shellCmd(args.getElement(i).getALocalSource().asExpr(), dashC) and + exists(int optionCount, int cIndex | + shellOptions(args, i, optionCount) and + cIndex = i + optionCount + 1 and + args.getElement(cIndex).getALocalSource().getStringValue() = dashC and + source = args.getElement(cIndex + 1) + ) and + not source.asExpr() instanceof SpreadElement + ) + or + exists( + DataFlow::ArrayCreationNode args, int i, string dashC, DataFlow::InvokeNode wrapperCall, + Function wrapper + | + positionalWrapperArray(sys, args, wrapperCall, wrapper) and + wrappedLauncherShell(sys, args, wrapperCall, wrapper, i) and + shellCmd(args.getElement(i).getALocalSource().asExpr(), dashC) and + exists(int optionCount, int cIndex | + shellOptions(args, i, optionCount) and + cIndex = i + optionCount + 1 and + args.getElement(cIndex).getALocalSource().getStringValue() = dashC and + source = args.getElement(cIndex + 1) + ) and + not source.asExpr() instanceof SpreadElement + ) } diff --git a/javascript/ql/src/change-notes/2026-11-24-shell-launcher-command-injection.md b/javascript/ql/src/change-notes/2026-11-24-shell-launcher-command-injection.md new file mode 100644 index 000000000000..5c4c78d0fb80 --- /dev/null +++ b/javascript/ql/src/change-notes/2026-11-24-shell-launcher-command-injection.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* The command injection queries now recognize shell commands launched through `env` or `nice`. \ No newline at end of file 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..1b6224ad0a04 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 @@ -87,6 +87,18 @@ | form-parsers.js:41:10:41:31 | "touch ... ds.name | form-parsers.js:40:26:40:31 | fields | form-parsers.js:41:10:41:31 | "touch ... ds.name | This command line depends on a $@. | form-parsers.js:40:26:40:31 | fields | user-provided value | | form-parsers.js:53:10:53:31 | "touch ... ds.name | form-parsers.js:52:34:52:39 | fields | form-parsers.js:53:10:53:31 | "touch ... ds.name | This command line depends on a $@. | form-parsers.js:52:34:52:39 | fields | user-provided value | | form-parsers.js:59:10:59:33 | "touch ... ilename | form-parsers.js:58:30:58:33 | part | form-parsers.js:59:10:59:33 | "touch ... ilename | This command line depends on a $@. | form-parsers.js:58:30:58:33 | part | user-provided value | +| launchers.js:8:3:8:49 | cp.spaw ... mmand]) | launchers.js:5:17:5:23 | req.url | launchers.js:8:41:8:47 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value | +| launchers.js:9:3:9:45 | cp.exec ... mmand]) | launchers.js:5:17:5:23 | req.url | launchers.js:9:37:9:43 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value | +| launchers.js:10:3:10:41 | cp.spaw ... mmand]) | launchers.js:5:17:5:23 | req.url | launchers.js:10:33:10:39 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value | +| launchers.js:11:3:11:72 | cp.spaw ... mmand]) | launchers.js:5:17:5:23 | req.url | launchers.js:11:64:11:70 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value | +| launchers.js:12:3:12:82 | cp.exec ... mmand]) | launchers.js:5:17:5:23 | req.url | launchers.js:12:74:12:80 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value | +| launchers.js:13:3:13:57 | cp.spaw ... mmand]) | launchers.js:5:17:5:23 | req.url | launchers.js:13:49:13:55 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value | +| launchers.js:14:3:14:55 | cp.spaw ... mmand]) | launchers.js:5:17:5:23 | req.url | launchers.js:14:47:14:53 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value | +| launchers.js:15:3:15:65 | cp.spaw ... mmand]) | launchers.js:5:17:5:23 | req.url | launchers.js:15:57:15:63 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value | +| launchers.js:18:3:18:23 | cp.spaw ... , args) | launchers.js:5:17:5:23 | req.url | launchers.js:17:30:17:36 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value | +| launchers.js:44:3:44:30 | cp.spaw ... Change) | launchers.js:5:17:5:23 | req.url | launchers.js:43:36:43:42 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value | +| launchers.js:49:3:49:30 | cp.spaw ... tional) | launchers.js:5:17:5:23 | req.url | launchers.js:47:36:47:42 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value | +| launchers.js:53:3:53:23 | cp.spaw ... , args) | launchers.js:5:17:5:23 | req.url | launchers.js:19:32:19:38 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value | | other.js:7:33:7:35 | cmd | other.js:5:25:5:31 | req.url | other.js:7:33:7:35 | cmd | This command line depends on a $@. | other.js:5:25:5:31 | req.url | user-provided value | | other.js:8:28:8:30 | cmd | other.js:5:25:5:31 | req.url | other.js:8:28:8:30 | cmd | This command line depends on a $@. | other.js:5:25:5:31 | req.url | user-provided value | | other.js:9:32:9:34 | cmd | other.js:5:25:5:31 | req.url | other.js:9:32:9:34 | cmd | This command line depends on a $@. | other.js:5:25:5:31 | req.url | user-provided value | @@ -407,6 +419,21 @@ edges | form-parsers.js:53:21:53:26 | fields | form-parsers.js:53:10:53:31 | "touch ... ds.name | provenance | | | form-parsers.js:58:30:58:33 | part | form-parsers.js:59:21:59:24 | part | provenance | | | form-parsers.js:59:21:59:24 | part | form-parsers.js:59:10:59:33 | "touch ... ilename | provenance | | +| launchers.js:5:9:5:13 | input | launchers.js:6:27:6:31 | input | provenance | | +| launchers.js:5:17:5:23 | req.url | launchers.js:5:9:5:13 | input | provenance | | +| launchers.js:6:9:6:15 | command | launchers.js:8:41:8:47 | command | provenance | | +| launchers.js:6:9:6:15 | command | launchers.js:9:37:9:43 | command | provenance | | +| launchers.js:6:9:6:15 | command | launchers.js:10:33:10:39 | command | provenance | | +| launchers.js:6:9:6:15 | command | launchers.js:11:64:11:70 | command | provenance | | +| launchers.js:6:9:6:15 | command | launchers.js:12:74:12:80 | command | provenance | | +| launchers.js:6:9:6:15 | command | launchers.js:13:49:13:55 | command | provenance | | +| launchers.js:6:9:6:15 | command | launchers.js:14:47:14:53 | command | provenance | | +| launchers.js:6:9:6:15 | command | launchers.js:15:57:15:63 | command | provenance | | +| launchers.js:6:9:6:15 | command | launchers.js:17:30:17:36 | command | provenance | | +| launchers.js:6:9:6:15 | command | launchers.js:19:32:19:38 | command | provenance | | +| launchers.js:6:9:6:15 | command | launchers.js:43:36:43:42 | command | provenance | | +| launchers.js:6:9:6:15 | command | launchers.js:47:36:47:42 | command | provenance | | +| launchers.js:6:27:6:31 | input | launchers.js:6:9:6:15 | command | provenance | | | other.js:5:9:5:11 | cmd | other.js:7:33:7:35 | cmd | provenance | | | other.js:5:9:5:11 | cmd | other.js:8:28:8:30 | cmd | provenance | | | other.js:5:9:5:11 | cmd | other.js:9:32:9:34 | cmd | provenance | | @@ -727,6 +754,22 @@ nodes | form-parsers.js:58:30:58:33 | part | semmle.label | part | | form-parsers.js:59:10:59:33 | "touch ... ilename | semmle.label | "touch ... ilename | | form-parsers.js:59:21:59:24 | part | semmle.label | part | +| launchers.js:5:9:5:13 | input | semmle.label | input | +| launchers.js:5:17:5:23 | req.url | semmle.label | req.url | +| launchers.js:6:9:6:15 | command | semmle.label | command | +| launchers.js:6:27:6:31 | input | semmle.label | input | +| launchers.js:8:41:8:47 | command | semmle.label | command | +| launchers.js:9:37:9:43 | command | semmle.label | command | +| launchers.js:10:33:10:39 | command | semmle.label | command | +| launchers.js:11:64:11:70 | command | semmle.label | command | +| launchers.js:12:74:12:80 | command | semmle.label | command | +| launchers.js:13:49:13:55 | command | semmle.label | command | +| launchers.js:14:47:14:53 | command | semmle.label | command | +| launchers.js:15:57:15:63 | command | semmle.label | command | +| launchers.js:17:30:17:36 | command | semmle.label | command | +| launchers.js:19:32:19:38 | command | semmle.label | command | +| launchers.js:43:36:43:42 | command | semmle.label | command | +| launchers.js:47:36:47:42 | command | semmle.label | command | | other.js:5:9:5:11 | cmd | semmle.label | cmd | | other.js:5:15:5:38 | url.par ... , true) | semmle.label | url.par ... , true) | | other.js:5:25:5:31 | req.url | semmle.label | req.url | diff --git a/javascript/ql/test/query-tests/Security/CWE-078/CommandInjection/launchers.js b/javascript/ql/test/query-tests/Security/CWE-078/CommandInjection/launchers.js new file mode 100644 index 000000000000..81ff18cf407f --- /dev/null +++ b/javascript/ql/test/query-tests/Security/CWE-078/CommandInjection/launchers.js @@ -0,0 +1,54 @@ +const cp = require("child_process"); +const http = require("http"); + +http.createServer((req, res) => { + const input = req.url; // $ Source + const command = "ls " + input; + + cp.spawn("/usr/bin/env", ["sh", "-c", command]); // $ Alert + cp.execFile("env", ["bash", "-c", command]); // $ Alert + cp.spawn("nice", ["sh", "-c", command]); // $ Alert + cp.spawnSync("/usr/bin/nice", ["-n", "5", "/bin/bash", "-c", command]); // $ Alert + cp.execFileSync("/bin/env", ["-i", "-u", "OLD", "FOO=bar", "sh", "-c", command]); // $ Alert + cp.spawn("env", ["--", "FOO=bar", "sh", "-c", command]); // $ Alert + cp.spawn("env", ["--unset=FOO", "sh", "-c", command]); // $ Alert + cp.spawn("nice", ["--adjustment=5", "--", "sh", "-c", command]); // $ Alert + const shell = "sh"; + const args = [shell, "-c", command]; // $ Sink + cp.spawn("env", args); // $ Alert + runWithLauncher(["sh", "-c", command]); // $ Sink + + cp.spawn("env", ["echo", "sh", "-c", command]); + cp.spawn("env", ["echo"].concat(["sh", "-c", command])); + cp.spawn("nice", ["echo", "sh", "-c", command]); + cp.spawn("env", ["-u", "sh", "-c", command]); + cp.spawn("env", ["sh", "-c", "echo safe", input]); + cp.spawn("env", ["sh", input, "-c", "echo safe"]); + cp.spawn("env", ["FOO=" + input, "sh", "-c", "echo safe"]); + cp.spawn("other", ["sh", "-c", command]); + cp.spawn("env", ["sh", "-c", ...[command]]); + + const overwritten = ["sh", "-c", command]; + overwritten[0] = "echo"; + cp.spawn("env", overwritten); + + const overwrittenCommand = ["sh", "-c", command]; + overwrittenCommand[2] = "echo safe"; + cp.spawn("env", overwrittenCommand); + + const shifted = ["sh", "-c", command]; + shifted.unshift("echo"); + cp.spawn("env", shifted); + + const laterChange = ["sh", "-c", command]; // $ Sink + cp.spawn("env", laterChange); // $ Alert + laterChange[0] = "echo"; + + const conditional = ["sh", "-c", command]; // $ Sink + if (input) conditional[0] = "echo"; + cp.spawn("env", conditional); // $ Alert +}); + +function runWithLauncher(args) { + cp.spawn("env", args); // $ 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..13808c45cf11 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,12 @@ | 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:153:2:153:39 | cp.spaw ... mmand]) | command-line-parameter-command-injection.js:152:26:152:37 | process.argv | command-line-parameter-command-injection.js:153:31:153:37 | command | This command depends on an unsanitized $@. | command-line-parameter-command-injection.js:152:26:152:37 | process.argv | command-line argument | +| command-line-parameter-command-injection.js:161:2:161:56 | cp.spaw ... mmand]) | command-line-parameter-command-injection.js:158:26:158:37 | process.argv | command-line-parameter-command-injection.js:161:48:161:54 | command | This command depends on an unsanitized $@. | command-line-parameter-command-injection.js:158:26:158:37 | process.argv | command-line argument | +| command-line-parameter-command-injection.js:162:2:162:67 | cp.spaw ... mmand]) | command-line-parameter-command-injection.js:158:26:158:37 | process.argv | command-line-parameter-command-injection.js:162:59:162:65 | command | This command depends on an unsanitized $@. | command-line-parameter-command-injection.js:158:26:158:37 | process.argv | command-line argument | +| command-line-parameter-command-injection.js:163:2:163:45 | cp.spaw ... mmand]) | command-line-parameter-command-injection.js:158:26:158:37 | process.argv | command-line-parameter-command-injection.js:163:37:163:43 | command | This command depends on an unsanitized $@. | command-line-parameter-command-injection.js:158:26:158:37 | process.argv | command-line argument | +| command-line-parameter-command-injection.js:164:2:164:48 | cp.spaw ... mmand]) | command-line-parameter-command-injection.js:158:26:158:37 | process.argv | command-line-parameter-command-injection.js:164:40:164:46 | command | This command depends on an unsanitized $@. | command-line-parameter-command-injection.js:158:26:158:37 | process.argv | command-line argument | +| command-line-parameter-command-injection.js:179:3:179:21 | cp.spawn(exe, args) | command-line-parameter-command-injection.js:158:26:158:37 | process.argv | command-line-parameter-command-injection.js:182:38:182:44 | command | This command depends on an unsanitized $@. | command-line-parameter-command-injection.js:158:26:158:37 | 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 +185,14 @@ 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:14 | command | command-line-parameter-command-injection.js:153:31:153:37 | command | provenance | | +| command-line-parameter-command-injection.js:152:26:152:37 | process.argv | command-line-parameter-command-injection.js:152:8:152:14 | command | provenance | | +| command-line-parameter-command-injection.js:158:8:158:14 | command | command-line-parameter-command-injection.js:161:48:161:54 | command | provenance | | +| command-line-parameter-command-injection.js:158:8:158:14 | command | command-line-parameter-command-injection.js:162:59:162:65 | command | provenance | | +| command-line-parameter-command-injection.js:158:8:158:14 | command | command-line-parameter-command-injection.js:163:37:163:43 | command | provenance | | +| command-line-parameter-command-injection.js:158:8:158:14 | command | command-line-parameter-command-injection.js:164:40:164:46 | command | provenance | | +| command-line-parameter-command-injection.js:158:8:158:14 | command | command-line-parameter-command-injection.js:182:38:182:44 | command | provenance | | +| command-line-parameter-command-injection.js:158:26:158:37 | process.argv | command-line-parameter-command-injection.js:158:8:158:14 | command | 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 +356,14 @@ 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:14 | command | semmle.label | command | +| command-line-parameter-command-injection.js:152:26:152:37 | process.argv | semmle.label | process.argv | +| command-line-parameter-command-injection.js:153:31:153:37 | command | semmle.label | command | +| command-line-parameter-command-injection.js:158:8:158:14 | command | semmle.label | command | +| command-line-parameter-command-injection.js:158:26:158:37 | process.argv | semmle.label | process.argv | +| command-line-parameter-command-injection.js:161:48:161:54 | command | semmle.label | command | +| command-line-parameter-command-injection.js:162:59:162:65 | command | semmle.label | command | +| command-line-parameter-command-injection.js:163:37:163:43 | command | semmle.label | command | +| command-line-parameter-command-injection.js:164:40:164:46 | command | semmle.label | command | +| command-line-parameter-command-injection.js:182:38:182:44 | command | semmle.label | command | 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..36ee8c1e296c 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,55 @@ cp.exec("cmd.sh " + require("optimist").argv.foo); // $ Alert cp.exec("cmd.sh " + program.pizzaType); // $ Alert cp.execFile(program.opts().pizzaType, ["foo", "bar"]); -}); \ No newline at end of file +}); + +(function() { + const command = "ls " + process.argv[2]; // $ Source + cp.spawn("env", ["sh", "-c", command]); // $ Alert + cp.spawn("nice", ["echo", "sh", "-c", command]); +}); + +(function() { + const command = "ls " + process.argv[2]; // $ Source + + const end = "--"; + cp.spawn("nice", ["-n", "5", end, "sh", "-c", command]); // $ Alert + cp.spawn("env", ["PATH=" + process.env.PATH, "sh", "-c", command]); // $ Alert + cp.spawn("env", ["sh", "-e", "-c", command]); // $ Alert + cp.spawn("nice", ["bash", "-l", "-c", command]); // $ Alert + + cp.spawn("env", ["--split-string=echo", "sh", "-c", command]); + cp.spawn("env", ["-", "--unset=FOO", "sh", "-c", command]); + + const deleted = ["sh", "-c", command]; + delete deleted[2]; + cp.spawn("env", deleted); + + const deletedUnknown = ["sh", "-c", command]; + const index = Number(process.argv[2]); + delete deletedUnknown[index]; + cp.spawn("env", deletedUnknown); + + function runWithLauncher(exe, args) { + cp.spawn(exe, args); // $ Alert + } + + runWithLauncher("env", ["sh", "-c", command]); // $ Sink + const changedBeforeForwarding = ["sh", "-c", command]; + changedBeforeForwarding[0] = "echo"; + runWithLauncher("env", changedBeforeForwarding); + + function runWithMutation(exe, args) { + args[0] = "echo"; + cp.spawn(exe, args); + } + + runWithMutation("env", ["sh", "-c", command]); + + function runWithSeparateValues(exe, args) { + cp.spawn(exe, args); + } + + runWithSeparateValues("env", ["echo"]); + runWithSeparateValues("echo", ["sh", "-c", command]); +})(); 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..239641240c22 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:14:2:14:49 | cp.spaw ... rname]) | tst_shell-command-injection-from-environment.js:14:39:14:47 | __dirname | tst_shell-command-injection-from-environment.js:14:31:14:47 | "ls " + __dirname | This shell command depends on an uncontrolled $@. | tst_shell-command-injection-from-environment.js:14:39:14:47 | __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,7 @@ 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:14:39:14:47 | __dirname | tst_shell-command-injection-from-environment.js:14:31:14:47 | "ls " + __dirname | 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 +21,6 @@ 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:14:31:14:47 | "ls " + __dirname | semmle.label | "ls " + __dirname | +| tst_shell-command-injection-from-environment.js:14:39:14:47 | __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..665a9d8b69ea 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.spawn("env", ["sh", "-c", "ls " + __dirname]); // $ Alert + cp.spawn("env", ["echo", "sh", "-c", "ls " + __dirname]); }); 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..fd08cff3c8f3 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:764:31:764:46 | "rm -rf " + name | lib/lib.js:763:37:763:40 | name | lib/lib.js:764:43:764:46 | name | This string concatenation which depends on $@ is later used in a $@. | lib/lib.js:763:37:763:40 | name | library input | lib/lib.js:764:2:764:48 | cp.spaw ... name]) | 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:37:763:40 | name | lib/lib.js:764:43:764:46 | 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:37:763:40 | name | semmle.label | name | +| lib/lib.js:764:43:764:46 | 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..5d427fcc2020 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.launcher = function (name) { // $ Source + cp.spawn("env", ["sh", "-c", "rm -rf " + name]); // $ Alert + cp.spawn("nice", ["echo", "sh", "-c", "rm -rf " + name]); +}