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.

41.0 KB
Raw Blame
import '@js/security/url-validator.js';
import '@js/security/xss-protection.js';
import '@js/services/ui.js';

const ui = window.ui;

beforeAll(async () => {
    // happy-dom's NodeIterator stops yielding nodes as soon as the current
    // node is removed from the tree. DOMPurify removes disallowed elements
    // WHILE iterating, so under happy-dom its traversal silently truncates
    // after the first removal and hostile markup after it survives
    // sanitization (DOMPurify's README explicitly lists happy-dom as an
    // unsupported DOM). Install a snapshot-based iterator with the spec
    // behavior DOMPurify requires — removals never truncate the traversal —
    // BEFORE importing dompurify, which captures document.createNodeIterator
    // at module-evaluation time.
    document.createNodeIterator = function(root, whatToShow) {
        const mask = whatToShow === undefined || whatToShow === null
            ? 0xffffffff
            : whatToShow;
        const nodes = [];
        (function collect(node) {
            if (mask & (1 << (node.nodeType - 1))) {
                nodes.push(node);
            }
            for (let i = 0; i < node.childNodes.length; i++) {
                collect(node.childNodes[i]);
            }
        })(root);
        let index = 0;
        return {
            nextNode() {
                return index < nodes.length ? nodes[index++] : null;
            },
            previousNode() {
                return index > 0 ? nodes[--index] : null;
            },
        };
    };

    // happy-dom's Node.prototype#nodeName base getter returns "" (the
    // correct values come from subclass getters on property access, but
    // DOMPurify caches the BASE getter at module-evaluation time and uses
    // it for every node, so every tag resolves to "" and falls into the
    // forbidden-element branch). Install a spec-correct getter before
    // dompurify captures it.
    Object.defineProperty(window.Node.prototype, 'nodeName', {
        configurable: true,
        get() {
            switch (this.nodeType) {
                case 1: return this.tagName;
                case 2: return this.name;
                case 3: return '#text';
                case 4: return '#cdata-section';
                case 7: return this.target;
                case 8: return '#comment';
                case 9: return '#document';
                case 10: return '#document-type';
                case 11: return '#document-fragment';
                default: return '';
            }
        },
    });

    // happy-dom's HTML parser never assigns foreign-content namespaces:
    // a parsed <math> subtree reports the XHTML namespace, and DOMPurify's
    // namespace-confusion defense (mXSS) removes every MathML-namespaced
    // tag found in the HTML namespace. Real browsers give parsed <math>
    // subtrees the MathML namespace. Map MathML-specific tag names onto
    // the MathML namespace so KaTeX output survives like it does in a
    // browser; SVG tag names are deliberately NOT mapped, so hostile
    // <svg> markup keeps failing the namespace check and is removed.
    const MATHML_TAG_NAMES = new Set([
        'math', 'semantics', 'annotation', 'annotation-xml', 'maction',
        'maligngroup', 'malignmark', 'menclose', 'merror', 'mfenced',
        'mfrac', 'mglyph', 'mi', 'mlabeledtr', 'mlongdiv', 'mmultiscripts',
        'mn', 'mo', 'mover', 'mpadded', 'mphantom', 'mprescripts', 'mroot',
        'mrow', 'ms', 'mscarries', 'msgroup', 'msline', 'mspace', 'msqrt',
        'msrow', 'mstack', 'mstyle', 'msub', 'msubsup', 'msup', 'mtable',
        'mtd', 'mtext', 'mtr', 'munder', 'munderover', 'none',
    ]);
    const MATHML_NAMESPACE_URI = 'http://www.w3.org/1998/Math/MathML';
    const originalNamespaceURI = Object.getOwnPropertyDescriptor(
        window.Element.prototype, 'namespaceURI',
    );
    Object.defineProperty(window.Element.prototype, 'namespaceURI', {
        configurable: true,
        get() {
            if (typeof this.tagName === 'string'
                && MATHML_TAG_NAMES.has(this.tagName.toLowerCase())) {
                return MATHML_NAMESPACE_URI;
            }
            return originalNamespaceURI && originalNamespaceURI.get
                ? originalNamespaceURI.get.call(this)
                : undefined;
        },
    });

    const { Marked } = await import('marked');
    const markedKatex = (await import('marked-katex-extension')).default;
    const DOMPurify = (await import('dompurify')).default;
    const markedInstance = new Marked();

    markedInstance.use(markedKatex({
        throwOnError: false,
        errorColor: 'currentColor',
    }));
    globalThis.marked = markedInstance;
    globalThis.DOMPurify = DOMPurify;
});

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

function renderContainer(markdown) {
    const container = document.createElement('div');
    // eslint-disable-next-line no-unsanitized/property -- test-only: renderMarkdown() sanitizes through the DOMPurify allowlist under test; parsing its output is how these tests assert on the sanitized DOM.
    container.innerHTML = ui.renderMarkdown(markdown);
    return container;
}

function eventHandlerAttributes(root) {
    return [...root.querySelectorAll('*')].flatMap((element) =>
        element.getAttributeNames()
            .filter((name) => /^on/i.test(name))
            .map((name) => `${element.tagName}[${name}]`),
    );
}

function hrefs(root) {
    return [...root.querySelectorAll('[href]')].map((element) =>
        element.getAttribute('href'),
    );
}

describe('renderMarkdown() XSS surface', () => {
    test('removes raw script, image, iframe, and SVG HTML', () => {
        // Regression: iframe/SVG nodes must not survive the sanitizer pipeline.
        const root = renderContainer([
            '<script>alert(1)</script>',
            '<img src="x" onerror="alert(1)">',
            '<iframe src="javascript:alert(1)"></iframe>',
            '<svg onload="alert(1)"><circle /></svg>',
        ].join('\n'));

        expect(root.querySelectorAll('script, iframe, svg')).toHaveLength(0);
        expect(root.querySelectorAll('img')).toHaveLength(0);
        expect(eventHandlerAttributes(root)).toEqual([]);
    });

    test('removes dangerous javascript, data, and vbscript markdown links', () => {
        // Regression: marked links must not retain dangerous URL schemes after sanitization.
        const root = renderContainer([
            '[javascript](javascript:alert(1))',
            '[data](data:text/html,<script>alert(1)</script>)',
            '[vbscript](vbscript:alert(1))',
        ].join('\n'));

        expect(hrefs(root).every((href) =>
            !/^(?:javascript|data|vbscript):/i.test(href),
        )).toBe(true);
    });

    test('does not create dangerous anchors from hostile autolinks', () => {
        // Regression: marked autolinks must not retain dangerous URL schemes after sanitization.
        const root = renderContainer([
            '<javascript:alert(1)>',
            '<data:text/html,alert(1)>',
            '<vbscript:alert(1)>',
        ].join('\n'));

        expect(hrefs(root).every((href) =>
            !/^(?:javascript|data|vbscript):/i.test(href),
        )).toBe(true);
        expect(eventHandlerAttributes(root)).toEqual([]);
    });

    it('sanitizes KaTeX-generated links and event attributes', () => {
        const root = renderContainer([
            '$\\href{javascript:alert(1)}{click}$',
            '$\\htmlClass{onmouseover=alert(1)}{text}$',
        ].join('\n'));

        expect(root.querySelectorAll('script, iframe, svg')).toHaveLength(0);
        expect(eventHandlerAttributes(root)).toEqual([]);
        expect(hrefs(root).every((href) =>
            !/^(?:javascript|data|vbscript):/i.test(href),
        )).toBe(true);
    });

    test('neutralizes entity-encoded dangerous URLs and event attributes', () => {
        // Regression: entity-encoded raw HTML must not retain an inline event handler.
        const root = renderContainer(
            '<a href="&#106;&#97;vascript:alert(1)" onmouseover="alert(1)">click</a>',
        );

        expect(eventHandlerAttributes(root)).toEqual([]);
        expect(hrefs(root).every((href) =>
            !/^(?:javascript|data|vbscript):/i.test(href),
        )).toBe(true);
    });

    it('does not preserve event attributes on benign markdown anchors', () => {
        const root = renderContainer(
            '[safe link](https://example.com "Example")',
        );
        const link = root.querySelector('a');

        expect(link).not.toBeNull();
        expect(link.getAttribute('href')).toBe('https://example.com');
        expect(link.getAttribute('target')).toBe('_blank');
        expect(link.getAttribute('rel')).toContain('noopener');
        expect(eventHandlerAttributes(root)).toEqual([]);
    });

    it('preserves benign markdown markup as a non-vacuity control', () => {
        const root = renderContainer('Intro.\n\n## Safe heading\n\n**bold** and `code`');

        expect(root.querySelector('h2')?.textContent).toBe('Safe heading');
        expect(root.querySelector('strong')?.textContent).toBe('bold');
        expect(root.querySelector('code')?.textContent).toBe('code');
    });

    it('still renders safe generated-image refs after sanitization', () => {
        const root = renderContainer('![ref:image-abc-123]');

        const wrap = root.querySelector('.ldr-generated-image');
        expect(wrap).not.toBeNull();
        expect(wrap.getAttribute('data-image-id')).toBe('image-abc-123');
        expect(wrap.parentElement.className).toBe('ldr-markdown-content');
        expect(wrap.getAttributeNames().sort()).toEqual([
            'class', 'data-image-id',
        ]);
        const img = wrap.querySelector('img');
        expect(img).not.toBeNull();
        expect(img.getAttribute('src')).toBe('/static/img/generated/image-abc-123.png');
        expect(img.getAttributeNames().sort()).toEqual([
            'alt', 'class', 'loading', 'src',
        ]);
        expect([...wrap.children].map((child) => child.tagName)).toEqual([
            'IMG', 'DIV',
        ]);
        expect(eventHandlerAttributes(root)).toEqual([]);
    });

    it('never expands generated-image markers from HTML attribute values', () => {
        // Regression: a post-sanitize string replacement used to search this
        // serialized attribute text, inject generated markup into it, and
        // materialize a live IMG[onerror] on the browser's final parse. Raw
        // HTML is now escaped before sanitization, and image directives are
        // expanded only from standalone paragraph text nodes.
        const root = renderContainer(
            '<a href="https://example.test/" '
            + 'title="![ref:image-a]<img src=x onerror=alert(1)>" '
            + 'data-note="![ref:image-data]">safe</a>',
        );

        expect(root.querySelector('a')).toBeNull();
        expect(root.querySelectorAll('.ldr-generated-image, img')).toHaveLength(0);
        expect(root.textContent).toContain('![ref:image-a]');
        expect(root.textContent).toContain('![ref:image-data]');
        expect(eventHandlerAttributes(root)).toEqual([]);
    });

    it('treats generated-image refs as standalone block directives', () => {
        const root = renderContainer([
            'before ![ref:image-inline] after',
            '',
            '`![ref:image-inline-code]`',
            '',
            '```text',
            '![ref:image-fenced-code]',
            '```',
        ].join('\n'));

        expect(root.querySelectorAll('.ldr-generated-image, img')).toHaveLength(0);
        expect(root.textContent).toContain('![ref:image-inline]');
        expect(root.textContent).toContain('![ref:image-inline-code]');
        expect(root.textContent).toContain('![ref:image-fenced-code]');
    });

    it('renders multiple standalone generated-image refs in source order', () => {
        const root = renderContainer(
            '![ref:image-one]\n\n![ref:image-two]',
        );
        const wraps = [...root.querySelectorAll('.ldr-generated-image')];

        expect(wraps.map((wrap) => wrap.dataset.imageId)).toEqual([
            'image-one', 'image-two',
        ]);
        expect(wraps.every((wrap) =>
            wrap.parentElement.className === 'ldr-markdown-content'
        )).toBe(true);
        expect(root.querySelectorAll('.ldr-markdown-content > p')).toHaveLength(0);
    });

    it('replaces generated-image directive paragraphs in place among siblings', () => {
        // Pins the directive swap in processSpecialMarkdown(): the wrapper
        // takes the directive paragraph's exact position (a pure node
        // swap), so surrounding content keeps its source order around it.
        const root = renderContainer('before\n\n![ref:image-x]\n\nafter');
        const children = [...root.querySelector('.ldr-markdown-content').children];

        expect(children.map((child) => child.tagName)).toEqual(['P', 'DIV', 'P']);
        expect(children[0].textContent).toBe('before');
        expect(children[1].className).toBe('ldr-generated-image');
        expect(children[2].textContent).toBe('after');
    });

    it('does not re-enable user-supplied images in the final image-aware pass', () => {
        const root = renderContainer([
            '![ref:image-safe]',
            '',
            '<img src="x" onerror="alert(1)">',
        ].join('\n'));
        const images = root.querySelectorAll('img');

        expect(images).toHaveLength(1);
        expect(images[0].getAttribute('src')).toBe(
            '/static/img/generated/image-safe.png',
        );
        expect(eventHandlerAttributes(root)).toEqual([]);
    });

    // FIX (defect (3) in the frontend-security-xss-hardening gate report):
    // the changelog claims `img` was re-admitted "with a value-level src
    // policy" that "closes that gap explicitly" for data:image/svg+xml and
    // javascript: sources, but nothing here rendered a real Markdown
    // `![alt](url)` image before this test was added -- deleting the entire
    // `for (const img of …)` block in hardenSanitizedMarkdown() (the code
    // that calls isSafeMarkdownImageSrc) failed nothing. These four cases
    // exercise that block through the real renderMarkdown() pipeline, not
    // isSafeMarkdownImageSrc() in isolation, so they fail if the block --
    // or its call to isSafeMarkdownImageSrc -- is removed or bypassed.
    it('drops a javascript: markdown image source', () => {
        const root = renderContainer('![x](javascript:alert(1))');

        expect(root.querySelectorAll('img')).toHaveLength(0);
        expect(eventHandlerAttributes(root)).toEqual([]);
    });

    it('drops a data:image/svg+xml markdown image source', () => {
        const root = renderContainer(
            '![x](data:image/svg+xml;base64,PHN2ZyBvbmxvYWQ9ImFsZXJ0KDEpIi8+)',
        );

        // Defence-in-depth: an <img> with an SVG data: source is
        // script-inert in browsers even if kept. The test asserts the
        // claimed control actually runs, not that a revert is exploitable.
        expect(root.querySelectorAll('img')).toHaveLength(0);
    });

    it('keeps a permitted data:image/png markdown image source', () => {
        const pngSrc = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=';
        const root = renderContainer(`![x](${pngSrc})`);
        const images = root.querySelectorAll('img');

        expect(images).toHaveLength(1);
        expect(images[0].getAttribute('src')).toBe(pngSrc);
    });

    it('keeps a normal https: markdown image source', () => {
        const root = renderContainer('![x](https://example.com/a.png)');
        const images = root.querySelectorAll('img');

        expect(images).toHaveLength(1);
        expect(images[0].getAttribute('src')).toBe('https://example.com/a.png');
    });

    // isSafeMarkdownImageSrc and URLValidator.safeAssign's data: carve-out
    // are separate policies, and the comments on both (services/ui.js and
    // security/url-validator.js) list how they differ. This table pins the
    // documented MIME and `;base64,` differences: changing how either policy
    // treats one of these rows fails here until those comments are updated
    // with it. A MIME type with no row (image/tiff, say) is not pinned, so
    // adding one to either list needs a row here too. Neither side may
    // accept SVG or a non-image type.
    it.each([
        // [data: URL, kept as a Markdown image, accepted by safeAssign on <img src>]
        ['data:image/png;base64,iVBORw0KGgo=', true, true],
        ['data:image/png,raw', true, false], // safeAssign requires ;base64,
        ['data:image/avif;base64,AAAA', false, true], // not in SAFE_DATA_MIME_TYPES
        ['data:image/x-icon;base64,AAAA', false, true], // not in SAFE_DATA_MIME_TYPES
        ['data:image/svg+xml;base64,PHN2Zy8+', false, false],
        ['data:text/html;base64,PHNjcmlwdD4=', false, false],
    ])('data: image policies stay as documented for %s', (src, markdownKeeps, safeAssignAccepts) => {
        const root = renderContainer(`![x](${src})`);
        expect(root.querySelectorAll('img')).toHaveLength(markdownKeeps ? 1 : 0);

        const img = document.createElement('img');
        expect(window.URLValidator.safeAssign(img, 'src', src)).toBe(safeAssignAccepts);
    });

    it('leaves malformed generated-image refs as literal text', () => {
        const root = renderContainer('![ref:image-" onerror="alert(1)]');

        expect(root.querySelectorAll('img, .ldr-generated-image')).toHaveLength(0);
        expect(root.textContent).toContain('![ref:');
    });

    it('preserves GFM task-list checkboxes', () => {
        const root = renderContainer('- [x] done\n- [ ] pending');

        const boxes = root.querySelectorAll('input[type="checkbox"]');
        expect(boxes).toHaveLength(2);
        expect(boxes[0].hasAttribute('checked')).toBe(true);
        expect(boxes[0].hasAttribute('disabled')).toBe(true);
        expect(boxes[1].hasAttribute('checked')).toBe(false);
    });

    it('preserves GFM table structure through the hardening pipeline', () => {
        // Pins only the rendered shape: the complete <table> subtree
        // marked emits (raw HTML is escaped before sanitization, so this
        // is the only table the pipeline sees) must survive
        // hardenSanitizedMarkdown() and processSpecialMarkdown() with its
        // cells in source order. It does NOT discriminate the inert
        // DOMParser parse from the former template-fragment parse -- this
        // harness handles this input identically under both -- and a bare
        // table fragment would only separate them under a real browser
        // parse. The parse-path change itself is pinned statically in
        // tests/security/test_client_side_xss_sinks.py.
        const root = renderContainer('| a | b |\n| --- | --- |\n| 1 | 2 |');

        const cells = [...root.querySelectorAll('th, td')].map((cell) => cell.textContent);
        expect(cells).toEqual(['a', 'b', '1', '2']);
    });

    it('preserves KaTeX HTML and MathML through the allowlist', () => {
        const root = renderContainer('$x^2 + y_i$');

        expect(root.querySelector('span.katex')).not.toBeNull();
        expect(root.querySelector('span.katex-html')).not.toBeNull();
        const math = root.querySelector('math');
        expect(math).not.toBeNull();
        expect(root.querySelector('math annotation')).not.toBeNull();
        expect(math.textContent).toContain('x');
    });

    it('renders raw interactive controls as inert source text', () => {
        const root = renderContainer([
            '<input type="password" value="secret">',
            '<input type="text" value="x">',
            '<input type="submit" value="go">',
            '<input autofocus>',
            '<input type="checkbox">',
            '<button>click</button>',
            '<form action="https://evil"><input type="text"></form>',
        ].join('\n'));

        expect(root.querySelectorAll('input')).toHaveLength(0);
        expect(root.querySelectorAll('button')).toHaveLength(0);
        expect(root.querySelectorAll('form')).toHaveLength(0);
        expect(root.textContent).toContain('<input type="password"');
        expect(root.textContent).toContain('<button>click</button>');
    });

    it('renders an error report as sections and keeps model-supplied HTML literal', () => {
        // ErrorReportGenerator.generate_error_report()
        // (error_handling/report_generator.py) output for a failed research
        // with partial results. The generator used to wrap the partial
        // results in <details><summary>; this renderer escapes raw HTML, so
        // those tags showed as text. It now writes a plain Markdown section,
        // and tests/error_handling/test_report_generator.py checks that it
        // emits no raw HTML. HTML inside the model's own findings text
        // stays literal.
        const root = renderContainer([
            '# ⚠️ Research Failed',
            '',
            '**Error Type:** Unexpected Error',
            '',
            '## 💬 Get Help',
            "We're here to help you get this working:",
            '- 📖 **Documentation & guides:** [Wiki](https://github.com/LearningCircuit/local-deep-research/wiki)',
            '',
            '## 📊 Partial Results Available',
            '',
            '### Research Summary',
            '',
            'Solar output rose in 2025, according to the sources collected before the failure.',
            '',
            '### Research Findings',
            '',
            '**Initial search:**',
            '<details><summary>Model notes</summary>hidden</details> Panels got cheaper.',
            '',
            '*Note: The above results were successfully collected before the error occurred.*',
        ].join('\n'));

        expect([...root.querySelectorAll('h1, h2, h3')].map(
            (heading) => `${heading.tagName}:${heading.textContent.trim()}`,
        )).toEqual([
            'H1:⚠️ Research Failed',
            'H2:💬 Get Help',
            'H2:📊 Partial Results Available',
            'H3:Research Summary',
            'H3:Research Findings',
        ]);
        expect(hrefs(root)).toContain('https://github.com/LearningCircuit/local-deep-research/wiki');
        expect(root.querySelector('strong')).not.toBeNull();
        // The only tag text on the page is the model's own <details>.
        expect(root.querySelectorAll('details, summary')).toHaveLength(0);
        expect(root.textContent.split('<details>')).toHaveLength(2);
        expect(root.textContent).toContain(
            '<details><summary>Model notes</summary>hidden</details> Panels got cheaper.',
        );
    });

    it('shows a raw HTML comment, such as the report sources sentinel, as literal text', () => {
        // The report generator's appended-sources sentinel
        // (text_optimization/citation_formatter.py) is an HTML comment, and
        // this renderer escapes it like any other raw HTML. That is why the
        // server removes it (strip_appended_sources_sentinel) before a
        // report is saved, assembled for display, or used as news findings.
        const root = renderContainer(
            'Answer.\n\n<!-- LDR_APPENDED_SOURCES_START -->\n\n## Sources',
        );

        expect(root.textContent).toContain('<!-- LDR_APPENDED_SOURCES_START -->');
        expect(root.querySelector('h2').textContent).toBe('Sources');
    });

    it('keeps only renderer-generated GFM checkboxes in a display-only state (outer layer only)', () => {
        // NOTE ON WHAT THIS DOES AND DOES NOT COVER: marked's renderer.html
        // hook (see the module header above) escapes ALL raw author-supplied
        // HTML in Markdown source to literal text BEFORE it ever reaches
        // DOMPurify or hardenSanitizedMarkdown's ALLOWED_INPUT_TYPES pass
        // (ui.js's `for (const input of ...)` block). That means
        // `rawCheckbox` below is never parsed into a real <input> element by
        // this pipeline -- it stays text, as the `root.textContent` assertion
        // shows -- so the single checkbox this test finds and inspects is
        // always the ONE marked's own GFM task-list renderer generates,
        // which marked already emits as `checked=""`/`disabled=""` with no
        // other attributes. The attribute assertions below therefore pin
        // marked's own output, not the ALLOWED_INPUT_TYPES/attribute-strip
        // defense-in-depth layer in hardenSanitizedMarkdown: deleting that
        // layer entirely would NOT fail this test, because no attacker
        // attribute-bearing <input> ever reaches it through Markdown source
        // once raw HTML is escaped upstream. That inner layer has no
        // revert-fails test of its own -- it is unreachable in the current
        // pipeline and kept only as defence-in-depth.
        const rawCheckbox = '<input type="checkbox" checked="" autofocus="autofocus" '
            + 'form="f" name="x" value="v" onfocus="alert(1)">';
        const root = renderContainer(`${rawCheckbox}\n\n- [x] generated task`);

        const boxes = root.querySelectorAll('input[type="checkbox"]');
        expect(boxes).toHaveLength(1);
        const box = boxes[0];
        expect(box.hasAttribute('checked')).toBe(true);
        expect(box.hasAttribute('disabled')).toBe(true);
        // Every other attribute — form-association, identity, value,
        // focus-grabbing, event handlers — must be stripped (or, per the
        // note above, never present in marked's own generated markup).
        for (const name of box.getAttributeNames()) {
            expect([
                'type', 'checked', 'disabled', 'class',
            ]).toContain(name);
        }
        expect(box.hasAttribute('autofocus')).toBe(false);
        expect(box.hasAttribute('form')).toBe(false);
        expect(box.hasAttribute('name')).toBe(false);
        expect(box.hasAttribute('value')).toBe(false);
        expect(box.hasAttribute('onfocus')).toBe(false);
        expect(root.textContent).toContain(rawCheckbox);
    });

    it('escapes raw inline styles that exfiltrate via network-bearing values', () => {
        const root = renderContainer([
            '<span style="background-image: url(http://evil.example/x.png); color: red">a</span>',
            '<span style="background-image: image-set(\'https://evil.example/a.png\' 1x)">a2</span>',
            '<span style="cursor: image-set(\'https://evil.example/cursor.png\' 1x), auto">a3</span>',
            '<span style="@import url(http://evil.example/x.css);">b</span>',
            '<span style="width: expression(alert(1));">c</span>',
            '<span style="background: javascript:alert(1);">d</span>',
            '<span style="background: data:image/svg+xml,<svg/>">e</span>',
        ].join('\n'));

        expect(root.querySelectorAll('span, [style]')).toHaveLength(0);
        expect(root.textContent).toContain('evil.example');
    });

    it('escapes raw inline styles that could create full-page overlays', () => {
        const root = renderContainer([
            '<span style="position: fixed; top: 0; left: 0">a</span>',
            '<span style="position:sticky">b</span>',
            '<span style="position:absolute;inset:0;z-index:2147483647">c</span>',
            '<span style="--overlay-position:fixed;position:var(--overlay-position);inset:0">d</span>',
            '<span style="height:100vh;width:100vw;opacity:0.01">e</span>',
            '<span style="-moz-binding: url(http://evil);">f</span>',
            '<span style="behavior: url(#x);">g</span>',
        ].join('\n'));

        expect(root.querySelectorAll('span, [style]')).toHaveLength(0);
        expect(root.textContent).toContain('position: fixed');
    });

    it('escapes raw styles that hide prohibited tokens behind CSS escapes', () => {
        // The positive policy validates parsed property/value pairs, so CSS
        // escapes cannot smuggle an unapproved declaration into the output.
        const root = renderContainer([
            String.raw`<span style="background-image: u\72 l(http://evil/x.png)">a</span>`,
            String.raw`<span style="p\6f sition: fix\65 d; top: 0">b</span>`,
            String.raw`<span style="behavi\6fr: url(#x)">c</span>`,
        ].join('\n'));

        expect(root.querySelectorAll('span, [style]')).toHaveLength(0);
        expect(root.textContent).toContain('background-image');
    });

    it('escapes raw styles that split overlay declarations with CSS comments', () => {
        // Regression for the PR review finding: CSS accepts a comment between
        // a property name and its colon, while a raw-token denylist does not
        // recognize the resulting position:fixed declaration.
        const root = renderContainer([
            '<span style="position/**/:fixed;top:0;left:0">fixed overlay</span>',
            '<span style="position/* separator */:sticky;top:0">sticky overlay</span>',
        ].join('\n'));

        expect(root.querySelectorAll('span, [style]')).toHaveLength(0);
        expect(root.textContent).toContain('position/**/:fixed');
        expect(root.textContent).toContain('position/* separator */:sticky');
    });

    it('removes indirect overlay and network styles from parsed and computed output', () => {
        const root = renderContainer([
            '<span style="--p:fixed;position:var(--p);inset:0;z-index:2147483647">a</span>',
            '<span style="position:absolute;top:0;right:0;bottom:0;left:0">b</span>',
            '<span style="background-image:image-set(\'https://evil.example/pixel\' 1x)">c</span>',
        ].join('\n'));
        document.body.append(root);
        try {
            expect(root.querySelectorAll('span, [style]')).toHaveLength(0);
            expect(root.textContent).toContain('image-set');
        } finally {
            root.remove();
        }
    });

    it('drops mixed declarations instead of retaining an allowed fragment', () => {
        const root = renderContainer(
            '<span style="color:red;position:absolute;inset:0">mixed</span>',
        );

        expect(root.querySelector('span')).toBeNull();
        expect(root.textContent).toContain('<span style=');
    });

    it('escapes raw HTML before it can compose trusted classes, styles, or actions', () => {
        // Model the utility classes loaded by app.js. The test does not need
        // to import all of Bootstrap to prove that the dangerous class token
        // is gone before the rendered fragment reaches the document.
        const bootstrapUtilities = document.createElement('style');
        bootstrapUtilities.textContent = [
            '.position-fixed{position:fixed!important}',
            '.top-0{top:0!important}',
            '.start-0{left:0!important}',
            '.w-100{width:100%!important}',
            '.h-100{height:100%!important}',
            '.z-3{z-index:3!important}',
        ].join('');
        document.head.append(bootstrapUtilities);

        const root = renderContainer([
            '<a id="relative-overlay" href="https://attacker.invalid/phish">',
            '  <div style="position:relative;top:-5em;left:0;width:50em;'
                + 'height:50em;background-color:white">Session expired — sign in</div>',
            '</a>',
            '<div class="katex-display"><span class="katex"><span class="katex-html">',
            '  <a id="forged-katex-overlay" class="tag" '
                + 'href="https://attacker.invalid/phish" '
                + 'style="top:-5em;left:0;width:50em;height:50em;background-color:#0000">'
                + 'Continue</a>',
            '</span></span></div>',
            '<a href="https://example.com" '
                + 'class="katex position-fixed top-0 start-0 w-100 h-100 z-3" '
                + 'data-bs-toggle="modal" data-bs-target="#login" '
                + 'data-news-action="vote" data-vote="up">overlay</a>',
        ].join('\n'));
        document.body.append(root);
        try {
            expect(root.querySelectorAll('a, [style], [data-news-action]')).toHaveLength(0);
            expect(root.querySelector('#relative-overlay')).toBeNull();
            expect(root.querySelector('#forged-katex-overlay')).toBeNull();
            expect(root.querySelector('.katex-display')).toBeNull();
            expect(root.textContent).toContain('Session expired — sign in');
            expect(root.textContent).toContain('forged-katex-overlay');
        } finally {
            root.remove();
            bootstrapUtilities.remove();
        }
    });

    it('preserves generated fenced-code language classes', () => {
        const root = renderContainer('```javascript\nconst answer = 42;\n```');

        const code = root.querySelector('pre code');
        expect(code).not.toBeNull();
        expect(code.classList.contains('language-javascript')).toBe(true);
    });

    it('preserves benign KaTeX inline styles through the hardening pass', () => {
        // \href without trust mode renders as a KaTeX error span with
        // the configured errorColor carried via inline style. That
        // style must survive because KaTeX's visual error indicator
        // depends on it, and the existing KaTeX tests assert that the
        // katex spans are still present in the output.
        const root = renderContainer('$\\href{javascript:alert(1)}{x}$');
        const styled = [...root.querySelectorAll('[style]')];
        expect(styled.length).toBeGreaterThan(0);
        for (const el of styled) {
            for (let i = 0; i < el.style.length; i++) {
                expect([
                    'background-color', 'border-bottom-width', 'border-color',
                    'border-right-style', 'border-right-width', 'border-style',
                    'border-top-width', 'border-width', 'bottom', 'color', 'height',
                    'left', 'margin', 'margin-bottom', 'margin-left', 'margin-right',
                    'margin-top', 'min-width', 'padding-left', 'position',
                    'text-shadow', 'top', 'vertical-align', 'width',
                ]).toContain(el.style.item(i));
            }
        }
        // Spot-check: at least one KaTeX error-marker span keeps a
        // benign color style.
        const errorSpan = root.querySelector('span[style*="color"]');
        expect(errorSpan).not.toBeNull();
    });

    it('accepts the rgb()/rgba() forms a real browser serializes a KaTeX hex color into', () => {
        // KaTeX only ever AUTHORS hex colors (e.g. `style="color:#df0030"`),
        // but no real browser echoes that hex text back through
        // element.style.getPropertyValue(): the CSS <hex-color> token has no
        // reciprocal CSSOM serialization, so every current engine reads it
        // back as `rgb(r, g, b)` (or `rgba(r, g, b, a)` when translucent).
        // happy-dom does NOT perform this normalization -- reading back a
        // style it parsed from `color:#df0030` returns the literal hex text
        // -- so this suite cannot observe the bug by rendering real KaTeX
        // output under happy-dom; the rgb()/rgba() value has to be supplied
        // explicitly, as if it came from a real browser's CSSOM.
        expect(ui.isSafeKatexColor('rgb(223, 0, 48)')).toBe(true);
        expect(ui.isSafeKatexColor('rgb(223,0,48)')).toBe(true);
        expect(ui.isSafeKatexColor('rgba(223, 0, 48, 0.5)')).toBe(true);
        expect(ui.isSafeKatexColor('RGBA(223, 0, 48, 1)')).toBe(true);
        // Still hex/keyword too -- this is additive, not a replacement.
        expect(ui.isSafeKatexColor('#df0030')).toBe(true);
        expect(ui.isSafeKatexColor('currentcolor')).toBe(true);
        // Out-of-range channels, and anything not a plain numeric triple,
        // stay rejected -- no url()/var()/calc() composition, no
        // wide-gamut color()/oklch()/lab() functions, no CSS comments or
        // escapes riding along inside the parens.
        expect(ui.isSafeKatexColor('rgb(300, 0, 0)')).toBe(false);
        expect(ui.isSafeKatexColor('rgba(0, 0, 0, 2)')).toBe(false);
        expect(ui.isSafeKatexColor('rgb(1, 2, 3) url(evil)')).toBe(false);
        expect(ui.isSafeKatexColor('rgb(1, 2, var(--x))')).toBe(false);
        expect(ui.isSafeKatexColor('color(srgb 1 0 0)')).toBe(false);
        expect(ui.isSafeKatexColor('oklch(0.5 0.2 30)')).toBe(false);
    });

    it('drops only the invalid declaration from a mixed KaTeX style, not the whole style', () => {
        // Regression test for the rendering bug: getSafeKatexInlineStyle
        // used to return null (dropping EVERY declaration on the element)
        // as soon as ANY single declaration failed validation. Since a
        // real browser serializes KaTeX's `color:#df0030` as `rgb(223, 0,
        // 48)` (see the test above) and the pre-fix isSafeKatexColor
        // rejected that form, every KaTeX span carrying both a color and
        // geometry (height/vertical-align/margin/...) lost its geometry
        // too -- the math rendered with broken layout. Build that exact
        // element shape directly (style set explicitly, since happy-dom
        // would not reproduce the rgb() serialization on its own) and
        // confirm geometry now survives regardless of the color's fate.
        const el = document.createElement('span');
        el.setAttribute(
            'style',
            'color: rgb(223, 0, 48); height: 0.68em; vertical-align: -0.19em; '
                + 'margin-right: 0.05em; position: absolute',
        );
        // `position: absolute` is not in SAFE_KATEX_STYLE_VALIDATORS
        // (only `relative` is), so it must be the one property dropped.
        const result = ui.getSafeKatexInlineStyle(el);
        expect(result).not.toBeNull();
        expect(result).toContain('color: rgb(223, 0, 48)');
        expect(result).toContain('height: 0.68em');
        expect(result).toContain('vertical-align: -0.19em');
        expect(result).toContain('margin-right: 0.05em');
        expect(result).not.toContain('position');
    });

    // RECORDABLE fix (frontend-security-xss-hardening gate report): the
    // `border-color` shorthand SAFE_KATEX_STYLE_VALIDATORS listed is never
    // enumerated by a browser's CSSOM when parsed from an inline style
    // attribute -- it is always expanded into the four border-*-color
    // longhands. happy-dom does not perform that expansion (like the hex
    // color the test above), so this constructs the element with the
    // longhand names directly, as a real browser's CSSOM would present
    // them, and asserts getSafeKatexInlineStyle now keeps them instead of
    // silently dropping the \fcolorbox border color to currentColor.
    it('preserves the border-*-color longhands a real browser expands border-color into', () => {
        const el = document.createElement('span');
        el.setAttribute(
            'style',
            'border-top-color: rgb(223, 0, 48); border-right-color: rgb(223, 0, 48); '
                + 'border-bottom-color: rgb(223, 0, 48); border-left-color: rgb(223, 0, 48); '
                + 'border-left-width: 0.05em; border-style: solid',
        );
        const result = ui.getSafeKatexInlineStyle(el);
        expect(result).not.toBeNull();
        expect(result).toContain('border-top-color: rgb(223, 0, 48)');
        expect(result).toContain('border-right-color: rgb(223, 0, 48)');
        expect(result).toContain('border-bottom-color: rgb(223, 0, 48)');
        expect(result).toContain('border-left-color: rgb(223, 0, 48)');
        expect(result).toContain('border-left-width: 0.05em');
    });

    it('preserves the generated katex-error class for themed parse failures', () => {
        const root = renderContainer('$\\frac$');
        const error = root.querySelector('.katex-error');

        expect(error).not.toBeNull();
        expect(error.style.color).toBe('currentcolor');
        expect(error.textContent).toContain('\\frac');
    });

    it.each([
        ['radical', '$\\sqrt{x}$', 'path'],
        ['stretchy brace', '$\\overbrace{x+y}$', 'path'],
        ['stretchy arrow', '$\\xrightarrow{abc}$', 'path'],
        ['wide accent', '$\\widehat{xyz}$', 'path'],
        ['cancellation line', '$\\cancel{x}$', 'line'],
    ])('preserves KaTeX-generated SVG for a %s', (_label, markdown, childTag) => {
        const root = renderContainer(markdown);
        const svg = root.querySelector('span.katex svg');
        const geometry = svg?.querySelector(childTag);

        expect(svg).not.toBeNull();
        expect(geometry).not.toBeNull();
        expect(svg.getAttribute('width')).not.toBeNull();
        expect(svg.getAttribute('height')).not.toBeNull();
        if (childTag === 'path') {
            expect(geometry.getAttribute('d')).not.toBeNull();
            expect(svg.getAttribute('viewBox')).not.toBeNull();
            expect(svg.getAttribute('preserveAspectRatio')).not.toBeNull();
        } else {
            expect(geometry.getAttribute('stroke-width')).toBe('0.046em');
        }
    });

    it('preserves the metrics used by complex real KaTeX output', () => {
        const root = renderContainer(
            '$\\pmb{x}+\\frac{a}{b}+\\begin{array}{c|c}a&b\\\\c&d\\end{array}$',
        );

        expect(root.querySelector('span.katex')).not.toBeNull();
        expect(root.querySelector('[style*="height"]')).not.toBeNull();
        expect(root.querySelector('[style*="text-shadow"]')).not.toBeNull();
        expect(root.querySelector('[style*="border-right"]')).not.toBeNull();
        expect(root.querySelectorAll('[style]').length).toBeGreaterThan(5);
    });

});