Cross-Site Scripting (XSS)
At a Glance
- Attacker injects malicious scripts into web pages viewed by other users
- Three types: Stored (persistent), Reflected (non-persistent), DOM-based
- Primary defense: output encoding and Content Security Policy
- Can steal session cookies, redirect users, or modify page content
- Consistently in OWASP Top 10 since its inception
Why XSS Matters
XSS is consistently one of the most common web vulnerabilities, appearing in the OWASP Top 10. It occurs when an application includes untrusted data in web pages without proper validation or escaping. There are three main types: stored XSS (malicious script saved in a database), reflected XSS (script in a URL parameter reflected in the response), and DOM-based XSS (script manipulation of the client-side DOM).
The impact of XSS ranges from nuisance (page defacement) to critical (session hijacking, credential theft). An attacker exploiting XSS on a banking site could transfer funds. On a social media site, they could spread malware to all of a user's contacts.
Defenses include Content Security Policy (CSP) with nonce-based script controls, output encoding (HTML-escaping user input before rendering), input validation, and using modern frameworks (React, Vue, Angular) that auto-escape output by default. CSP is the strongest server-side defense because it prevents injected scripts from executing even if escaping is missed.
Real-World Example
In 2018, a stored XSS vulnerability in British Airways' website allowed attackers to inject a malicious script that captured payment card details as customers entered them. The Magecart group exploited the flaw for over two weeks, compromising approximately 380,000 payment cards. BA was fined 20 million GBP by the ICO under GDPR.
How to Check
A security audit checks for CSP headers that help prevent XSS. Proper CSP with nonce-based policies blocks most XSS attacks at the browser level. For application-level XSS testing, use specialized security scanning tools.