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.

21.6 KB
Raw Blame
/**
 * XSS regression coverage for:
 *
 *  - components/chat.js step-accordion header sinks:
 *      :1315  ensureLiveAccordion()  header.innerHTML  (static literals only)
 *      :1447  wrapStepsInAccordion() header.innerHTML  (static literals + integer .length)
 *    Step row content itself flows through createStepElement -> textContent
 *    (:1287), and the live count through label.textContent (:1344).
 *  - the DOM-API builders flagged as structurally safe:
 *      :1216  _appendResearchLink()  (setAttribute('href', encodeURIComponent))
 *      :2155  _setBtnLabel()         (replaceChildren + createTextNode)
 *  - chat session metadata title path (updateTitle -> chatTitle.textContent).
 *    The renderMarkdown innerHTML path (:1005) is covered by
 *    markdown-render-xss.test.js and is intentionally not re-tested here.
 *  - deletion/delete_manager.js's single innerHTML sink (:554,
 *    showNotification bootstrap toast) whose `message` argument can carry
 *    server-echoed error text (`result.error` -> error.message).
 *
 * Every hostile-payload test drives a real production path (socket progress
 * events, session reload, delete API responses) and asserts inertness:
 * zero script/img/svg/iframe descendants, zero on* attributes, and the
 * payload surviving as visible text.
 */
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';

const SESSION_ID = 'sess-1';
const RESEARCH_ID = 'research-1';

/** Hostile payloads covering element injection and attribute quote-breakout. */
const IMG_PAYLOAD = '<img src=x onerror=alert(1)>';
const SVG_QUOTE_PAYLOAD = '"><svg onload=alert(2)>';

function buildChatDom() {
    // Static literal (no interpolation) so no-unsanitized/property is
    // satisfied; SESSION_ID must match this meta content.
    document.head.innerHTML = `
        <meta name="chat-session-id" content="sess-1">
    `;
    document.body.innerHTML = `
        <div id="chat-welcome"></div>
        <textarea id="chat-input"></textarea>
        <button id="send-btn"></button>
        <div id="chat-title"></div>
        <div id="chat-messages" role="log"></div>
        <template id="thinking-template">
            <div class="ldr-chat-message ldr-chat-message-assistant ldr-chat-message-thinking" role="status">
                <div class="ldr-chat-message-avatar"><i class="fas fa-robot"></i></div>
                <div class="ldr-chat-message-content">
                    <div class="ldr-chat-thinking-text" hidden></div>
                    <div class="ldr-chat-thinking-dots"><span></span><span></span><span></span></div>
                </div>
            </div>
        </template>
        <template id="message-template">
            <div class="ldr-chat-message">
                <div class="ldr-chat-message-avatar"><i class="fas fa-user"></i></div>
                <div class="ldr-chat-message-content">
                    <div class="ldr-chat-message-text"></div>
                    <div class="ldr-chat-message-meta"><span class="ldr-chat-message-time"></span></div>
                </div>
            </div>
        </template>
    `;
}

const json = (payload) => ({
    ok: true,
    status: 200,
    json: async () => payload,
});

const flush = async (n = 10) => {
    for (let i = 0; i < n; i++) await Promise.resolve();
};

/** Install the window stubs chat.js needs; returns the captured progress cb. */
function stubChatGlobals({ inProgressResearchId }) {
    let progressCb = null;
    window.api = { getCsrfToken: () => '' };
    // Mirrors production's DOMPurify-backed renderer only for BENIGN content
    // in these tests; hostile payloads never travel the renderMarkdown path
    // here (that sink is covered by markdown-render-xss.test.js).
    window.ui = { renderMarkdown: (t) => t };
    window.socket = {
        subscribeToResearch: vi.fn((id, cb) => {
            progressCb = cb;
        }),
        unsubscribeFromResearch: vi.fn(),
        getSocketInstance: vi.fn(() => ({
            on: vi.fn(),
            off: vi.fn(),
        })),
    };
    return { getProgressCb: () => progressCb, inProgressResearchId };
}

/** Standard session-restore fetch mock; messages override via arguments. */
function stubChatFetch({ messages, session }) {
    globalThis.fetch = vi.fn(async (url) => {
        const u = String(url);
        if (u.includes(`/api/chat/sessions/${SESSION_ID}/messages`)) {
            return json({
                success: true,
                messages,
                has_more: false,
                in_progress_research_id: null,
            });
        }
        if (u.includes(`/api/chat/sessions/${SESSION_ID}`)) {
            return json({ success: true, session });
        }
        return json({ success: true, sessions: [] });
    });
}

/** Shared inertness assertion: no active content, no event-handler attrs. */
function expectInert(root) {
    expect(root).not.toBeNull();
    expect(root.querySelectorAll('img, script, svg, iframe, object, embed').length).toBe(0);
    for (const element of root.querySelectorAll('*')) {
        expect(
            [...element.attributes].some(({ name }) => name.toLowerCase().startsWith('on')),
        ).toBe(false);
    }
}

async function loadChatModule() {
    await import('@js/components/chat.js');
    await flush();
}

// =============================================================================
// chat.js — live accordion header sink (ensureLiveAccordion :1315)
// =============================================================================

describe('chat.js live steps header (ensureLiveAccordion :1315)', () => {
    let getProgressCb;

    beforeEach(async () => {
        vi.resetModules();
        buildChatDom();
        const stubs = stubChatGlobals({ inProgressResearchId: RESEARCH_ID });
        getProgressCb = stubs.getProgressCb;
        stubChatFetch({
            messages: [
                {
                    id: 'm1',
                    role: 'user',
                    message_type: 'chat',
                    content: 'hello',
                    created_at: '2026-07-11T00:00:00Z',
                },
            ],
            session: { id: SESSION_ID, title: 'Test session' },
        });
        // Rewire the in-progress research id the harness reports.
        globalThis.fetch = vi.fn(async (url) => {
            const u = String(url);
            if (u.includes(`/api/chat/sessions/${SESSION_ID}/messages`)) {
                return json({
                    success: true,
                    messages: [
                        {
                            id: 'm1',
                            role: 'user',
                            message_type: 'chat',
                            content: 'hello',
                            created_at: '2026-07-11T00:00:00Z',
                        },
                    ],
                    has_more: false,
                    in_progress_research_id: RESEARCH_ID,
                });
            }
            if (u.includes(`/api/chat/sessions/${SESSION_ID}`)) {
                return json({
                    success: true,
                    session: { id: SESSION_ID, title: 'Test session' },
                });
            }
            return json({ success: true, sessions: [] });
        });

        await loadChatModule();
        expect(getProgressCb()).toBeTypeOf('function');
    });

    afterEach(() => {
        delete globalThis.fetch;
    });

    it('renders hostile live step message/content inertly and keeps the header static', () => {
        getProgressCb()({
            research_id: RESEARCH_ID,
            phase: 'observation',
            message: `From the web (SearXNG): ${IMG_PAYLOAD}`,
            content: SVG_QUOTE_PAYLOAD,
            progress: 42,
        });

        const group = document.querySelector('.ldr-chat-steps-group[data-live="true"]');
        expectInert(group);

        // Header markup is the static template: chevron + flask icons and a
        // label span — nothing derived from the step text.
        const header = group.querySelector('.ldr-chat-steps-header');
        expect(header.querySelectorAll(':scope > *').length).toBe(3);
        expect(header.querySelector('.ldr-chat-steps-chevron')).not.toBeNull();
        // Live count is maintained via textContent (:1344), not innerHTML.
        expect(header.querySelector('.ldr-chat-steps-label').textContent)
            .toBe('Research Steps (1)');

        // Hostile payloads survive as visible text in the step row.
        const stepText = group.querySelector('.ldr-chat-message-step .ldr-chat-message-text');
        expect(stepText.textContent).toContain(IMG_PAYLOAD);
        expect(stepText.textContent).toContain(SVG_QUOTE_PAYLOAD);
    });

    it('renders a benign live step with its text and count', () => {
        getProgressCb()({
            research_id: RESEARCH_ID,
            phase: 'search',
            message: 'Searching arXiv for "quantum computing"',
            progress: 10,
        });

        const group = document.querySelector('.ldr-chat-steps-group[data-live="true"]');
        const stepText = group.querySelector('.ldr-chat-message-step .ldr-chat-message-text');
        expect(stepText.textContent).toContain('Searching arXiv for "quantum computing"');
        expect(
            group.querySelector('.ldr-chat-steps-label').textContent,
        ).toBe('Research Steps (1)');
    });
});

// =============================================================================
// chat.js — reloaded steps header sink (wrapStepsInAccordion :1447)
// =============================================================================

describe('chat.js reloaded steps header (wrapStepsInAccordion :1447)', () => {
    beforeEach(async () => {
        vi.resetModules();
        buildChatDom();
        stubChatGlobals({ inProgressResearchId: null });
        stubChatFetch({
            messages: [
                {
                    id: 'm1',
                    role: 'user',
                    message_type: 'chat',
                    content: 'hello',
                    created_at: '2026-07-11T00:00:00Z',
                },
                {
                    id: 'step-1',
                    role: 'assistant',
                    message_type: 'step',
                    phase: 'observation',
                    content: `From PubMed: ${IMG_PAYLOAD}`,
                    created_at: '2026-07-11T00:00:01Z',
                },
                {
                    id: 'step-2',
                    role: 'assistant',
                    message_type: 'step',
                    phase: 'search',
                    content: SVG_QUOTE_PAYLOAD,
                    created_at: '2026-07-11T00:00:02Z',
                },
            ],
            session: { id: SESSION_ID, title: 'Test session' },
        });
        await loadChatModule();
    });

    afterEach(() => {
        delete globalThis.fetch;
    });

    it('wraps hostile persisted steps in an accordion whose header only carries the integer count', () => {
        const groups = document.querySelectorAll(
            '#chat-messages > .ldr-chat-steps-group',
        );
        expect(groups.length).toBe(1);
        const group = groups[0];

        expectInert(group);

        // The :1447 sink's only dynamic operand is stepElements.length —
        // rendered as the integer 2, never step content.
        const label = group.querySelector('.ldr-chat-steps-label');
        expect(label.textContent).toBe('Research Steps (2)');

        const header = group.querySelector('.ldr-chat-steps-header');
        expect(header.querySelectorAll(':scope > *').length).toBe(3);

        // Persisted hostile step content stays text.
        const stepTexts = [
            ...group.querySelectorAll('.ldr-chat-message-step .ldr-chat-message-text'),
        ];
        expect(stepTexts.length).toBe(2);
        expect(stepTexts[0].textContent).toContain(IMG_PAYLOAD);
        expect(stepTexts[1].textContent).toContain(SVG_QUOTE_PAYLOAD);
    });
});

// =============================================================================
// chat.js — session metadata (title) and research link (:1216)
// =============================================================================

describe('chat.js session metadata and research link', () => {
    beforeEach(async () => {
        vi.resetModules();
        buildChatDom();
        stubChatGlobals({ inProgressResearchId: null });
    });

    afterEach(() => {
        delete globalThis.fetch;
    });

    it('renders a hostile session title as text in #chat-title', async () => {
        const hostileTitle = `Session ${IMG_PAYLOAD}`;
        stubChatFetch({
            messages: [],
            session: { id: SESSION_ID, title: hostileTitle },
        });
        await loadChatModule();

        const chatTitle = document.getElementById('chat-title');
        expect(chatTitle.textContent).toBe(hostileTitle);
        expect(chatTitle.children.length).toBe(0);
        expect(document.querySelectorAll('#chat-title img, #chat-title svg').length).toBe(0);
    });

    it('encodes a hostile research_id into the view-research link href (:1216)', async () => {
        const hostileResearchId = `"><svg onload=alert(1)>`;
        stubChatFetch({
            messages: [
                {
                    id: 'a1',
                    role: 'user',
                    message_type: 'chat',
                    content: 'query',
                    created_at: '2026-07-11T00:00:00Z',
                },
                {
                    id: 'a2',
                    role: 'assistant',
                    message_type: 'chat',
                    content: 'Done',
                    research_id: hostileResearchId,
                    created_at: '2026-07-11T00:00:01Z',
                },
            ],
            session: { id: SESSION_ID, title: 'T' },
        });
        await loadChatModule();

        const anchor = document.querySelector('.ldr-chat-research-link-btn');
        expect(anchor).not.toBeNull();
        // URLBuilder is absent in tests, so the encoded fallback path runs.
        expect(anchor.getAttribute('href')).toBe(
            `/results/${encodeURIComponent(hostileResearchId)}`,
        );
        expectInert(anchor.closest('.ldr-chat-research-link'));
        // Built via DOM APIs: exactly one icon element plus a text node —
        // no injected children from the id.
        expect(anchor.children.length).toBe(1);
        expect(anchor.textContent).toBe(' View full research');
    });

    it('renders the research link normally for a benign research_id', async () => {
        stubChatFetch({
            messages: [
                {
                    id: 'a2',
                    role: 'assistant',
                    message_type: 'chat',
                    content: 'Done',
                    research_id: 'res-123',
                    created_at: '2026-07-11T00:00:01Z',
                },
            ],
            session: { id: SESSION_ID, title: 'T' },
        });
        await loadChatModule();

        const anchor = document.querySelector('.ldr-chat-research-link-btn');
        expect(anchor).not.toBeNull();
        expect(anchor.getAttribute('href')).toBe('/results/res-123');
        expect(anchor.textContent).toBe(' View full research');
    });
});

// =============================================================================
// chat.js — structural lock on the sinks (source-level)
// =============================================================================

describe('chat.js sink structure (source-level lock)', () => {
    const chatSource = fs.readFileSync(
        path.resolve(
            path.dirname(fileURLToPath(import.meta.url)),
            '../../../src/local_deep_research/web/static/js/components/chat.js',
        ),
        'utf8',
    );

    it('keeps both header.innerHTML templates free of string interpolation', () => {
        const sinks = [];
        const sinkRe = /header\.innerHTML\s*=/g;
        for (let m = sinkRe.exec(chatSource); m !== null; m = sinkRe.exec(chatSource)) {
            const end = chatSource.indexOf(';', m.index + m[0].length);
            sinks.push(chatSource.slice(m.index + m[0].length, end));
        }
        // :1315 (ensureLiveAccordion) and :1447 (wrapStepsInAccordion).
        expect(sinks).toHaveLength(2);
        for (const body of sinks) {
            expect(body.includes('${')).toBe(false);
        }
        // Strip string literals: whatever remains is the dynamic part.
        const strip = (body) => body.replace(/'[^']*'/g, '').replace(/[\s+]/g, '');
        expect(strip(sinks[0])).toBe('');
        expect(strip(sinks[1])).toBe('stepElements.length');
    });

    it('builds the research link and stop-button label without innerHTML', () => {
        const nextFunction = (from) => chatSource.indexOf('\n    function ', from);
        const linkStart = chatSource.indexOf('function _appendResearchLink');
        expect(linkStart).toBeGreaterThan(-1);
        const linkBody = chatSource.slice(linkStart, nextFunction(linkStart));
        expect(/\.innerHTML\s*=/.test(linkBody)).toBe(false);
        expect(linkBody.includes("setAttribute('href'")).toBe(true);
        expect(linkBody.includes('encodeURIComponent')).toBe(true);

        const labelStart = chatSource.indexOf('const _setBtnLabel');
        expect(labelStart).toBeGreaterThan(-1);
        const labelEnd = chatSource.indexOf('\n        };', labelStart);
        const labelBody = chatSource.slice(labelStart, labelEnd);
        expect(/\.innerHTML\s*=/.test(labelBody)).toBe(false);
        expect(labelBody.includes('createTextNode')).toBe(true);
    });
});

// =============================================================================
// delete_manager.js — showNotification toast innerHTML sink (:554)
// =============================================================================

describe('delete_manager.js showNotification toast (:554)', () => {
    let DM;

    beforeAll(async () => {
        // Production globals: the real escaper and alert-type mapper.
        await import('@js/security/xss-protection.js');
        await import('@js/utils/alert-helpers.js');
        window.api = { fetchWithErrorHandling: vi.fn(), getCsrfToken: () => 'test-token' };
        // Toast branch requires bootstrap.Toast; show() is a no-op stub.
        globalThis.bootstrap = {
            Toast: class {
                show() {}
            },
        };
        window.DeleteConfirmation = { confirmAndRun: vi.fn() };
        await import('@js/deletion/delete_manager.js');
        DM = window.DeleteManager;
    });

    beforeEach(() => {
        window.api.fetchWithErrorHandling.mockReset();
        window.DeleteConfirmation.confirmAndRun.mockReset();
        document.querySelectorAll('.ldr-toast-container').forEach((c) => c.remove());
    });

    const flushPromises = () => new Promise((resolve) => setTimeout(resolve, 0));

    function renderedToasts() {
        return document.querySelectorAll('.ldr-toast-container .toast');
    }

    it('renders a server-echoed hostile error message inertly via the real delete path', async () => {
        const onError = vi.fn();
        window.api.fetchWithErrorHandling.mockResolvedValueOnce({
            success: false,
            error: IMG_PAYLOAD,
        });

        await DM.deleteDocument('doc-1', { skipConfirm: true, onError });
        await flushPromises();

        // Non-vacuity: the error path actually ran and produced a toast.
        expect(onError).toHaveBeenCalled();
        expect(onError.mock.calls[0][0].message).toContain(IMG_PAYLOAD);
        const toasts = renderedToasts();
        expect(toasts.length).toBe(1);

        expectInert(toasts[0]);
        const body = toasts[0].querySelector('.toast-body');
        expect(body.textContent).toContain(IMG_PAYLOAD);
        expect(body.innerHTML).not.toContain('<img');
        expect(body.innerHTML).toContain('&lt;img');
    });

    it('keeps quote-breakout payloads inert in the toast body', () => {
        DM.showNotification('error', `Failed to remove PDFs: ${SVG_QUOTE_PAYLOAD}`);

        const toasts = renderedToasts();
        expect(toasts.length).toBe(1);
        expectInert(toasts[0]);
        const body = toasts[0].querySelector('.toast-body');
        expect(body.textContent).toContain(SVG_QUOTE_PAYLOAD);
        expect(body.innerHTML).not.toContain('<svg');
        expect(body.innerHTML).toContain('&lt;svg');
    });

    it('renders a benign success toast', () => {
        DM.showNotification('success', 'Document deleted successfully');

        const toasts = renderedToasts();
        expect(toasts.length).toBe(1);
        const body = toasts[0].querySelector('.toast-body');
        expect(body.textContent).toBe('Document deleted successfully');
        expect(toasts[0].className).toContain('toast');
    });

    it('passes hostile preview titles to DeleteConfirmation as plain data, not markup', async () => {
        window.DeleteConfirmation.confirmAndRun.mockImplementation(async () => {});
        window.api.fetchWithErrorHandling
            .mockResolvedValueOnce({
                success: true,
                title: `Report ${IMG_PAYLOAD}`,
                has_blob: false,
                has_text: true,
                chunks_count: 3,
                collections_count: 0,
            })
            .mockResolvedValueOnce({ success: true });

        await DM.deleteDocument('doc-2');

        expect(window.DeleteConfirmation.confirmAndRun).toHaveBeenCalledTimes(1);
        const opts = window.DeleteConfirmation.confirmAndRun.mock.calls[0][0];
        expect(opts.title).toBe(`Delete "Report ${IMG_PAYLOAD}"?`);
        // delete_manager itself must not render anything from the title.
        expect(renderedToasts().length).toBe(0);
        expect(document.querySelectorAll('img, svg, script').length).toBe(0);
    });
});