Mutation XSS
mXSS defeats a correct HTML sanitizer by exploiting the gap between the DOM the sanitizer validates and the DOM the browser later produces:
sanitize → serialize → re-parseisn’t idempotent, so a benign-looking tree mutates into an executing one on re-insertion.
How it works
A string-in/string-out sanitizer (DOMPurify et al.) does: parse input to a DOM, strip disallowed nodes/attributes per config, serialize back to a string. The app then does element.innerHTML = safeHtml, which re-parses. If the browser re-parses the serialized output differently from how the sanitizer parsed the input, markup the sanitizer deemed inert can revive as live. Three engines drive this:
- Node flattening / foreign-content. Browsers relocate elements that are illegal in a given context (
<p>inside<svg>, content “jumping out” of<svg>/<table>).<style>is text in the HTML namespace but markup inside SVG/MathML (HTML/MathML-text integration points), so a tag can mean different things before and after flattening. - Insertion modes & the stack of open elements. “in caption”/“in table” rules pop elements regardless of namespace; nested
<form>/<caption>/<table>constructs trigger mutations. DOMPurify ≤3.1.0 was bypassed via node flattening past its nesting limit (@IcesFont); the fix added a depth counter (max 255), re-broken by using the mutation twice (255×2 ≥ 510) and by clobbering__depthwith<form><input id="__depth">. - Parser state confusion.
<style>/comment/CDATA boundaries let an encoded-->or]]>reopen as markup after serialization.
Techniques / payloads
- MathML+
<style>comment mutation (DOMPurify ≤2.0.x, Chrome):<math><mtext><table><mglyph><style><!--</style><img title="--><img src=1 onerror=alert(1)>">. Firefox variant swaps the comment for CDATA:<math><mtext><table><mglyph><style><![CDATA[</style><img title="]]></mglyph><img src=1 onerror=alert(1)>">. - XML parser differentials (
PARSER_MEDIA_TYPE: application/xhtml+xml): Processing Instructions — HTML enters “bogus comment” state on<?, ending at>not?>:<?img ><img src onerror=alert(1)>?>. CDATA outside SVG/MathML becomes a bogus comment ending at>:<![CDATA[ ><img src onerror=alert(1)> ]]>(DOMPurify 3.0.10/3.0.11). - Dirty namespace (slonser): DOMPurify’s
DATA_ATTRregex lacked an end anchor, allowing a colon in a data-attr name →data-slonser:href="javascript:alert(1)"within XHTML-parsed SVG. <selectedcontent>/ no-equals (Kinugawa): newly introduced element enablinginnerHTMLexecution even without=:<select><selectedcontent></selectedcontent><option><svg><script>alert(1)</script>(Safari 27.2; Chrome closed it in 153). Grew out of a Kabir Acharya DOMPurify bypass.- Fuzzing triplets of
<tag><tag><tag></tag></tag><style></style>across namespaces surfaces new flattening mutations (mizu.re found 3.1.1/3.1.2 bypasses this way). SAFE_FOR_TEMPLATESreparse gaps (GHSA-qm7q-q8h9-jg8q, fixed after 3.4.17): DOMPurify scrubs{{…}}per text node and on the final string, but not after the consumer reparses the output. (1) A nested<form>survives the first parse because an orphaned</form>clears the form-element pointer; on reparse the inner<form>is ignored and the{,{expr},}text nodes merge into one live expression. (2) The final-string regex branch^[\w\W]*}}deletes leading start tags but leaves their end tags, which the reparse drops, again rejoining the braces. Vue 3 in-DOM templates and AngularJS 1.8.3 then evaluate it.- Shadow-root split expressions (GHSA-cg2g-xh64-v4q7, DOMPurify 3.4.8, fixed in 3.4.17): with
RETURN_DOM/RETURN_DOM_FRAGMENT, an expression split across adjacent nodes inside attached shadow-root content never meets the template regex in reassembled form. <selectedcontent>opt-in path (trace37): withADD_TAGS: ['selectedcontent'], stripping an invalid attribute from an<option>makes Chromium re-clone the option’s still-unsanitized children into the already-walked<selectedcontent>mirror. Reproduces on DOMPurify 1.0.0–3.4.13 (Chromium customizable-<select>, Chrome 134+). It is browser-gated, not version-gated.
Notable cases
- DOMPurify < 2.0.1 (CVE-2019-16728, Masato Kinugawa): an SVG/MathML-context mutation in which the sanitizer saw a harmless
<style>block but the browser’s re-parse broke out of it, a bug in the sanitizer itself. jQuery CVE-2020-11022/11023 is a cousin:htmlPrefiltermishandled<option>/<style>before.html()/.append()insertion, so even pre-sanitized HTML could execute (isv0x1). - DOMPurify bypass chain:
<2.0.17(SecurityMB, MathML) →≤2.1/≤3.1.0/3.1.1/3.1.2, each patch re-broken; history curated in the DOMPurify “Attack Classes & Bypass History” wiki. - Angular stored XSS (CVE-2025-66412) via SVG-animation/MathML attributes bypassing the template compiler’s security schema — a sanitizer-schema gap in the same family.
- Cloudflare
/cdn-cgi/image/XSS chained an SVG-sanitizer (svg-hush) namespace-serialization bug with a Chromium gadget (SVG and File Upload XSS). - DOMPurify 3.4.4 (CVE-2026-47423, CVSS 8.2):
<selectedcontent>briefly entered the default allow-list; 3.4.5 removed it. The maintainer declined to fix theADD_TAGSopt-in path, so every app that re-allows the element stays exposed (trace37). - DOMPurify
SAFE_FOR_TEMPLATES: two independent reparse bypasses (GHSA-qm7q-q8h9-jg8q) and a shadow-root/DOM-output bypass (GHSA-cg2g-xh64-v4q7).
Defenses
- Keep DOMPurify current; mutation bypasses are patched reactively, so pin and update.
- Prefer Trusted Types so the typed value is what reaches the sink, and avoid round-tripping through
innerHTMLwhere possible. - Don’t change
PARSER_MEDIA_TYPEto XHTML unless required — it opens the XML-differential surface. - Set
SAFE_FOR_XML/keep CDATA & PI filtering enabled; avoid re-sanitizing already-serialized output (“trusting your own output”). - Treat
SAFE_FOR_TEMPLATESas defense in depth, not a guarantee: don’t mount or compile sanitized HTML with a client-side template engine (Vue in-DOM templates, AngularJS, Alpine, Handlebars overinnerHTML), and upgrade to DOMPurify ≥ 3.4.17. - Don’t re-allow
<selectedcontent>viaADD_TAGS. cure53’s own rule for cloned or deferred elements: forbid them by default, or inspect again after the engine settles.
Related
- Cross-Site Scripting · DOM-Based XSS · XSS Filter Bypasses · SVG and File Upload XSS · Content Security Policy
Sources
- Beyond XSS: Mutation XSS Explained · original
- Bypassing DOMPurify Again with Mutation XSS · original
- Exploring the DOMPurify Library: Bypasses and Fixes · original
- Bypassing DOMPurify with Good Old XML · original
- DOMPurify — dirty namespace bypass · original
- GreHack 2024 — bypassing DOMPurify on default config · original
- XSS in innerHTML without equals · original
- selectedcontent mXSS on every DOMPurify version · original
- DOMPurify SAFE_FOR_TEMPLATES bypass when sanitized output is reparsed · original
- DOMPurify 3.4.8 SAFE_FOR_TEMPLATES bypass via shadow root content · original
- XSS Explained: From a Simple alert(1) to Real-World Impact CVEs · original