Fixing DOM-based Cross-Site Scripting (DOM XSS)
Vulnerability assessment details, CWE reference metrics, and complete code-level patches.
Threat Profile
Vulnerability Analysis
DOM-based Cross-Site Scripting occurs purely on the client-side. JavaScript reads data from a user-controlled source (like window.location.hash or document.referrer) and passes it unsafely to a sink (like document.write, element.innerHTML, or eval).
How it is Detected
Identified by analyzing client-side JavaScript code structures for unsafe inputs and source-sink paths.
Remediation Guidelines
Avoid using innerHTML or document.write for user-controlled strings. Use textContent or secure DOM manipulation APIs instead.
Remediation Script (Vanilla JavaScript)
// VULNERABLE: element.innerHTML = location.hash;
// SECURE REMEDIATION: Using textContent
const targetElement = document.getElementById('output');
targetElement.textContent = window.location.hash.substring(1);Frequently Asked Questions
Is DOM XSS visible in server logs?
No. The fragment identifier (#) and local variables are processed locally inside the browser, meaning payload requests do not hit server logs.
How does CSP mitigate DOM XSS?
CSP limits where scripts can run, reducing the impact even if an attacker successfully injects elements into the DOM.
What are safe sinks in JavaScript?
Use textContent, setAttribute, or secure HTML sanitizers (like DOMPurify) rather than innerHTML or eval.
Related Vulnerability Profiles
SQL Injection (SQLi)
Attackers execute arbitrary SQL commands, bypassing authentication and manipulating database schemas.
Stored Cross-Site Scripting (Stored XSS)
Malicious scripts are stored on the server (e.g. database) and executed when users request the compromised resource.
Reflected Cross-Site Scripting (Reflected XSS)
Malicious scripts are reflected off the web server (e.g. search queries) and executed immediately in the user's browser.
Cross-Site Request Forgery (CSRF)
Attackers force authenticated users to execute unauthorized actions on a web application where they are logged in.