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.

3.9 KB
Raw Blame
/** Late-bound sanitizer coverage through the real Markdown rendering path. */
import createDOMPurify from 'dompurify';
import { Marked } from 'marked';

describe('renderMarkdown with late-bound DOMPurify', () => {
    let addHook;

    beforeEach(async () => {
        delete globalThis.DOMPurify;
        delete globalThis.marked;
        vi.resetModules();
        await import('@js/security/xss-protection.js');
        await import('@js/services/ui.js');
        const purifier = createDOMPurify(window);
        addHook = vi.spyOn(purifier, 'addHook');
        // eslint-disable-next-line require-atomic-updates -- deliberate late binding after both application modules have loaded.
        globalThis.DOMPurify = purifier;
        // eslint-disable-next-line require-atomic-updates -- deliberate late binding after both application modules have loaded.
        globalThis.marked = new Marked();
    });

    afterEach(() => {
        vi.restoreAllMocks();
        delete globalThis.DOMPurify;
        delete globalThis.marked;
        delete window.XSSProtection;
        delete window.ui;
        ['escapeHtml', 'escapeHtmlAttribute', 'safeSetInnerHTML',
            'safeCreateElement', 'safeSetTextContent', 'createSafeAlertElement',
            'sanitizeUserInput', 'sanitizeHtml', 'safeUpdateButton',
            'createSafeLoadingOverlay', 'safeSetStyles', 'showSafeAlert'
        ].forEach(name => delete window[name]);
    });

    function parse(markdown) {
        const template = document.createElement('template');
        // eslint-disable-next-line no-unsanitized/property -- test-only: inspect the actual renderer output.
        template.innerHTML = window.ui.renderMarkdown(markdown);
        return template.content;
    }

    it('isolates a Markdown link on the first render after late binding', () => {
        const root = parse('[source](https://example.test/paper)');
        const anchor = root.querySelector('a');
        expect(anchor.getAttribute('href')).toBe('https://example.test/paper');
        expect(anchor.getAttribute('target')).toBe('_blank');
        expect(anchor.getAttribute('rel').split(/\s+/)).toEqual(
            expect.arrayContaining(['noopener', 'noreferrer'])
        );
        expect(addHook).toHaveBeenCalledTimes(1);
    });

    it.each([
        '<a href="https://example.test" target="_blank">raw link</a>',
        '<a href="https://example.test" target="popup" rel="opener">named</a>',
        '<map name="m"><area href="https://example.test" target="_blank"></map>',
        '<form action="https://example.test" target="_blank"></form>',
    ])('shows author-supplied HTML as text: %s', markup => {
        const root = parse(markup);
        expect(root.querySelector('a, area, form, input')).toBeNull();
        expect(root.textContent).toContain(markup);
    });

    it.each([
        ['name', 'tagName'], ['id', 'tagName'],
        ['name', 'getAttribute'], ['id', 'getAttribute'],
        ['name', 'setAttribute'], ['id', 'setAttribute'],
    ])('does not create a form with a control %s=%s', (attr, prop) => {
        const markup = `<form action="https://example.test" target="popup"><input ${attr}="${prop}"><button>go</button></form>`;
        const root = parse(markup);
        expect(root.querySelector('form, input, button')).toBeNull();
        expect(root.textContent).toContain(markup);
    });

    it('registers the hook once across repeated renders', () => {
        for (let i = 0; i < 3; i++) {
            expect(parse('[source](https://example.test)').querySelector('a')).not.toBeNull();
        }
        expect(addHook).toHaveBeenCalledTimes(1);
    });

    it('escapes raw anchors when marked is unavailable', () => {
        delete globalThis.marked;
        const markup = 'x <a href="https://example.test" target="_blank">raw</a>';
        const root = parse(markup);
        expect(root.querySelector('a')).toBeNull();
        expect(root.querySelector('pre').textContent).toBe(markup);
    });
});