Skip to content

JS: Ignore positional parameters to sh -c in command injection - #23284

Draft
owen-mc wants to merge 1 commit into
github:mainfrom
owen-mc:copilot/fix-js-sh-c-positional-params
Draft

owen-mc wants to merge 1 commit into
github:mainfrom
owen-mc:copilot/fix-js-sh-c-positional-params

Conversation

@owen-mc

@owen-mc owen-mc commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

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

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 github#23274

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@owen-mc
owen-mc requested review from a team and an adaptive review from Copilot November 27, 2026 13:39
*/
private string uniqueElementString(DataFlow::ArrayCreationNode args, int i) {
forex(DataFlow::Node elem | elem = arrayElementAt(args, i) |
exists(string s | elem.mayHaveStringValue(s))

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

Argument narrowing can omit executed, attacker-controlled shell input in dynamic-option, mutated-array, ambiguous-origin, and shell-interpreted operand cases, creating critical command-injection false negatives.

6 open issues
What changed in this PR

Refines JavaScript command-injection analysis to distinguish sh -c and bash -c scripts from positional data, addressing the false positives reported in #23274.

Changes:

  • Adds argument-prefix and shell-option tracking to select command-string sinks while preserving Windows cmd.exe /c behavior.
  • Adds regression cases across four command-injection query suites and updates expected results.
  • Documents the analysis change.
File Description
javascript/​ql/​test/​query-tests/​Security/​CWE-078/​UnsafeShellCommandConstruction/​UnsafeShellCommandConstruction.expected Updates expected library-input alerts.
javascript/​ql/​test/​query-tests/​Security/​CWE-078/​UnsafeShellCommandConstruction/​lib/​lib.js Contrasts positional data with concatenated shell code.
javascript/​ql/​test/​query-tests/​Security/​CWE-078/​ShellCommandInjectionFromEnvironment/​tst_shell-command-injection-from-environment.js Adds environment-derived positional and script cases.
javascript/​ql/​test/​query-tests/​Security/​CWE-078/​ShellCommandInjectionFromEnvironment/​ShellCommandInjectionFromEnvironment.expected Updates environment-query results and flow paths.
javascript/​ql/​test/​query-tests/​Security/​CWE-078/​IndirectCommandInjection/​IndirectCommandInjection.expected Adds the expected tainted-script result.
javascript/​ql/​test/​query-tests/​Security/​CWE-078/​IndirectCommandInjection/​command-line-parameter-command-injection.js Adds positional, concatenated-array, and unsafe-script cases.
javascript/​ql/​test/​query-tests/​Security/​CWE-078/​CommandInjection/​CommandInjection.expected Updates direct-query alerts and flow paths.
javascript/​ql/​test/​query-tests/​Security/​CWE-078/​CommandInjection/​child_process-test.js Adds shell-option, positional, concatenation, and mutation cases.
javascript/​ql/​lib/​semmle/​javascript/​security/​dataflow/​IndirectCommandArgument.qll Implements shell-specific command-operand selection.
javascript/​ql/​lib/​change-notes/​2026-11-27-sh-c-positional-parameters.md Documents reduced positional-argument false positives.

Review effort: Max (adaptive)

Comment on lines +102 to +106
result = pred.backtrack(t2, t)
or
t = t2.continue() and
pred.(DataFlow::MethodCallNode).getMethodName() = "concat" and
result = pred.(DataFlow::MethodCallNode).getReceiver().getALocalSource()
Comment on lines +157 to +162
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))
}
)
or
unixOptionScanIndex(args, result) and
not any(string s | arrayElementAt(args, result).mayHaveStringValue(s)).charAt(0) = ["-", "+"]
Comment on lines +263 to +264
if isUnix = true and args = argumentListPrefix(sys) and exists(unixCommandStringIndex(args))
then source = arrayElementAt(args, unixCommandStringIndex(args))
Comment on lines +263 to +264
if isUnix = true and args = argumentListPrefix(sys) and exists(unixCommandStringIndex(args))
then source = arrayElementAt(args, unixCommandStringIndex(args))
Comment on lines +263 to +264
if isUnix = true and args = argumentListPrefix(sys) and exists(unixCommandStringIndex(args))
then source = arrayElementAt(args, unixCommandStringIndex(args))

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: false positive in js/command-line-injection on sh -c positional parameters ($1/$@)

3 participants