# Sanitizer bypass via namespace and mutation

Sanitizer output re-parsed in SVG, MathML, shadow DOM or entity context into live script

_Area: Web injection and code execution. Seen in 4 disclosed reports; one confirmed bounty: $3,000; latest 2026-11-18. Updated 2026-12-01._

## How it works
A sanitizer validates one parse tree, but the browser (or a downstream template engine) parses the same bytes again in a different context. The second parse produces nodes the sanitizer never saw: an SVG-namespaced `<script>` that keeps its "not started" flag, a `<style>` inside MathML that reparses into executable markup, an XML entity that makes the allowlist path bail out, or a shadow root the scrub walker cannot descend into. The broken assumption is "what I validated is what will execute." Namespace, mutation, and container boundaries all break it.

## Where it shows up
- Sanitizer has a post-walk "merge text nodes and re-check" pass; ask which containers the walker cannot enter (shadow roots, `<template>.content`, iframes) [E1](https://bugbountylibrary.com/r/ghsa%3AGHSA-cg2g-xh64-v4q7.md)
- Declarative shadow DOM in a plain HTML string reaching a sanitizer that only handles programmatic `attachShadow` [E1](https://bugbountylibrary.com/r/ghsa%3AGHSA-cg2g-xh64-v4q7.md)
- New HTML elements that clone or re-parent children (`<selectedcontent>`, `cloneNode(true)`) [E2](https://bugbountylibrary.com/r/blog%3Aportswigger-net-research-achieving-the-impossible-xss-in-innerhtml-without-equals.md)
- SVG-namespaced `<script>` vs HTML `<script>` "already started" flag differences [E2](https://bugbountylibrary.com/r/blog%3Aportswigger-net-research-achieving-the-impossible-xss-in-innerhtml-without-equals.md)
- Allowlist sanitizers fed XML entities or malformed markup that may skip validation [E3](https://bugbountylibrary.com/r/h1%3A299424.md)
- Rich-text editors sanitizing pasted attachment JSON (`data-trix-attachment`) [E4](https://bugbountylibrary.com/r/h1%3A2819573.md)
- MathML mutation vectors like `<math><mtext><table><mglyph><style>` [E4](https://bugbountylibrary.com/r/h1%3A2819573.md)

## How to review
The invariant: every byte the sanitizer emits must be parsed by the browser in the same namespace and container the sanitizer validated, with no re-parse step in between. Check:
- Does the scrub/merge pass recurse into every container the DOM spec allows (shadow roots, template content, iframes)? [E1](https://bugbountylibrary.com/r/ghsa%3AGHSA-cg2g-xh64-v4q7.md)
- Does the sanitizer `normalize()` and re-strip after element removal, in each container? [E1](https://bugbountylibrary.com/r/ghsa%3AGHSA-cg2g-xh64-v4q7.md)
- Are namespace-sensitive elements (SVG/MathML script, style) treated identically to their HTML counterparts? [E2](https://bugbountylibrary.com/r/blog%3Aportswigger-net-research-achieving-the-impossible-xss-in-innerhtml-without-equals.md)[E4](https://bugbountylibrary.com/r/h1%3A2819573.md)
- Does an XML entity or parse error cause the allowlist to be skipped while the browser still renders? [E3](https://bugbountylibrary.com/r/h1%3A299424.md)
- Is pasted/attachment HTML re-parsed after sanitization? [E4](https://bugbountylibrary.com/r/h1%3A2819573.md)

## How finders spotted it
- Enumerated every call site of the template-scrub function, saw none received a shadow root, and noted the shadow sanitizer never calls `normalize()` or the scrub; the earlier template-content fix had left a parallel boundary uncovered [E1](https://bugbountylibrary.com/r/ghsa%3AGHSA-cg2g-xh64-v4q7.md)
- Noticed `<selectedcontent>` clones selected `<option>` children including elements, tested whether a cloned SVG `<script>` executes, then assigned the same HTML to `innerHTML` and it fired [E2](https://bugbountylibrary.com/r/blog%3Aportswigger-net-research-achieving-the-impossible-xss-in-innerhtml-without-equals.md)
- Read the SVG design-guideline whitelist, then tested whether an XML entity would make the whitelist be skipped while still rendering [E3](https://bugbountylibrary.com/r/h1%3A299424.md)
- Took a known MathML mutation bypass of DOMPurify and hypothesized Trix's sanitizer shared the weakness, delivering it via copy-paste attachments [E4](https://bugbountylibrary.com/r/h1%3A2819573.md)

## Variants seen
- Shadow-root container the scrub walker cannot reach, with split template expressions re-merging [E1](https://bugbountylibrary.com/r/ghsa%3AGHSA-cg2g-xh64-v4q7.md)
- Namespace flag difference plus a cloning element resurrecting script through `innerHTML` [E2](https://bugbountylibrary.com/r/blog%3Aportswigger-net-research-achieving-the-impossible-xss-in-innerhtml-without-equals.md)
- XML entity skipping an SVG allowlist [E3](https://bugbountylibrary.com/r/h1%3A299424.md)
- MathML mutation reparsing `<style>` into an `<img onerror>` [E4](https://bugbountylibrary.com/r/h1%3A2819573.md)

## Impact and severity
Stored XSS in admin panels and partner dashboards, widening victim scope to every shop that authorized the app [E3](https://bugbountylibrary.com/r/h1%3A299424.md); XSS escalated to server-side template injection where templates are evaluated downstream [E1](https://bugbountylibrary.com/r/ghsa%3AGHSA-cg2g-xh64-v4q7.md); arbitrary JS in the user's session via editor paste [E4](https://bugbountylibrary.com/r/h1%3A2819573.md). Severity rises with reach (stored, cross-tenant) and with combination into template injection [E1](https://bugbountylibrary.com/r/ghsa%3AGHSA-cg2g-xh64-v4q7.md)[E3](https://bugbountylibrary.com/r/h1%3A299424.md).

## Seen in real software
Open-source packages and projects, and products, where this pattern was found. If the target runs one, compare the version it runs with the fix.
- **Safari** (innerHTML / <selectedcontent> cloning) Safari 27.2: Achieving the impossible: XSS in innerHTML without equals. Report: https://bugbountylibrary.com/r/blog%3Aportswigger-net-research-achieving-the-impossible-xss-in-innerhtml-without-equals.md
- **dompurify (npm)** >= 3.4.4, <= 3.4.16, fixed in 3.4.17: DOMPurify SAFE_FOR_TEMPLATES bypass: split template expressions survive in attached shadow root content on DOM output mo. Report: https://bugbountylibrary.com/r/ghsa%3AGHSA-cg2g-xh64-v4q7.md. Fix: https://github.com/cure53/DOMPurify/commit/a77a3e78b59f72fa4d1c83f22e121ad9cc7d3575

## Evidence
- E1: https://bugbountylibrary.com/r/ghsa%3AGHSA-cg2g-xh64-v4q7.md
- E2: https://bugbountylibrary.com/r/blog%3Aportswigger-net-research-achieving-the-impossible-xss-in-innerhtml-without-equals.md
- E3: https://bugbountylibrary.com/r/h1%3A299424.md
- E4: https://bugbountylibrary.com/r/h1%3A2819573.md
