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.

19.2 KB
Raw Blame
import DOMPurify from 'dompurify';

globalThis.DOMPurify = DOMPurify;
window.DOMPurify = DOMPurify;

await import('@js/security/xss-protection.js');

const {
    safeSetInnerHTML,
    safeCreateElement,
    sanitizeHtml,
    sanitizeStable,
} = window.XSSProtection;

const removeDOMPurify = () => {
    delete globalThis.DOMPurify;
    delete window.DOMPurify;
};

const restoreDOMPurify = () => {
    globalThis.DOMPurify = DOMPurify;
    window.DOMPurify = DOMPurify;
};

describe('safeSetInnerHTML DOMPurify branches', () => {
    afterEach(() => {
        restoreDOMPurify();
    });

    it('sanitizes hostile HTML while preserving allowed markup', () => {
        const element = document.createElement('div');

        safeSetInnerHTML(element, 'Intro <p>Safe <strong>content</strong></p>', true);

        expect(element.querySelector('p strong')?.textContent).toBe('content');

        for (const payload of [
            '<script>alert(1)</script>',
            '<iframe src="javascript:alert(2)"></iframe>',
            '<img src=x onerror="alert(3)">',
            '<a href="javascript:alert(4)" onclick="alert(5)">link</a>',
        ]) {
            safeSetInnerHTML(element, payload, true);
            expect(element.querySelectorAll('script, iframe, img').length).toBe(0);
            expect(element.querySelectorAll('[onerror], [onclick], [onload]').length).toBe(0);
            // DOMPurify strips the unsafe href but KEEPS the anchor, so this is
            // null when an <a> survives and undefined when the payload produced
            // none. Both mean "no href reached the DOM"; normalise to null so the
            // assertion does not depend on which of the two happened.
            expect(element.querySelector('a')?.getAttribute('href') ?? null).toBeNull();
        }
    });

    it('falls back to text content and warns when DOMPurify is unavailable', () => {
        const element = document.createElement('div');
        const warning = vi.spyOn(SafeLogger, 'warn');
        removeDOMPurify();

        safeSetInnerHTML(element, '<strong>not markup</strong>', true);

        expect(element.textContent).toBe('<strong>not markup</strong>');
        expect(element.children.length).toBe(0);
        expect(warning).toHaveBeenCalledWith(
            'DOMPurify not available, escaping HTML instead of sanitizing'
        );
        warning.mockRestore();
    });

    it('stringifies non-string content in the sanitizing branch', () => {
        const element = document.createElement('div');

        safeSetInnerHTML(element, 42, true);

        expect(element.children.length).toBe(0);
    });
});

describe('safeCreateElement', () => {
    it('rejects tags outside the safe whitelist', () => {
        expect(() => safeCreateElement('script')).toThrow(/disallowed tag/);
        expect(() => safeCreateElement('img')).toThrow(/disallowed tag/);
        expect(() => safeCreateElement('form')).toThrow(/disallowed tag/);
    });

    it('normalizes uppercase tags and preserves text, benign attributes, and classes', () => {
        const element = safeCreateElement(
            'BUTTON',
            '',
            { type: 'button', title: 'Run action', 'data-count': 7 },
            ['primary', 'wide']
        );

        expect(element.tagName).toBe('BUTTON');
        expect(element.textContent).toBe('');
        expect(element.children.length).toBe(0);
        expect(element.getAttribute('type')).toBe('button');
        expect(element.getAttribute('title')).toBe('Run action');
        expect(element.getAttribute('data-count')).toBe('7');
        expect(element.className).toBe('primary wide');
    });

    it('neutralizes hostile attributes while preserving safe link content', () => {
        const textElement = safeCreateElement('a', '<b>literal text</b>');
        const element = safeCreateElement(
            'a',
            '',
            { onclick: 'alert(1)', href: 'javascript:alert(2)', title: 'Safe title' },
            []
        );

        expect(textElement.textContent).toBe('<b>literal text</b>');
        expect(element.getAttribute('onclick')).toBeNull();
        expect(element.getAttribute('href')).toBeNull();
        expect(element.getAttribute('title')).toBe('Safe title');
    });

    it('accepts empty attributes and handles edge inputs', () => {
        const empty = safeCreateElement('div', undefined, {}, []);
        const numeric = safeCreateElement('div', 0, { tabindex: 2 }, []);

        expect(empty.textContent).toBe('');
        expect(numeric.textContent).toBe('');
        expect(numeric.getAttribute('tabindex')).toBe('2');
        expect(() => safeCreateElement(null)).toThrow(/disallowed tag/);
    });

    it('rejects protocol-relative and backslash hrefs', () => {
        for (const href of [
            '//evil.com',
            '/\\evil.com',
            '\\\\evil.com',
            '/\\\\evil.com',
        ]) {
            const element = safeCreateElement('a', 'link', { href }, []);
            expect(element.getAttribute('href')).toBeNull();
        }
    });

    it('rejects control-character-smuggled dangerous schemes', () => {
        for (const href of [
            'java\tscript:alert(1)',
            'ja\nvascript:alert(1)',
            'data:text/html,<img src=x>',
            '\tvbscript:alert(1)',
            'file:///etc/passwd',
            'about:blank',
        ]) {
            const element = safeCreateElement('a', 'link', { href }, []);
            expect(element.getAttribute('href')).toBeNull();
        }
    });

    it('allows benign href values', () => {
        for (const href of [
            'https://example.com/page',
            'http://example.com',
            'mailto:[email protected]',
            'tel:+15551234567',
            '#section-anchor',
            '/reports/latest',
            'details.html',
            './relative/path',
            '?query=1',
        ]) {
            const element = safeCreateElement('a', 'link', { href }, []);
            expect(element.getAttribute('href')).toBe(href);
        }
    });

    it('forces noopener and noreferrer on target="_blank" while preserving safe rel tokens', () => {
        const blank = safeCreateElement(
            'a',
            '',
            { href: 'https://example.com', target: '_blank' },
            []
        );
        const blankTokens = (blank.getAttribute('rel') || '').split(/\s+/).filter(Boolean);
        expect(blankTokens).toContain('noopener');
        expect(blankTokens).toContain('noreferrer');

        const preserved = safeCreateElement(
            'a',
            '',
            {
                href: 'https://example.com',
                target: '_blank',
                rel: 'external nofollow',
            },
            []
        );
        const preservedTokens = (preserved.getAttribute('rel') || '').split(/\s+/).filter(Boolean);
        expect(preservedTokens).toContain('external');
        expect(preservedTokens).toContain('nofollow');
        expect(preservedTokens).toContain('noopener');
        expect(preservedTokens).toContain('noreferrer');

        const selfTarget = safeCreateElement(
            'a',
            '',
            { href: 'https://example.com', rel: 'nofollow' },
            []
        );
        expect(selfTarget.getAttribute('rel')).toBe('nofollow');

        const mixedCase = safeCreateElement(
            'a',
            '',
            {
                href: 'https://example.com',
                target: '  _BLANK  ',
                rel: 'External NoOpEnEr OPENER noReFeRrEr external',
            },
            []
        );
        const mixedCaseTokens = (mixedCase.getAttribute('rel') || '')
            .split(/\s+/).filter(Boolean).map(token => token.toLowerCase());
        expect(mixedCase.getAttribute('target')).toBe('_blank');
        expect(mixedCaseTokens.filter(token => token === 'noopener')).toHaveLength(1);
        expect(mixedCaseTokens.filter(token => token === 'noreferrer')).toHaveLength(1);
        expect(mixedCaseTokens.filter(token => token === 'external')).toHaveLength(1);
        expect(mixedCaseTokens).not.toContain('opener');

        const namedTarget = safeCreateElement(
            'a',
            '',
            { href: 'https://example.com', target: 'report-preview', rel: 'opener' },
            []
        );
        expect(namedTarget.getAttribute('target')).toBe('_blank');
        expect((namedTarget.getAttribute('rel') || '').toLowerCase()).toBe('noopener noreferrer');

        const topTarget = safeCreateElement(
            'a',
            '',
            { href: '/settings', target: '_TOP', rel: 'nofollow' },
            []
        );
        expect(topTarget.getAttribute('target')).toBe('_top');
        expect(topTarget.getAttribute('rel')).toBe('nofollow');
    });
});

describe('sanitizeHtml DOMPurify branches', () => {
    afterEach(() => {
        restoreDOMPurify();
    });

    it('uses the default SANITIZE_CONFIG and preserves only allowed markup', () => {
        const result = sanitizeHtml(
            'Intro <div id="kept" class="note"><b>bold</b><span>span</span></div>'
        );
        const container = document.createElement('div');
        container.innerHTML = result;

        // SANITIZE_NAMED_PROPS is on, so DOMPurify rewrites id/name to a
        // `user-content-` prefix. That is anti-DOM-clobbering hardening, not
        // an accident: without it, `id="kept"` would become
        // `document.kept` and could shadow a real property. Assert the
        // prefixed form, and pin the rewrite itself below so that turning
        // SANITIZE_NAMED_PROPS off is a test failure rather than a silent
        // loss of protection.
        expect(container.querySelector('div#user-content-kept.note b')?.textContent).toBe('bold');
        expect(container.querySelector('div#kept')).toBeNull();
        expect(container.querySelector('span')?.textContent).toBe('span');
        for (const payload of [
            '<script>alert(1)</script>',
            '<img src=x onerror="alert(2)">',
            '<a href="javascript:alert(3)" onclick="alert(4)">link</a>',
        ]) {
            container.innerHTML = sanitizeHtml(payload);
            expect(container.querySelectorAll('script, img').length).toBe(0);
            expect(container.querySelectorAll('[onclick], [onerror]').length).toBe(0);
            // See the note in the safeSetInnerHTML suite: the anchor survives
            // without its href, so getAttribute returns null rather than undefined.
            expect(container.querySelector('a')?.getAttribute('href') ?? null).toBeNull();
        }
    });

    it('isolates mixed-case blank and auxiliary named targets without retaining rel=opener', () => {
        const result = sanitizeHtml(
            '<a href="https://example.com/one" target="_BLANK" rel="External OPENER NoOpEnEr">one</a>' +
            '<a href="https://example.com/two" target="report-preview" rel="opener noReFeRrEr">two</a>'
        );
        const container = document.createElement('div');
        container.innerHTML = result;
        const links = container.querySelectorAll('a');

        expect(links).toHaveLength(2);
        links.forEach(link => {
            const relTokens = (link.getAttribute('rel') || '')
                .split(/\s+/).filter(Boolean).map(token => token.toLowerCase());
            expect(link.getAttribute('target')).toBe('_blank');
            expect(relTokens.filter(token => token === 'noopener')).toHaveLength(1);
            expect(relTokens.filter(token => token === 'noreferrer')).toHaveLength(1);
            expect(relTokens).not.toContain('opener');
        });
        expect((links[0].getAttribute('rel') || '').toLowerCase()).toContain('external');
    });

    it('exposes a lazy hook installer for direct DOMPurify consumers', async () => {
        const originalXSSProtection = window.XSSProtection;
        removeDOMPurify();
        vi.resetModules();
        await import('@js/security/xss-protection.js');
        const lateLoadedXSSProtection = window.XSSProtection;
        const addHook = vi.spyOn(DOMPurify, 'addHook');
        restoreDOMPurify();
        let installedHook;

        try {
            // Mirrors app.js publishing its deferred module import after the
            // classic security script has already executed.
            lateLoadedXSSProtection.ensureDOMPurifyHooks();
            const result = DOMPurify.sanitize(
                '<a href="https://example.com" target="named-window" rel="opener">link</a>',
                {
                    ALLOWED_TAGS: ['a'],
                    ALLOWED_ATTR: ['href', 'target', 'rel'],
                }
            );
            installedHook = addHook.mock.calls.find(
                ([entryPoint]) => entryPoint === 'afterSanitizeAttributes'
            )?.[1];
            const container = document.createElement('div');
            // eslint-disable-next-line no-unsanitized/property -- test-only: result is the output of the lazily hooked real sanitizer under assertion.
            container.innerHTML = result;
            const link = container.querySelector('a');
            const relTokens = (link.getAttribute('rel') || '')
                .split(/\s+/).filter(Boolean).map(token => token.toLowerCase());

            expect(addHook).toHaveBeenCalledWith(
                'afterSanitizeAttributes',
                expect.any(Function)
            );
            expect(link.getAttribute('target')).toBe('_blank');
            expect(relTokens).toContain('noopener');
            expect(relTokens).toContain('noreferrer');
            expect(relTokens).not.toContain('opener');
        } finally {
            if (installedHook) {
                DOMPurify.removeHook('afterSanitizeAttributes', installedHook);
            }
            addHook.mockRestore();
            window.XSSProtection = originalXSSProtection;
        }
    });

    it('applies custom config overrides', () => {
        const result = sanitizeHtml(
            'Intro <em data-note="kept">emphasis</em>',
            { ALLOWED_TAGS: ['em'], ALLOWED_ATTR: ['data-note'], ALLOW_DATA_ATTR: true }
        );

        expect(result).toContain('<em data-note="kept">emphasis</em>');
        expect(result).toContain('data-note="kept"');
    });

    it('returns an empty string for empty, null, and undefined input', () => {
        expect(sanitizeHtml('')).toBe('');
        expect(sanitizeHtml(null)).toBe('');
        expect(sanitizeHtml(undefined)).toBe('');
    });

    it('stringifies non-string input', () => {
        // 42 is truthy, so it does NOT take the `if (!dirty) return ''` early
        // exit above: it reaches sanitizeStable, which String()s it. The
        // default config sets KEEP_CONTENT, so DOMPurify allows '#text' and
        // the number survives as its own text node.
        expect(sanitizeHtml(42)).toBe('42');
    });

    it('falls back to escapeHtml and warns when DOMPurify is unavailable', () => {
        const warning = vi.spyOn(SafeLogger, 'warn');
        removeDOMPurify();

        expect(sanitizeHtml('<script>alert(1)</script>')).toBe(
            '&lt;script&gt;alert(1)&lt;&#x2F;script&gt;'
        );
        expect(warning).toHaveBeenCalledWith(
            'DOMPurify not available, falling back to HTML escaping'
        );
        warning.mockRestore();
    });
});


// sanitizeStable is the primitive both public entry points route through, and
// it had no direct coverage at all: `grep -rn sanitizeStable tests/js/`
// returned nothing. The property that makes the mXSS claim true is a
// FIXPOINT -- sanitize(sanitize(x)) === sanitize(x) -- because the sanitizer
// inspects a parse tree but ships a serialization of it, and the browser
// re-parses that serialization into a possibly different tree. Only when the
// string stops changing is the tree the browser builds one DOMPurify has
// already cleared.
describe('sanitizeStable reaches a sanitization fixpoint', () => {
    const CONFIG = { ALLOWED_TAGS: ['b', 'i', 'em', 'strong', 'span', 'p', 'div', 'a'], ALLOWED_ATTR: ['class', 'href', 'title'] };

    const PAYLOADS = [
        '<p>plain</p>',
        '<p>benign <strong>markup</strong> with <a href="https://example.com" title="ok">a link</a></p>',
        '<script>alert(1)</script>',
        '<img src=x onerror=alert(1)>',
        '<a href="javascript:alert(1)">click</a>',
        '<div><svg><p><style><!--</style><img src=x onerror=alert(1)></svg></div>',
        '<table><caption><svg><foreignObject><table><tr><td><img src=x onerror=alert(1)>'
            + '</td></tr></table></foreignObject></svg></caption></table>',
        '<noscript><p title="</noscript><img src=x onerror=alert(1)>">',
        '<form><table><form><tr><td onmouseover=alert(1)><a href=javascript:alert(1)>x</a></td></tr></form></table></form>',
        '<div title="prefix<!--suffix"><a title="<script src=x>" href="javascript:alert(1)">bad</a></div><!--',
    ];

    it.each(PAYLOADS)('is idempotent for %j', (payload) => {
        const once = sanitizeStable(payload, CONFIG);
        expect(typeof once).toBe('string');
        // The defining property: applying the sanitizer again changes nothing.
        expect(sanitizeStable(once, CONFIG)).toBe(once);
        // And the same must hold for the public entry point.
        const viaPublic = sanitizeHtml(payload);
        expect(sanitizeHtml(viaPublic)).toBe(viaPublic);
    });

    it.each(PAYLOADS)('output survives a serialize -> reparse round trip unchanged for %j', (payload) => {
        const sanitized = sanitizeStable(payload, CONFIG);
        const host = document.createElement('div');
        // eslint-disable-next-line no-unsanitized/property -- test-only: this is the serialize -> reparse hop the fixpoint has to survive.
        host.innerHTML = sanitized;
        // Reparsing must not resurrect executable nodes or handlers.
        expect(host.querySelectorAll('script, iframe, object, embed').length).toBe(0);
        for (const element of host.querySelectorAll('*')) {
            expect(element.getAttributeNames().some((name) => /^on/i.test(name))).toBe(false);
        }
        for (const anchor of host.querySelectorAll('[href]')) {
            expect((anchor.getAttribute('href') || '').toLowerCase())
                .not.toMatch(/^(?:javascript:|vbscript:|data:text\/html)/);
        }
    });

    it('keeps allowed markup intact instead of over-stripping', () => {
        const result = sanitizeStable('<p>hello <strong>world</strong></p>', CONFIG);
        expect(result).toContain('<strong>world</strong>');
    });

    it('falls closed to escaped text when the markup never stabilises', () => {
        const warning = vi.spyOn(SafeLogger, 'warn');
        // A sanitizer that never converges: each pass appends a marker, so no
        // two consecutive outputs are ever equal.
        let pass = 0;
        const original = globalThis.DOMPurify;
        globalThis.DOMPurify = { isSupported: true, sanitize: () => `<b>${++pass}</b>` };
        window.DOMPurify = globalThis.DOMPurify;

        try {
            const result = sanitizeStable('<b>x</b>', CONFIG);
            // No markup may be emitted -- the value is escaped, not shipped.
            expect(result).not.toContain('<b>');
            expect(result).toContain('&lt;b&gt;');
            expect(warning).toHaveBeenCalledWith(
                expect.stringContaining('did not reach a sanitization fixpoint')
            );
        } finally {
            globalThis.DOMPurify = original;
            window.DOMPurify = original;
            warning.mockRestore();
        }
    });
});