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.

10.3 KB
Raw Blame
/**
 * Security regression tests for collection_upload.js and collection_create.js.
 *
 * Interpolation map:
 * collection_upload.js
 *   :267 file.name — escapeHtml; :517/:520/:529/:533 and :587/:590/:603 —
 *   file/batch/byte totals derived from numeric file metadata; :612 file.name
 *   — escapeHtml; :613 file size — numeric; :643/:648 — progress arguments
 *   supplied by numeric XHR progress; :673 — uploaded/errors array lengths;
 *   :714/:721/:728/:739/:750/:761/:772 — array lengths; :742/:753/:764
 *   uploaded filenames — escapeHtml; :775 failed filename/error — escapeHtml;
 *   :782 COLLECTION_ID — encodeURIComponent in href (regression test below).
 * collection_create.js
 *   :108 collection.id — raw same-origin redirect path; :139 collection.id —
 *   escapeHtml; :141/:144 collection.id — encodeURIComponent in href; :156
 *   static onclick only; :179 error message — escapeHtml. collection.name is
 *   not interpolated by this module.
 */

const HOSTILE_FILE_NAMES = [
    '<img src=x onerror=alert(1)>.pdf',
    '"><svg onload=alert(2)>.txt',
];
const HOSTILE_ERROR = '<script>alert(3)</script><img src=x onerror=alert(4)>';

let createResponse;
let uploadResponse;

class MockXMLHttpRequest {
    constructor() {
        this.upload = {};
        this.status = 0;
        this.responseText = '';
    }

    open() {}

    setRequestHeader() {}

    send() {
        Promise.resolve().then(() => {
            this.upload.onprogress?.({ lengthComputable: true, loaded: 1, total: 1 });
            this.status = uploadResponse.status;
            this.responseText = uploadResponse.body;
            this.onload();
        });
    }
}

beforeAll(async () => {
    window.URLS = {
        LIBRARY_API: {
            SUPPORTED_FORMATS: '/library/api/supported-formats',
            COLLECTION_UPLOAD: '/library/api/collections/upload',
            COLLECTION_CREATE: '/library/api/collections/create',
        },
    };
    window.URLBuilder = { build: (base, id) => `${base}/${id}` };
    window.URLValidator = {};
    window.api = { getCsrfToken: () => 'test-csrf' };
    globalThis.COLLECTION_ID = 'collection-safe';
    globalThis.XMLHttpRequest = MockXMLHttpRequest;

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

beforeEach(() => {
    createResponse = { success: true, collection: { id: 'collection-safe', name: 'Safe collection' } };
    uploadResponse = { status: 200, body: JSON.stringify({ success: true, uploaded: [], errors: [] }) };
    globalThis.COLLECTION_ID = 'collection-safe';
    globalThis.safeFetchWithAuth = vi.fn((url) => {
        if (url === window.URLS.LIBRARY_API.SUPPORTED_FORMATS) {
            return Promise.resolve({ ok: true, json: () => Promise.resolve({ count: 1, extensions: ['.txt'], accept_string: '.txt' }) });
        }
        if (url === window.URLS.LIBRARY_API.COLLECTION_CREATE) {
            return Promise.resolve({ ok: true, json: () => Promise.resolve(createResponse) });
        }
        return Promise.reject(new Error(`Unexpected request: ${url}`));
    });
    document.body.innerHTML = `
        <form id="upload-files-form"><input id="files-input" type="file" multiple><button type="submit">Upload</button></form>
        <div id="drop-zone"></div><div id="selected-files" style="display: none"><ul id="file-list"></ul></div>
        <div id="upload-progress" style="display: none"></div><div id="upload-results" style="display: none"></div>
        <span id="format-details"></span><span id="full-format-list"></span>
        <form id="create-collection-form"><input id="collection-name" name="name"><textarea name="description"></textarea><button id="create-collection-btn" type="submit">Create</button></form>
        <input id="collection-is-public" type="checkbox"><input id="collection-agent-enabled" type="checkbox"><span id="name-counter"></span>
        <div id="create-results" style="display: none"></div>
    `;
    document.dispatchEvent(new Event('DOMContentLoaded'));
});

function selectFiles(files) {
    const input = document.getElementById('files-input');
    Object.defineProperty(input, 'files', { configurable: true, value: files });
    input.dispatchEvent(new Event('change'));
}

function submitUpload() {
    document.getElementById('upload-files-form').dispatchEvent(new Event('submit', { cancelable: true }));
}

function submitCreate(name = 'Safe collection') {
    document.getElementById('collection-name').value = name;
    document.getElementById('create-collection-form').dispatchEvent(new Event('submit', { cancelable: true }));
}

function expectNoInjectedMarkup(root) {
    expect(root.querySelectorAll('img, script, svg, iframe')).toHaveLength(0);
    expect(root.querySelectorAll('[onerror], [onload], [onmouseover]')).toHaveLength(0);
}

describe('collection upload XSS surface', () => {
    it('renders hostile File names inert in selected-file and real upload-progress/result paths', async () => {
        const files = HOSTILE_FILE_NAMES.map((name) => new File(['content'], name, { type: 'text/plain' }));
        uploadResponse.body = JSON.stringify({
            success: true,
            uploaded: [
                { filename: HOSTILE_FILE_NAMES[0], status: 'uploaded' },
                { filename: HOSTILE_FILE_NAMES[1], status: '<svg onload=alert(5)>' },
            ],
            errors: [{ filename: HOSTILE_FILE_NAMES[1], error: HOSTILE_ERROR }],
        });

        selectFiles(files);
        const preview = document.getElementById('selected-files');
        expect(preview.querySelectorAll('li')).toHaveLength(2);
        expect(preview.textContent).toContain(HOSTILE_FILE_NAMES[0]);
        expect(preview.textContent).toContain(HOSTILE_FILE_NAMES[1]);
        expectNoInjectedMarkup(preview);

        submitUpload();
        const progress = document.getElementById('upload-progress');
        await vi.waitFor(() => expect(progress.querySelector('#file-progress-list')).not.toBeNull());
        expect(progress.textContent).toContain(HOSTILE_FILE_NAMES[0]);
        expectNoInjectedMarkup(progress);

        const results = document.getElementById('upload-results');
        await vi.waitFor(() => expect(results.style.display).toBe('block'));
        expect(results.textContent).toContain(HOSTILE_FILE_NAMES[0]);
        expect(results.textContent).toContain(HOSTILE_FILE_NAMES[1]);
        expect(results.textContent).toContain(HOSTILE_ERROR);
        expectNoInjectedMarkup(results);
    });

    it('renders a hostile server error message inert through the real XHR failure path', async () => {
        selectFiles([new File(['content'], 'safe.txt', { type: 'text/plain' })]);
        uploadResponse = { status: 500, body: HOSTILE_ERROR };

        submitUpload();
        const results = document.getElementById('upload-results');
        await vi.waitFor(() => expect(results.style.display).toBe('block'));
        expect(results.querySelector('.ldr-alert-danger')).not.toBeNull();
        expect(results.textContent).toContain(HOSTILE_ERROR);
        expectNoInjectedMarkup(results);
    });

    // Regression: collection_upload.js:782 must URI-encode COLLECTION_ID in the View Collection href.
    test('does not parse a hostile collection id as markup in the View Collection href', async () => {
        globalThis.COLLECTION_ID = '"><svg onload=alert(6)>';
        selectFiles([new File(['content'], 'safe.txt', { type: 'text/plain' })]);
        uploadResponse.body = JSON.stringify({
            success: true,
            uploaded: [{ filename: 'safe.txt', status: 'uploaded' }],
            errors: [],
        });

        submitUpload();
        const results = document.getElementById('upload-results');
        await vi.waitFor(() => expect(results.style.display).toBe('block'));
        expectNoInjectedMarkup(results);
    });
});

describe('collection create XSS surface', () => {
    it('renders hostile API collection id/name inert and URI-encodes both result links', async () => {
        const id = '"><svg onload=alert(7)>';
        createResponse = { success: true, collection: { id, name: '<img src=x onerror=alert(8)>' } };

        submitCreate('<script>alert(9)</script>');
        const results = document.getElementById('create-results');
        await vi.waitFor(() => expect(results.style.display).toBe('block'));
        expect(results.textContent).toContain(id);
        expect(results.textContent).not.toContain(createResponse.collection.name);
        const links = results.querySelectorAll('a');
        expect(links).toHaveLength(2);
        expect(links[0].getAttribute('href')).toBe(`/library/collections/${encodeURIComponent(id)}`);
        expect(links[1].getAttribute('href')).toBe(`/library/collections/${encodeURIComponent(id)}/upload`);
        expectNoInjectedMarkup(results);
    });

    it('renders hostile API error messages inert through the actual create submit handler', async () => {
        createResponse = { success: false, error: HOSTILE_ERROR };

        submitCreate('Safe collection');
        const results = document.getElementById('create-results');
        await vi.waitFor(() => expect(results.style.display).toBe('block'));
        expect(results.querySelector('.ldr-alert-danger')).not.toBeNull();
        expect(results.textContent).toContain(HOSTILE_ERROR);
        expectNoInjectedMarkup(results);
    });

    it('keeps benign file preview and successful View/Upload controls working', async () => {
        const fileName = 'meeting-notes.txt';
        selectFiles([new File(['notes'], fileName, { type: 'text/plain' })]);
        const preview = document.getElementById('selected-files');
        expect(preview.querySelectorAll('li')).toHaveLength(1);
        expect(preview.textContent).toContain(fileName);

        createResponse = { success: true, collection: { id: 'collection-42', name: 'Research notes' } };
        submitCreate('Research notes');
        const results = document.getElementById('create-results');
        await vi.waitFor(() => expect(results.style.display).toBe('block'));
        const links = results.querySelectorAll('a');
        expect(links).toHaveLength(2);
        expect(links[0].textContent).toContain('View Collection');
        expect(links[0].getAttribute('href')).toBe('/library/collections/collection-42');
        expect(links[1].textContent).toContain('Upload Files');
        expect(links[1].getAttribute('href')).toBe('/library/collections/collection-42/upload');
    });
});