How to fix: child_process.exec with concatenated command string
child_process.exec(cmd) always routes cmd through /bin/sh (or cmd.exe), so exec("..." + userInput) or exec(`...${userInput}`) lets any shell metacharacter in userInput (& ; | $ ` \n) execute attacker commands.
Why it matters
Attacker-controlled input concatenated into exec() gains full shell-injection capability, leading to RCE on the host.
The `code-cmdi-exec` check verifies that the server does not expose the `cmdi-exec` 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
- 1Use execFile with an argument array instead of exec with a shell string.
- 2Validate every argument against a strict allowlist before invoking.
- 3If exec is unavoidable, escape all shell metacharacters in every interpolated value.
Code examples
Use execFile with argument arrays
import { execFile } from "child_process";
import { promisify } from "util";
const execFileAsync = promisify(execFile);
// BAD: exec(`ls ${userPath}`), shell injection possible
// GOOD: argument array, no shell
const { stdout } = await execFileAsync("ls", ["-la", userPath], {
shell: false, // never true with user input
timeout: 5000,
});References
Detected automatically by VulnRadar. The check id is code-cmdi-exec, 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.exec with concatenated command string?
- 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-exec, which stays the same between runs so you can filter, diff, or gate CI on it.
- Which standards does child_process.exec with concatenated command string 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.