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.

5.2 KB
Raw Blame
let hook;

beforeAll(async () => {
    window.__VITEST_TEST__ = true;
    globalThis.escapeHtml = (value) => String(value ?? '')
        .replace(/&/g, '&')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/"/g, '&quot;');
    globalThis.safeFetch = vi.fn();
    globalThis.safeFetchWithAuth = (...args) => globalThis.safeFetch(...args);
    await import('@js/pages/unified_search.js');
    hook = window.__unifiedSearchTest;
});

afterAll(() => {
    delete window.__VITEST_TEST__;
});

function makeDom() {
    const input = document.createElement('input');
    const results = document.createElement('div');
    const empty = document.createElement('div');
    const notice = document.createElement('div');
    const modeBtn = document.createElement('button');
    const modeMenu = document.createElement('ul');
    const modeLabel = document.createElement('span');
    document.body.append(input, results, empty, notice, modeBtn, modeMenu, modeLabel);
    return { input, results, empty, notice, modeBtn, modeMenu, modeLabel };
}

function expectNoActiveMarkup(container) {
    expect(container.querySelectorAll('script').length).toBe(0);
    const eventAttributes = [...container.querySelectorAll('*')]
        .flatMap((element) => [...element.attributes])
        .filter((attribute) => attribute.name.toLowerCase().startsWith('on'));
    expect(eventAttributes).toHaveLength(0);
    expect(container.querySelectorAll('a[href^="javascript:"]').length).toBe(0);
}

beforeEach(() => {
    document.body.innerHTML = '';
});

describe('unified search result rendering XSS surface', () => {
    it('keeps hostile titles, snippets, and source types inert and visible', () => {
        const dom = makeDom();
        hook.setDomRefs(dom);
        const hostileTitle = '<img src=x onerror=alert(1)>';
        const quoteTitle = '"><a href="javascript:alert(1)">x</a>';
        const scriptTitle = '<script>alert(1)</script>';
        const hostileSnippet = '<svg onload=alert(1)>summary</svg>';

        hook.renderUnifiedSearchResults([
            { title: hostileTitle, content_preview: hostileSnippet, url: '/notes/1', source_type: '<img onerror=alert(1)>' },
            { title: quoteTitle, content_preview: hostileSnippet, url: '/notes/2', source_type: '"><b onmouseover=alert(1)>' },
            { title: scriptTitle, content_preview: hostileSnippet, url: '/notes/3', source_type: '<script>alert(1)</script>' },
        ]);

        expectNoActiveMarkup(dom.results);
        expect(dom.results.textContent).toContain(hostileTitle);
        expect(dom.results.textContent).toContain(quoteTitle);
        expect(dom.results.textContent).toContain(scriptTitle);
        expect(dom.results.textContent).toContain(hostileSnippet);
    });

    it('constrains hostile URLs while preserving inert quote-containing paths', () => {
        const dom = makeDom();
        hook.setDomRefs(dom);
        hook.renderUnifiedSearchResults([
            { title: 'JavaScript', content_preview: '', url: 'javascript:alert(1)', source_type: 'note' },
            { title: 'Data', content_preview: '', url: 'data:text/html,<script>alert(1)</script>', source_type: 'note' },
            { title: 'Quote', content_preview: '', url: '/notes/1" onmouseover="alert(1)', source_type: 'note' },
        ]);

        const links = [...dom.results.querySelectorAll('a.ldr-unified-result-card')];
        expect(links[0].getAttribute('href')).toBe('#');
        expect(links[1].getAttribute('href')).toBe('#');
        expect(links[2].getAttribute('href')).toBe('/notes/1" onmouseover="alert(1)');
        expectNoActiveMarkup(dom.results);
    });

    it('does not echo a hostile query into the empty state', async () => {
        const dom = makeDom();
        hook.setDomRefs(dom);
        hook.setUnifiedSearchMode('text');
        dom.input.value = '<script>alert(1)</script>';
        globalThis.safeFetch = vi.fn(() => Promise.resolve({
            json: () => Promise.resolve({ success: true, results: [] }),
        }));

        await hook.runUnifiedSearch();

        expectNoActiveMarkup(dom.empty);
        expect(dom.empty.textContent).not.toContain(dom.input.value);
        expect(dom.empty.textContent).toContain('No matches found');
    });

    it('renders benign results as clickable links without over-escaping', () => {
        const dom = makeDom();
        hook.setDomRefs(dom);
        hook.renderUnifiedSearchResults([
            {
                title: 'A & useful note',
                content_preview: 'Read this summary',
                url: '/notes/benign-1?tab=summary',
                source_type: 'note',
            },
            {
                title: 'Research report',
                content_preview: 'Report preview',
                url: '/results/report-2',
                source_type: 'research_report',
            },
        ]);

        const links = [...dom.results.querySelectorAll('a.ldr-unified-result-card')];
        expect(links).toHaveLength(2);
        expect(links[0].getAttribute('href')).toBe('/notes/benign-1?tab=summary');
        expect(links[1].getAttribute('href')).toBe('/results/report-2');
        expect(links[0].textContent).toContain('A & useful note');
        expect(links[0].textContent).toContain('Read this summary');
    });
});