Input Validation
Checking that data entering a system has the expected type, format, range and meaning before the system acts on it.
Also known as: Data Validation, Input Checking, Request Validation
Category: Software Development
Tags: software-development, security, cybersecurity, best-practices, validation, software-design, reliability
Explanation
Input validation is the gatekeeping step at the edge of a system. Anything that comes from outside, whether a form field, an API request, a file, a message from another service or the output of an AI model, is checked before it's used. Valid input goes through. Invalid input is rejected with a clear error.
**Two levels**:
- **Syntactic validation**: Is it well-formed? Right type, length, format, character set. A date looks like a date
- **Semantic validation**: Does it make sense here? The end date is after the start date. The quantity is positive. The account belongs to the caller
**Good practices**:
- **Validate at the trust boundary**: Check input where it enters, on the server side. Client-side validation is a convenience for the user, not a protection
- **Allowlist, don't denylist**: Define what is acceptable and reject the rest. A list of known bad patterns is always incomplete
- **Reject, don't repair**: Guessing what the sender meant creates ambiguity. Two components that repair differently end up disagreeing about the same input
- **Validate early, fail fast**: Stop before bad data gets stored or passed on
- **Parse into types**: Turn checked input into a typed value once, so the rest of the code can't forget the check (see Parse, Don't Validate)
- **Set limits**: Maximum sizes, depths and counts protect against resource exhaustion
**What it is not**:
- **Not sanitization**: Sanitizing changes the input to make it safe. Validating accepts or rejects it
- **Not a complete defense against injection**: A valid name can contain an apostrophe. Parameterized queries and output encoding are the real fix. Validation is one layer among several
**Tension with Postel's Law**:
The robustness principle says to be liberal in what you accept. Input validation says the opposite at the security boundary. The modern reading: be precise about what you accept, tolerate harmless variation that is clearly defined (such as unknown extra fields), and reject everything else.
Related Concepts
← Back to all concepts