Injection: data must never become code
Injection happens when untrusted input is interpreted as part of a command.
Injection happens when untrusted input is interpreted as part of a command: SQL, a shell, an HTML page, an outgoing URL. The root cause is string building. The interpreter cannot tell your data from your instructions, so you have to keep them apart.
Identifiers cannot be parameters
You cannot parameterise a column name or the direction of ORDER BY. Map the user's choice to a fixed value from an allowlist, for example ORDER_COLUMNS = {"date": "t.created_at"}.
Commands
Never pass input to a shell. Use an argument array (ProcessBuilder, subprocess with a list, execFile). Validate the value too: a host starting with “-” is read as an option.
SSRF
A server that fetches user-supplied URLs can reach networks the attacker cannot, including internal services and the cloud metadata address 169.254.169.254. Allowlist the hosts, require https, disable automatic redirects, and check the resolved IP address.
Questions to ask yourself
- Which interpreter receives this string?
- Is the input a value, an identifier, a host, or a path?
- What can the interpreter do with a quote, a semicolon, or a leading dash?
Practise this
- Statement query built with an f-string · Injection & input
- Network diagnostics in Java · Injection & input
- Webhook test feature · Injection & input
- PHP login built with string concatenation · Injection & input
- Ping through a shell in Rust · Injection & input