Input Validation &
Output Encoding
Learn how applications defend against malicious data by checking what goes in and safely transforming what comes out.
What Is User Input?
Every time you type into a search bar, fill out a form, upload a file, or click a URL, you are sending input to a web application. The server must decide: is this data safe to use, or could it cause harm?
Defining Input Validation
Input validation is the process of checking whether data received from a user (or any external source) conforms to the expected format, type, length, and range before the application processes it.
Defining Output Encoding
Output encoding (also called escaping) is the process of converting special characters in data into a safe representation before the data is rendered in a browser, database query, or other interpreter.
Why Does It Matter?
Without these defenses, attackers can inject malicious code that the application trusts and executes. Two of the most common injection attacks are:
The Core Defense Rule
Allowlist vs Denylist
There are two fundamental strategies for input validation. One is far more secure than the other.
| Allowlist (Whitelist) | Denylist (Blacklist) | |
|---|---|---|
| Approach | Define what IS allowed | Define what IS NOT allowed |
| Default | Deny everything unless matched | Allow everything unless matched |
| New attacks | Blocked automatically | Pass through until list is updated |
| Security | Stronger | Weaker |
Validation vs Encoding
| Input Validation | Output Encoding | |
|---|---|---|
| When | Before processing | Before rendering |
| Purpose | Reject malformed data | Neutralize special characters |
| Prevents | SQLi, command injection, buffer overflow | XSS, HTML injection |
| Analogy | Bouncer checking IDs | Translator converting languages |
| Location | Server-side (and client-side for UX) | Right before output context |
How to Apply: Step-by-Step
Example 1: SQL Injection
A login form takes a username. An attacker types:
' OR '1'='1' --
Without validation, the server builds this query:
Example 2: Cross-Site Scripting
A comment box accepts user text and displays it on a page. An attacker submits:
Without output encoding, the browser executes this script for every visitor, sending their cookies to the attacker.
Safe vs Unsafe Code
Compare two approaches for a search feature that displays the user's query:
document.getElementById('results').innerHTML = userQuery;
// SAFE: encoding before inserting
document.getElementById('results').textContent = userQuery;
Context-Dependent Encoding
The correct encoding depends on where the data will appear. Using the wrong encoding for the wrong context leaves vulnerabilities open.
Client vs Server Validation
Common Mistakes
Summary: Defense Layers
| Defense | What It Does | Prevents | Example |
|---|---|---|---|
| Input Validation | Checks format, type, length | SQLi, command injection | Reject non-numeric age field |
| Output Encoding | Escapes special characters | XSS, HTML injection | < becomes < |
| Parameterized Queries | Separates code from data | SQL injection | Prepared statements |
| Allowlisting | Only permits known-good values | All injection types | Regex: [a-zA-Z0-9] |
Question 1 of 5
What is the primary purpose of input validation?
Question 2 of 5
An attacker enters <script>alert('XSS')</script> into a comment field. Which defense prevents the browser from executing this script?
Question 3 of 5
Why is an allowlist approach considered more secure than a denylist?
Question 4 of 5
A developer adds JavaScript validation to a registration form that requires the email field to match a valid format. Is this sufficient for security?
Question 5 of 5
User data will be inserted into a JavaScript string on a web page. Which encoding should be applied?