fix(core): prevent command injection by removing unsafe cmd.exe fallback on Windows#1448
Open
l3tchupkt wants to merge 1 commit intogoogle:mainfrom
Open
fix(core): prevent command injection by removing unsafe cmd.exe fallback on Windows#1448l3tchupkt wants to merge 1 commit intogoogle:mainfrom
l3tchupkt wants to merge 1 commit intogoogle:mainfrom
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
When bash is absent, zx silently fell through to child_process with
shell:true — which on Windows means cmd.exe. cmd.exe does not interpret
bash-style ANSI-C quoting (\$'...''), so every interpolated argument is a
straight injection vector: '& calc &' passes through the quoting layer
unmodified and the metacharacters are executed by cmd.exe.
Fix: make the bash-not-found path Windows-aware.
- Probe PATH for pwsh then powershell.exe in order of preference.
- If a PS shell is found, use it with the matching quotePowerShell quoting
convention and the '; exit \0' postfix.
- If no safe shell is found at all on win32, throw a Fail with a clear
diagnostic rather than silently executing in an insecure context.
- Non-Windows platforms retain the prior (acceptable) behaviour.
- Explicit shell/prefix/postfix CLI overrides are still honoured after
auto-detection runs.
Reported-by: LAKSHMIKANTHAN K (letchupkt)
CWE: CWE-78 (OS Command Injection)
0ba62e5 to
dd8808a
Compare
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.
Reported-by: LAKSHMIKANTHAN K (letchupkt)
CWE: CWE-78 (Command Injection)
Fixes #issue / suggests an improvement
Summary
This PR fixes a command injection vulnerability on Windows caused by an unsafe fallback to
cmd.exewhenbashis unavailable.Previously, if
useBash()failed, the error was silently ignored and execution continued with Node’s defaultshell: true, which resolves tocmd.exeon Windows. However,zxcontinued using Bash-style quoting ($'...'), which is not supported bycmd.exe, allowing special characters (e.g.,&) to break out of argument boundaries and execute arbitrary commands.This change ensures that
zxnever falls back tocmd.exein an unsafe configuration.Relevant technical choices
Added explicit Windows-specific fallback handling when
bashis unavailableIntroduced safe shell resolution:
pwshpowershell.exeIf no safe shell is available, throw a
Failerror instead of falling back tocmd.exeUpdated quoting behavior to use
quotePowerShellfor PowerShell-based shellsPreserved original behavior for non-Windows environments
The fix ensures that command execution always uses a shell compatible with the quoting mechanism, preventing injection vulnerabilities
Impact
Prevents command injection on Windows systems where
bashis not installed.This mitigates:
Usage demo
npm buildbefore committing and verified the bundle updates correctly.run testand confirmed all tests succeed. Added tests to cover my changes if needed.