XSS Attack Flow
Cross-Site Scripting (XSS) is a class of injection attack where malicious JavaScript is injected into a web page and executed in the victim's browser, giving the attacker access to everything the JavaScript security model can access: cookies, session storage, DOM contents, and the ability to make authenticated requests.
Cross-Site Scripting (XSS) is a class of injection attack where malicious JavaScript is injected into a web page and executed in the victim's browser, giving the attacker access to everything the JavaScript security model can access: cookies, session storage, DOM contents, and the ability to make authenticated requests.
There are three main XSS variants. Reflected XSS occurs when a server reflects unsanitized user input directly in the HTML response. The attacker crafts a URL containing a malicious script payload in a query parameter, tricks the victim into clicking it, and the server echoes the script into the page. Stored (persistent) XSS is more dangerous: the attacker submits a malicious script to a form (comment, profile field, message), the server saves it to the database, and every user who subsequently views that content executes the script. DOM-based XSS happens entirely client-side — a JavaScript framework reads from location.hash, document.referrer, or other attacker-controlled sources and writes unsanitized values into the DOM.
The impact of a successful XSS attack is severe. An attacker can steal non-HttpOnly cookies and forward them to a remote server, log every keystroke (credential harvesting), modify page content (phishing), make API requests as the authenticated user, and establish persistent backdoors via service workers.
Prevention layers: output encoding — always encode untrusted data before inserting it into HTML, JavaScript, CSS, or URL contexts using context-aware encoding (the root cause of most XSS is encoding the wrong way or not at all). Content Security Policy (CSP) restricts which scripts can execute — see Content Security Policy Flow. HttpOnly cookies prevent script access to session cookies — see Secure Cookie Flow. Input validation and sanitization (using an allowlist, not a blocklist) are additional layers but not substitutes for output encoding.