Repository navigation
fix(libnpmexec): escape inferred executable names for selected shell - #10044
Merged
Merged
Conversation
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>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Use PowerShell's call operator and literal-string escaping for inferred executable names, with construction and Windows runtime coverage. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 3e53db77-0fe4-4320-8ada-f2962ddd303c
martinrrm
force-pushed
the
martinrrm/cli-170-windows-fix
branch
from
October 28, 2026 01:42
8d1dd0b to
3f7f129
Compare
martinrrm
marked this pull request as ready for review
October 28, 2026 01:53
jeffkl
previously approved these changes
Oct 28, 2026
There was a problem hiding this comment.
🟡 Changes recommended
PowerShell escaping still permits command injection through Unicode quote delimiters, while POSIX-style argument escaping breaks valid arguments containing apostrophes.
2 open issues
What changed in this PR
This PR hardens libnpmexec’s executable handling for npm exec and npx, using shell-specific quoting and installed package metadata on cache hits.
Changes:
- Add executable escaping for cmd.exe, PowerShell, and POSIX shells while preserving explicit
callscripts. - Select cached executables from installed manifests rather than registry metadata.
- Add regression tests and document shell-specific restrictions.
| File | Description |
|---|---|
workspaces/libnpmexec/test/run-script.js |
Tests shell quoting, rejected names, and literal argument handling. |
workspaces/libnpmexec/test/registry.js |
Tests normalized bin names and cache metadata mismatches. |
workspaces/libnpmexec/README.md |
Documents executable escaping, restrictions, and cache behavior. |
workspaces/libnpmexec/lib/run-script.js |
Adds shell-specific executable encoding and cmd.exe validation. |
workspaces/libnpmexec/lib/index.js |
Uses installed bin metadata on matching cache hits. |
Review effort: Balanced
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 155db33f-8c59-4859-a9b9-374c3a77e721
jeffkl
approved these changes
Nov 2, 2026
Contributor
|
🎉 Backport to |
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 status
The required promise-spawn fix is released and bundled as @npmcli/promise-spawn@10.0.1 through #10152. This branch is rebased onto latest and no longer carries a duplicate bundled dependency diff.
Validation
Review status
Ready for review.