Mutation XSS (mXSS)
You'll learn how seemingly safe HTML can transform into dangerous executable code after passing validation checks.
What is mXSS?
Mutation XSS occurs when HTML that has been sanitized and deemed "safe" mutates during the browser's parsing process, unexpectedly creating executable JavaScript. The sanitization process sees one thing, but the browser's HTML parser interprets it differently—turning harmless markup into an active attack.
Think of it like checking luggage at airport security: the scanner shows a harmless item, but once it goes through the conveyor system and gets jostled around, components accidentally combine to form something dangerous.
How It Works
The mutation happens because browsers perform multiple parsing passes and DOM normalizations. Sanitizers check the initial HTML string, but the browser's parser may:
- Reconstruct malformed tags differently than expected
- Normalize nested or broken attributes
- Apply quirks-mode corrections that change structure
- Combine fragments in unexpected ways during DOM insertion
For example, a sanitizer might allow <noscript> tags with certain content. But in contexts where JavaScript is enabled, the browser may reparse that content differently, potentially extracting and executing script from what looked like plain text.
The Danger
mXSS bypasses traditional sanitization because the validation happens before the mutation. The payload looks harmless in its initial form but becomes executable after the browser processes it. This makes it particularly insidious—your security checks pass, yet users still get attacked.
This is especially problematic with rich-text editors, comment systems, and any feature that stores then re-displays user-supplied HTML.
Key Takeaway: Mutation XSS exploits the gap between what sanitizers see and what browsers ultimately execute—validated HTML can mutate during parsing into dangerous payloads, defeating input filters that only check the original form.