CVE-2024-27980: Node.js Command Injection in child_process on Windows (BatBadBut)
A command injection vulnerability in Node.js on Windows when spawning batch files (.bat or .cmd). Upstream fixes shipped for Node.js 20.x (and late 18.x), so only older or unupgraded EOL Node.js deployments need backport help.
1. Affected Environments & Upstream Patch Scope
- Operating System: Windows environments running Node.js server runtimes, worker processes, or build tools. Linux and macOS are unaffected because they do not invoke
cmd.exefor batch files. - Node.js 20.x (Fixed Upstream): Node.js 20.x received official upstream fixes starting in
v20.12.2(andv20.15.1for follow-up bypassCVE-2024-36138). Applications on Node.js 20 should upgrade directly upstream. - Legacy Scope (Node.js 14, 16, and frozen 18.x): Node.js 14.x and 16.x were already EOL and received zero official patches. Node.js 18.x received upstream patches (
v18.20.2/v18.20.4) before reaching EOL in April 2025. Only older EOL deployments that cannot upgrade need backport support. - Trigger Condition: Applications calling
child_process.spawn(),child_process.spawnSync(), orchild_process.execFile()on.bator.cmdtargets (directly or resolved viaPATH) with untrusted arguments.
2. Technical Root Cause
On Windows, batch files are not native PE binaries. When an application passes a .bat or .cmd path to the Windows CreateProcessW API, the operating system transparently invokes cmd.exe /c to parse and run the script.
Developers frequently rely on child_process.spawn(command, [args]) assuming that passing arguments as an array avoids shell injection. Because the target is a batch script, cmd.exe parses the concatenated command line after process creation. Metacharacters such as &, |, <, >, ^, and % are interpreted by cmd.exe as command delimiters or variable expansions rather than literal argument text.
As a result, passing untrusted values into the argument list of a batch script lets an attacker break out of the argument context and execute arbitrary commands with the privileges of the Node.js process.
3. Upstream Fix Behavior vs Legacy Impact
The upstream Node.js security release fixed this by introducing an explicit behavioral check on Windows: spawning a .bat or .cmd file without setting { shell: true } now fails with an EINVAL error (and follow-up patches addressed edge cases tracked under CVE-2024-36138).
For legacy applications pinned to Node.js 14 or 16 (or older 18.x builds blocked by native C++ addon incompatibilities), no official patch exists for 14/16 and upgrading straight to Node 20+ can require extensive dependency rewrites. Note also that passing { shell: true } without strict, complete input sanitization still leaves batch invocations exposed.
4. Remediation & Architecture Hardening
Where upgrading to patched Node.js 20.15.1+ (or 22 LTS+) is not immediately possible, prefer eliminating batch wrappers rather than relying on ad-hoc regex filters:
- Invoke JavaScript directly: Replace
.bat/.cmdwrapper scripts with Node.js scripts invoked vianode.exe(for examplespawn('node.exe', ['script.js', ...])), which avoidscmd.exeparsing. - Target native executables directly: Where possible, call the underlying binary (such as
git.exe) or invoke Node-based CLI tools vianode.exeand their JS entry point (for examplenpm-cli.js) instead of.cmdshims. - Test thoroughly before deploying: Because Windows command-line quoting,
PATHEXTresolution, andcmd.exeescaping have subtle bypass vectors, validate any runtime patch or invocation change against actual Windows workloads in staging.
Running Legacy Node.js Services in Production?
Legacy Node.js applications (v14, v16, and frozen v18) running on Windows and Linux face unpatched core vulnerabilities and native addon incompatibilities. CrawlShield delivers tested runtime security patches and compliance documentation tailored for older EOL Node.js environments.
Request legacy Node.js coverage