5 rules for running subprocesses in Python without the injection risk

Tags: python · cybersecurity · development · shell

My teammate shut down a pipeline with a single prompt containing a parenthesis. Another stayed up all
night because a generated command contained backticks. Both were caused by the same one-line mistake:
handing untrusted text to a shell.

This is the classic shell=True mistake. It is also the reason I built shellguard.

If you run subprocesses in Python, read the five rules below. They are short and they will keep you
safe.

Rule 1 — Never pass a shell string built from text you do not trust

The moment you build a command as a string and ask the shell to interpret it, you have handed the shell
a program you did not write.

# DANGEROUS cmd = f"cd {dir} && run --model {model} {prompt}" subprocess.run(cmd, shell=True) 

A prompt containing ( makes bash abort with syntax error near unexpected token. A prompt containing
backticks or $(...) executes whatever the attacker chose.

Rule 2 — Pass an argv list; keep the shell off

The safe default is to give subprocess a list and never enable the shell.

# SAFE subprocess.run(["run", "--model", model, prompt]) 

With a list, the OS execve system call runs the program directly. Each list element stays one token. No
parser, no metacharacters, no surprises.

Rule 3 — Never interpolate data into a command string at all

The cleanest way to avoid the trap is to never build a string from data in the first place. Split your
command into its real parts: the executable, the flags, the values. Keep each value in its own list
element.

args = ["run", "--model", model, prompt] subprocess.run(args) 

Rule 4 — Do not use os.system or os.popen

os.system and os.popen always go through a shell. There is no way to make them safe with data. Pick
subprocess.run and pass a list.

Rule 5 — If you truly need a shell, use shlex.quote with care

Sometimes your command has to go to a shell (for example, running a pipeline). Then quote every
interpolated value with shlex.quote and understand that this only protects the shell layer, not the
underlying command:

import shlex, subprocess arg = shlex.quote(user_input) subprocess.run(f"process {arg}", shell=True) 

shlex.quote prevents the shell from interpreting your value. It does not make the value harmless to the
program that receives it.

Checklists

  • [ ] No string command built from user input, passed to a shell.
  • [ ] Every command is a list of argv, shell=False.
  • [ ] No os.system / os.popen with data.
  • [ ] Dynamic flags split into separate list elements.
  • [ ] If you use a shell, every interpolated value is quoted.

The better way

If you want a tested, dependency-free implementation that already applies every rule above, shellguard
builds an argv list from untrusted text and refuses to run it through a shell.

  • 26 real attack cases executed against real subprocesses.
  • Zero dependencies, Python 3.8+.
  • Commercial license EUR49.
  • Or pay what you can from €1.

Get shellguard: https://derbaroser-eng.github.io/shellguard/

Buy directly (EUR49): https://buy.stripe.com/00weVg5HQawocP77lS4ko0S

Pay what you can from €1: https://buy.stripe.com/aFafZk3zIbAs16p7lS4ko0T