How to fix: child_process.spawn with shell:true
spawn(cmd, { shell: true }) or spawn("sh -c " + cmd) re-routes the command through /bin/sh, so any unescaped metacharacter in arguments (& ; | $ ` \n) executes attacker commands.
Why it matters
Attacker-controlled arguments gain full shell-injection capability, leading to RCE on the host.
The `code-cmdi-spawn-shell-true` check verifies that the server does not expose the `cmdi-spawn-shell-true` weakness in the code category. The detection runs against the response headers, body, or auxiliary probes (DNS, TLS, async fetch) as appropriate.
How to fix it
- 1Pass args as a separate array; spawn with shell:false and let Node escape them.
- 2Validate every argument against a strict allowlist before invoking.
- 3Run the child process under a non-root user and inside a sandboxed environment.
Code examples
Set shell: false in spawn options
import { spawn } from "child_process";
// BAD: shell: true, user input can inject shell commands
spawn("convert", [userInput], { shell: true });
// GOOD: shell: false (default) with argument array
spawn("convert", ["-resize", "100x100", validatedInputPath, outputPath], {
shell: false, // no shell expansion
timeout: 10_000,
});References
Detected automatically by VulnRadar. The check id is code-cmdi-spawn-shell-true, stable across every scan so you can filter or diff on it.
More code checks
- CriticalForm Submits Data Over Insecure HTTP
- MediumPotential Prototype Pollution Sinks Detected
- HighPotential Command Injection Vectors
- HighJWT Decoded Without Signature Verification
- HighPotential Path Traversal Vulnerability
- HighInsecure Authentication Mechanisms Detected
- HighPotential Insecure Deserialization
- Mediumeval() Usage in Inline Scripts
Questions
- How severe is child_process.spawn with shell:true?
- VulnRadar rates this critical severity in the code category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
- Does VulnRadar check for this automatically?
- Yes. It runs as part of every scan under the check id code-cmdi-spawn-shell-true, which stays the same between runs so you can filter, diff, or gate CI on it.
- Which standards does child_process.spawn with shell:true map to?
- It maps to CWE-78. In the OWASP Top 10 it falls under A03:2021.
Missing a check?
The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.