Cross-Site Scripting
& Mitigations
Understanding how XSS attacks exploit web applications and the defensive strategies that stop them: output encoding, input validation, and Content Security Policy.
What is Cross-Site Scripting?
Cross-Site Scripting (XSS) is a code injection vulnerability that allows an attacker to insert malicious scripts into web pages viewed by other users. When the browser loads the page, it executes the injected script as if it were legitimate code from the trusted website.
Why XSS Matters
The XSS Attack Flow
The Three Types of XSS
Stored vs. Reflected vs. DOM-Based
| Stored | Reflected | DOM-Based | |
|---|---|---|---|
| Payload Location | Server database | URL / request | Client-side JS |
| Persistence | Permanent | Per-request | Per-request |
| Server Involved? | Yes | Yes | No |
| Impact Scope | All visitors | Targeted link | Targeted link |
| Severity | Highest | High | High |
The Core Rule: Never Trust User Input
Every XSS vulnerability shares a single root cause: untrusted data reaches an execution context (HTML, JavaScript, CSS, or URL) without proper sanitization or encoding. The fundamental defense principle is:
Mitigation 1: Output Encoding
Output encoding converts special characters into their safe entity equivalents before inserting data into HTML. The browser then renders them as text, not as executable code.
Mitigation 2: Content Security Policy
Content Security Policy (CSP) is an HTTP response header that tells the browser exactly which sources of scripts, styles, and other resources are permitted to load. Even if XSS payload is injected, CSP can block it from executing.
More Defenses: Validation & Cookies
How to Defend Step by Step
Example 1: Stored XSS in a Comment
A blog allows users to post comments. The server saves the comment and renders it directly in HTML without encoding:
Fixing the Stored XSS
Example 2: Reflected XSS in Search
A search page echoes the user's query back in the results without encoding. An attacker crafts a malicious URL and sends it to a victim:
Example 3: DOM-Based XSS
Client-side JavaScript reads from the URL hash and writes it to the page without sanitization. The server is never involved:
Common Mistakes
Summary: Defenses at a Glance
| Defense | What It Does | Protects Against |
|---|---|---|
| Output Encoding | Escapes special characters | Stored & Reflected XSS |
| Content Security Policy | Restricts script sources | All XSS types (secondary) |
| Input Validation | Rejects unexpected patterns | All injection attacks |
| HttpOnly Cookies | Blocks JS cookie access | Session hijacking via XSS |
| textContent (not innerHTML) | Prevents DOM parsing | DOM-Based XSS |
Question 1 of 5
Which type of XSS attack stores the malicious payload permanently on the server so that every visitor to the affected page is impacted?
Question 2 of 5
What is the primary purpose of output encoding as an XSS defense?
Question 3 of 5
A developer sets the HTTP header Content-Security-Policy: script-src 'self'. What does this accomplish?
Question 4 of 5
Why is using a blocklist (e.g., filtering out <script> tags) considered a weak XSS defense?
Question 5 of 5
A web page uses innerHTML to display user input from the URL. Which change would most effectively prevent DOM-based XSS?