OWASP Input Validation
Every web application accepts input: names, emails, search words, file names, numbers, and more. Input validation checks that each piece of data matches what the application expects before the application uses it. Strong validation blocks a large share of common attacks at the front door.
The Core Idea in Plain Words
Imagine a pizza shop with an order form. The form asks for a phone number, and the clerk expects digits. A customer who writes a poem in the phone field confuses the system. A careful clerk rejects the entry and asks again. Input validation plays the role of that careful clerk.
Where Validation Happens
User types data
|
↓
+------------------+
| Browser checks | Fast feedback for the user
+------------------+
|
↓
+------------------+
| Server checks | The check that actually protects you
+------------------+
|
↓
+------------------+
| Database / Logic | Receives only clean, expected data
+------------------+
Browser checks improve the user experience, but attackers skip the browser and talk to the server directly. The server must repeat every check.
Allowlist Versus Denylist
Allowlist (Recommended)
An allowlist defines exactly what good input looks like and rejects everything else. A username rule that permits only letters, digits, and underscores, with a length of 3 to 20 characters, is an allowlist.
Denylist (Weak)
A denylist tries to list bad input and block it. Attackers invent new bad input faster than defenders can list it, so denylists leak. Use them only as an extra layer.
Four Types of Checks
| Check | Question It Answers | Example |
|---|---|---|
| Type | Is the value the right kind of data? | Age must be a whole number |
| Length | Is the value too short or too long? | Name between 1 and 50 characters |
| Range | Does the value stay inside sensible limits? | Quantity between 1 and 99 |
| Format | Does the value match the expected pattern? | Postal code matches a country pattern |
A Simple Code Example
import re
def is_valid_username(value):
pattern = r"[A-Za-z0-9_]{3,20}"
return re.fullmatch(pattern, value) is not None
print(is_valid_username("maria_22")) # True
print(is_valid_username("maria; DROP")) # False
The function accepts only the characters and lengths on the allowlist. Everything else fails.
Validation Is Not Enough Alone
Some fields must accept risky characters. A comment box may need apostrophes, and a name field may contain symbols from many languages. Validation cannot ban those characters without hurting real users. Pair validation with parameterized queries for databases and output encoding for web pages. Each layer covers a gap in the others.
Best Practices
- Validate on the server for every request, including requests from mobile apps and APIs.
- Define an allowlist for each field during design.
- Normalize text before checking it. Convert it to a standard character set so tricky encodings cannot sneak through.
- Reject invalid input with a clear, short message. Avoid echoing the raw input back.
- Limit request size to stop oversized payloads.
- Validate data from every source: forms, URLs, headers, cookies, files, and third-party services.
Common Mistakes
- Trusting hidden form fields and dropdown values. Users can change them.
- Checking only the first item in a list of values.
- Writing complex patterns that run slowly on long input.
- Stripping bad characters silently instead of rejecting the request.
Key Takeaways
- Validation confirms that data fits expectations before the application uses it.
- Server-side checks protect the application. Browser checks only help users.
- Allowlists beat denylists.
- Validation works best combined with safe queries and output encoding.
