Injection Attacks Explained: SQL, NoSQL, Command and Template Injection
How injection vulnerabilities work across SQL, NoSQL, OS commands, LDAP and server-side templates, with vulnerable vs fixed code and prevention rules.
Published December 6, 2026
Injection happens whenever untrusted input is interpreted as part of a command or query. SQL injection is the famous example, but the same mistake appears in NoSQL queries, shell commands, template engines, LDAP filters, XPath and even LLM prompts. The fix is the same idea everywhere: keep code and data separate.
The concept
When an application builds a query by concatenating strings, user input can change the query's structure: closing a quote, adding OR 1=1, or appending a second statement. Parameterised queries (prepared statements) send the query structure and the data separately, so input is always treated as a value, never as syntax. ORMs and query builders do this by default, unless you drop into raw string building.
Other interpreters need equivalent safe APIs. Run commands with argument arrays instead of a shell string. Use query operators safely in MongoDB and reject objects where strings are expected (to stop operator injection like {"$ne": null}). Never render user input as a server-side template. Validate input types and formats as a second layer, and run with least privilege so a successful injection does limited damage.
Injection by interpreter
| Type | Vulnerable pattern | Safe pattern |
|---|---|---|
| SQL | "... WHERE email = '" + email + "'" | Parameterised query with placeholders |
| NoSQL (MongoDB) | find({ user: req.body.user, pass: req.body.pass }) | Validate types; cast to string; use hashed password compare |
| OS command | exec("convert " + filename + " out.png") | execFile("convert", [filename, "out.png"]) |
| Server-side template | render(userSuppliedTemplate) | Fixed templates; user data only as variables |
| LDAP | "(uid=" + user + ")" | Escape per RFC 4515 or use safe filter builders |
SQL injection in action
Defence in depth
Practical examples
SQL: vulnerable vs fixed (Python)
Placeholders keep data out of the query structure.
# Vulnerable
cur.execute(f"SELECT id FROM users WHERE email = '{email}'")
# Safe
cur.execute("SELECT id FROM users WHERE email = %s", (email,))MongoDB operator injection
Reject non-string input where strings are expected.
// Attacker sends {"username": "admin", "password": {"$ne": null}}
const username = String(req.body.username)
const user = await users.findOne({ username })
if (!user || !(await argon2.verify(user.passwordHash, String(req.body.password)))) return res.sendStatus(401)Commands without a shell (Node.js)
Arguments are passed directly; no shell parsing.
import { execFile } from 'node:child_process'
execFile('ffmpeg', ['-i', inputPath, '-vf', 'scale=640:-1', outputPath], (err) => { /* ... */ })Common mistakes
- Building queries with string interpolation "just for this one report".
- Escaping by hand instead of using parameters.
- Passing user input to shell commands or eval.
- Database accounts with full admin rights for the web app.
Key takeaways
- Injection = untrusted input interpreted as code or query syntax.
- Use parameterised queries and safe APIs for every interpreter.
- Validate types and formats as a second layer.
- Limit privileges so successful injection does less damage.
FAQ
Do ORMs make SQL injection impossible?
They prevent it for normal queries, but raw SQL methods and dynamic column/order-by names can still be vulnerable.
How do I safely use dynamic ORDER BY columns?
Map user choices to an allow-list of column names; never insert raw input as identifiers.
Is prompt injection the same thing?
It is conceptually similar (instructions mixed with data) but has no complete technical fix like parameterisation, so it is handled with capability limits.