- by x32x01 ||
When people first study DOM XSS, it often feels complicated.
But the truth is much simpler:
If you understand how the DOM works, DOM XSS becomes much easier to analyze.
In many cases, it starts looking similar to Reflected XSS, because the core idea is the same:
It converts HTML into objects such as:
That structured browser representation is called the DOM.
One common example:
The name explains itself: Get an element by ID.
Example HTML:
JavaScript:
What happened here?
Another API example:
Example:
This API retrieves elements by tag name.
So the DOM basically allows JavaScript to:
They are:
That data can come from:
Common Sources include:
URL:
location.search extracts:
The source simply pulls data from the URL.
This retrieves values stored locally inside the browser.
Unlike URLs, this data comes from browser storage.
After JavaScript retrieves data…
it usually does something with it.
That destination point is called a Sink.
Sinks can:
Different sinks carry different security risks.
URL:
Target element:
Sink:
Flow explanation:
URL → Source → Variable → Sink → HTML Output
If unsafe input reaches innerHTML, browser interpretation becomes important.
URL:
Retrieved value:
Sink usage:
Now the data influences browser navigation.
Same pattern: Source → Processing → Sink
Because payload construction depends on where the data finally appears.
HTML payload example:
Payload idea:
Payload concept:
Each context changes payload strategy completely.
Reading everything manually is impractical.
A better approach is using Browser DevTools.
Open:
Then trace the flow:
The core workflow is simple: Source → JavaScript Flow → Sink → Context
Track the data.
Understand the DOM.
Identify dangerous sinks.
Determine the final rendering context.
And suddenly…
DOM XSS stops looking mysterious and starts looking like structured data tracing inside the browser.
That mindset is one of the most valuable skills in web security analysis 🔥
But the truth is much simpler:
If you understand how the DOM works, DOM XSS becomes much easier to analyze.
In many cases, it starts looking similar to Reflected XSS, because the core idea is the same:
That tracing process is the real key to understanding DOM-Based XSS.Track the data.
Where does it come from?
Where does it go?
How does the browser interpret it?
What Is The DOM? 🧠
Before talking about the vulnerability itself, we need to separate two concepts:- DOM (Document Object Model)
- DOM APIs
It converts HTML into objects such as:
- Elements
- Tags
- Attributes
- Text nodes
That structured browser representation is called the DOM.
DOM APIs: How JavaScript Interacts With HTML ⚙️
Developers use DOM APIs to interact with page elements.One common example:
JavaScript:
document.getElementById() Example HTML:
HTML:
<div id="out">tabcode</div> JavaScript:
const user = document.getElementById("out");
user.innerHTML = "lol"; - JavaScript located the element with ID out
- Stored it inside a variable
- Modified its content using innerHTML
tabcode to: lolAnother API example:
JavaScript:
document.getElementsByTagName() Example:
HTML:
<div>One</div>
<div>Two</div>
<div>Three</div> So the DOM basically allows JavaScript to:
✅ Find elements
✅ Read values
✅ Modify content
✅ Create dynamic behavior
✅ Read values
✅ Modify content
✅ Create dynamic behavior
The Three Critical Concepts Behind DOM XSS 🔥
If you understand these three ideas… DOM XSS becomes dramatically easier.They are:
- Sources
- Sinks
- Contexts
Sources: Where The Data Comes From 📥
Sources are APIs that provide data to JavaScript.That data can come from:
- User input
- URLs
- Browser storage
- Cookies
- Server responses
Common Sources include:
Code:
location.href
location.search
location.hash
location.pathname
document.URL
document.documentURI
localStorage.getItem()
sessionStorage.getItem()
fetch()
XMLHttpRequest.responseText
response.json() Example: location.search
Code:let data = location.search;URL:
site.com/?name=tabcodelocation.search extracts:
?name=tabcodeThe source simply pulls data from the URL.
Example: localStorage Source
JavaScript:
localStorage.getItem("username");
localStorage.getItem("token"); Unlike URLs, this data comes from browser storage.
Sinks: Where Data Gets Used ⚠️
Now comes the dangerous part.After JavaScript retrieves data…
it usually does something with it.
That destination point is called a Sink.
Sinks can:
- Render text
- Modify HTML
- Execute code
- Change URLs
- Trigger navigation
Code:
innerHTML
outerHTML
insertAdjacentHTML()
document.write()
eval()
Function()
setTimeout()
setInterval()
location.href
location.assign()
location.replace()
iframe.src
script.src
img.src
setAttribute() Example: DOM XSS Using innerHTML
Source:let data = location.search;URL:
site.com/?name=<b>tabcode</b>Target element:
let output = document.getElementById("out");Sink:
output.innerHTML = data;Flow explanation:
URL → Source → Variable → Sink → HTML Output
If unsafe input reaches innerHTML, browser interpretation becomes important.
Another Example: location.hash → Redirect Flow 🔄
Source:let data = location.hash;URL:
site.com/#https://example.comRetrieved value:
https://example.comSink usage:
location.href = data;Now the data influences browser navigation.
Same pattern: Source → Processing → Sink
Contexts: Where The Payload Lands 🎯
This part is extremely important during XSS analysis.Because payload construction depends on where the data finally appears.
HTML Context
Example: HTML:
<div id="out">DATA</div> HTML:
<img src=x onerror=alert(1)> Tag Break Context
Breaking outside tags: HTML:
</div><script>alert(1)</script> Attribute Context
Example: HTML:
<img src="DATA"> JavaScript:
" autofocus onfocus=alert(1) " JavaScript Context
Example:var x = "DATA";Payload concept:
alert(1)Each context changes payload strategy completely.
DOM XSS Analysis Using DevTools 🔎
Real applications often contain massive JavaScript files.Reading everything manually is impractical.
A better approach is using Browser DevTools.
Open:
- Inspect Element
- Sources Tab
- Console Tab
Code:
location
hash
search
cookie
localStorage
innerHTML
document.write
eval
setTimeout
location.href Then trace the flow:
- Where did the data originate? → Source
- What processing occurred?
- Where did it end up? → Sink
- How was it interpreted? → Context
Final Thoughts 🚀
DOM XSS becomes much less intimidating once you understand the browser model.The core workflow is simple: Source → JavaScript Flow → Sink → Context
Track the data.
Understand the DOM.
Identify dangerous sinks.
Determine the final rendering context.
And suddenly…
DOM XSS stops looking mysterious and starts looking like structured data tracing inside the browser.
That mindset is one of the most valuable skills in web security analysis 🔥