Repository navigation
Conversation
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>
Contributor
There was a problem hiding this comment.
🟡 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
Check intermediate arrays for mutations before narrowing · New Require every possible option value to be known · New Treat unknown operands as possible shell options · New Preserve sinks for positional arguments evaluated by the script · New Require all argument origins to have known command indexes · New Preserve whole-array sinks for shell-enabled executions · New
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 /cbehavior. - 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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