Why you never trust user input: validation basics
Day 17 of our web security series: what counts as user input, why it can't be trusted, and how to validate it on the server without making life hard for real users.
If there’s one sentence that runs through this whole series, it’s the one from Day 2: never trust anything the browser sends. We’ve seen it with IDs in addresses, roles in cookies and the Host header in reset emails.
Today we look at the habit behind it. Almost every serious web vulnerability, from database injection to cross-site scripting, starts the same way: data from outside the application is treated as if it were safe. Validation is the first line of defence.
What counts as user input
It’s tempting to think “user input” means the text boxes on a form. It’s much wider than that. Anything that arrives from outside your server is input:
- Form fields, including hidden ones and drop-down lists.
- The address: paths, query strings and fragments.
- Cookies and headers, such as
User-Agent,RefererandHost. - JSON or XML sent to an API.
- Uploaded files, including their names and types.
- Data from other systems: webhooks, partner APIs, imported spreadsheets, even your own database if users wrote to it earlier.
A drop-down that only offers “small, medium, large” doesn’t stop anyone sending “extra-large” or a thousand characters of nonsense. The browser’s interface is a suggestion, not a rule.
Why it matters for security
Attackers rarely break in by doing something your application wasn’t built to do. They send unexpected input to something it was built to do, and see what happens. A search box that passes text straight into a database query, a name field that ends up inside a web page, a file name that ends up in a path on disk: each is a place where input crosses into something more powerful.
Validation narrows what can cross that line. It won’t fix every problem on its own, as the next two days will show, but it removes a huge share of the risk cheaply, and it catches honest mistakes too.
Validate on the server, always
Checking input in the browser with JavaScript or HTML attributes like required and maxlength is good for users: they get instant feedback. But it’s no protection, because anyone can skip the browser and send requests directly.
So the rule is simple: browser checks are for convenience, server checks are for security. Every check that matters must run on the server, even if it also runs in the browser.
Allow what you expect, rather than block what you fear
There are two ways to check input:
- Block-listing: reject anything that looks dangerous, such as
<script>or quote marks. - Allow-listing: accept only what you expect, and reject everything else.
Block-lists always miss something. There are endless ways to write the same dangerous text, and new tricks appear all the time. Allow-lists are far stronger, because they describe what good input looks like, which you already know.
In practice, that means asking for each field: what should this be?
- Type. A quantity should be a whole number. An age shouldn’t be text.
- Length. A postcode or phone number has a known length. Set a sensible maximum on everything, even free text.
- Range. A quantity between 1 and 99. A date not in the year 3000.
- Format. An email address, a PIN code, a currency amount, an ID in a known pattern.
- Allowed values. “small”, “medium” or “large”, and nothing else.
Free text is different
Some fields can’t be narrowed much: a comment, a name, an address, a support message. People’s names contain apostrophes, hyphens, accents and characters from every script in the world, and rejecting them is both wrong and unfriendly.
For these fields, validation sets sensible limits (length, no control characters, valid text encoding) and then lets the text through. The protection comes from handling it safely wherever it’s used: parameterised queries when it goes into a database (Day 18), and output encoding when it’s shown on a page (Day 19). Validation and safe handling work together; neither replaces the other.
A few habits that help
Use your framework’s validation tools. Most frameworks and languages have schema or form validation libraries that check types, lengths and formats in one place. They’re better tested than hand-written checks.
Validate the whole request. Reject unexpected fields rather than ignoring them. An API that quietly accepts an extra "isAdmin": true field and saves it to the user record has a serious problem, often called mass assignment.
Normalise before you check. Text can be written in different but equivalent ways, for example with different Unicode forms or extra spaces. Convert it to one standard form first, then validate.
Fail clearly and safely. Reject bad input with a helpful message for the user, but don’t echo back technical details or the raw input in error pages.
Check files properly. Don’t trust a file’s name or the type the browser claims. Check its size, its actual contents and its extension against an allow-list. We’ll cover uploads fully on Day 23.
Treat data from other systems the same way. A webhook or partner API can be compromised or simply buggy. Validate it like any other input.
What you can do today
If you use websites:
- If a site shows a technical error message, a database error or a strange page after you type something ordinary, that’s a sign of weak input handling. Avoid entering sensitive information there.
- Be careful pasting text from untrusted sources into sites and apps where you’re logged in, especially admin panels and spreadsheets.
- Report odd behaviour to the site’s security contact rather than experimenting further.
If you run a website:
- List the inputs to your most important pages and API endpoints, including headers, cookies and uploads.
- Add server-side validation for type, length, range and format to each one, using your framework’s validation library.
- Reject unexpected fields in API requests, and never bind request data directly onto database records.
- Set maximum lengths on every field, including free text.
- Read the OWASP Input Validation Cheat Sheet and the Mass Assignment Cheat Sheet.
Coming up next
On Day 18 we follow user input into the database: keeping databases safe with parameterised queries, the simple habit that prevents SQL injection.