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.

4.6 KB
Raw Blame
/**
 * XSS regression coverage for components/progress.js updateAgentThinking().
 *
 * Live progress data can contain user queries, search-derived web content,
 * and LLM output. The renderer's innerHTML sink must keep all of that in text
 * context.
 */
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

let progressComponent;

beforeAll(async () => {
    await import('@js/components/progress.js');
    progressComponent = window.progressComponent;
});

beforeEach(() => {
    document.body.innerHTML = `
        <div id="agent-thinking-panel" style="display: none;">
            <div id="agent-thinking-content"></div>
        </div>
    `;
});

function renderStep(data) {
    progressComponent.updateAgentThinking(data);
    return document
        .getElementById('agent-thinking-content')
        .querySelector('.ldr-agent-step:last-child');
}

function expectInertStep(step, visiblePayload) {
    expect(step).not.toBeNull();
    expect(step.querySelectorAll('img, script, svg, iframe, object, embed').length).toBe(0);
    for (const element of step.querySelectorAll('*')) {
        expect([...element.attributes].some(({ name }) => name.toLowerCase().startsWith('on'))).toBe(false);
    }
    expect(step.textContent).toContain(visiblePayload);
}

describe('updateAgentThinking XSS surfaces', () => {
    it('keeps hostile thought output as inert visible text', () => {
        const payload = '<img src=x onerror=alert(1)><script>alert(1)</script>';
        const step = renderStep({ phase: 'thought', thought: payload });

        expectInertStep(step, payload);
    });

    it('keeps hostile tool-call message and tool name inert', () => {
        const message = '<img src=x onerror=alert(1)> Searching <svg onload=alert(2)>';
        const step = renderStep({
            phase: 'tool_call',
            tool: '<svg onload=alert(3)>',
            message,
        });

        expectInertStep(step, message);
    });

    it('keeps hostile fallback tool name and query inert', () => {
        const tool = '<svg onload=alert(1)>';
        const query = '"><svg onload=alert(2)>';
        const step = renderStep({
            phase: 'tool_call',
            tool,
            arguments: { query },
        });

        expectInertStep(step, `Using ${tool}\nQuery: "${query}"`);
    });

    it('keeps hostile observation web content inert', () => {
        const content = '<script>alert(1)</script><img src=x onerror=alert(1)>';
        const step = renderStep({
            phase: 'observation',
            message: 'Search result',
            content,
        });

        expectInertStep(step, `Search result\n${content}`);
    });

    it('keeps hostile errors inert', () => {
        const error = '<img src=x onerror=alert(1)><script>alert(1)</script>';
        const step = renderStep({ phase: 'error', error });

        expectInertStep(step, error);
    });

    it('documents quote-unsafe escapeHtml output and guards sink interpolations', () => {
        const step = renderStep({ phase: 'thought', thought: 'x" y' });
        const renderedContent = step.querySelector('.ldr-agent-step-content');

        // The local escapeHtml() leaves quotes unchanged. This is safe here
        // only because the interpolation is text context, never an attribute.
        expect(renderedContent.innerHTML).toContain('x" y');
        expect(renderedContent.innerHTML).not.toContain('&quot;');

        const progressPath = path.resolve(
            path.dirname(fileURLToPath(import.meta.url)),
            '../../../src/local_deep_research/web/static/js/components/progress.js',
        );
        const source = fs.readFileSync(progressPath, 'utf8');
        const template = source.match(/step\.innerHTML\s*=\s*`([^`]*)`/);
        expect(template).not.toBeNull();

        const interpolations = template[1].match(/\$\{[^}]*\}/g) || [];
        expect(interpolations).toHaveLength(2);
        const dollar = '$';
        expect(interpolations).toEqual([
            `${dollar}{escapeHtml(label)}`,
            `${dollar}{escapeHtml(stepContent)}`,
        ]);
    });

    it('escapes content that crosses the 800-character truncation boundary', () => {
        const content = `${'A'.repeat(795)}<img src=x onerror=alert(1)>`;
        const step = renderStep({ phase: 'observation', message: 'M\n', content });
        const renderedContent = step.querySelector('.ldr-agent-step-content');

        expect(step.querySelectorAll('img, script, svg').length).toBe(0);
        expect(renderedContent.textContent).toContain(`${'A'.repeat(795)}<i...`);
        expect(renderedContent.innerHTML).toContain('&lt;i...');
    });
});