How to Fix Command Injection in Express
Learn how to prevent and fix Command Injection vulnerabilities in Express applications. Step-by-step guide with code examples, security checklists, and best practices.
What Is Command Injection?
Command Injection (also known as OS Command Injection or Shell Injection) occurs when an application passes user-supplied input to a system shell for execution. An attacker can inject shell metacharacters to append or modify the intended command, executing arbitrary commands on the server with the application's privileges.
The vulnerability typically arises when applications use functions like `child_process.exec()`, `os.system()`, or `Runtime.exec()` with user-controlled input. Shell metacharacters such as `;`, `&&`, `||`, `|`, and backticks allow command chaining or substitution. For example, if an application runs `ping -c 1 <user_input>` and the user provides `google.com; cat /etc/passwd`, both commands execute.
In Node.js applications, command injection is possible through `child_process.exec()` (which spawns a shell) and `child_process.execSync()`. It can also occur indirectly through libraries that shell out to system commands (e.g., image processing tools, PDF generators, or build tools that invoke shell commands). Server-side applications that interact with the operating system for tasks like file conversion, image manipulation, or system monitoring are particularly at risk.
Why It Matters
Command injection is one of the most severe vulnerabilities because it gives attackers direct access to the server's operating system. An attacker can read or modify any file the application has access to, install backdoors and malware, pivot to other systems on the network, exfiltrate data, launch attacks against other targets, and potentially gain root access through privilege escalation. Because the commands execute with the application's permissions, the impact depends on the application's privilege level -- but even unprivileged access can be devastating.
How to Fix It in Express
Avoid executing OS commands based on user input whenever possible. Use language-native libraries instead of shelling out (e.g., use Node.js `dns.lookup()` instead of spawning `ping`). When shell execution is unavoidable, use `child_process.execFile()` or `child_process.spawn()` which do not invoke a shell and pass arguments as arrays. Never pass user input through `child_process.exec()`. Validate input against strict allowlists (e.g., only allow alphanumeric characters for hostnames). Escape shell metacharacters if you must include user input in commands. Run the application with minimal OS privileges. Use containerization and sandboxing to limit the blast radius.
Express-Specific Advice
- Use `helmet` middleware for setting security headers (CSP, HSTS, X-Frame-Options, etc.) with sensible defaults.
- Use `express-rate-limit` for rate limiting. Apply stricter limits to authentication endpoints and API routes.
- Always use parameterized queries with your database driver. Never concatenate user input into SQL strings.
- Validate request bodies using `express-validator`, Zod, or Joi middleware. Reject requests that do not match expected schemas.
Express Security Checklist for Command Injection
Express Security Best Practices
Use `helmet` middleware for setting security headers (CSP, HSTS, X-Frame-Options, etc.) with sensible defaults.
Use `express-rate-limit` for rate limiting. Apply stricter limits to authentication endpoints and API routes.
Always use parameterized queries with your database driver. Never concatenate user input into SQL strings.
Validate request bodies using `express-validator`, Zod, or Joi middleware. Reject requests that do not match expected schemas.
Use `cors` middleware with explicit origin allowlists. Never use `cors({ origin: '*' })` in production.
Disable the `X-Powered-By` header with `app.disable('x-powered-by')` or by using helmet.
Use `multer` or `busboy` for file uploads with strict file type and size limits. Store files outside the web root.
Implement proper error handling middleware that does not leak stack traces or internal details in production.
Scan Your Express App with SafeVibe
Stop guessing if your Express app is vulnerable to Command Injection. Run an automated penetration test in minutes and get actionable results.
Start Free Scan