Repository navigation
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
martinrrm
added a commit
to npm/promise-spawn
that referenced
this pull request
Oct 12, 2026
## Summary Decode an already quoted and caret-escaped executable token before using it for Windows executable lookup. This is a supporting change for npm/cli#10044. That proposal protects an executable name before placing it in a command interpreted by `cmd.exe`. `promise-spawn` must still identify the protected executable as a `.cmd` or `.bat` shim so it can apply the existing extra escaping required for batch-file arguments. ## Why this is needed There are two distinct representations of the executable: 1. **Execution representation** — protected for `cmd.exe`, and kept unchanged in the final command string. 2. **Lookup representation** — decoded temporarily so `which.sync()` can resolve the executable and determine whether it is a batch shim. For example, a caller may pass this protected executable token: ```text ^"tool.cmd^" ``` The existing parser derives the lookup value directly from that text: ```text ^"tool.cmd^" ``` That is not an executable name that `which.sync()` can resolve. If lookup fails, `promise-spawn` may not recognize that the command is a `.cmd` shim, so `doubleEscape` remains false. The updated parser derives this lookup-only value instead: ```text tool.cmd ``` The original `^"tool.cmd^"` token is still retained in the command sent to `cmd.exe`; only the temporary value used for executable detection is decoded. ## Why batch shims need detection `promise-spawn` already distinguishes native executables from `.cmd` and `.bat` shims: ```js doubleEscape = pathToInitial.endsWith('.cmd') || pathToInitial.endsWith('.bat') ``` A batch shim causes its arguments to pass through an additional `cmd.exe` parsing layer. The existing `escape.cmd(arg, doubleEscape)` implementation therefore adds another level of caret escaping when `doubleEscape` is true. Using the safe argument `hello world` as an example: | Executable detection | Encoded argument | |---|---| | Native executable or unresolved command | `^"hello^ world^"` | | `.cmd`/`.bat` shim | `^^^"hello^^^ world^^^"` | If an escaped `tool.cmd` token cannot be resolved, the first form may be selected even though the second form is required for the batch shim. ## Data flow with npm CLI The related npm CLI proposal has this flow on Windows: ```text package bin name -> libnpmexec protects the executable token for cmd.exe -> promise-spawn receives the protected script -> promise-spawn decodes only the first token for which.sync() -> .cmd/.bat detection selects the existing double-escape mode -> the original protected script is sent to cmd.exe ``` This PR implements only the lookup step. It does not decide which executable names are valid, add shell quoting to arbitrary commands, or decode the command that is executed. ## Changes - Decode caret escapes outside double-quoted sections while preparing the temporary lookup token. - Treat only `"` as a `cmd.exe` quote delimiter; single quotes have no quoting semantics in `cmd.exe`. - Remove surrounding double quotes before calling `which.sync()`. - Leave the original command string unchanged for execution. - Preserve the existing `.cmd`/`.bat` argument double-escaping behavior. ## Before and after | Input command token | Previous lookup value | New lookup value | Executed token | |---|---|---|---| | `tool` | `tool` | `tool` | `tool` | | `"tool.cmd"` | `"tool.cmd"` | `tool.cmd` | `"tool.cmd"` | | `^"tool.cmd^"` | `^"tool.cmd^"` | `tool.cmd` | `^"tool.cmd^"` | | `^"two^ words.cmd^"` | `^"two^ words.cmd^"` | `two words.cmd` | `^"two^ words.cmd^"` | ## Tests Added mock-based regression coverage confirming that: - An escaped and quoted executable token is decoded before lookup. - An escaped `.cmd` shim is recognized. - Arguments for the recognized batch shim receive the existing double-escaping treatment. - The final command sent to `cmd.exe` retains the protected executable token. The tests validate command construction and executable lookup without executing an injection payload. ## Related change - npm/cli#10044 --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Summary
When
npx <package>infers an executable from packagebinmetadata, the executable name becomes part of a synthetic npm script. On Windows, that script is ultimately interpreted bycmd.exe. This change protects the inferred executable token according to the selected shell instead of forwarding it unquoted.Supporting dependency PR: npm/promise-spawn#166
Changes
lib/run-script.jscallscript was supplied.scriptShell, including POSIX shells selected on Windows.cmd.exe, protect command separators, spaces, command keywords, and other cmd metacharacters.callas an intentionally supplied shell script; its arguments are not treated as inferred executable names.lib/index.jsThis cache-manifest adjustment is related hardening and may be split into a separate change during review if preferred.
Tests and documentation
Dependency requirement
The npm CLI change depends on the promise-spawn fix in npm/promise-spawn#166. That change allows promise-spawn to recognize an executable token that has already been quoted and caret-escaped, preserving
.cmd/.batdouble escaping for arguments.The dependency should be consumed through released
@npmcli/promise-spawnand@npmcli/run-scriptversions before merging this PR. The tracked bundled promise-spawn copy is included here to show the integrated behavior and should be regenerated from the released dependency during final dependency updates.Validation
git diff --checkpasses.Review status
This is intentionally a draft while the promise-spawn dependency change and release sequence are reviewed.