command injection
An attack where input meant as data, such as a file name or form field, gets run as commands because a program built code from it.
An attacker who can inject commands runs anything the script's user is allowed to run.
In more detail
It happens when untrusted text is pasted into a command string that a shell or interpreter then parses, for example through eval or sh -c. Escaping special characters is a losing denylist; the fix is never to turn data into code, passing values as separate arguments instead.
Example
subprocess.run(f"echo resizing {name}", shell=True) with name = "x; id" runs idRelated terms
- eval — A shell builtin that joins its arguments into one string and runs that string as a command, as if you had typed it.
- untrusted input — Any data a program did not create itself, including arguments, form fields, request bodies, file contents, file names and environment variables.
- deny list — A list of known-bad values to reject, with everything else accepted.
- minimal base image — A base image with as little inside as possible, such as Alpine, a slim variant or a distroless image, to shrink size and attack surface.
- shell — The program that reads the commands you type, runs them and shows the results; sh and bash are common ones.
- child_process.exec() — The node:child_process function that runs a whole command string through a shell and buffers all its output in memory.
- execFile() — The node:child_process function that runs a program directly, without a shell, and buffers its output.
- argument vector — The list of separate strings a program is started with: its own name followed by each argument.
- shell metacharacter — A character with special meaning to a shell, such as ; & | $ ` or >, that can start a new command or redirect output.
- injection — A bug where untrusted data reaches an interpreter and part of it is read as code instead of as a value.
Taught in
- eval, And What To Use Instead — Bash Scripting & Command Line Automation
- Shift-Left Security Principles & Automated Pipeline Gates — Cloud Security & DevSecOps Engineering
- Container Hardening: Distroless & Rootless — Cloud Security & DevSecOps Engineering
- Runtime Anomaly Detection with Falco & eBPF — Cloud Security & DevSecOps Engineering
- Child Processes: spawn, execFile, fork & IPC — Node.js Internals & Backend Services
- Injection In All Its Forms — Application Security & Threat Modeling for Engineers
- Static Application Security Testing — Application Security & Threat Modeling for Engineers