Skip to content

Lesson 5 of 14 · 13m

Sanitizers and Mutation XSS

Sanitization is a parse-transform-serialize pipeline, and mutation XSS lives in the gap between your sanitizer's parse and the browser's re-parse.

Sanitization is a parse, not a regex

The instinct every developer has once — "just strip the <script> tags" — fails because HTML is not a regular language. A sanitizer has to parse the input into a DOM tree, walk that tree against an allowlist of elements and attributes, drop what isn't allowed, and serialize the survivors back to a string. Regex cannot model raw-text elements, foreign content, or attribute boundaries; every regex sanitizer in history has fallen to the first <svg> someone fed it.

Mutation XSS: when re-parsing changes the tree

HTML parsing is context-dependent. Inside <style>, <title>, <textarea>, and <noscript> (when scripting is enabled), everything up to the closing tag is raw text — no elements exist inside. Inside <svg> and <math> you are in foreign content, with different rules, and elements like <desc> and <mtext> are "integration points" that drop back to HTML parsing. Whether a given byte sequence produces an <img> element or inert text depends on which of these contexts the parser is in.

The trigger is a parse boundary that moves between contexts: <noscript> content is raw text when scripting is enabled and markup when it is not; <title> and <textarea> are RCDATA where child tags do not exist but entities still decode; and the foreign-content rules inside <svg>/<math> flip back to HTML at elements like <desc> and <mtext>.

Mutation XSS exploits the gap between parses. Markup that is safe in the tree your sanitizer built can serialize into a string that the browser re-parses into a different tree — one with a live <img onerror> where text used to be. This is not a hypothetical. Masato Kinugawa produced a run of mutation-XSS bypasses against DOMPurify in 2019–2020 using nested <math><mtext><table><mglyph><style> structures; each worked because the serialized "safe" output mutated on re-parse. CVE-2020-26870 is one of them; DOMPurify's 2.0.x line shipped fix after fix for exactly this class. The project survived because it parses with the browser's own rules, treats namespaces explicitly, and fixes fast — which is also why you pin and update it like any other dependency.

The self-inflicted variant

You do not need an exotic vector to create mXSS; the serializer creates it for you. el2.innerHTML = el1.innerHTML on attacker-derived content reads a serialized string and re-parses it — the exact mutation engine the bypasses rely on. Sanitized DOM that gets serialized and re-inserted is attacker input again. Sanitize once, insert once, keep it in one document.

Sanitizer choice

DOMPurify is the answer on every stack — it is namespace-aware, it maintains an explicit list of dangerous elements rather than pattern-matching, and it is the library whose bypasses get found and patched in public. Configure it deliberately: {USE_PROFILES: {html: true}} gives you the HTML profile; FORBID_TAGS/FORBID_ATTR shrink the surface further; SANITIZE_NAMED_PROPS: true stops the DOM-clobbering path from lesson 4. RETURN_TRUSTED_TYPE: true returns a TrustedHTML object for the Trusted Types enforcement covered next.

There is now a native option. Firefox 148 shipped the standardized HTML Sanitizer API in February 2026 — el.setHTML(dirty) or el.setHTML(dirty, {sanitizer: new Sanitizer({elements: ['p', 'b', 'a'], attributes: ['href']})}) — and Chrome and Edge followed in version 146 in March 2026. Safari has not shipped it. setHTML() sanitizes inside the parse that inserts — no serialize/re-parse roundtrip, which is precisely the gap mXSS lives in. Until Safari catches up, DOMPurify plus Trusted Types is the portable answer; setHTML is where the platform is going.

What to actually do

  • Sanitize with DOMPurify (or setHTML where supported), never with regex, never with "we strip script tags."
  • Sanitize the final string once and insert it once; never feed serialized DOM back into a parse.
  • Pin the sanitizer version and update it — mXSS fixes are security fixes, and a two-year-old DOMPurify is a bypass list with a name.
  • Prefer element allowlists (USE_PROFILES) over letting everything through except a blocklist.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.