Never trust what arrives: check and clean every input, to stop injection and bad data.
Input validation means never trusting what arrives. Every value that comes from outside your server (forms, URLs, headers, JSON, files) is checked before you use it.
Your server cannot control who sends it a request. A real user might type something odd by mistake. A script might send a thousand fields. An attacker might craft a value to trick your database, your file system or another user’s browser. All of it looks like “input”.
So the rule is simple: treat all input as untrusted until it has been checked. If it fails the check, stop right away and answer with a clear error, such as 400 or 422. If it passes, you still need to handle it safely when you use it.
| Risk | What goes wrong | The right defence |
|---|---|---|
| SQL injection | Input becomes part of the SQL command | Parameterized queries (prepared statements). Validation helps but is not the main fix. |
| Cross-site scripting (XSS) | Input is shown as HTML or script in a page | Output encoding for the place it appears (HTML, attribute, URL) and a Content Security Policy. |
| Path traversal | Input like ../../ escapes the intended folder | Do not build paths from input. Use IDs, and canonicalize and check the final path. |
| Command injection | Input is passed to a shell | Avoid the shell. Use safe APIs with an argument list. |
| Mass assignment | A client sets fields it should not (isAdmin) | Accept only a fixed list of fields and map them yourself. |
| Resource exhaustion | Huge bodies, deep JSON, giant lists | Limits on size, depth, count and time, plus rate limiting. |
| Bad file uploads | A script or huge file is stored and served | Check size, real content type, store outside the web root, and rename the file. |
// Unsafe: the input becomes part of the command
query("SELECT * FROM users WHERE name = '" + name + "'")
// Safe: the input is sent separately, as data
query("SELECT * FROM users WHERE name = ?", [name])SELECT name FROM users WHERE name = 'alice_01'
A teaching toy. The “database” only understands the classic textbook example and four made-up users, and the rules are simplified. Real systems should use parameterized queries and an allow-list together, and also validate on the server even when the client already checks.
| Term | Meaning in easy words |
|---|---|
| Input validation | Checking that incoming data is acceptable before using it |
| Sanitization | Cleaning or changing input to make it safe. Often less reliable than rejecting it |
| Allow-list | A list of accepted values or patterns. Everything else is refused |
| Deny-list | A list of banned values. Easy to bypass by something unlisted |
| Canonicalization | Turning input into one standard form before checking it |
| SQL injection | Input that changes the meaning of a database command |
| Parameterized query | A query where the input is sent apart from the command, as data |
| XSS | Cross-site scripting: attacker script runs in another user’s browser |
| Output encoding | Turning special characters into safe text for where they are shown |
| Schema validation | Checking data against a written description of its shape (JSON Schema, OpenAPI) |
| Mass assignment | A client setting fields it should not be allowed to set |
| Choice | We gain | We lose |
|---|---|---|
| Strict allow-list | Strong safety, simple to reason about | Real but unusual values may be refused |
| Loose rules | Fewer rejected users | More attack surface and bad data |
| Reject bad input | Predictable, safe behaviour | The user must fix and resend |
| Silently clean input | Fewer errors shown to users | Surprising changes, and sanitizers can be bypassed |
| Validate at the edge only | One place, simple | Internal bugs and other callers can still send bad data |
| Validate in every layer | Defence in depth | Duplicate rules and more upkeep |