Skip to content

JS: false positive in js/command-line-injection on sh -c positional parameters ($1/$@) #23274

In progress

Description

@wildoranges

Description of the false positive

js/command-line-injection (and js/indirect-command-line-injection) reports untrusted values passed as positional parameters ($0, $1, $@) to a constant sh -c / bash -c script. Per POSIX sh, only the command string after -c is parsed as shell code; subsequent operands are bound to positional parameters and used as data (the safe pattern recommended by ShellCheck SC2156).

The cause is isIndirectCommandArgument: whenever the executable is a shell and the argument array contains -c or /c, every element written to the array is treated as a command sink (L111-L115), regardless of position. That matches cmd.exe /c (which joins remaining arguments, as tested in child_process-test.js L56-L57), but not sh/bash — as shellCmd itself documents, only <cmd> runs as a shell command. The same predicate is shared by js/shell-command-injection-from-environment and js/shell-command-constructed-from-input.

Code samples or links to source code

app.js:

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

// false positive: req.query.q is passed as positional parameter $1
app.get("/count", (req, res) => {
  cp.execFile("sh", ["-c", 'grep -c -- "$1" /var/log/app.log', "sh", req.query.q], (err, stdout) => { // reported (FP)
    res.send(String(stdout));
  });
});

// false positive: same with spawn and bash
app.get("/lines", (req, res) => {
  const child = cp.spawn("bash", ["-c", 'wc -l < "$1"', "bash", req.query.file]); // reported (FP)
  child.stdout.pipe(res);
});

// true positive control: value interpolated into the script string
app.get("/count-unsafe", (req, res) => {
  cp.execFile("sh", ["-c", "grep -c -- " + req.query.q + " /var/log/app.log"], (err, stdout) => { // reported (TP)
    res.send(String(stdout));
  });
});

// control: no shell
app.get("/count-noshell", (req, res) => {
  cp.execFile("grep", ["-c", "--", req.query.q, "/var/log/app.log"], (err, stdout) => { // not reported
    res.send(String(stdout));
  });
});

app.listen(3000);

cli.js:

const cp = require("child_process");

// false positive: argv elements are passed as "$@"
const out = cp.execFileSync("sh", ["-c", 'for f in "$@"; do wc -c < "$f"; done', "sh"].concat(process.argv.slice(2))); // reported (FP)
console.log(String(out));

On CodeQL 2.27.5 (codeql/javascript-queries 2.5.1, including default javascript-code-scanning.qls), js/command-line-injection alerts on app.js:6, :13, and :19, and js/indirect-command-line-injection alerts on cli.js:4. Only app.js:19 is a real injection.

Possible change

For -c (sh/bash, keeping cmd.exe /c unchanged), only treat elements that can be the command string after -c as sinks, and exclude elements known to follow it (both by array index and via concat after the command string, while keeping the current fallback when element positions are unknown). Would that approach make sense?

Activity

  1. jketema commented on Nov 27, 2026

    @jketema
    Contributor

    Hi @wildoranges,

    I'll ask the responsible team to have a look. At first sight this seems to behave as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions