You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 10ffa9f
Browse filesBrowse the repository at this point in the historyBrowse files
fix(libnpmexec): escape inferred executable names for selected shell (#10044)
## Summary
When `npx <package>` infers an executable from package `bin` metadata,
the executable name becomes part of a synthetic npm script. On Windows,
that script is ultimately interpreted by `cmd.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.js`
- Escape the executable token when no explicit `call` script was
supplied.
- Select the escaping strategy from `scriptShell`, including POSIX
shells selected on Windows.
- For `cmd.exe`, protect command separators, spaces, command keywords,
and other cmd metacharacters.
- Reject executable names containing double quotes, percent signs,
exclamation marks, or control characters that cannot be safely
represented as a cmd executable token.
- Preserve `call` as an intentionally supplied shell script; its
arguments are not treated as inferred executable names.
### `lib/index.js`
- On a matching npx-cache hit, select the inferred command from the
installed package manifest rather than mutable registry metadata.
This cache-manifest adjustment is related hardening and may be split
into a separate change during review if preferred.
### Tests and documentation
- Add unit coverage for cmd.exe token encoding and rejection cases.
- Add coverage for POSIX escaping when a POSIX shell is selected on
Windows.
- Add normalized-bin registry regressions covering command separators
and cache metadata mismatches.
- Document the shell-specific executable handling and rejected cmd.exe
characters.
## Dependency status
The required promise-spawn fix is released and bundled as
@npmcli/[email protected] through #10152. This branch is rebased onto
latest and no longer carries a duplicate bundled dependency diff.
## Validation
- Focused and full libnpmexec tests, eslint, and template-oss postlint
pass locally on Node 24.15.0.
- Mutation testing confirms the shell and cache regressions fail without
the production changes.
- Added PowerShell-specific construction coverage and a Windows runtime
test for inferred executable invocation.
- The npm/cli CI matrix passes on Linux, macOS, and Windows across all
configured Node versions.
- No vulnerability reproduction or malicious command execution was run.
## Review status
Ready for review.
---------
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 3e53db77-0fe4-4320-8ada-f2962ddd303c
Copilot-Session: 155db33f-8c59-4859-a9b9-374c3a77e721
Copy file name to clipboardExpand all lines: workspaces/libnpmexec/README.md
+9-1Lines changed: 9 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -28,7 +28,7 @@ await libexec({
28
28
29
29
-`opts`:
30
30
-`args`: List of pkgs to execute **Array<String>**, defaults to `[]`
31
-
-`call`: An alternative command to run when using `packages` option **String**, defaults to empty string.
31
+
-`call`: An alternative shell script to run when using `packages` option **String**, defaults to empty string. Unlike an executable selected from `args` or a package's `bin`, this is intentionally interpreted as shell syntax.
32
32
-`cache`: The path location to where the npm cache folder is placed **String**
33
33
-`npxCache`: The path location to where the npx cache folder is placed **String**
34
34
-`chalk`: Chalk instance to use for colors? **Required**
@@ -42,6 +42,14 @@ await libexec({
42
42
-`yes`: Should skip download confirmation prompt when fetching missing packages from the registry? **Boolean**
43
43
-`registry`, `cache`, and more options that are forwarded to [@npmcli/arborist](https://github.com/npm/cli/blob/latest/workspaces/arborist/README.md) and [pacote](https://github.com/npm/pacote/#options)**Object**
44
44
45
+
Executable names are escaped for the selected shell, including POSIX shells and
46
+
PowerShell used on Windows. For inferred PowerShell invocations, arguments are
47
+
also escaped as literal strings. With `cmd.exe`, executable names containing double
48
+
quotes, percent signs, exclamation marks, or control characters are rejected
49
+
with `EINVALIDCOMMAND`. Use a bin name without these characters. On a matching
50
+
npx-cache hit, command selection uses the installed package's `bin` rather than
0 commit comments