Explore our new Cybersecurity Terms Glossary, explained by our experts.
Cybersecurity Terms Glossary
Input validation decides whether data is acceptable when it arrives; output encoding makes it safe for the place it is going. Each solves a different problem, and neither replaces the other.

What Are Input Validation and Output Encoding?
Two defences are regularly confused, and the confusion causes real vulnerabilities. Input validation asks whether a value is acceptable at the point it enters the application. Output encoding transforms a value so that it cannot be misread as syntax by whatever will consume it next. They operate at opposite ends of the data’s journey, they fail in different ways, and an application needs both.
Validation is a business decision expressed in code. A UK postcode has a format, an order quantity has a range, a status field has a fixed set of permitted values, and an uploaded file has a size limit. Reject anything that does not fit, and reject it early.
An allow-list is stronger than a deny-list every time. Specifying that a reference must match [A-Z]{3}-[0-9]{6} is precise and durable; listing the characters you consider dangerous is a guess that encoding, Unicode normalisation, and a new parser will eventually defeat. Where free text genuinely must be accepted, validate the length, the character set, and the structure, and accept that content safety will be handled on output instead.
Two rules make the difference in practice. Validate on the server, treating any client-side check as a usability feature. And canonicalise before validating: decode percent-encoding, normalise Unicode, and resolve paths first, or the check runs against a different string from the one the application will eventually use. Validating %2e%2e%2f and then decoding it is how path traversal gets through a filter.
Encoding has no opinion about whether a value is reasonable. It ensures that when the value reaches an interpreter, every character is treated as data rather than as instruction. The correct transformation depends entirely on the destination, and there is no universal escape function:
&, <, >, and the quote characters.Nesting contexts is where experienced developers come unstuck. A value placed inside an onclick attribute sits in a JavaScript context inside an HTML attribute context and needs both transformations, in the correct order. The safest answer is to stop nesting contexts rather than to escape twice.
Consider a customer surname. O’Brien contains an apostrophe, which is a legitimate value that must be accepted. Validation cannot reject it, so the apostrophe reaches storage; if the name is later concatenated into a query it becomes SQL injection, and if it is written into an HTML attribute it becomes cross-site scripting. The reverse holds too: encoding a value correctly on output does nothing to stop a negative price or a quantity of ten thousand from being accepted in the first place.
Stored data deserves the same treatment as fresh input. Content that was validated when it arrived may be rendered years later in a context nobody anticipated, in an email template, a PDF export, or a mobile client. Encode at the point of use, not at the point of storage, because the correct transformation is only knowable at the destination.
Modern templating engines encode by default. React escapes interpolated values, Django and Jinja2 autoescape, Razor encodes, and Thymeleaf does the same. The vulnerabilities appear at the deliberate exceptions, such as dangerouslySetInnerHTML, the safe filter, and Html.Raw, which is why a search for those identifiers is one of the highest-yield things a reviewer can do.
Where real HTML must be accepted from users, parse and sanitise it with a maintained library such as DOMPurify, or the OWASP HTML sanitiser for Java, rather than writing a filter. Layer a Content Security Policy behind both as containment, and have the patterns checked during web application security testing rather than assuming the framework covers every path.
Both, for different reasons. Validation enforces what your business rules permit and stops malformed data entering the system at all. Encoding stops data being misinterpreted as code by a database, a browser, or a shell. Neither covers the other’s job: a valid surname containing an apostrophe still breaks an unparameterised query, and perfect encoding still accepts a negative order quantity.
Generally no. Stripping or escaping at the point of storage corrupts the data, makes search and export unreliable, and applies a transformation chosen before anyone knows the destination. The same record may later appear in HTML, a CSV file, a PDF, and a mobile app, each needing different handling. Store what the user supplied and transform it when you use it.
Because the list is never complete. Attackers use alternative encodings, Unicode equivalents, case variation, double encoding, and syntax you did not anticipate, and every new parser in the stack introduces more possibilities. A deny-list also breaks legitimate data such as names, addresses, and free text. Specify what is allowed instead, and handle the rest with correct encoding at the point of use.
For the common path, usually yes. Templating engines in React, Django, Rails, and ASP.NET Core escape interpolated values by default, and object-relational mappers parameterise queries. The gaps are the deliberate escape hatches, raw query methods, and anywhere strings are concatenated manually. Those are the places worth searching for and reviewing by hand.
You May Be Interested In These
We use a few cookies that are needed to make this website work, and they are always on. We would also like to set analytics cookies, to see which pages are useful; functional cookies, so that our live chat works; and advertising cookies, to measure our campaigns. We will not set any of those until you say yes, and you can change your mind at any time from the Cookie Settings link in the footer.
Choose the cookies you are happy for us to use. Strictly necessary cookies are always on; everything else is off until you turn it on.
These cookies keep the website working: they hold your security checks, your form submissions and the choice you make here. The site cannot run without them, so they are always on.
These let our live chat open and remember your conversation, and let the newsletter sign-up work. Switch them off and the chat window will not appear.
These tell us which pages are read and how people find us, so that we can improve the site. The reports are about groups of visitors, never about you personally.
These let us measure our advertising and show you our adverts on other websites, such as LinkedIn, Google and Meta. Switch them off and you will still see adverts, but they will be less relevant.