| name | command-injection-anti-pattern |
| description | Security anti-pattern for OS Command Injection vulnerabilities (CWE-78). Use when generating or reviewing code that executes shell commands, runs system processes, or handles user input in command-line operations. Detects shell string concatenation and recommends argument arrays. |
Command Injection Anti-Pattern
Severity: Critical
Summary
Command injection allows attackers to execute arbitrary OS commands by manipulating user input. This anti-pattern occurs when applications concatenate user input into shell command strings. Common in AI-generated code. Enables complete system compromise, data exfiltration, malware installation, and lateral movement.
The Anti-Pattern
User input embedded in shell command strings enables command injection. The shell cannot distinguish between intended commands and attacker-injected commands.
BAD Code Example
import os
def ping_host(hostname):
command = "ping -c 4 " + hostname
os.system(command)
GOOD Code Example
subprocess
():
re
re.(, hostname):
ValueError()
:
subprocess.run([, , , hostname], check=, shell=)
subprocess.CalledProcessError e:
()