DOM-Based Cross-Site Scripting (XSS)
The page's own JavaScript passes attacker-controllable data, such as part of the URL, to a function that treats it as markup or code, so a crafted link can run script.
| Severity | Medium |
| CVSS 3.1 | 6.1 CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:C/C:L/I:L/A:N Open in the calculator |
| CVSS 4.0 | 5.1 CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:A/VC:N/VI:N/VA:N/SC:L/SI:L/SA:N Open in the calculator |
| CWE | CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting') |
| OWASP Top 10 | A05:2025 Injection |
| MITRE ATT&CK | T1189 Drive-by Compromise, T1539 Steal Web Session Cookie |
| Category | Input Validation |
Description
Client-side JavaScript in the application reads data from a source that an attacker can control, such as location.hash, location.search, document.referrer, window.name, web storage or messages received through postMessage, and passes it to a sink that interprets it as markup or code, such as innerHTML, outerHTML, insertAdjacentHTML(), document.write(), eval(), setTimeout() with a string argument, or an element's href or src set to a javascript: URL. Library functions that parse HTML, such as jQuery's html(), act as sinks too.
Because the payload is handled entirely in the browser, it may never appear in a server response, and anything after the # in a URL is not sent to the server at all, so server-side validation and logging do not see it. As with reflected cross-site scripting, each victim must be persuaded to open a crafted link, and the attacker's script then runs in their session, so it can read data shown to the victim, act on their behalf, capture what they type and read session tokens that are not protected by the HttpOnly attribute. The impact is greater where victims include administrators, and lower where a Content Security Policy or Trusted Types block the affected sink.
The weakness typically shows itself on a test through review of the page's JavaScript for flows from sources to sinks, helped by tools such as Burp Suite's DOM Invader, and is confirmed by executing a payload through a crafted URL. Burp Suite reports it as DOM-based cross-site scripting, and scanners that run a browser engine detect common cases.
Remediation
Keep untrusted data away from sinks that interpret markup or code:
- Use sinks that treat data as text: set
textContentrather thaninnerHTML, create elements and set attributes with DOM methods, and never pass strings derived from input toeval(),Function()orsetTimeout(). - Where HTML built from input must be inserted, sanitise it first with a maintained client-side sanitiser such as DOMPurify.
- Before assigning a URL from input to
href,srcorlocation, parse it and allow only thehttpandhttpsschemes. - In frameworks, rely on their automatic encoding and avoid the functions that bypass it, such as
dangerouslySetInnerHTMLin React,v-htmlin Vue andbypassSecurityTrustHtml()in Angular, unless the value has been sanitised. - Enforce Trusted Types with the Content Security Policy directive
require-trusted-types-for 'script'in browsers that support it, so that dangerous sinks accept only values that have passed through an approved policy. - Keep third-party JavaScript libraries up to date, as several have contained DOM-based XSS flaws.
Confirm the fix by repeating the original crafted URLs and reviewing the code path from each source to its sink.
References
Add This Finding to Your Library
In Pental this write-up is in Settings, Library. Add to my library copies it into your own, rated in your firm's CVSS version, ready to put in your own words.
Free trialYour trial starts when your portal goes live, not when you sign up, so the time you spend setting up does not count. · No card required
Only need the reporting? Our reporting tool is free and open source, with the same library.