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.

6.2 KB
Raw Blame
/**
 * Runtime contracts for the DOMPurify-backed XSS helpers.
 *
 * The general unit suite deliberately exercises the no-library fallbacks.
 * Production loads DOMPurify through app.js before this classic script, so
 * these cases mirror that load order and pin the sanitizer boundary, config,
 * and hook wiring. DOMPurify's DOM traversal is not supported by happy-dom
 * (its cached Node.nodeName getter returns an empty name; see happy-dom #2182),
 * so a strict boundary spy drives production helpers below. The real
 * DOMPurify policy is asserted separately through isValidAttribute(), which
 * does not depend on the incompatible traversal path.
 */

import createDOMPurify from 'dompurify';
import '@js/utils/alert-helpers.js';

let realDOMPurify;
let purifierBoundary;
let afterSanitizeAttributes;

beforeAll(async () => {
    realDOMPurify = createDOMPurify(window);
    purifierBoundary = {
        isSupported: true,
        addHook: vi.fn((name, callback) => {
            if (name === 'afterSanitizeAttributes') {
                afterSanitizeAttributes = callback;
            }
        }),
        sanitize: vi.fn(),
    };
    globalThis.DOMPurify = purifierBoundary;

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

afterAll(() => {
    delete globalThis.DOMPurify;
    delete window.XSSProtection;
    delete window.escapeHtml;
    delete window.escapeHtmlAttribute;
    delete window.safeSetInnerHTML;
    delete window.safeCreateElement;
    delete window.safeSetTextContent;
    delete window.createSafeAlertElement;
    delete window.sanitizeUserInput;
    delete window.sanitizeHtml;
    delete window.safeUpdateButton;
    delete window.createSafeLoadingOverlay;
    delete window.safeSetStyles;
    delete window.showSafeAlert;
});

beforeEach(() => {
    document.body.replaceChildren();
    delete window.__xssExecuted;
    purifierBoundary.sanitize.mockReset();
});

afterEach(() => {
    document.body.replaceChildren();
    delete window.__xssExecuted;
});

it('installs and enforces the target-blank tabnabbing hook', () => {
    expect(purifierBoundary.addHook).toHaveBeenCalledWith(
        'afterSanitizeAttributes',
        expect.any(Function),
    );

    const link = document.createElement('a');
    link.target = '_blank';
    afterSanitizeAttributes(link);

    expect(link.getAttribute('rel')).toBe('noopener noreferrer');
});

it('the installed DOMPurify policy rejects executable attributes and URLs', () => {
    expect(realDOMPurify.isValidAttribute(
        'a',
        'href',
        'javascript:alert(1)',
    )).toBe(false);
    expect(realDOMPurify.isValidAttribute(
        'img',
        'onerror',
        'window.__xssExecuted=true',
    )).toBe(false);
    expect(realDOMPurify.isValidAttribute(
        'a',
        'href',
        'https://example.test/source',
    )).toBe(true);
});

it('safeSetInnerHTML inserts only the sanitizer result with the strict config', () => {
    const host = document.createElement('div');
    const dirty = `
        <strong id="kept">Safe report</strong>
        <img src="x" onerror="window.__xssExecuted = true">
        <script>window.__xssExecuted = true</script>
        <a href="javascript:alert(1)">bad link</a>
    `;
    purifierBoundary.sanitize.mockReturnValue(
        '<strong id="kept">Safe report</strong>',
    );

    window.safeSetInnerHTML(host, dirty, true);

    expect(purifierBoundary.sanitize).toHaveBeenCalledWith(
        dirty,
        expect.objectContaining({
            ALLOWED_TAGS: expect.arrayContaining(['strong', 'a']),
            FORBID_TAGS: expect.arrayContaining(['script', 'iframe', 'form']),
            FORBID_ATTR: expect.arrayContaining(['onerror', 'onclick']),
            ALLOW_DATA_ATTR: false,
            SAFE_FOR_TEMPLATES: true,
        }),
    );
    expect(host.querySelector('#kept')?.textContent).toBe('Safe report');
    expect(host.querySelector('img')).toBeNull();
    expect(host.querySelector('script')).toBeNull();
    expect(window.__xssExecuted).toBeUndefined();
});

it('safeCreateElement rejects unsafe tags and sanitizes unsafe attributes', () => {
    expect(() => window.safeCreateElement('script', 'alert(1)'))
        .toThrow('safeCreateElement: disallowed tag "script"');

    purifierBoundary.sanitize.mockImplementation((dirty, config) => {
        expect(dirty).toContain('href="javascript:alert(1)"');
        expect(dirty).toContain('onclick="window.__xssExecuted = true"');
        expect(config).toEqual({
            ALLOWED_TAGS: ['a'],
            RETURN_DOM_FRAGMENT: true,
        });

        const fragment = document.createDocumentFragment();
        const sanitizedLink = document.createElement('a');
        sanitizedLink.textContent = 'Safe label';
        sanitizedLink.className = 'trusted-link';
        sanitizedLink.target = '_blank';
        sanitizedLink.rel = 'noopener noreferrer';
        fragment.appendChild(sanitizedLink);
        return fragment;
    });

    const link = window.safeCreateElement(
        'a',
        'Safe label',
        {
            href: 'javascript:alert(1)',
            onclick: 'window.__xssExecuted = true',
            target: '_blank',
        },
        ['trusted-link'],
    );

    expect(link.tagName).toBe('A');
    expect(link.textContent).toBe('Safe label');
    expect(link.classList.contains('trusted-link')).toBe(true);
    expect(link.hasAttribute('href')).toBe(false);
    expect(link.hasAttribute('onclick')).toBe(false);
    expect(link.getAttribute('rel')).toBe('noopener noreferrer');
});

it('sanitizeUserInput allowHtml uses DOMPurify instead of HTML escaping', () => {
    const dirty = '  <strong>kept</strong><img src=x onerror="window.__xssExecuted=true">  ';
    purifierBoundary.sanitize.mockReturnValue('<strong>kept</strong>');

    const clean = window.sanitizeUserInput(
        dirty,
        { allowHtml: true },
    );

    expect(clean).toBe('<strong>kept</strong>');
    expect(clean).not.toContain('onerror');
    expect(window.__xssExecuted).toBeUndefined();
    expect(purifierBoundary.sanitize).toHaveBeenCalledWith(
        dirty.trim(),
        expect.objectContaining({
            ALLOWED_TAGS: expect.arrayContaining(['strong']),
            FORBID_TAGS: expect.arrayContaining(['script']),
        }),
    );
});