DOM XSS Explained: The Bug That Lives in Your Browser


You’ve probably heard of cross-site scripting — reflected XSS and stored XSS, where a malicious script travels through the server and lands in someone else’s browser. DOM XSS is the third member of the family, and it’s the sneakiest: the attack never touches the server at all. It happens entirely inside the victim’s browser, which makes it harder for server-side defenses to catch. If you’re hunting bugs, this is a class worth knowing.

What Makes DOM XSS Different

In reflected XSS, the server echoes your input back in its response. In stored XSS, the server saves your input and serves it to every visitor. In DOM XSS, the server is innocent — it sends a perfectly safe page, and then the page’s own JavaScript takes untrusted data (usually from the URL) and writes it into the page in a dangerous way. The vulnerability is in the client-side code.

The Classic Vulnerable Pattern

Imagine a page that greets you by name from the URL:

const name = new URLSearchParams(location.search).get("name");
document.getElementById("welcome").innerHTML = "Welcome, " + name;

Now visit this URL:

https://example.com/welcome?name=<img src=x onerror=alert(1)>

The script reads name from the URL and drops it into the page with innerHTML — which the browser parses as HTML. Your <img> tag executes, onerror fires, and the attacker’s JavaScript runs in the victim’s session. The server never saw the payload; it only served the same safe page to everyone.

The Dangerous Sinks (Where to Look)

Bug bounty hunters look for “sinks” — JavaScript functions that write data into the page unsafely. The big ones:

  • element.innerHTML and document.write() — parse strings as HTML. The number one DOM XSS sink.
  • eval() and setTimeout(“…”) with a string — execute strings as code.
  • location.href or location.assign() built from user input — can trigger javascript: URLs.
  • jQuery.html() — same problem as innerHTML, wearing a nicer API.

And the “sources” — where untrusted data comes from: location.search (the query string), location.hash (the part after #), document.referrer, and window.name. When a source flows into a sink without sanitization, you have DOM XSS.

How to Find It in a Bug Bounty

  1. Pick a target in scope. Only test programs that explicitly allow it — DOM XSS is still XSS, and testing outside scope can get you banned.
  2. Read the JavaScript. Use your browser’s DevTools (Sources tab) and search the site’s scripts for innerHTML, document.write and eval. Look for ones fed by location.hash or location.search.
  3. Test with a harmless payload. Try #<img src=x onerror=alert(1)> in the hash, or the same string in a query parameter, and see if it fires. alert(1) proves the sink; never use a payload that steals data on someone else’s site.
  4. Trace the flow. In your report, show the exact source, the exact sink, and the URL that triggers it. A screenshot of the alert plus the code path makes a report that gets paid.

How Developers Fix It

  • Use textContent instead of innerHTML when inserting user data. textContent treats input as plain text — no HTML parsing, no script execution.
  • Sanitize with a library like DOMPurify if HTML insertion is genuinely needed.
  • Encode output and set a strong Content Security Policy as defense in depth — but remember, CSP is a seatbelt, not the fix.

DOM XSS hides in plain sight: the server logs look clean, the page looks normal, and the bug only appears when JavaScript runs. That’s exactly why bounty programs pay for it — and why learning to read client-side code is one of the highest-value skills a web hunter can build. Only test systems you own or have written permission to test.

Comments

Popular Posts