Skip to content
BENIBIX Learn

Input Validation vs Output Encoding

People think checking what users type stops cross-site scripting, and it does not. Input validation turns away data with the wrong shape as it comes in. But a comment box has to take almost any text. Output encoding works on the way out. It turns signs like the less-than sign into safe codes, so the browser shows the text and never runs it. You want both.

Development🔐 Security

Input Validation

Making sure what users send fits the rules. Then your code uses it.

Input validation checks outside data against rules before your code uses it. An age should be a whole number between 0 and 150. An email should look like an email. Anything that breaks the rules is turned away with a clear message. Checks in the browser are only for comfort; the server must check again, because anyone can skip the page.

Example
Lucy's sign-up form asks for an age. Someone sends minus 5 straight to her API, and the server answers 400 Bad Request instead of saving it.
Why it matters
Most attacks start with input the code did not expect. Checking it first stops whole kinds of bug.
Full entry
Development🔐 Security

Output Encoding

Turning signs like < into safe codes. Then user text can go on a page.

Output encoding changes special characters before user text is put on a page. In HTML, < becomes &lt; and > becomes &gt;. The browser then shows the text instead of running it as code. Each place needs its own rules, so HTML, links and scripts are each encoded their own way. It is the main defence against cross-site scripting.

Example
Emma types <b>hi</b> as her name. The page encodes it, so other users see the tags as plain text, not bold words.
Why it matters
It turns an attacker's script into harmless words. Most modern frameworks do it for you by default.
Full entry

In one line

Input Validation
Making sure what users send fits the rules. Then your code uses it.
Output Encoding
Turning signs like < into safe codes. Then user text can go on a page.