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.

8.2 KB
Raw Blame
/**
 * Security regression tests for renderCollections() in collections_manager.js
 * — the frontend half of the collection XSS incident (8d11eed08; the
 * Python-side coverage lives in tests/security/test_collection_id_xss.py).
 *
 * WHICH escapeHtml is in scope: collections_manager.js defines NO local
 * fallback. Every interpolation calls the bare global `escapeHtml`, i.e.
 * window.escapeHtml installed by security/xss-protection.js (loaded first via
 * base.html). That variant escapes & < > " ' / — so quotes are safe even in
 * attribute context. Unlike collections-manager.test.js (which stubs an
 * identity escaper), these tests install the REAL xss-protection.js escaper
 * so the assertions exercise production escaping end-to-end.
 *
 * renderCollections() reads the module-private `collections` array, so each
 * test drives it through loadCollections(): mock safeFetchWithAuth to return
 * the payload, re-dispatch DOMContentLoaded, and wait for the grid to render.
 */

// --- Global stubs the module reaches for at call time ----------------------
window.URLS = {
    LIBRARY_API: {
        COLLECTIONS: '/library/api/collections',
    },
};
window.api = { getCsrfToken: () => 'test-csrf' };
globalThis.alert = () => {};

// Benign default so the import-time DOMContentLoaded handler (if happy-dom
// fires one) doesn't explode; each test overrides this with its payload.
globalThis.safeFetchWithAuth = vi.fn(() =>
    Promise.resolve({ ok: true, status: 200, json: () => Promise.resolve({ success: true, collections: [] }) })
);

let container;

beforeAll(async () => {
    // Load the REAL escaper first so window.escapeHtml is the production
    // xss-protection.js implementation (the module resolves it as a global
    // at call time, so this must be in place before any render happens).
    await import('@js/security/xss-protection.js');
    await import('@js/collections_manager.js');
});

beforeEach(() => {
    globalThis.safeFetchWithAuth.mockReset();
    globalThis.safeFetchWithAuth.mockImplementation(() =>
        Promise.resolve({ ok: true, status: 200, json: () => Promise.resolve({ success: true, collections: [] }) })
    );
    document.body.innerHTML = '';
    container = document.createElement('div');
    container.id = 'collections-container';
    const noMsg = document.createElement('div');
    noMsg.id = 'no-collections-message';
    document.body.append(container, noMsg);
});

/** Feed `list` through loadCollections() and wait for renderCollections(). */
async function renderCollectionsFrom(collections) {
    globalThis.safeFetchWithAuth.mockImplementation(() =>
        Promise.resolve({ ok: true, status: 200, json: () => Promise.resolve({ success: true, collections }) })
    );
    // The module registered loadCollections() on DOMContentLoaded at import;
    // re-dispatch it to drive fetch -> collections = payload -> render.
    document.dispatchEvent(new window.Event('DOMContentLoaded'));
    await vi.waitFor(() => {
        if (container.children.length === 0) {
            throw new Error('renderCollections() has not rendered yet');
        }
    });
    return container;
}

/** Names of any inline event-handler attributes (on*) anywhere under root. */
function eventHandlerAttributes(root) {
    const found = [];
    for (const el of root.querySelectorAll('*')) {
        for (const name of el.getAttributeNames()) {
            if (/^on/i.test(name)) found.push(`${el.tagName}[${name}]`);
        }
    }
    return found;
}

describe('renderCollections() XSS surface', () => {
    it('has the attribute-safe escapeHtml from security/xss-protection.js in scope', () => {
        // Documents WHICH escaper renderCollections() resolves: window.escapeHtml
        // set by security/xss-protection.js. It must escape & < > " ' / — the
        // quote and slash escaping is what keeps attribute contexts safe.
        expect(window.escapeHtml('<img src=x onerror=alert(1)>')).toBe('&lt;img src=x onerror=alert(1)&gt;');
        expect(window.escapeHtml('x" onmouseover="alert(1)')).toBe('x&quot; onmouseover=&quot;alert(1)');
        expect(window.escapeHtml("'")).toBe('&#39;');
        expect(window.escapeHtml('/')).toBe('&#x2F;');
    });

    it('renders hostile name/description/id as visible text, never as markup', async () => {
        await renderCollectionsFrom([
            {
                id: '"><svg onload=alert(1)>',
                name: '<img src=x onerror=alert(1)>',
                description: '<script>alert(1)</script>',
                document_count: 3,
                created_at: '2026-01-02T03:04:05Z',
                embedding: { provider: 'openai', model: 'text-embedding-3-small' },
            },
        ]);

        // No injected elements...
        expect(container.querySelectorAll('script')).toHaveLength(0);
        expect(container.querySelectorAll('img')).toHaveLength(0);
        expect(container.querySelectorAll('svg')).toHaveLength(0);
        // ...and no inline event-handler attribute anywhere in the grid.
        expect(eventHandlerAttributes(container)).toEqual([]);

        // The payloads survive as VISIBLE TEXT (decoded)...
        expect(container.textContent).toContain('<img src=x onerror=alert(1)>');
        expect(container.textContent).toContain('<script>alert(1)</script>');
        // ...and the serialized markup shows them entity-encoded.
        expect(container.innerHTML).toContain('&lt;img');
        expect(container.innerHTML).toContain('&lt;script');

        // The quote-breakout id stays a single attribute VALUE (data-id).
        const wrapper = container.querySelector('.ldr-collection-card-wrapper');
        expect(wrapper.getAttribute('data-id')).toBe('"><svg onload=alert(1)>');
    });

    it('contains embedding provider/model payloads inside the title attribute (no quote-breakout)', async () => {
        const providerPayload = 'x" onmouseover="alert(1)';
        const modelPayload = 'y" onmouseover="alert(2)';
        await renderCollectionsFrom([
            {
                id: 'coll-attr',
                name: 'attr xss',
                description: '',
                document_count: 1,
                embedding: { provider: providerPayload, model: modelPayload },
            },
        ]);

        // Quote-breakout would create a real onmouseover attribute — none exists.
        expect(container.querySelector('[onmouseover]')).toBeNull();
        expect(eventHandlerAttributes(container)).toEqual([]);

        // Both payloads are contained INSIDE the title value, decoded verbatim:
        // "Embedding: <provider>/<model>" with the quotes inert.
        const span = container.querySelector('.ldr-embedding-info span[title]');
        expect(span).not.toBeNull();
        expect(span.getAttribute('title')).toBe(`Embedding: ${providerPayload}/${modelPayload}`);
        // The visible label is the model string as text, not markup.
        expect(span.textContent).toBe(modelPayload);
    });

    it('never lets a javascript: collection id reach a rendered href', async () => {
        await renderCollectionsFrom([{ id: 'javascript:alert(1)//', name: 'js-uri', document_count: 0 }]);

        const card = container.querySelector('a.ldr-collection-card');
        const view = container.querySelector('a.ldr-collection-view-link');
        // encodeURIComponent() neutralizes the scheme: ':' -> %3A, '/' -> %2F.
        const expected = '/library/collections/javascript%3Aalert(1)%2F%2F';
        expect(card.getAttribute('href')).toBe(expected);
        expect(view.getAttribute('href')).toBe(expected);
        expect(card.getAttribute('href').toLowerCase()).not.toMatch(/^javascript:/);
    });

    it('coerces a hostile document_count to 0 documents with no markup leak', async () => {
        await renderCollectionsFrom([
            { id: 'coll-count', name: 'count xss', description: '', document_count: '<img onerror=x>' },
        ]);

        // Number('<img onerror=x>') is NaN -> || 0 -> "0 documents"; the raw
        // string never reaches the HTML at all.
        const icon = container.querySelector('.ldr-stat-item i.fa-file');
        expect(icon).not.toBeNull();
        expect(icon.nextElementSibling.textContent).toBe('0 documents');
        expect(container.querySelectorAll('img')).toHaveLength(0);
        expect(container.innerHTML).not.toContain('<img');
    });
});