Home / patterns

OS command injection from unsanitized shell calls

A developer at a desk reviewing code on a laptop with a sticky note that says never pass user input to shell, soft office lighting, no brand logos
Quick answer: AI coding tools often “solve” file conversion, image processing or deploy hooks by calling a shell with string-built commands. If any part of that string comes from a user, upload filename or webhook body, attackers can append extra OS commands. MITRE calls that CWE-78 (Improper Neutralization of Special Elements used in an OS Command). Prefer library APIs over shells, pass arguments as arrays, and never concatenate untrusted input into a command line.

Last updated November 12, 2026

Steps

  1. Search the generated project for child_process.exec, execSync, spawn with shell:true, os.system, subprocess with shell=True, Runtime.exec that builds a string, backticks in Ruby/PHP, and similar “run this command” helpers.
  2. For each hit, list every value that ends up in the command string: request params, uploaded filenames, env overrides from clients, AI-generated paths, webhook payloads.
  3. Replace shelling out with a native library or SDK when possible (image libraries, zip libraries, cloud SDKs) so no OS shell is involved.
  4. If you must run an external binary, use an argv array / spawn without a shell, keep the executable path hard-coded, and pass user data only as separate arguments—never via string concatenation or shell interpolation.
  5. Validate and constrain arguments: allowlists for subcommands and flags, reject path traversal and shell metacharacters, and keep working directories fixed.
  6. Run the process with least privilege (dedicated service user, no unnecessary network or filesystem rights) and capture exit codes without reflecting raw stderr to clients.
  7. Add a regression test or CI check that refuses builds containing shell:true / shell=True / exec(string) patterns on request-handling paths.
  8. Retest defensively: confirm trusted inputs still work through the safe API path, and that previously string-built shell calls no longer exist on those routes.

Why it matters

MITRE CWE-78 covers software that constructs an operating system command using externally influenced input without neutralizing special elements. The result can be execution of unintended commands with the privileges of the vulnerable process. OWASP’s OS Command Injection Defense Cheat Sheet recommends avoiding OS commands when a library API exists, and when a command is unavoidable, separating arguments from the program name and never building a shell string from untrusted data. This maps to injection-class failures in the OWASP Top Ten (A03:2021 Injection).

Sources

MITRE CWE-78: Improper Neutralization of Special Elements used in an OS Command — https://cwe.mitre.org/data/definitions/78.html

OWASP Cheat Sheet: OS Command Injection Defense — https://cheatsheetseries.owasp.org/cheatsheets/OS_Command_Injection_Defense_Cheat_Sheet.html

Checked at the source on November 5, 2026.

Related patterns

Found a problem you'd rather not fix yourself?

Tell us what's wrong. A StorenTech engineer reviews your app and replies with a fixed quote, so you know the cost before any work starts.

Get it fixed by StorenTech

Vibe Security Weekly

One email a week with new failure patterns and how to fix them.

By joining you agree we may email you this newsletter. See our privacy policy. Free. Unsubscribe anytime. We'll never sell your email.