Skip to content

JS: Should isIndirectCommandArgument recognize a shell that is started through env, as in spawn("/usr/bin/env", ["sh", "-c", cmd])? #23220

In progress

Description

@wildoranges

Description of the issue

js/command-line-injection reports remote input in spawn("sh", ["-c", "ls " + dir]), but not when the shell is started through env, as in spawn("/usr/bin/env", ["sh", "-c", "ls " + dir]) or execFile("env", ["bash", "-c", "ls " + dir]). I'm wondering whether this is intended: env only adjusts the environment and then runs its utility operand (POSIX env), so the shell interprets the command in the same way, and /usr/bin/env is a common way to look a program up on PATH.

Affected model

isIndirectCommandArgument requires the executable itself to satisfy shellCmd (sh, bash, /bin/sh, /bin/bash, cmd or cmd.exe). With env as the executable, the shell is an element of the argument array, so the command after -c is not a sink. The predicate is shared by js/command-line-injection, js/indirect-command-line-injection, js/shell-command-injection-from-environment and js/shell-command-constructed-from-input.

For Java, #22818 added env, nice, sudo, timeout and other launchers to java/command-line-injection.

Steps to reproduce

app.js:

const cp = require("child_process");
const app = require("express")();

app.get("/a", (req, res) => {
  cp.spawn("/usr/bin/env", ["sh", "-c", "ls " + req.query.dir]); // not reported
  res.send("ok");
});

app.get("/b", (req, res) => {
  cp.execFile("env", ["bash", "-c", "ls " + req.query.dir], () => {}); // not reported
  res.send("ok");
});

app.get("/c", (req, res) => {
  cp.spawn("nice", ["sh", "-c", "ls " + req.query.dir]); // not reported
  res.send("ok");
});

// control: no launcher
app.get("/d", (req, res) => {
  cp.spawn("sh", ["-c", "ls " + req.query.dir]); // reported
  res.send("ok");
});

app.listen(3000);

With CodeQL 2.27.5, js/command-line-injection reports only line 21. At runtime (Node.js v24.18.0, GNU coreutils env 9.4), dir = "x; touch /tmp/marker" runs touch /tmp/marker as a second command in all four routes.

The call shape is not rare: GitHub code search finds it in 43 non-test files in 33 repositories (2026-11-14). It also appears in well-known repositories, for example execFileAsync("/usr/bin/env", ["sh", "-c", `command -v ${command}`]) in the Raycast extensions repository.

Question

Is it intended that a shell started through env is not recognized? Would it be reasonable to model env (and possibly similar launchers such as nice), for example as sketched below?

Possible change

One possible change, against current main: in isIndirectCommandArgument, also accept a launcher such as env or nice as the executable when an element of its argument array satisfies shellCmd:

--- a/javascript/ql/lib/semmle/javascript/security/dataflow/IndirectCommandArgument.qll
+++ b/javascript/ql/lib/semmle/javascript/security/dataflow/IndirectCommandArgument.qll
@@ -22,6 +22,14 @@ private predicate shellCmd(Expr shell, string arg) {
   )
 }
 
+/**
+ * Holds if `launcher` is a program such as `env` or `nice` that runs the program given by
+ * one of its operands.
+ */
+private predicate launcherCmd(Expr launcher) {
+  launcher.getStringValue() = ["env", "/usr/bin/env", "/bin/env", "nice", "/usr/bin/nice"]
+}
+
 /**
  * Gets a data-flow node whose value ends up being interpreted as the command argument in `sys`
  * after a flow summarized by `t`.
@@ -105,7 +113,13 @@ 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
-    shell = commandArgument(sys) and
+    (
+      shell = commandArgument(sys)
+      or
+      // `env sh -c <cmd>`, `nice bash -c <cmd>` and similar
+      launcherCmd(commandArgument(sys).asExpr()) and
+      shell = argumentListElement(args)
+    ) and
     args = argumentList(sys) and
     argumentListElement(args).mayHaveStringValue(dashC) and
     exists(DataFlow::SourceNode argsSource | argsSource = argumentList(sys) |

With this change, js/command-line-injection also reports lines 5, 10 and 15 of the example, and the existing tests in javascript/ql/test/query-tests/Security/CWE-078/ still pass with unchanged .expected files.

This is only a sketch. Which launchers to include is open: it has env and nice, while #22818 covers more for Java. Like the existing check for -c, it does not look at positions, so a launcher that runs another program with sh and -c among that program's arguments would also match, and env options that take a separate value, such as env -u NAME or env -S, are not handled.

Environment

  • CodeQL CLI 2.27.5 (codeql/javascript-queries 2.5.1)
  • CodeQL main f7a381fd5a8eee235dda7f83dd1f81e0a109e3f0 (commit for the links above)
  • Node.js v24.18.0 and GNU coreutils 9.4 on Linux/amd64

Activity

  1. added
    question
    on Nov 24, 2026
  2. self-assigned this
    on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

question

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions