---
title: "Never pass input to a shell · Agent Wranglers"
url: https://agentwranglers.se/playbook/adversarial-testing/injection/command-injection
updated: 2026-10-20
licence: "All rights reserved"
---

# Never pass input to a shell

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Required
- Chapter: [Injection](https://agentwranglers.se/playbook/adversarial-testing/injection)
- Rule: Run other programs without a shell, with input as separate arguments. Better still, use a library instead of a program.
- How: Call the program directly with an argument list, such as `execFile` in Node or `subprocess.run([...])` in Python. Check input against an allow list, such as a known file name pattern. Put `--` before user values, so they cannot pass as options.
- Check: Send `PENTEST; sleep 5` and `$(sleep 5)` wherever input might reach a program, such as file names and image tools. Replies come back at normal speed. A search of the code for `shell=True`, `exec(`, `system(` and backticks finds no call that holds user input.

Example:

```js
// Bad: the shell reads ; and $( )
exec(`gzip --keep ${fileName}`);
// Good: no shell, input is one argument, and -- ends the options
execFile('gzip', ['--keep', '--', fileName]);
```

```python
# Bad: shell=True reads ; and $( )
subprocess.run(f"gzip --keep {file_name}", shell=True)
# Good: an argument list, no shell
subprocess.run(["gzip", "--keep", "--", file_name], check=True)
```

```go
// Go: exec.Command never starts a shell
err := exec.Command("gzip", "--keep", "--", fileName).Run()
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers security testing playbook: https://agentwranglers.se/playbook/adversarial-testing/injection/command-injection`.

- Previous: [Escape everything you show](https://agentwranglers.se/playbook/adversarial-testing/injection/cross-site-scripting)
- Next: [Keep input out of templates](https://agentwranglers.se/playbook/adversarial-testing/injection/template-injection)
- [Injection](https://agentwranglers.se/playbook/adversarial-testing/injection)
