DOMPurify bypass: XSS via HTML namespace confusion
DOMPurify's sanitisation could be bypassed because the HTML parser can create nested form elements that disappear on re-parsing, and the mglyph element changes namespace, causing a style element to be parsed differently and allowing an img onerror payload to execute. An attacker could inject script into pages that rely on DOMPurify to sanitise untrusted HTML.
research.securitum.com
Published on research.securitum.com
In this blogpost I’ll explain my recent bypass in DOMPurify – the popular HTML sanitizer library. In a nutshell, DOMPurify’s job is to take an untrusted HTML snippet, supposedly coming from an end-user, and remove all elements and attributes that can lead to Cross-Site Scripting (XSS).
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the sanitizer's reliance on a serialize-parse roundtrip that is not guaranteed to preserve the DOM tree, combined with spec-compliant mutations involving nested forms and namespace changes for mglyph.
Impact
An attacker could execute arbitrary JavaScript in the context of a victim's browser if a vulnerable pattern is used, potentially leading to account compromise or data theft.
What the attacker needed
Application uses DOMPurify.sanitize with innerHTML; Attacker can inject HTML into sanitized content
The fix
The bypass was fixed by Cure53, and users should pass RETURN_DOM or RETURN_DOM_FRAGMENT options to DOMPurify to avoid the serialize-parse roundtrip.
How it was found
- Craft HTML with mis-nested form tags to create nested forms in the DOM.
- Include a math element with mtext and mglyph to manipulate namespaces.
- Add a style element and an img with onerror inside it.
- Pass the HTML through DOMPurify.sanitize, which returns a serialized string.
- Assign the sanitized string to innerHTML, causing re-parsing that changes namespaces and executes the script.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
DOMPurify is a tool that cleans HTML to remove dangerous code. The problem is that when the cleaned HTML is put back into a page, the browser can interpret it differently and turn harmless-looking text into executable code. This happens because of quirks in how browsers parse HTML, especially with forms and special MathML elements.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.