Notes
Two different jobs
The two defenses answer different questions. Validation asks: is this input well-formed and allowed? It runs when data arrives. Encoding, or parameterising, asks: how do I make this data safe for the specific place it is going, a database, an HTML page, a shell? It runs when data is used. People constantly confuse them, and try to stop injection with validation alone. You need both, and encoding at the point of use is the one that actually prevents injection.
- validation: is this input well-formed and allowed? done when data arrives
- encoding / parameterising: make data safe for a specific destination done when data is used
- you need BOTH; neither replaces the other
Validate with allowlists
Validation should use an allowlist: define exactly what valid input looks like and reject everything else. An age is an integer from zero to a hundred and fifty. A country is one of a known set. A username matches a specific pattern. The opposite, a blocklist that tries to enumerate bad input, is always incomplete; attackers find the case you missed. Check type, length, range, format and set membership. And always validate on the server, because client-side checks are for user experience, not security.
- allowlist: define the exact shape that is valid, reject the rest (age is 0-150; country is in this set)
- blocklist: try to list bad input -> always incomplete
- check type, length, range, format, set membership
- validate on the server; client checks are UX only
Validation is not enough
Here is the insight that trips people up. The name O'Brien is perfectly valid input, but that apostrophe breaks a SQL query built by string concatenation. The comment with angle brackets may be legitimate text, but it is dangerous when placed in an HTML page. Validation cannot know the syntax rules of every place the data will go later. So you cannot validate your way out of injection. You make the data safe at each destination, which is encoding or parameterising.
- a name field: O'Brien is valid input
- but O'Brien breaks a string- built SQL query
- a comment: <b>hi</b> may be valid text, but dangerous in HTML
- validation cannot know every destination's rules
- so: make data safe AT the destination
Output encoding
Output encoding transforms data so it is treated as data, not code, in its destination. And it is context-specific: encoding for an HTML body differs from an HTML attribute, from JavaScript, from a URL. Angle brackets become HTML entities so the browser shows them instead of running them. Use your framework's encoder for the right context rather than hand-rolling it. Modern templating engines auto-escape by default, which handles most cross-site scripting; the danger is the places you turn escaping off.
- HTML body: HTML-encode (< becomes <)
- HTML attribute: attribute- encode
- JavaScript: JS-encode / avoid
- URL: URL-encode
- use the framework's encoder for each context
- auto-escaping templates do this for you; keep it on
Parameterise queries
For SQL the best defense is not encoding but parameterised queries, also called prepared statements. Instead of building a query string with the data inside it, you send the query with placeholders and the data separately. The database treats the parameters as pure data, never as command, so injection is impossible by construction. The rule is simple and absolute: never build a SQL statement by concatenating user input. The same idea applies to shell commands and other interpreters.
// NEVER build SQL by // concatenation: query = "... WHERE id = " + id; // parameterise: code and data // travel separately stmt = conn.prepareStatement( "... WHERE id = ?"); stmt.setString(1, id);
Put it together
Put together, the pattern for every piece of untrusted input is: validate it on arrival with an allowlist, on the server. Then, when you use it, rely on safe APIs: parameterised queries for SQL, auto-escaping templates for HTML, passing arguments as a list rather than a shell string for commands. And encode for the exact output context where needed. One subtlety: canonicalise input first, decoding it to a single normal form before validating, so an attacker cannot hide an attack behind double-encoding.
- validate on arrival (allowlist, server-side)
- use safe APIs: parameterise SQL, auto-escape HTML, pass args to exec as a list
- encode for the exact output context
- canonicalise before validating (decode once)
Common mistakes
Mistakes. Relying on validation to stop injection instead of parameterising, which does not work. Blocklists of bad strings, which are always bypassable. Building SQL or shell commands by concatenation. Encoding for the wrong context, such as HTML-encoding data that goes into JavaScript. Turning off a template's auto-escaping with a raw or safe helper and forgetting why. And trusting client-side validation, which an attacker simply skips by calling the API directly.
- validation instead of parameterising (not a fix)
- blocklists of "bad" strings
- building SQL or shell strings by concatenation
- encoding for the wrong context
- turning off auto-escaping with "raw" or "safe"
- trusting client-side validation
Say it like this
In the interview: two separate defenses. I validate input with allowlists on the server to reject malformed data early. But validation does not prevent injection, because valid data like an apostrophe can still break a query. For injection I keep code and data apart: parameterised queries for SQL, auto-escaping templates with context encoding for HTML, and passing arguments as a list rather than a shell string for commands. Validation and encoding are complementary, not interchangeable.
- "Two defenses. Validate input
- with allowlists on the server
- to reject malformed data. But validation does not stop
- injection; for that I keep
- code and data apart: parameterised queries, auto- escaping templates with context encoding, args as a list for commands."