Cross-Site Scripting (XSS) is a client-side code injection vulnerability that allows an attacker to execute arbitrary malicious JavaScript in the browser of an unsuspecting victim. Because the injected script executes within the context of the victim's authenticated session, the attacker can silently steal session tokens and cookies, capture keystrokes (keylogging), perform actions on the user's behalf, or redirect the victim to credential harvesting sites.
Figure 23.1: Stored XSS vs Reflected XSS vs DOM-Based Execution Flows & Multi-layered CSP Defense
| XSS Type | Payload Storage & Flow | Impact & Common Sinks |
|---|---|---|
| 1. Stored XSS (Persistent) | The payload is permanently stored in the application's database (e.g. blog comment, user profile bio, product review). | Most Dangerous! Every user who loads the page automatically executes the script. Can create self-propagating worm attacks (e.g. the historic Samy MySpace Worm). |
| 2. Reflected XSS (Non-Persistent) | The payload is carried inside the HTTP request (e.g. in a search query parameter ?q=<script>...) and immediately reflected in the server's HTML response without sanitization. |
Requires tricking the victim into clicking a phishing link. Often used to target administrative users via email lures. |
| 3. DOM-Based XSS | The vulnerability exists entirely in client-side JavaScript. The payload never travels to the server backend! Client script reads from an untrusted Source and writes into a dangerous Sink. | Sources: location.search, document.referrer, window.name.Dangerous Sinks: element.innerHTML, document.write(), eval(). |
Relying on a single line of defense against XSS is dangerous because JavaScript executes across multiple contexts (HTML body, attributes, inline JS, URLs). Complete immunity requires three coordinated layers:
Never render user input directly into HTML without transforming special characters into safe HTML entities:
// In PHP: Safe HTML context encoding
echo htmlspecialchars($userInput, ENT_QUOTES | ENT_HTML5, 'UTF-8');
// Converts:
// < --> <
// > --> >
// " --> "
// ' --> '
// & --> &
When an application legitimately needs users to format rich text (e.g. bold, italics in blog comments), simple entity encoding breaks formatting. Use a hardened sanitization library like DOMPurify:
// Client-side JavaScript DOM sanitization
const cleanHtml = DOMPurify.sanitize(dirtyUserInput);
document.getElementById('comment-box').innerHTML = cleanHtml;
A strict CSP header acts as a safety net: even if an attacker manages to inject a <script> tag into your HTML, the browser refuses to execute it unless it carries a cryptographically random, one-time server nonce!
Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-EDNnf03nceI196c_mQ'; object-src 'none';
The #1 objective of XSS attackers is stealing document.cookie to hijack user sessions. Setting the HttpOnly attribute on session cookies instructs the browser that JavaScript is strictly forbidden from ever reading or modifying the cookie! Even if XSS occurs, your session cookie remains 100% inaccessible to the attacker's script.