Cross-site scripting (XSS) injects foreign script code into a web page which then executes in other users’ browsers — with their session and rights. This becomes possible when an application builds user input into its pages unfiltered.
Consequences range from session theft and manipulated content to fake login masks within the real page context — practically undetectable for users because the address bar is correct.
The three XSS types
Reflected XSS delivers the payload via a crafted link that the application mirrors into the response HTML. Stored XSS is more dangerous: the code is saved — in comments or profile fields, say — and hits everyone who opens the page. DOM-based XSS arises entirely in the browser when client-side code processes input unsafely.
The core protection is consistent escaping: output is encoded per context (HTML, attribute, JavaScript, URL) — modern frameworks do this largely automatically as long as their protections are not bypassed. A content security policy additionally limits what injected code could do at all; the WAF filters known patterns at the edge.
Layers of defence against XSS
- Context-appropriate output encoding — enforced by the framework, not hand-rolled.
- Content security policy as damage limitation for the worst case.
- HttpOnly cookies: sessions stay unreadable for scripts.
- WAF rules against known XSS patterns, especially in front of legacy applications.