# 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-parse` isn'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 `__depth` with `<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_ATTR` regex 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 enabling `innerHTML` execution 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_TEMPLATES` reparse 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): with `ADD_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 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 the `ADD_TAGS` opt-in path, so every app that re-allows the element stays exposed ([[raw/labs.trace37.com/selectedcontent-mxss-on-every-dompurify-version-15623|trace37]]). - **DOMPurify `SAFE_FOR_TEMPLATES`**: two independent reparse bypasses ([[raw/github.com/dompurify-safe-for-templates-bypass-when-sanitized-output-is-reparsed-15624|GHSA-qm7q-q8h9-jg8q]]) and a shadow-root/DOM-output bypass ([[raw/orbisappsec.com/dompurify-3-4-8-safe-for-templates-bypass-via-shadow-root-content-15626|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 `innerHTML` where possible. - Don't change `PARSER_MEDIA_TYPE` to 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_TEMPLATES` as 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 over `innerHTML`), and upgrade to DOMPurify ≥ 3.4.17. - Don't re-allow `<selectedcontent>` via `ADD_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 - [[raw/aszx87410.github.io/beyond-xss-mutation-xss-explained-3070|Beyond XSS: Mutation XSS Explained]] - [[raw/portswigger.net/bypassing-dompurify-again-with-mutation-xss-3072|Bypassing DOMPurify Again with Mutation XSS]] - [[raw/mizu.re/exploring-the-dompurify-library-bypasses-and-fixes-4170|Exploring the DOMPurify Library: Bypasses and Fixes]] - [[raw/flatt.tech/bypassing-dompurify-with-good-old-xml-4169|Bypassing DOMPurify with Good Old XML]] - [[raw/blog.slonser.info/dom-purify-dirty-namespace-bypass-11407|DOMPurify — dirty namespace bypass]] - [[raw/slides.com/grehack-2024-playing-with-html-parsing-to-bypass-dompurify-on-default-5014|GreHack 2024 — bypassing DOMPurify on default config]] - [[raw/portswigger.net/achieving-the-impossible-xss-in-innerhtml-without-equals-15302|XSS in innerHTML without equals]] - [[raw/labs.trace37.com/selectedcontent-mxss-on-every-dompurify-version-15623|selectedcontent mXSS on every DOMPurify version]] - [[raw/github.com/dompurify-safe-for-templates-bypass-when-sanitized-output-is-reparsed-15624|DOMPurify SAFE_FOR_TEMPLATES bypass when sanitized output is reparsed]] - [[raw/orbisappsec.com/dompurify-3-4-8-safe-for-templates-bypass-via-shadow-root-content-15626|DOMPurify 3.4.8 SAFE_FOR_TEMPLATES bypass via shadow root content]]