CWE-78OWASP A03:2021OWASP API8:2023

CWE-78: OS Command Injection

Untrusted input is placed into a command run by a shell, letting an attacker execute their own commands.

Also known as: Command injection, Shell injection, OS command injection

Risk

Typically critical. A single injectable command field can give an attacker full control of the server with the application's privileges.

Definition

What it is

CWE-78 occurs when user input becomes part of a command line that is interpreted by a shell. The shell treats characters like ;, |, and $(...) as syntax, so injected input can run additional commands. Because the commands run with the application's privileges, the impact is often full server compromise. The fix is structural: invoke programs directly with separate arguments and no shell.

Use-cases

How it shows up in real code

Hostname field runs commands

A diagnostics page pings a host from a shell string. An attacker submits a value with ; and a second command runs with the server's privileges.

Filename in a conversion

An image tool shells out with the uploaded filename in the command. A crafted name injects flags or a chained command into the call.

Archive extraction helper

A feature calls a shell to unzip a file by name. Metacharacters in the name let the request execute arbitrary commands.

Chained command via metacharacters

A field assumed to be a simple argument carries $(...) so the shell runs the attacker's substituted command before the intended one.

AI risk

Why AI-generated code has increased the exposure

  • Asked to run a tool like ping, git, or ffmpeg with a user value, assistants often build a shell string and pass it to a shell, because that reads naturally and runs in the demo.
  • Models learned from scripting tutorials and legacy glue code that call system() or exec with an interpolated command line rather than an argument array.
  • Generated code assumes the value is a hostname or filename, because the prompt described the feature and not an attacker inserting ; or a command substitution.
  • When scaffolding features that shell out, such as image conversion, archive extraction, or DNS lookups, the easiest generated path concatenates input into the command.
  • Each generated endpoint that shells out is an independent injection site, so more generated features means more command strings an attacker can reach.

Vibe coding

How vibe coding amplifies the risk

  • Vibe coding ships the generated shell-out unread, so a concatenated command line reaches production with the shell still interpreting metacharacters.
  • Prompt-and-ship skips the review where passing a string to a shell would be flagged in favor of an argument array.
  • Iterating by prompt tends to extend the existing command string rather than switch to a safe exec API.
  • Benign inputs during testing produce the right output, so injection stays hidden until a value carries shell metacharacters.
  • Rapid growth adds more features that invoke external tools faster than review can harden each call.

Detection

How to find it

  • Look for calls to a shell or to exec-style APIs whose command includes request data rather than a fixed program with a separate argument list.
  • Trace every request value that reaches a process-spawning call and confirm it is passed as an argument, not concatenated into a command line.
  • Watch for use of shell=true or a single command string where an argument array is available.
  • Test inputs containing semicolons, pipes, ampersands, backticks, and command substitution and confirm they are treated as literal arguments.

Remediation

How to fix it

  • Call the external program directly with an argument array and no shell, so input cannot be parsed as a command.
  • Avoid shell features entirely, including shell=true and string command lines, when spawning processes.
  • Validate arguments against a strict allow-list or expected format before passing them through.
  • Prefer a native library over shelling out when one exists for the task.
  • Run the process with least privilege so a successful injection cannot reach the wider system.

Frequently asked questions

What is CWE-78 (os command injection)?
CWE-78, OS command injection, is when untrusted input is placed into a command that a shell interprets, so characters like ; or a command substitution let an attacker run their own commands with the application's privileges, often leading to full server compromise.
How have AI assistants and vibe coding increased OS command injection risk?
Assistants commonly build a shell string from a user value because it reads naturally and runs in the demo, learned from scripting glue code that uses system() with interpolation. Vibe coding ships that call unread, so the shell keeps interpreting metacharacters in production, and benign test inputs hide the flaw until a value carries shell syntax.
How do I fix os command injection?
Invoke the program directly with an argument array and no shell so input cannot be parsed as a command, avoid shell=true and string command lines, validate arguments against an allow-list, prefer a native library over shelling out, and run with least privilege.

Related weaknesses

Catch CWE-78 before you ship

Tethlyn Audit scans your codebase and its git history for CWE-78 and the rest of the injection & code vulnerabilities class, then ranks each finding by real exploitability and data sensitivity.

See Tethlyn Audit