Agent Wranglers

DAT-8Required

Pass values as parameters

Send every value as a query parameter. Never build SQL by joining strings with input.

How to do it

Use placeholders such as $1, ? or %s in the driver or query builder. Placeholders hold values only. Table names, column names and sort directions cannot be parameters, so map them from a fixed allow-list in code. ORMs are safe until you pass input to their raw SQL functions. The SQL injection rule shows the attack this stops.

Examples

JavaScript example

// Bad
db.query(`SELECT id FROM customers WHERE email = '${email}'`);
// Good
db.query('SELECT id FROM customers WHERE email = $1', [email]);

Python example

# Bad
cursor.execute(f"SELECT id FROM customers WHERE email = '{email}'")
# Good
cursor.execute("SELECT id FROM customers WHERE email = %s", (email,))

PHP example

// Good in PHP with PDO
$statement = $pdo->prepare('SELECT id FROM customers WHERE email = ?');
$statement->execute([$email]);

How to check

Search the code for SQL built with string joins or template strings that hold variables. None remain.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/data/database-security/parameters">Agent Wranglers data and databases playbook</a>. In code you write, add a comment next to it with the same link.