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.7 KB
Raw Blame
/**
 * Fail-closed contracts for an unsupported DOMPurify at every direct
 * sanitizer call site outside renderMarkdown.
 *
 * DOMPurify computes ``isSupported`` from an ``&&`` chain that can yield
 * ``null``/``undefined`` as well as ``false`` (e.g. a broken DOM
 * environment), and ``sanitize()`` returns its input UNCHANGED whenever
 * it is falsy. A presence check (``typeof DOMPurify !== 'undefined'`` or
 * ``!window.DOMPurify``) therefore treats such an instance as a working
 * sanitizer and fails open. Each site must instead take its existing
 * no-sanitizer fallback unless ``isSupported === true``:
 *
 *   - xss-protection.js sanitizeHtml      -> escaped text
 *   - xss-protection.js safeSetInnerHTML  -> textContent
 *   - xss-protection.js safeCreateElement -> allowlisted DOM creation
 *   - semantic_search.js renderSnippet    -> escaped text
 *   - news.js safeRenderHTML              -> textContent
 *   - pdf.js generatePdf                  -> throws, temp DOM removed
 *
 * The stand-in below behaves like an unsupported DOMPurify: sanitize()
 * hands its input back untouched, so any site that still calls it would
 * emit raw markup.
 */

import '@js/security/xss-protection.js';
import '@js/components/semantic_search.js';
import '@js/services/pdf.js';
import { installNewsRenderHarness, makeNewsItem } from '../pages/helpers/news-render-harness.js';

const RAW = '<img src=x onerror="window.__ldrPwned=1">';
const UNSUPPORTED = [false, null, undefined];

function unsupportedPurifier(isSupported) {
    return {
        isSupported,
        addHook: vi.fn(),
        // Real DOMPurify with a falsy isSupported returns its input as-is.
        sanitize: vi.fn((dirty) => dirty),
    };
}

function bindPurifier(purifier) {
    globalThis.DOMPurify = purifier;
    window.DOMPurify = purifier;
}

let newsHarness;
let savedPurifier;

beforeAll(async () => {
    newsHarness = await installNewsRenderHarness();
    savedPurifier = window.DOMPurify;
});

afterEach(() => {
    bindPurifier(savedPurifier);
    delete window.marked;
    delete globalThis.jsPDF;
    delete globalThis.html2canvas;
    delete window.__ldrPwned;
    vi.restoreAllMocks();
});

describe('xss-protection.js with an unsupported DOMPurify', () => {
    it.each(UNSUPPORTED)('sanitizeHtml escapes instead of passing through (isSupported=%s)', (flag) => {
        const purifier = unsupportedPurifier(flag);
        bindPurifier(purifier);

        const out = window.sanitizeHtml(RAW);

        expect(purifier.sanitize).not.toHaveBeenCalled();
        expect(out).not.toContain('<img');
        expect(out).toContain('&lt;img');
    });

    it.each(UNSUPPORTED)('safeSetInnerHTML falls back to text (isSupported=%s)', (flag) => {
        const purifier = unsupportedPurifier(flag);
        bindPurifier(purifier);
        const el = document.createElement('div');

        window.safeSetInnerHTML(el, RAW, true);

        expect(purifier.sanitize).not.toHaveBeenCalled();
        expect(el.querySelector('img')).toBeNull();
        expect(el.textContent).toBe(RAW);
    });

    it.each(UNSUPPORTED)('safeCreateElement strips unsafe attributes without a sanitizer (isSupported=%s)', (flag) => {
        const purifier = unsupportedPurifier(flag);
        bindPurifier(purifier);

        const element = window.safeCreateElement('a', 'x', {
            onclick: 'window.__ldrPwned=1', href: 'javascript:alert(1)', title: 'safe',
        });
        expect(element.textContent).toBe('x');
        expect(element.hasAttribute('onclick')).toBe(false);
        expect(element.hasAttribute('href')).toBe(false);
        expect(element.getAttribute('title')).toBe('safe');
        expect(purifier.sanitize).not.toHaveBeenCalled();
    });

    it.each(UNSUPPORTED)('sanitizeStable escapes when unsupported (isSupported=%s)', flag => {
        const purifier = unsupportedPurifier(flag);
        bindPurifier(purifier);
        const out = window.XSSProtection.sanitizeStable(RAW, {});
        expect(purifier.sanitize).not.toHaveBeenCalled();
        expect(out).not.toContain('<img');
        expect(out).toContain('&lt;img');
    });

    it('still sanitizes when isSupported is exactly true', () => {
        const purifier = unsupportedPurifier(true);
        purifier.sanitize = vi.fn(() => '<b>clean</b>');
        bindPurifier(purifier);

        expect(window.sanitizeHtml(RAW)).toBe('<b>clean</b>');
        expect(purifier.sanitize).toHaveBeenCalled();
    });
});

describe('semantic_search.js renderSnippet with an unsupported DOMPurify', () => {
    it.each(UNSUPPORTED)('escapes the snippet (isSupported=%s)', (flag) => {
        const purifier = unsupportedPurifier(flag);
        bindPurifier(purifier);
        window.marked = { parseInline: vi.fn((md) => String(md)) };

        const out = window.SemanticSearch.renderSnippet(RAW, 'img');

        expect(purifier.sanitize).not.toHaveBeenCalled();
        expect(window.marked.parseInline).not.toHaveBeenCalled();
        expect(out).not.toContain('<img');
        expect(out).toContain('&lt;img');
    });
});

describe('news.js safeRenderHTML with an unsupported DOMPurify', () => {
    it.each(UNSUPPORTED)('renders the feed as text (isSupported=%s)', async (flag) => {
        const purifier = unsupportedPurifier(flag);
        bindPurifier(purifier);
        vi.spyOn(SafeLogger, 'warn').mockImplementation(() => {});

        await newsHarness.renderWith([makeNewsItem({ headline: 'UNSUPPORTED_CANARY' })]);

        // Every safeRenderHTML call in the cycle (loading placeholder,
        // feed or error message) must take the textContent fallback.
        const container = document.getElementById('news-feed-content');
        expect(purifier.sanitize).not.toHaveBeenCalled();
        expect(container.children).toHaveLength(0);
        // The template markup shows up as inert text, not elements.
        expect(container.textContent).toContain('<p');
    });
});

describe('pdf.js generatePdf with an unsupported DOMPurify', () => {
    it.each(UNSUPPORTED)('refuses and removes its temporary DOM (isSupported=%s)', async (flag) => {
        const purifier = unsupportedPurifier(flag);
        bindPurifier(purifier);
        globalThis.jsPDF = vi.fn();
        globalThis.html2canvas = vi.fn();
        window.marked = { parse: vi.fn(() => RAW) };
        vi.spyOn(SafeLogger, 'error').mockImplementation(() => {});

        await expect(window.pdfService.generatePdf('Title', 'Report')).rejects.toThrow(
            'DOMPurify not loaded. Cannot generate PDF safely.',
        );
        expect(purifier.sanitize).not.toHaveBeenCalled();
        expect(document.querySelector('.ldr-pdf-content')).toBeNull();
        expect(document.querySelector('img[onerror]')).toBeNull();
        expect(window.__ldrPwned).toBeUndefined();
    });
});