XSS happens when a site outputs user input into HTML without escaping it, and the browser executes it as code.
Three main types
- Stored: malicious code is saved in the database and runs for every visitor (e.g. a malicious comment).
- Reflected: the code is reflected back within the same request, like a URL parameter.
- DOM-based: the flaw lives entirely in client-side JavaScript, no server involved.
What can an attacker actually do?
Steal session cookies, impersonate the user, deface the page for phishing, or trigger unwanted actions on behalf of a logged-in user.
The core defense
Always escape output (most modern frameworks do this by default), and set a strict Content-Security-Policy so injected code can't execute even if it slips through.
Output escaping and CSP are the core defenses against XSS.