skills/mutation-xss-anti-pattern/SKILL.md
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.
npx skillsauth add igbuend/grimbard mutation-xss-anti-patternInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Severity: High
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 is HTML sanitizers ignoring browser's unpredictable parsing. Final browser DOM differs from sanitizer's checked DOM.
// VULNERABLE: A simple sanitizer that is unaware of browser mutations.
function simpleSanitize(html) {
// Naive sanitizer: removes `<script>` tags only.
// Doesn't understand browser's HTML parsing quirks.
return html.replace(/<script.*?>.*?<\/script>/gi, '');
}
function renderComment(commentHtml) {
const sanitizedHtml = simpleSanitize(commentHtml);
// Sanitized HTML inserted into page.
document.getElementById('comments').innerHTML = sanitizedHtml;
}
// Attack: '<noscript><p title="</noscript><img src=x onerror=alert(1)>">'
// 1. simpleSanitize sees no `<script>` tags, does nothing
// 2. Browser receives: '<noscript><p title="</noscript><img src=x onerror=alert(1)>">'
// 3. Browser parsing "fixes" broken structure:
// - Sees `<noscript>`, `<p title="`
// - Treats `</noscript>` as malformed text in title attribute
// - Continues, sees `<img src=x onerror=alert(1)>`
// - Creates `<img>` with `onerror` attribute
// 4. `onerror` fires, executes script. Sanitizer bypassed.
renderComment(payload);
// SECURE: Use a mature, well-maintained, and mutation-aware HTML sanitizer like DOMPurify.
function renderCommentSafe(commentHtml) {
// DOMPurify designed to understand and defeat mXSS.
// Parses HTML in sandbox, removes dangerous content, serializes to clean HTML.
// Aware of browser parsing quirks.
const sanitizedHtml = DOMPurify.sanitize(commentHtml);
document.getElementById('comments').innerHTML = sanitizedHtml;
}
// DOMPurify correctly identifies broken HTML,
// strips malicious `onerror` attribute, neutralizes attack.
const payload = '<noscript><p title="</noscript><img src=x onerror=alert(1)>">';
renderCommentSafe(payload);
// Combine with strong CSP (defense-in-depth).
<style>, <svg>, <math>) unless absolutely necessary.onerror) and untrusted scripts, preventing mXSS execution if sanitizer fails.development
Security anti-pattern for Cross-Site Scripting vulnerabilities (CWE-79). Use when generating or reviewing code that renders HTML, handles user input in web pages, uses innerHTML/document.write, or builds dynamic web content. Covers Reflected, Stored, and DOM-based XSS. AI code has 86% XSS failure rate.
development
Security anti-pattern for XPath injection vulnerabilities (CWE-643). Use when generating or reviewing code that queries XML documents, constructs XPath expressions, or handles user input in XML operations. Detects unescaped quotes and special characters in XPath queries.
development
Security anti-pattern for weak password hashing (CWE-327, CWE-759). Use when generating or reviewing code that stores or verifies user passwords. Detects use of MD5, SHA1, SHA256 without salt, or missing password hashing entirely. Recommends bcrypt, Argon2, or scrypt.
development
Security anti-pattern for weak encryption (CWE-326, CWE-327). Use when generating or reviewing code that encrypts data, handles encryption keys, or uses cryptographic modes. Detects DES, ECB mode, static IVs, and custom crypto implementations.