DOM-based Cross-Site Scripting (XSS) is a client-side vulnerability where the attack payload is executed as a result of modifying the DOM environment in the victim's browser. The malicious data never reaches the server—instead, the client-side JavaScript code processes untrusted input and dynamically updates the page in an unsafe way.
The vulnerability exists entirely in client-side code. JavaScript reads data from an attacker-controllable source (like location.hash, location.search, or document.referrer) and passes it to a dangerous sink (like innerHTML, eval(), or document.write()).
Vulnerable JavaScript code that reads from the URL hash:
// Vulnerable code
var name = location.hash.substring(1);
document.getElementById('welcome').innerHTML = 'Hello ' + name;
// Attacker's URL
https://example.com/page#<img src=x onerror=alert(1)>
Sources (attacker-controlled):
location.hash, location.search, location.hrefdocument.referrer, document.cookiepostMessage data, localStorageDangerous Sinks:
innerHTML, outerHTML, document.write()eval(), setTimeout(), setInterval()element.src, element.hreftextContent instead of innerHTML when possibleeval()