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.

22.8 KB
Raw Blame
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';

const pageMarkup = `
    <div class="ldr-page-header"></div>
    <input id="subscription-search" />
    <select id="status-filter"></select>
    <select id="frequency-filter"></select>
    <button id="add-folder-btn"></button>
    <button id="create-subscription-btn"></button>
    <div id="subscriptions-grid"></div>
    <span id="total-subscriptions"></span>
    <span id="active-subscriptions"></span>
    <span id="paused-subscriptions"></span>
    <span id="updates-today"></span>
    <span id="status-indicator"></span>
    <span id="status-text"></span>
    <div id="scheduler-details"></div>
    <nav class="ldr-folder-list">
        <div class="ldr-folder-item" data-folder="all">All <span class="ldr-folder-count">0</span></div>
        <div class="ldr-folder-item" data-folder="uncategorized">Uncategorized <span class="ldr-folder-count">0</span></div>
    </nav>
`;

function subscription(overrides = {}) {
    return {
        id: 'subscription-1',
        name: 'AI news',
        query: 'latest AI research',
        is_active: true,
        refresh_minutes: 60,
        total_runs: 5,
        last_refreshed: null,
        next_refresh: null,
        folder_id: null,
        source_id: null,
        ...overrides,
    };
}

function response(data) {
    return Promise.resolve({
        ok: true,
        json: () => Promise.resolve(data),
    });
}

async function renderThroughLoadPath(subscriptions, folders = []) {
    // eslint-disable-next-line no-unsanitized/property -- test fixture: pageMarkup is a hardcoded static template with no interpolated values.
    document.body.innerHTML = pageMarkup;
    globalThis.fetch = vi.fn((url) => {
        switch (url) {
            case '/news/api/subscriptions/current':
                return response({ subscriptions });
            case '/news/api/subscription/folders':
                return response(folders);
            case '/news/api/scheduler/status':
                return response({ scheduler_available: false });
            default:
                return Promise.reject(new Error(`Unexpected fetch: ${url}`));
        }
    });

    document.dispatchEvent(new Event('DOMContentLoaded'));
    const grid = document.getElementById('subscriptions-grid');
    await vi.waitFor(() => {
        expect(grid.querySelectorAll('.ldr-subscription-card')).toHaveLength(subscriptions.length);
    });
    if (folders.length > 0) {
        await vi.waitFor(() => {
            expect(document.querySelectorAll(`.ldr-folder-item[data-folder="${folders[0].id}"]`)).toHaveLength(1);
        });
        document.getElementById('subscription-search').dispatchEvent(new Event('input'));
    }
    return grid;
}

async function renderHistoryModal(historyData, subscriptionOverrides = {}) {
    const historySubscription = subscription({ source_id: 'source-1', ...subscriptionOverrides });
    // eslint-disable-next-line no-unsanitized/property -- test fixture: pageMarkup is a hardcoded static template with no interpolated values.
    document.body.innerHTML = pageMarkup;
    globalThis.fetch = vi.fn((url) => {
        switch (url) {
            case '/news/api/subscriptions/current':
                return response({ subscriptions: [historySubscription] });
            case '/news/api/subscription/folders':
                return response([]);
            case '/news/api/scheduler/status':
                return response({ scheduler_available: false });
            case `/news/api/subscriptions/${encodeURIComponent(historySubscription.id)}/history`:
                return response(historyData);
            default:
                return Promise.reject(new Error(`Unexpected fetch: ${url}`));
        }
    });

    document.dispatchEvent(new Event('DOMContentLoaded'));
    const grid = document.getElementById('subscriptions-grid');
    await vi.waitFor(() => {
        expect(grid.querySelectorAll('.ldr-subscription-card')).toHaveLength(1);
    });

    grid.querySelector('.ldr-subscription-card').click();
    await vi.waitFor(() => {
        expect(document.getElementById('historyModal')).not.toBeNull();
    });
    return document.getElementById('historyModal');
}

function historyRunsText(modal) {
    const line = Array.from(modal.querySelectorAll('.ldr-history-summary p'))
        .find((p) => p.querySelector('strong')?.textContent === 'Total research runs:');
    return line?.textContent;
}

beforeAll(async () => {
    window.LdrAlertHelpers = { mapAlertType: (type) => type };
    globalThis.bootstrap = {
        Modal: vi.fn(function() {
            this.show = () => {};
        }),
    };
    await import('@js/security/xss-protection.js');
    await import('@js/pages/subscriptions.js');
});

describe('subscriptions adversarial rendering', () => {
    afterEach(() => {
        vi.unstubAllGlobals();
    });

    it('renders benign subscription fields through the real load path', async () => {
        const grid = await renderThroughLoadPath([subscription()]);
        const card = grid.querySelector('.ldr-subscription-card');

        expect(card.dataset.subscriptionId).toBe('subscription-1');
        expect(card.querySelector('h4').textContent).toBe('AI news');
        expect(card.querySelector('.ldr-frequency-badge').textContent).toBe('Hourly');
        expect(card.querySelector('.ldr-subscription-stats').textContent).toContain('5 research runs');
    });

    it('escapes hostile names instead of creating header markup', async () => {
        const name = '<img src=x onerror=alert(1)><script>alert(2)</script>';
        const grid = await renderThroughLoadPath([subscription({ name })]);
        const heading = grid.querySelector('.ldr-card-header h4');

        expect(heading.querySelector('img, script')).toBeNull();
        expect(heading.textContent).toBe(`${name.substring(0, 50)}...`);
        expect(heading.getAttribute('title')).toBe(name);
    });

    it('escapes hostile queries instead of creating query markup', async () => {
        const query = '<img src=x onerror=alert(1)><script>alert(2)</script>';
        const grid = await renderThroughLoadPath([subscription({ query })]);
        const queryElement = grid.querySelector('.ldr-query-text');

        expect(queryElement.querySelector('img, script')).toBeNull();
        expect(queryElement.textContent).toBe(query);
        expect(queryElement.getAttribute('title')).toBe(query);
    });

    it('escapes hostile folder names in both the card badge and sidebar', async () => {
        const folder = { id: 'folder-1', name: '<img src=x onerror=alert(1)>' };
        const grid = await renderThroughLoadPath([subscription({ folder_id: folder.id })], [folder]);
        const badge = grid.querySelector('.ldr-folder-badge');
        const sidebarFolder = document.querySelector('.ldr-folder-item[data-folder="folder-1"]');

        expect(badge.querySelector('img')).toBeNull();
        expect(badge.textContent).toBe(folder.name);
        expect(sidebarFolder.querySelector('img')).toBeNull();
        expect(sidebarFolder.textContent).toContain(folder.name);
    });

    it('percent-encodes hostile source IDs in progress links', async () => {
        const sourceId = '"><script>alert(1)</script>';
        const grid = await renderThroughLoadPath([subscription({ source_id: sourceId })]);
        const link = grid.querySelector('.ldr-source-link');

        expect(link.getAttribute('href')).toBe(`/progress/${encodeURIComponent(sourceId)}`);
        expect(grid.innerHTML).not.toContain('<script>');
    });

    it('keeps hostile IDs within the data attribute HTML context', async () => {
        const id = '"><img src=x onerror=alert(1)>';
        const grid = await renderThroughLoadPath([subscription({ id })]);
        const card = grid.querySelector('.ldr-subscription-card');

        expect(card.dataset.subscriptionId).toBe(id);
        expect(card.querySelectorAll('img')).toHaveLength(0);
    });

    // FIXED (was subscriptions.js:210,214,217,220,226): card actions no
    // longer interpolate IDs into inline onclick JavaScript strings. The
    // card and its buttons name their action in data-subscription-action,
    // and one listener on the grid reads the ID from data-subscription-id,
    // so there is no handler string for a hostile ID to break out of.
    // happy-dom never runs inline handlers, so the [onclick] count and the
    // request below carry this test.
    it('blocks hostile IDs from executing through card action handlers', async () => {
        const id = "subscription-');window.__subscription_onclick_xss=1;//";
        const grid = await renderThroughLoadPath([subscription({ id })]);
        const card = grid.querySelector('.ldr-subscription-card');

        expect(card.getAttribute('onclick')).toBeNull();
        expect(grid.querySelectorAll('[onclick]')).toHaveLength(0);
        expect(card.dataset.subscriptionId).toBe(id);

        card.click();
        await vi.waitFor(() => {
            expect(globalThis.fetch).toHaveBeenCalledWith(
                `/news/api/subscriptions/${encodeURIComponent(id)}/history`,
                expect.anything()
            );
        });
    });

    it('percent-encodes a hostile ID into one segment of each request path', async () => {
        vi.stubGlobal('confirm', vi.fn(() => true));
        const id = '../../api/v1/settings?reset=1#top';
        const grid = await renderThroughLoadPath([subscription({ id })]);
        const encodedPath = '/news/api/subscriptions/..%2F..%2Fapi%2Fv1%2Fsettings%3Freset%3D1%23top';

        for (const action of ['toggle', 'history', 'delete']) {
            grid.querySelector(`button[data-subscription-action="${action}"]`).click();
        }

        const requests = globalThis.fetch.mock.calls
            .map(([url, options = {}]) => [url, options.method || 'GET'])
            .filter(([url]) => url.startsWith('/news/api/subscriptions/..'));
        expect(requests).toEqual([
            [encodedPath, 'PUT'],
            [`${encodedPath}/history`, 'GET'],
            [encodedPath, 'DELETE'],
        ]);
        // The ID can neither climb out of /news/api/subscriptions/ nor add a
        // query string or fragment.
        for (const [url] of requests) {
            const parsed = new URL(url, 'http://localhost');
            expect(parsed.pathname.startsWith(encodedPath)).toBe(true);
            expect(parsed.search).toBe('');
            expect(parsed.hash).toBe('');
        }
    });

    // FIXED (was subscriptions.js:178-196,235): refresh_minutes is coerced
    // to an integer before innerHTML interpolation; hostile values render
    // as a safe fallback label instead of markup.
    it('does not materialize markup from hostile refresh_minutes values', async () => {
        const refreshMinutes = '<img src=x onerror=alert(1)>';
        const grid = await renderThroughLoadPath([subscription({ refresh_minutes: refreshMinutes })]);
        const badge = grid.querySelector('.ldr-frequency-badge');

        expect(badge.querySelector('img')).toBeNull();
        expect(badge.textContent).toBe('Custom schedule');
    });

    // FIXED (was subscriptions.js:241): total_runs is coerced to an
    // integer (default 0) everywhere it is interpolated, so a hostile API
    // value can never become live DOM.
    it('does not materialize markup from hostile total_runs values', async () => {
        const totalRuns = '<img src=x onerror=alert(1)>';
        const grid = await renderThroughLoadPath([subscription({ total_runs: totalRuns })]);
        const stats = grid.querySelector('.ldr-subscription-stats');

        expect(stats.querySelector('img')).toBeNull();
        expect(stats.textContent).toContain('0 research runs');
    });

    // Guard: the secure structure keeps benign controls functional:
    // data-subscription-action buttons exist, no inline onclick remains, the
    // edit link stays a real navigable anchor, and delegated clicks still act.
    it('keeps benign cards functional through delegated data-subscription-action controls', async () => {
        const grid = await renderThroughLoadPath([subscription()]);
        const card = grid.querySelector('.ldr-subscription-card');

        expect(grid.querySelectorAll('[onclick]')).toHaveLength(0);
        const actions = Array.from(
            card.querySelectorAll('[data-subscription-action]'),
            (el) => el.dataset.subscriptionAction,
        );
        expect(actions).toEqual(expect.arrayContaining(['run', 'toggle', 'history', 'delete']));

        const edit = card.querySelector('a[href="/news/subscriptions/subscription-1/edit"]');
        expect(edit).not.toBeNull();

        // Edit is a normal anchor: delegation must ignore a click on its
        // icon (no fetch).
        globalThis.fetch.mockClear();
        edit.querySelector('i').click();
        expect(globalThis.fetch).not.toHaveBeenCalled();

        // Action buttons route through delegation using data-subscription-id.
        card.querySelector('[data-subscription-action="run"]').click();
        await vi.waitFor(() => {
            expect(globalThis.fetch).toHaveBeenCalledWith('/news/api/subscriptions/subscription-1/run', expect.anything());
        });
    });

    // happy-dom never loads images, so no onerror handler could run here
    // either way; the img/script counts carry this test.
    it('keeps hostile history data inert through the real history-modal path', async () => {
        const researchId = '"><script>alert(1)</script>';
        const modal = await renderHistoryModal({
            total_runs: 1,
            history: [{
                headline: '<img src=x onerror=alert(1)>',
                status: '<img src=x onerror=alert(1)>',
                duration_seconds: '<img src=x onerror=alert(1)>',
                topics: ['<img src=x onerror=alert(1)>'],
                research_id: researchId,
                created_at: '2026-01-01T00:00:00Z',
            }],
        });

        expect(globalThis.bootstrap.Modal).toHaveBeenCalled();
        expect(modal.querySelectorAll('img, script')).toHaveLength(0);
        expect(modal.querySelector('.ldr-duration')).toBeNull();

        const historyLink = modal.querySelector('.ldr-history-item-header a');
        expect(historyLink.getAttribute('href')).toBe(`/progress/${encodeURIComponent(researchId)}`);
        expect(historyLink.getAttribute('rel')).toContain('noopener');
        expect(historyLink.getAttribute('rel')).toContain('noreferrer');

        const sourceLink = modal.querySelector('.ldr-history-summary a');
        expect(sourceLink.getAttribute('rel')).toContain('noopener');
        expect(sourceLink.getAttribute('rel')).toContain('noreferrer');
        expect(modal.querySelector('.ldr-status-badge').querySelector('img')).toBeNull();
        expect(modal.querySelector('.ldr-topic-badge').querySelector('img')).toBeNull();
    });

    // FIXED: the history modal coerces total_runs to an integer (default 0),
    // as the card does, so a hostile API value can never become live DOM.
    it('does not materialize markup from a hostile history total_runs value', async () => {
        const modal = await renderHistoryModal({
            total_runs: '<img src=x onerror=alert(1)>',
            history: [],
        });

        expect(modal.querySelectorAll('img, script')).toHaveLength(0);
        expect(historyRunsText(modal)).toBe('Total research runs: 0');
    });

    it.each([
        ['number', 7, 'Total research runs: 7'],
        ['numeric string', '12', 'Total research runs: 12'],
    ])('shows a %s history total_runs value as the count', async (_label, totalRuns, expected) => {
        const modal = await renderHistoryModal({ total_runs: totalRuns, history: [] });

        expect(historyRunsText(modal)).toBe(expected);
    });

    it('renders benign numeric history durations', async () => {
        const modal = await renderHistoryModal({
            total_runs: 1,
            history: [{
                headline: 'Benign headline',
                status: 'completed',
                duration_seconds: 42,
                topics: [],
                research_id: 'research-1',
                created_at: '2026-01-01T00:00:00Z',
            }],
        });

        expect(modal.querySelector('.ldr-duration').textContent).toBe('42s');
    });

    it.each([
        ['null', null],
        ['empty string', ''],
        ['whitespace string', '   '],
        ['NaN', NaN],
        ['positive infinity', Infinity],
        ['negative infinity', -Infinity],
        ['undefined', undefined],
    ])('omits %s history durations', async (_label, durationSeconds) => {
        const modal = await renderHistoryModal({
            total_runs: 1,
            history: [{
                headline: 'No duration yet',
                status: 'running',
                duration_seconds: durationSeconds,
                topics: [],
                research_id: 'research-no-duration',
                created_at: '2026-01-01T00:00:00Z',
            }],
        });

        expect(modal.querySelector('.ldr-duration')).toBeNull();
    });

    it.each([
        ['numeric zero', 0],
        ['numeric-string zero', '0'],
    ])('renders intentional %s history durations', async (_label, durationSeconds) => {
        const modal = await renderHistoryModal({
            total_runs: 1,
            history: [{
                headline: 'Immediate result',
                status: 'completed',
                duration_seconds: durationSeconds,
                topics: [],
                research_id: 'research-zero-duration',
                created_at: '2026-01-01T00:00:00Z',
            }],
        });

        expect(modal.querySelector('.ldr-duration').textContent).toBe('0s');
    });
});

// showAlert renders its message as sanitized HTML, which keeps links and
// headings. The page escapes the subscription query, folder names and server
// error text before it composes a message, so they show as text.
describe('subscription alerts', () => {
    const HOSTILE_MARKUP =
        '<a href="https://attacker.example/">Claim your reward</a><h1>Account locked</h1>';
    let purify;

    beforeEach(() => {
        // DOMPurify's DOM walk does not run under happy-dom (see
        // xss-protection-dompurify-runtime.test.js), so this stand-in returns
        // the message unchanged. For the markup used here, the real sanitizer
        // returns it unchanged too: links (a href) and headings are on
        // SANITIZE_CONFIG's allowlist.
        purify = {
            isSupported: true,
            addHook: vi.fn(),
            sanitize: vi.fn((dirty) => String(dirty)),
        };
        globalThis.DOMPurify = purify;
        window.RESEARCH_STATUS = { QUEUED: 'queued' };
        vi.stubGlobal('confirm', vi.fn(() => true));
        vi.stubGlobal('prompt', vi.fn(() => 'Reading list'));
    });

    afterEach(() => {
        delete globalThis.DOMPurify;
        delete window.RESEARCH_STATUS;
        vi.unstubAllGlobals();
    });

    function stubResponse(payload, ok = true, status = ok ? 200 : 500) {
        return {
            ok,
            status,
            statusText: ok ? 'OK' : 'Request failed',
            json: () => Promise.resolve(payload),
        };
    }

    // Answer one request with a stub; every other request keeps the load
    // path's responses.
    function overrideFetch(matches, stub) {
        const loadPathFetch = globalThis.fetch;
        globalThis.fetch = vi.fn((url, options = {}) => (
            matches(url, options) ? Promise.resolve(stub) : loadPathFetch(url, options)
        ));
    }

    async function alertOfType(type) {
        await vi.waitFor(() => {
            expect(document.querySelector(`.alert-${type}`)).not.toBeNull();
        });
        return document.querySelector(`.alert-${type}`);
    }

    it('asks DOMPurify to keep template-like text and still strip data-* attributes', async () => {
        await renderThroughLoadPath([subscription()]);

        window.showSchedulerInfo();

        expect(purify.sanitize).toHaveBeenCalled();
        const [, config] = purify.sanitize.mock.calls.at(-1);
        expect(config.SAFE_FOR_TEMPLATES).toBe(false);
        expect(config.ALLOW_DATA_ATTR).toBe(false);
        expect(config.FORBID_TAGS).toEqual(expect.arrayContaining(['script', 'button']));
        // The page's own HTML message still renders as markup.
        const alert = await alertOfType('info');
        expect(alert.querySelector('h6').textContent).toBe('About the Subscription Scheduler');
        expect(alert.querySelectorAll('li')).toHaveLength(5);
    });

    it('shows a hostile query in the Run alert as text', async () => {
        const grid = await renderThroughLoadPath([subscription({ query: HOSTILE_MARKUP })]);

        grid.querySelector('[data-subscription-action="run"]').click();

        const alert = await alertOfType('info');
        expect(alert.querySelector('a, h1')).toBeNull();
        expect(alert.textContent).toContain(`Starting research for: ${HOSTILE_MARKUP}`);
    });

    it('shows a hostile folder name in the created-folder alert as text', async () => {
        await renderThroughLoadPath([subscription()]);
        overrideFetch(
            (url, options) => url === '/news/api/subscription/folders' && options.method === 'POST',
            stubResponse({ id: 'folder-hostile', name: HOSTILE_MARKUP }),
        );

        document.getElementById('add-folder-btn').click();

        const alert = await alertOfType('success');
        expect(alert.querySelector('a, h1')).toBeNull();
        expect(alert.textContent).toContain(`Folder "${HOSTILE_MARKUP}" created successfully!`);
    });

    it.each([
        [
            'a rejected Run',
            (grid) => grid.querySelector('[data-subscription-action="run"]'),
            (url) => url === '/news/api/subscriptions/subscription-1/run',
            stubResponse({ detail: HOSTILE_MARKUP }, false, 503),
        ],
        [
            'a Run the server did not start',
            (grid) => grid.querySelector('[data-subscription-action="run"]'),
            (url) => url === '/news/api/subscriptions/subscription-1/run',
            stubResponse({ status: 'error', message: HOSTILE_MARKUP }),
        ],
        [
            'a failed Delete',
            (grid) => grid.querySelector('[data-subscription-action="delete"]'),
            (_url, options) => options.method === 'DELETE',
            stubResponse({ error: `Subscription not found: ${HOSTILE_MARKUP}` }, false, 404),
        ],
        [
            'a failed folder create',
            () => document.getElementById('add-folder-btn'),
            (url, options) => url === '/news/api/subscription/folders' && options.method === 'POST',
            stubResponse({ error: HOSTILE_MARKUP }, false, 500),
        ],
    ])('shows the server text of %s as text', async (_label, control, matches, stub) => {
        const grid = await renderThroughLoadPath([subscription()]);
        overrideFetch(matches, stub);

        control(grid).click();

        const alert = await alertOfType('error');
        expect(alert.querySelector('a, h1')).toBeNull();
        expect(alert.textContent).toContain(HOSTILE_MARKUP);
    });
});