QA
Beginner LevelWeb Security & Cyber Defense

What is Command Injection and why should you avoid passing user inputs to child_process.exec()?

❓ Interview Prompt: Explain Command Injection, OS shell invocation, shell metacharacters (`;&|`), and using parameterized `child_process.execFile()`.

Command Injection occurs when un-sanitized user input is passed into an OS system shell execution function (like 'child_process.exec()' in Node.js or 'system()' in C/PHP):

Exploit Mechanics:

Code runs: 'exec('ping -c 1 ' + userInput)'.

Attacker inputs: '8.8.8.8 ; cat /etc/passwd'.

The shell executes the ping command AND the injected command ('cat /etc/passwd') sequentially!

Defense:

Avoid system shell invocation! Use 'child_process.execFile()' or 'execFile' variants that execute binaries directly without passing commands through a shell interpreter.

Code Implementation Example

typescript
import { execFile } from 'child_process';

// ❌ VULNERABLE: Invokes shell, allowing metacharacter injection (; cat /etc/passwd)
// exec('ping -c 1 ${userInput}');

// ✅ SECURE: execFile passes arguments directly to process without shell interpreter!
execFile('ping', ['-c', '1', userInput], (error, stdout) => {
  console.log(stdout);
});
💡Preventing Command Injection by executing binaries directly with execFile.
🎯 Interview Key Takeaway

Never concatenate user input into system shell exec calls; pass parameters as an array to execFile to bypass shell interpreters.

#Command Injection#Node.js#OS Security#Beginner
Back to all Web Security & Cyber Defense Questions→

More Web Security & Cyber Defense Interview Questions

Beginner LevelSecurity & Web Security

Explain OWASP Top 10 vulnerabilities (XSS, CSRF, SQL Injection) and their defenses.

Beginner LevelSecurity & Web Security

What are the 3 types of XSS (Stored, Reflected, DOM-based) and how do they differ?

Beginner LevelSecurity & Web Security

How do Parameterized Queries (Prepared Statements) prevent SQL Injection attacks?