Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

14.5 KB
Raw Blame
/**
 * mXSS reparse invariant coverage for every sanitizer that returns markup for
 * an innerHTML sink. The contract is sanitize -> parse -> serialize -> reparse:
 * reparsing must not create executable nodes, attributes, unsafe URLs, or a
 * foreign-content transition back into executable HTML.
 *
 * ============================================================================
 * WHAT THIS SUITE DOES AND DOES NOT ESTABLISH — READ BEFORE TRUSTING IT
 * ============================================================================
 *
 * This suite runs under happy-dom (Vitest's simulated DOM), not a real
 * browser engine. It asserts sanitizer behaviour under that SIMULATED DOM. It
 * does NOT, by itself, establish a browser mXSS invariant, for one concrete
 * reason: happy-dom 20.11.12 has no MathML foreign-content support at all —
 * no tokenizer or tree-construction branch for `<math>` (see
 * html-parser/HTMLParser.ts: there is no reference to `mathml` in it
 * anywhere). Elements under a `<math>` subtree get created in the plain HTML
 * namespace, indistinguishable from any other unrecognized HTML tag, and
 * that cannot be repaired by patching `Element.prototype.namespaceURI`
 * afterwards — the tree was already built wrong by the time any getter runs.
 * A getter that fabricated MathML namespace purely from tag name (which an
 * earlier version of this suite, and this repo's shared vitest setup file,
 * both did) does not fix that: it hides it, by making every MathML-tag-named
 * element report the same namespace regardless of where it actually sits in
 * the tree — precisely the axis mXSS namespace-confusion payloads exploit.
 * Under that fabrication the MathML vectors below could never fail no matter
 * what the application did. This file therefore does NOT install any
 * namespace approximation (see tests/js/happy-dom-mathml-namespace-
 * approximation.js for where that lives now and why it must not be used
 * here), and relies only on happy-dom's real, unmodified behaviour:
 *
 *   - SVG: happy-dom's own HTML parser DOES track SVG namespace by tree
 *     position (NamespaceURI.svg is assigned during parsing, inherited down
 *     the tree it actually builds). The SVG half of the "foreign-content
 *     confusion" vector, and DOMPurify's own `_checkSvgNamespace` validity
 *     check that runs against it, are exercising real (if not fully
 *     browser-faithful — see the parser-algorithm gaps below) namespace
 *     tracking, not a fabrication. That part of the invariant is meaningful.
 *   - MathML: happy-dom reports plain HTML namespace for every MathML tag,
 *     always. DOMPurify's OWN namespace-validity check (`_checkHtmlNamespace`
 *     in dompurify/dist/purify.cjs.js) force-removes any element whose tag
 *     name is MathML-only but whose reported namespace is HTML — a real
 *     mXSS defense, not a test artifact — so these vectors' MathML content
 *     is stripped wholesale regardless of whether it was legitimately nested
 *     under `<math>` or maliciously placed. The assertions below can still
 *     pass, but that tells you DOMPurify fails closed when happy-dom cannot
 *     grant real MathML namespace; it tells you NOTHING about whether a
 *     REAL browser's foreign-content parser would ever hand DOMPurify a tree
 *     where MathML namespace confusion actually occurred. This suite cannot
 *     exercise that question at all, with or without a namespace shim.
 *
 * Independently of namespace tracking, happy-dom also does not implement the
 * HTML5 "in foreign content" insertion-mode algorithm: no HTML-integration-
 * point / MathML-text-integration-point reparenting, no rawtext/RCDATA-state
 * divergence inside foreign content, no foster parenting, no breakout on a
 * reparenting-list tag (e.g. `<p>`) encountered inside `<svg>`/`<math>`. Its
 * parser applies the same tokenization rules everywhere (see
 * parseRawTextElementContent in html-parser/HTMLParser.ts, which keys
 * purely off tag name, not namespace/foreign-content state). Direct probes
 * of these vectors serialize identically on the first and second parse in
 * this harness. None of that can be patched from test code without
 * reimplementing the tokenizer's foreign-content branches — well beyond what
 * a test shim should do.
 *
 * Keep the vectors: this suite still guards the application's required
 * sanitize -> serialize -> reparse round trip against regressions in this
 * simulated DOM, and the sentinel/script/on*-attribute/href-scheme checks
 * (unlike the namespace check) do not depend on foreign-content parsing
 * being modeled at all. But treat it as regression coverage for the
 * sanitizer's string-level behaviour, not as proof of a browser mXSS
 * invariant.
 *
 * Real browser coverage lives in
 * tests/ui_tests/playwright/tests/security-mxss-reparse.spec.js. It loads the
 * production sanitizers and locked rendering dependencies in Chromium,
 * Firefox and WebKit, drives these vectors through all four render paths,
 * and checks each reparsed tree for executable surface. Its positive TeX
 * control also verifies that real KaTeX-generated MathML survives. This
 * simulated-DOM suite complements that coverage; it does not replace it.
 * ============================================================================
 */
import { afterAll, afterEach, beforeAll, describe, expect, test, vi } from 'vitest';

const HTML_NAMESPACE = 'http://www.w3.org/1999/xhtml';
const FOREIGN_NAMESPACES = new Set([
    'http://www.w3.org/2000/svg',
    'http://www.w3.org/1998/Math/MathML',
]);

const VECTORS = [
    {
        family: 'foreign-content confusion',
        sentinel: 'foreign-sentinel',
        payload: '<p>foreign-sentinel</p><svg><p><style><!--</style><img src=x onerror=alert(1)>'
            + '<math><mtext><table><mglyph><style><!--</style><img src=x onerror=alert(1)>'
            + '</table></mtext></math>',
    },
    {
        family: 'rawtext/RCDATA breakout',
        sentinel: 'rawtext-sentinel',
        payload: '<p>rawtext-sentinel</p><style><a title="</style><img src=x onerror=alert(1)">'
            + '<noscript><p title="</noscript><a href="vbscript:alert(1)" onclick="alert(1)">bad</a>">'
            + '<textarea><a title="</textarea><a href="data:text/html,%3Cscript%3Ealert(1)%3C/script%3E" '
            + 'onfocus="alert(1)">bad</a>">',
    },
    {
        family: 'foster-parenting/table repair',
        sentinel: 'foster-sentinel',
        payload: '<p>foster-sentinel</p><table><caption><svg><foreignObject><table><tr><td>'
            + '<img src=x onerror=alert(1)></td></tr></table></foreignObject></svg></caption></table>'
            + '<form><table><form><tr><td onmouseover=alert(1)>'
            + '<a href=javascript:alert(1)>bad</a></td></tr></form></table></form>',
    },
    {
        family: 'deep-nesting flattening',
        sentinel: 'deep-sentinel',
        payload: '<p>deep-sentinel</p>' + '<div><svg>'.repeat(100)
            + '<a href="javascript:alert(1)"><img src=x onerror=alert(1)></a>'
            + '</svg></div>'.repeat(100),
    },
    {
        family: 'serialization instability',
        sentinel: 'serialization-sentinel',
        payload: '<p>serialization-sentinel</p><div title="prefix<!--suffix">'
            + '<a title="<script src=x>" href="javascript:alert(1)">bad</a></div><!--',
    },
];

let productionDOMPurify;
let productionMarked;

// No local namespace-approximation shim here, deliberately: this suite must
// see happy-dom's real (unfabricated) namespaceURI behaviour for its
// FOREIGN_NAMESPACES check to mean anything. See the file header. The
// createNodeIterator/nodeName repairs DOMPurify still needs are installed
// globally by tests/js/happy-dom-dompurify-shim.js (a Vitest setupFile,
// so they run before the dynamic `import('dompurify')` in beforeAll below).

function reparse(html) {
    const first = document.createElement('div');
    // eslint-disable-next-line no-unsanitized/property -- test-only: this deliberately parses a sanitizer result to assert the mXSS reparse invariant.
    first.innerHTML = html;
    const serialized = first.innerHTML;
    const second = document.createElement('div');
    // eslint-disable-next-line no-unsanitized/property -- test-only: this is the required serialize -> reparse hop under test.
    second.innerHTML = serialized;
    return second;
}

function expectReparseInvariant(html, sentinel) {
    expect(html).not.toBe('');
    const root = reparse(html);
    expect(root.innerHTML).not.toBe('');
    expect(root.textContent).toContain(sentinel);
    expect(root.querySelectorAll('script, iframe, object, embed')).toHaveLength(0);

    for (const element of root.querySelectorAll('*')) {
        expect(element.getAttributeNames().some((name) => /^on/i.test(name))).toBe(false);
    }
    for (const element of root.querySelectorAll('[href]')) {
        // The previous pattern was /^(?:javascript|vbscript|data:text\/html):/i,
        // which required a literal ':' AFTER 'data:text/html' -- so no real
        // 'data:text/html,<payload>' href could ever match it, and three of
        // the five vectors carry exactly that payload. Alternate on complete
        // prefixes instead, and strip the control characters and whitespace
        // browsers ignore inside a scheme before matching.
        // eslint-disable-next-line no-control-regex -- intentional: browsers ignore control characters inside URL schemes
        const href = (element.getAttribute('href') || '').replace(/[\x00-\x20\x7F]/g, '');
        expect(href).not.toMatch(/^(?:javascript:|vbscript:|data:text\/html)/i);
    }
    for (const element of root.querySelectorAll('*')) {
        if (element.namespaceURI !== HTML_NAMESPACE) {
            continue;
        }
        expect(FOREIGN_NAMESPACES.has(element.parentElement?.namespaceURI)).toBe(false);
    }
}

function pdfStub() {
    return {
        internal: { pageSize: { getWidth: () => 612, getHeight: () => 792 } },
        output: () => new Blob(),
        addImage() {}, addPage() {}, getTextWidth: () => 1, line() {}, rect() {},
        setDrawColor() {}, setFillColor() {}, setFont() {}, setFontSize() {},
        setLineWidth() {}, setTextColor() {}, splitTextToSize: (text) => [text],
        text() {}, textWithLink() {},
    };
}

async function sanitizeForPdf(rawHtml) {
    const previews = [];
    const appendChild = document.body.appendChild.bind(document.body);
    vi.spyOn(document.body, 'appendChild').mockImplementation((node) => {
        if (node.classList?.contains('ldr-pdf-content')) {
            previews.push(node);
        }
        return appendChild(node);
    });
    // Feed hostile HTML as if marked itself had emitted it (bypassing the
    // renderer.html escape, like a renderer extension would): the PDF body
    // must still come out of renderMarkdown's sanitizeStable() allowlist.
    vi.spyOn(productionMarked, 'parse').mockReturnValue(rawHtml);
    globalThis.marked = productionMarked;
    globalThis.DOMPurify = productionDOMPurify;
    window.DOMPurify = productionDOMPurify;
    globalThis.jsPDF = class { constructor() { return pdfStub(); } };
    globalThis.html2canvas = vi.fn();
    vi.resetModules();
    await import('@js/services/pdf.js');
    await window.pdfService.generatePdf('mXSS', 'ignored');
    expect(previews).toHaveLength(1);
    return previews[0].querySelector('#pdf-body-content').innerHTML;
}

beforeAll(async () => {
    const { Marked } = await import('marked');
    const markedKatex = (await import('marked-katex-extension')).default;
    productionDOMPurify = (await import('dompurify')).default;
    const marked = new Marked();
    marked.use(markedKatex({ throwOnError: false, errorColor: 'currentColor' }));
    productionMarked = marked;
    globalThis.marked = marked;
    globalThis.DOMPurify = productionDOMPurify;
    window.DOMPurify = productionDOMPurify;
    await import('@js/security/xss-protection.js');
    await import('@js/services/ui.js');
});

afterEach(() => {
    vi.restoreAllMocks();
    globalThis.marked = productionMarked;
    delete window.pdfService;
    document.body.replaceChildren();
});

afterAll(() => {
    delete globalThis.DOMPurify;
    delete globalThis.marked;
    delete globalThis.jsPDF;
    delete globalThis.html2canvas;
});

describe('mXSS sanitize -> serialize -> reparse invariant', () => {
    for (const vector of VECTORS) {
        // Regression: markdown output must remain safe after table repair reparsing.
        test(`renderMarkdown resists ${vector.family}`, () => {
            expectReparseInvariant(window.ui.renderMarkdown(vector.payload), vector.sentinel);
        });

        // Regression: returned sanitizer strings must remain safe after table repair reparsing.
        test(`sanitizeHtml resists ${vector.family}`, () => {
            expectReparseInvariant(
                window.XSSProtection.sanitizeHtml(vector.payload),
                vector.sentinel,
            );
        });

        // Regression: assigned sanitizer strings must remain safe after table repair reparsing.
        test(`safeSetInnerHTML output resists ${vector.family}`, () => {
            const target = document.createElement('div');
            window.XSSProtection.safeSetInnerHTML(target, vector.payload, true);
            expectReparseInvariant(target.innerHTML, vector.sentinel);
        });
    }

    test.each(VECTORS)('PDF preview markup resists $family', async ({ payload, sentinel }) => {
        expectReparseInvariant(await sanitizeForPdf(payload), sentinel);
    });

    test('renderMarkdown keeps benign headings and links', () => {
        const html = window.ui.renderMarkdown('## Safe heading\n\n[Safe link](https://example.com)');
        const root = reparse(html);
        expect(root.innerHTML).not.toBe('');
        expect(root.querySelector('h2')?.textContent).toBe('Safe heading');
        expect(root.querySelector('a')?.getAttribute('href')).toBe('https://example.com');
    });

    test('sanitizeHtml keeps allowed tags', () => {
        const root = reparse(window.XSSProtection.sanitizeHtml('<p><strong>safe</strong> <a href="https://example.com">link</a></p>'));
        expect(root.innerHTML).not.toBe('');
        expect(root.querySelector('strong')?.textContent).toBe('safe');
        expect(root.querySelector('a')?.getAttribute('href')).toBe('https://example.com');
    });

    test('PDF preview keeps benign markdown markup', async () => {
        const root = reparse(await sanitizeForPdf('<h2>Safe heading</h2><p><a href="https://example.com">Safe link</a></p>'));
        expect(root.innerHTML).not.toBe('');
        expect(root.querySelector('h2')?.textContent).toBe('Safe heading');
        expect(root.querySelector('a')?.getAttribute('href')).toBe('https://example.com');
    });
});