CVE-2026-137197: Command Injection on Windows in Node.js cross-spawn via Batch File Resolution Divergence
Vulnerability ID: CVE-2026-137197
CVSS Score: 8.1
Published: 2026-12-04
CVE-2026-137197 (GHSA-w3gc-668g-rf8f) is an OS command injection vulnerability (CWE-78) in the Node.js cross-spawn library affecting versions prior to 6.0.8 and 7.1.1 on Microsoft Windows. The flaw stems from parsing and resolution discrepancies between how cross-spawn identifies batch scripts (.bat and .cmd) and how Windows cmd.exe resolves target files. When a command string contains embedded double quotes, token delimiters, trailing whitespace, or relies on a child-specific PATHEXT environment variable, cross-spawn fails to detect that the target is a batch script and omits cmd.exe metacharacter escaping on user-supplied arguments, enabling arbitrary OS command execution.
TL;DR
An OS command injection vulnerability in the Node.js cross-spawn library on Windows allows attackers to execute arbitrary commands via cmd.exe when untrusted arguments are passed to batch scripts invoked with quotes, delimiters, trailing tabs, or custom child PATHEXT variables. Upgrade to cross-spawn 6.0.8 or 7.1.1.
⚠️ Exploit Status: POC
Technical Details
- CVE / GHSA ID: CVE-2026-137197 / GHSA-w3gc-668g-rf8f
- Weakness Class: CWE-78 (OS Command Injection)
- CVSS v3.1 Score: 8.1 High (CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:H/A:H)
- EPSS Score: 0.824% (58.66th Percentile)
- Affected Package: cross-spawn (npm) < 6.0.8 and >= 7.0.0, < 7.1.1
- Affected Platform: Microsoft Windows (win32)
- Exploit Status: Proof-of-Concept (Unit test reproduction cases)
- CISA KEV Status: Not Listed
Affected Systems
- moxystudio/node-cross-spawn (npm package: cross-spawn) running on Microsoft Windows
- cross-spawn: < 6.0.8 (Fixed in:
6.0.8) - cross-spawn: >= 7.0.0, < 7.1.1 (Fixed in:
7.1.1)
Code Analysis
Commit: d0ead35
fix: properly escape commands and detect batch files on Windows (v7 branch)
--- a/lib/parse.js +++ b/lib/parse.js @@ -9,8 +9,9 @@ const isWin = process.platform === 'win32'; const isExecutableRegExp = /\.(?:com|exe)$/i; const isBatchFileRegExp = /\.(?:bat|cmd)$/i; // Windows strips trailing dots and spaces from file names, so `cmd.exe` runs `foo.bat ` as `foo.bat` +// Other trailing whitespace, such as tabs, is treated the same way to be safe // See CVE-2024-36138 for the equivalent Node.js vulnerability -const trailingDotsAndSpacesRegExp = /[. ]+$/; +const trailingDotsAndSpacesRegExp = /[.\s]+$/; function detectShebang(parsed) { parsed.file = resolveCommand(parsed); @@ -45,6 +46,11 @@ function parseNonShell(parsed) { return parsed; } + // Windows file names can't contain double quotes, and cmd.exe ignores them in command names, so remove them + // Otherwise a quoted command, e.g. `"foo.bat"`, wouldn't be found and could be run as a batch file without + // being detected as one + parsed.command = parsed.command.replace(/"/g, ''); + // Detect & add support for shebangs const commandFile = detectShebang(parsed); --- a/lib/util/escape.js +++ b/lib/util/escape.js @@ -56,10 +56,11 @@ function quoteArgument(arg, escapedQuote) { function escapeCommand(arg) { assertNoLineBreaks(arg); - // Escape meta chars and neutralize %VAR% expansion - arg = escapePercent(arg.replace(metaCharsRegExp, '^$1')); - - return arg; + // Quote the command, so that cmd.exe treats it as a single name + // Otherwise cmd.exe would end the name at the first space, tab, `,`, `;` or `=`, even if escaped with ^, + // and run whatever it finds with that name, e.g. a batch file, with the rest as extra arguments + // Command names can't contain double quotes, see `parse.js`, and meta chars are literal inside quotes + return `"${escapePercent(arg)}"`; } --- a/lib/util/resolveCommand.js +++ b/lib/util/resolveCommand.js @@ -4,6 +4,13 @@ const path = require('path'); const which = require('which'); const getPathKey = require('path-key'); +// Returns the child's PATHEXT, which cmd.exe will use to find the command +function getPathExt(env) { + const key = Object.keys(env).find((name) => name.toUpperCase() === 'PATHEXT'); + + return key ? env[key] : undefined; +} + function resolveCommandAttempt(parsed, withoutPathExt) { const env = parsed.options.env || process.env; @@ -26,7 +33,7 @@ function resolveCommandAttempt(parsed, withoutPathExt) { try { resolved = which.sync(parsed.command, { path: env[getPathKey({ env })], - pathExt: withoutPathExt ? path.delimiter : undefined, + pathExt: withoutPathExt ? path.delimiter : getPathExt(env), }); } catch (e) { /* Empty */ Commit: 4a15965
fix: properly escape commands and detect batch files on Windows (v6 branch backport)
Exploit Details
- GitHub: Official regression unit tests in test/index.test.js demonstrating batch metacharacter escaping bypass via quoted commands, trailing tabs, delimiters, and custom child PATHEXT.
Mitigation Strategies
- Upgrade cross-spawn to version 7.1.1 (for 7.x users) or version 6.0.8 (for 6.x and earlier users).
- Enforce strict separation between the command name and command arguments, passing all arguments exclusively via the args array rather than concatenating them into the command string.
- Strip surrounding double quotes and trailing whitespace from command strings before passing them to child process spawning functions.
- Validate untrusted user input against a strict character allowlist and reject Windows cmd.exe metacharacters (&, |, <, >, ^, %, ") when executing child processes on Windows.
Remediation Steps:
- Run
npm ls cross-spawnto identify all direct and transitive installations of cross-spawn across the project. - Run
npm install cross-spawn@^7.1.1(or^6.0.8for legacy 6.x branches) or runnpm audit fixto update lockfiles. - If cross-spawn is locked by a third-party package, add an
overrides(npm) orresolutions(Yarn/pnpm) block to package.json forcingcross-spawnto>=7.1.1or>=6.0.8 <7.0.0. - Audit application source code for calls matching
spawn('cmd arg')orspawn('"cmd.bat"')and refactor them intospawn('cmd', ['arg'])andspawn('cmd.bat')to prevent ENOENT errors after upgrading.
References
- GitHub Security Advisory: GHSA-w3gc-668g-rf8f
- National Vulnerability Database (NVD) - CVE-2026-137197
- CVE.org Record: CVE-2026-137197
- CVE Project GitHub Repository Record: CVE-2026-137197
- cross-spawn Pull Request #196
- cross-spawn Pull Request #197
- Fix Commit d0ead352a7380492cbb0f6548fa86f19448323c9 (v7 branch)
- Fix Commit 4a15965b05415e9f817315206771b25558804b16 (v6 branch)
- cross-spawn Official Release v7.1.1
- cross-spawn Official Release v6.0.8
- Open Source Vulnerabilities (OSV) Record: GHSA-w3gc-668g-rf8f
- Wiz Vulnerability Database: CVE-2026-137197
Read the full report for CVE-2026-137197 on our website for more details including interactive diagrams and full exploit analysis.
