What is DOMPurify?
DOMPurify before version 2.2.2 could be bypassed by a mutation XSS that exploits namespace confusion between SVG and MathML, allowing an attacker to inject a malicious script that survives sanitisation. The bypass uses an ownership mutation gadget to change the parent of an element, causing a style tag to be reinterpreted as HTML and an img tag with an onerror handler to execute.
vovohelo.medium.com
Published on vovohelo.medium.com
For those who are only interested in the final payload here you go (I won’t judge). For the ones interested in why it works, please bear with me.
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 assumption that parsing and serialising a DOM fragment is idempotent, combined with namespace confusion where an element's namespace changes after re-parsing due to an ownership mutation gadget.
Impact
An attacker could execute arbitrary JavaScript in the context of a victim's browser when the sanitised content is rendered, potentially leading to account compromise or data theft.
What the attacker needed
Application uses DOMPurify < 2.2.2; attacker can submit HTML content
The fix
The vulnerability was fixed in DOMPurify version 2.2.2.
How it was found
- Construct an HTML fragment with a nested form ownership mutation gadget that changes the parent of an mglyph tag to mtext, moving descendants from SVG to MathML namespace.
- Include a style tag and an img tag with an onerror handler inside the mglyph element.
- Pass the fragment to DOMPurify for sanitisation.
- After sanitisation, the serialised output is parsed again by the browser, causing the style tag to be treated as raw text and the img tag to execute its onerror handler.
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. This report shows a way to sneak a script past it by using a quirk in how browsers parse HTML when tags from different languages (like SVG and MathML) are mixed. The attacker could run their own JavaScript in the context of a victim's browser.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.