DOM-Based XSS · Lab 15
Lab 15 Module 3 Medium Theory

DOM-Based XSS

DOM-based XSS is the strangest kind. The payload never reaches the server. The bug lives entirely in the client-side JavaScript — which reads attacker-controlled data from the URL and writes it into the page.

Why this lab is theory, not live. A live DOM-based XSS lab would execute untrusted JavaScript on the same origin as your real account. So this lab teaches through explanation, code, and screenshots. A sandboxed live version on an isolated subdomain is coming.

The problem

The server is innocent.

In reflected and stored XSS, the vulnerable code is on the server. In DOM-based XSS, it is in the browser. The server's response contains nothing suspicious. The JavaScript on the page reads something from the URL — and writes it into the DOM as HTML.

The classic example

A page has a friendly greeting: "Hello, <your name>." The name comes from the URL fragment: #name=Alice.

Vulnerable JavaScript
const name = decodeURIComponent(location.hash.slice(1).split('=')[1]);
document.getElementById('greeting').innerHTML = 'Hello, ' + name;

The server sends the page with no user data in it at all. The URL carries the payload. The JavaScript reads it and writes it into the DOM with innerHTML.

The crafted URL

Crafted URL
https://shop.example/welcome#name=<img src=x onerror=alert(1)>

The hash fragment is not sent to the server at all — it stays in the browser. So even a perfect server-side filter cannot see it. The entire attack lives in the client.

Screenshot Network tab showing no request with the payload — the fragment never left the browser. The alert fired anyway.

How it works

Sources and sinks.

DOM-based XSS has two ingredients: a source (a place the attacker can put a value) and a sink (a place the value can execute).

Sources — where the attacker can put data

Sources
location.href
location.hash
location.search
document.referrer
document.cookie
window.name
localStorage / sessionStorage

Sinks — where data can execute

Sinks
element.innerHTML = ...
element.outerHTML = ...
document.write(...)
eval(...)
setTimeout("...", ...)
setInterval("...", ...)
new Function(...)
jQuery .html()
React dangerouslySetInnerHTML

The vulnerability is the path from a source to a sink, with no sanitization in between. Modern frameworks like React and Vue are designed so that data flows into text nodes, not into HTML. The bug happens when a developer reaches for a raw-HTML sink — innerHTML, dangerouslySetInnerHTML, or similar.

The impact

Same as reflected XSS, but harder to find.

The impact is the same: script execution on the victim's origin, leading to session theft, account takeover, data exfiltration.

What makes it particularly nasty is that it is hard to find and hard to fix. Server-side tools — WAFs, filters, CSP applied at the network — often cannot see the payload. Browser security scanners miss it because the vulnerable code is in application JavaScript. The fix has to happen in the client-side code, not on the server.

How to fix it

Never write user data into a sink.

Use text, not HTML. Where you would write el.innerHTML = data, write el.textContent = data. textContent is inert — it never executes.

Use safe DOM APIs. Instead of building HTML strings, create elements with document.createElement and set text with textContent.

If you must insert HTML, sanitize it first. Use a well-known sanitizer like DOMPurify on the value before assigning to innerHTML. Never write your own sanitizer.

Use a strict CSP. A CSP without 'unsafe-inline' and 'unsafe-eval' means even a successful DOM XSS cannot execute the payload. Defense in depth.

Audit the sink list. Search your codebase for every innerHTML, eval, document.write, and similar. Every one is a potential DOM XSS. Most should be textContent.

This lab is free. The deep and the full knowledge is in the book. Full 30 Lessons

Lesson 15 — DOM-Based XSS

You've practiced the exploit. Now learn why it happens, how attackers weaponize it, developer mistakes, how to fix it etc in the full Hunterlian book.

Buy the book

Already have the book? Back to reader →

Previous Lab 15 of 30 Next lab