
command-injection-anti-pattern
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.
"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 is a critical vulnerability that allows attackers to execute arbitrary operating system commands by manipulating user input. This anti-pattern arises when an application constructs and executes shell commands by concatenating user-provided data into a command string. This is a common and dangerous practice often found in AI-generated code. A successful attack can lead to complete system compromise, data exfiltration, malware installation, and lateral movement within a network.
The Anti-Pattern
The command injection anti-pattern occurs when user input is insecurely embedded within a command string that is executed by a shell interpreter. The shell cannot distinguish between the intended command and the attacker's injected commands.
BAD Code Example
# VULNERABLE: Shell command with user input
import os
def ping_host(hostname):
# User input is directly concatenated into the command string.
# An attacker can inject malicious commands separated by a semicolon or other shell metacharacters.
command = "ping -c 4 " + hostname
os.system(command)
# Example of a successful attack:
# hostname = "google.com; rm -rf /"
# Resulting command: "ping -c 4 google.com; rm -rf /"
# This executes the ping and then attempts to delete the entire filesystem.
GOOD Code Example
# SECURE: Use argument arrays, avoid shell
import subprocess
def ping_host(hostname):
# Input should be validated against an allowlist of characters or a specific format.
# For simplicity, this example proceeds directly to safe execution.
# The command and its arguments are passed as a list.
# The underlying OS API executes the command directly without invoking a shell,
# so shell metacharacters in `hostname` are treated as a literal string.
try:
subprocess.run(["ping", "-c", "4", hostname], check=True, shell=False)
except subprocess.CalledProcessError as e:
print(f"Error executing ping: {e}")
Detection
- Look for the use of functions that execute shell commands, such as
os.system(),subprocess.popen(), orsubprocess.run()withshell=True. - Search for string concatenation (
+), f-strings, or template literals used to build command strings that include user input. - Review any code where user-controlled variables are passed into functions that execute system commands.
Prevention
- [ ] Use argument arrays instead of shell strings (e.g.,
subprocess.run(["command", "arg1", "arg2"], shell=False)). - [ ] Never pass
shell=Truewith user-controlled input to execution functions. - [ ] Validate all input against a strict allowlist of known-good values or formats.
- [ ] Use language-specific libraries or APIs instead of external shell commands whenever possible.
- [ ] Apply the Principle of Least Privilege to the process executing the command, restricting its permissions to the absolute minimum required.
Related Security Patterns & Anti-Patterns
- SQL Injection Anti-Pattern: A similar injection pattern targeting databases.
- Path Traversal Anti-Pattern: Often combined with command injection to access or create files in unintended locations.
- Missing Input Validation Anti-Pattern: A fundamental weakness that enables command injection.
References
You Might Also Like
Related Skills

create-pr
Creates GitHub pull requests with properly formatted titles that pass the check-pr-title CI validation. Use when creating PRs, submitting changes for review, or when the user says /pr or asks to create a pull request.
n8n-io
electron-chromium-upgrade
Guide for performing Chromium version upgrades in the Electron project. Use when working on the roller/chromium/main branch to fix patch conflicts during `e sync --3`. Covers the patch application workflow, conflict resolution, analyzing upstream Chromium changes, and proper commit formatting for patch fixes.
electron
pr-creator
Use this skill when asked to create a pull request (PR). It ensures all PRs follow the repository's established templates and standards.
google-gemini
clawdhub
Use the ClawdHub CLI to search, install, update, and publish agent skills from clawdhub.com. Use when you need to fetch new skills on the fly, sync installed skills to latest or a specific version, or publish new/updated skill folders with the npm-installed clawdhub CLI.
moltbot
tmux
Remote-control tmux sessions for interactive CLIs by sending keystrokes and scraping pane output.
moltbot
create-pull-request
Create a GitHub pull request following project conventions. Use when the user asks to create a PR, submit changes for review, or open a pull request. Handles commit analysis, branch management, and PR creation using the gh CLI tool.
cline