Skip to content

JS: Detect shell commands launched through env and nice - #23222

Draft
hvitved wants to merge 2 commits into
github:mainfrom
hvitved:hvitved-js-shell-launcher-injection
Draft

hvitved wants to merge 2 commits into
github:mainfrom
hvitved:hvitved-js-shell-launcher-injection

Conversation

@hvitved

@hvitved hvitved commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Command injection queries miss shell commands launched through env or nice, even though the shell still interprets the command string.

Recognize these launchers and common options while checking argument positions and avoiding stale array elements after mutations. The model does not yet handle env -S or dynamically assembled argument arrays.

Added regression coverage for all four affected queries, including non-shell operands and array mutations.

Fixes #23220

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved launcher parsing, array mutation handling, and wrapper-call correlation defects cause false-positive and missed shell-command sinks, while overly broad recursive reachability risks poor performance on large databases.

7 open issues
What changed in this PR

Extends JavaScript’s shared command-injection model to recognize shells launched through env and nice, addressing #23220.

Changes:

  • Add launcher-option parsing, positional argument checks, and array-mutation checks.
  • Add positive and negative regression cases for all four affected queries and update expected results.
  • Add library and query change notes.
File Description
javascript/​ql/​test/​query-tests/​Security/​CWE-078/​UnsafeShellCommandConstruction/​UnsafeShellCommandConstruction.expected Expected launcher detection result.
javascript/​ql/​test/​query-tests/​Security/​CWE-078/​UnsafeShellCommandConstruction/​lib/​lib.js Launcher case and non-shell control.
javascript/​ql/​test/​query-tests/​Security/​CWE-078/​ShellCommandInjectionFromEnvironment/​tst_shell-command-injection-from-environment.js Environment-derived command regression cases.
javascript/​ql/​test/​query-tests/​Security/​CWE-078/​ShellCommandInjectionFromEnvironment/​ShellCommandInjectionFromEnvironment.expected Expected environment-query launcher alert.
javascript/​ql/​test/​query-tests/​Security/​CWE-078/​IndirectCommandInjection/​IndirectCommandInjection.expected Expected indirect-command launcher alert.
javascript/​ql/​test/​query-tests/​Security/​CWE-078/​IndirectCommandInjection/​command-line-parameter-command-injection.js Launcher regression using command-line input.
javascript/​ql/​test/​query-tests/​Security/​CWE-078/​CommandInjection/​launchers.js Launcher options, positions, wrappers, and mutations.
javascript/​ql/​test/​query-tests/​Security/​CWE-078/​CommandInjection/​CommandInjection.expected Expected launcher and wrapper results.
javascript/​ql/​src/​change-notes/​2026-11-24-shell-launcher-command-injection.md Query analysis change note.
javascript/​ql/​lib/​semmle/​javascript/​security/​dataflow/​IndirectCommandArgument.qll Shared launcher recognition and mutation checks.
javascript/​ql/​lib/​change-notes/​2026-11-24-shell-launcher-command-injection.md Public predicate change note.

Review effort: Max (adaptive)

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
DataFlow::ArrayCreationNode args, ControlFlowNode node, SystemCommandExecution sys
) {
args = positionalArgumentList(sys, DataFlow::TypeBackTracker::end()) and
exists(int i | launcherShell(sys, args, i)) and
Comment on lines +292 to +326
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() = "--"
)
)
Comment on lines +367 to +369
not exists(DataFlow::ParameterNode parameter |
parameter = sys.getACommandArgument().getALocalSource()
) and

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved shell-option, wrapper-flow, and array-mutation modeling defects produce false positives and miss command-injection cases.

10 open issues
1 previously missed issue

In code that hasn't changed since last review

Medium severity Treat indirect array mutations as barriers

javascript/​ql/​lib/​semmle/​javascript/​security/​dataflow/​IndirectCommandArgument.qll:227

Calls that mutate the array are not barriers: Object.assign(args, {0: "echo"}), or a helper that writes args[0], leaves the original shell element eligible even though env now runs echo. Account for mutations through known copying calls and resolved helpers before trusting initializer positions. The same gap exists in the wrapper walk at lines 257–263.

7 resolved since last review

Review effort: Max (adaptive)

Comment on lines +114 to +127
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_]*=")
)
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"]
private predicate positionalArrayReachesUse(
DataFlow::SourceNode args, ControlFlowNode node, ControlFlowNode use
) {
node = use and args.getFile() = use.getFile()
sys.asExpr().getContainer() = wrapper and
wrapperCall.getACallee() = wrapper and
args = wrapperCall.getArgument(argsIndex).getALocalSource() and
positionalArrayReachesUse(args, args.asExpr().getASuccessor(), wrapperCall.asExpr()) 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
Comment on lines +278 to +283
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())
)
Comment on lines +303 to +316
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() |
Comment on lines +364 to +369
not exists(VarAccess access |
access = sys.getACommandArgument().asExpr() and access.getVariable().isParameter()
) and
not exists(DataFlow::ParameterNode parameter |
parameter = sys.getACommandArgument().getALocalSource()
) and
Comment on lines +376 to +392
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
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]);

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants