How implicit cmd.exe invocation during child_process.spawn() on Windows allowed arbitrary command execution when invoking .bat or .cmd files.
Imagine you tell a helper: 'Go tell the computer to run the script `build.bat` with the parameter `file.txt`'. On Windows, the helper doesn't run the file directly—he passes it to a command prompt assistant. If the parameter is `file.txt & calc.exe`, the command prompt sees the `&` symbol and immediately launches the calculator program with full access.
A Node.js backend on Windows calls child_process.spawn('setup.bat', [userInput]).
The attacker supplies input containing quotes and command chaining operators: test" & calc.exe.
Windows launches cmd.exe /c setup.bat "test" & calc.exe.
The shell interprets the & symbol, executing the attacker's arbitrary command.
सरल और समझने योग्य उच्च स्तरीय प्रोग्रामिंग कोड में प्रस्तुत (बिना किसी बाइनरी या असेंबली के)।
// VULNERABLE: lib/child_process.js before patch
function spawn(file, args, options) {
// ROOT CAUSE:
// On Windows, if 'file' ends in .bat or .cmd, CreateProcess cannot execute it directly.
// Node.js spawned 'cmd.exe /d /s /c' without properly escaping quotes inside args!
if (process.platform === 'win32' && /\.(bat|cmd)$/i.test(file)) {
return spawn('cmd.exe', ['/d', '/s', '/c', file, ...args], options);
}
}
// SECURE: lib/child_process.js patch
function spawn(file, args, options) {
if (process.platform === 'win32' && /\.(bat|cmd)$/i.test(file)) {
// 1. By default, refuse to spawn batch files without explicit shell acknowledgment
if (!options.shell) {
const err = new Error(`EINVAL: Spawning batch files on Windows requires explicit options.shell`);
err.code = 'EINVAL';
throw err;
}
// 2. Perform rigorous Windows command argument escaping
args = args.map(arg => sanitizeBatchArg(arg));
}
}