| name | mutation-xss-anti-pattern |
| description | Security anti-pattern for mutation XSS (mXSS) vulnerabilities (CWE-79 variant). Use when generating or reviewing code that sanitizes HTML content, handles user-provided markup, or processes rich text. Detects sanitizer bypass through browser parsing mutations. |
Mutation XSS (mXSS) Anti-Pattern
Severity: High
Summary
Mutation XSS bypasses HTML sanitizers through inconsistent parsing. Attackers provide HTML appearing safe to sanitizers. When inserted into DOM, browser parsing "corrects" malformed code, creating executable scripts. Sanitizer sees one DOM, browser creates a different, malicious one.
The Anti-Pattern
The anti-pattern is HTML sanitizers ignoring browser's unpredictable parsing. Final browser DOM differs from sanitizer's checked DOM.
BAD Code Example
function simpleSanitize(html) {
return html.replace(/<script.*?>.*?<\/script>/gi, '');
}
function renderComment(commentHtml) {
const sanitizedHtml = simpleSanitize(commentHtml);
document.getElementById('comments').innerHTML = sanitizedHtml;
}
renderComment(payload);
GOOD Code Example
function renderCommentSafe(commentHtml) {
const sanitizedHtml = DOMPurify.sanitize(commentHtml);
document.getElementById('comments').innerHTML = sanitizedHtml;
}
const payload = '<noscript><p title="</noscript><img src=x onerror=alert(1)>">';
renderCommentSafe(payload);
Detection
- mXSS is extremely difficult to detect manually. It relies on deep knowledge of browser-specific parsing edge cases.
- Review Sanitizer Choice: Check if the application uses a known-vulnerable or homegrown HTML sanitizer. If it's not a library like DOMPurify that is actively maintained to fight mXSS, it is likely vulnerable.
- Use mXSS-specific payloads: Test the application's sanitizer with known mXSS payloads from security research (e.g., from the Cure53 research paper).
Prevention
Related Security Patterns & Anti-Patterns
References