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.

9.6 KB
Raw Blame
/**
 * XSS regression coverage for the context-overflow stats tables.
 *
 * The requested symbols are implemented in components/context-overflow.js,
 * not components/progress.js.  The production path is
 * loadContextData() -> displayContextData() -> renderRequestsTable() /
 * displayModelStats() / displayTruncatedRequests().
 *
 * Interpolation map (context-overflow.js):
 *   renderRequestsTable :439 timestamp is Date output; :434 research_id is
 *   URL-encoded + escaped; :441 model and :450 query use attribute-context
 *   escaping for titles and text-context escaping for display; :442 provider,
 *   :443 phase are escaped; :444-449 number cells use the local
 *   Number()/isFinite-coercing formatNumber; badges/classes are literals.
 *   displayModelStats :509 model title/display and :510 provider are escaped;
 *   :524/:528/:532/:540/:544/:548 are formatNumber-coerced; :514 truncation
 *   rate and :569 response time are finite-number-coerced. Computed widths/colors
 *   are numeric/hardcoded.
 *   displayTruncatedRequests :612 timestamp is Date output; :614 query and
 *   :616 model are escaped; :617-619 are formatNumber-coerced; :621 is URL-
 *   encoded. displayContextData's dynamic-section values are textContent or
 *   number-derived. This component has no cost/formatCurrency interpolation.
 */
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';

const MARKUP = `
    <div id="loading"></div>
    <div id="content"></div>
    <div id="empty-no-data"></div>
    <div id="empty-no-truncation"></div>
    <div id="empty-no-context-data"></div>
    <div id="warning-banner"></div>
    <span id="warning-rate"></span>
    <div id="context-overflow-section"></div>
    <table><tbody id="requests-tbody"></tbody></table>
    <span id="pagination-info"></span>
    <button id="pagination-prev"></button>
    <button id="pagination-next"></button>
`;

const MARKUP_PAYLOAD = '<img src=x onerror=alert(1)>';
const SVG_PAYLOAD = '<svg onload=alert(2)>';
const ATTRIBUTE_PAYLOAD = 'x" onmouseover="alert(3)';

beforeAll(async () => {
    window.__VITEST_TEST__ = true;
    globalThis.Chart = class { destroy() {} };
    globalThis.URLValidator = { safeAssign: vi.fn() };
    window.HTMLCanvasElement.prototype.getContext = () => null;
    await import('@js/components/context-overflow.js');
});

beforeEach(() => {
    // eslint-disable-next-line no-unsanitized/property -- hardcoded fixture markup.
    document.body.innerHTML = MARKUP;
});

function payload({ allRequests = [], modelStats = [], modelTokenStats = [], truncated = [] } = {}) {
    return {
        status: 'success',
        overview: {
            truncation_rate: 5,
            truncated_requests: 1,
            requests_with_context_data: 1,
            total_requests: 1,
            avg_tokens_truncated: 20,
        },
        token_summary: { total_requests: 1 },
        model_stats: modelStats,
        model_token_stats: modelTokenStats,
        recent_truncated: truncated,
        chart_data: [],
        context_limits: [],
        phase_breakdown: [],
        current_context_window: 8192,
        all_requests: allRequests,
        pagination: { page: 1, total_pages: 1 },
    };
}

async function render(data) {
    globalThis.fetch = vi.fn(() => Promise.resolve({
        ok: true,
        json: () => Promise.resolve(data),
    }));
    await window.contextOverflowController.loadContextData('30d');
}

function expectNoLiveMarkup() {
    expect(document.querySelectorAll('img, script, svg, iframe, object, embed')).toHaveLength(0);
    for (const element of document.querySelectorAll('*')) {
        expect([...element.attributes].some(({ name }) => /^on/i.test(name))).toBe(false);
    }
}

describe('context-overflow stats table XSS surfaces', () => {
    it('renders hostile request strings inertly and coerces every request numeric cell', async () => {
        await render(payload({
            allRequests: [{
                timestamp: '2026-01-02T03:04:05Z',
                research_id: 'id?<svg>',
                model: SVG_PAYLOAD,
                provider: MARKUP_PAYLOAD,
                research_phase: SVG_PAYLOAD,
                research_query: MARKUP_PAYLOAD,
                prompt_tokens: MARKUP_PAYLOAD,
                completion_tokens: MARKUP_PAYLOAD,
                total_tokens: MARKUP_PAYLOAD,
                context_limit: MARKUP_PAYLOAD,
                tokens_truncated: MARKUP_PAYLOAD,
                context_truncated: true,
            }],
        }));

        const row = document.querySelector('#requests-tbody tr');
        expect(row).not.toBeNull();
        expect(row.textContent).toContain(SVG_PAYLOAD);
        expect(row.textContent).toContain(MARKUP_PAYLOAD);
        expect(row.textContent).toContain('0');
        expectNoLiveMarkup();
    });

    it('renders hostile truncated-request text inertly and coerces its numeric cells', async () => {
        await render(payload({
            truncated: [{
                timestamp: '2026-01-02T03:04:05Z',
                research_id: `id/${MARKUP_PAYLOAD}`,
                research_query: MARKUP_PAYLOAD,
                model: SVG_PAYLOAD,
                prompt_tokens: MARKUP_PAYLOAD,
                context_limit: MARKUP_PAYLOAD,
                tokens_truncated: MARKUP_PAYLOAD,
            }],
        }));

        const table = document.querySelector('#truncated-list table');
        expect(table).not.toBeNull();
        expect(table.textContent).toContain(MARKUP_PAYLOAD);
        expect(table.textContent).toContain(SVG_PAYLOAD);
        expect(table.textContent).toContain('0');
        expectNoLiveMarkup();
    });

    it('keeps model-card names/providers and formatNumber-fed values inert', async () => {
        await render(payload({
            modelStats: [{
                model: SVG_PAYLOAD,
                provider: MARKUP_PAYLOAD,
                total_requests: MARKUP_PAYLOAD,
                truncated_count: MARKUP_PAYLOAD,
                truncation_rate: 5,
                avg_context_limit: MARKUP_PAYLOAD,
            }],
            modelTokenStats: [{
                model: SVG_PAYLOAD,
                provider: MARKUP_PAYLOAD,
                min_prompt: MARKUP_PAYLOAD,
                avg_prompt: MARKUP_PAYLOAD,
                max_prompt: MARKUP_PAYLOAD,
            }],
        }));

        const card = document.querySelector('#model-stats .ldr-model-card');
        expect(card).not.toBeNull();
        expect(card.textContent).toContain(SVG_PAYLOAD);
        expect(card.textContent).toContain(MARKUP_PAYLOAD);
        expect(card.textContent).toContain('0');
        expectNoLiveMarkup();
    });

    // Regression: renderRequestsTable model titles must not allow quote breakouts.
    it('prevents quote breakout in renderRequestsTable model title', async () => {
        await render(payload({
            allRequests: [{
                timestamp: '2026-01-02T03:04:05Z', model: ATTRIBUTE_PAYLOAD,
                provider: 'openai', prompt_tokens: 1, completion_tokens: 2,
                total_tokens: 3, context_limit: 4, tokens_truncated: 0,
            }],
        }));

        expectNoLiveMarkup();
    });

    // Regression: model-card truncation rates must be numerically coerced.
    it('rejects raw model-card truncation-rate interpolation', async () => {
        await render(payload({
            modelStats: [{
                model: 'safe-model', provider: 'openai', total_requests: 1,
                truncated_count: 0, truncation_rate: MARKUP_PAYLOAD,
            }],
        }));

        expectNoLiveMarkup();
    });

    // Regression: model-card response durations must be numerically coerced.
    it('rejects raw model-card response-duration interpolation', async () => {
        await render(payload({
            modelStats: [{
                model: 'safe-model', provider: 'openai', total_requests: 1,
                truncated_count: 0, truncation_rate: 5,
            }],
            modelTokenStats: [{
                model: 'safe-model', provider: 'openai', avg_response_time_ms: MARKUP_PAYLOAD,
            }],
        }));

        expectNoLiveMarkup();
    });

    it('renders a benign end-to-end stats table with real numbers', async () => {
        await render(payload({
            allRequests: [{
                timestamp: '2026-01-02T03:04:05Z', research_id: 'research-123',
                model: 'gpt-4', provider: 'openai', research_phase: 'analysis',
                research_query: 'quantum computing', prompt_tokens: 1234,
                completion_tokens: 567, total_tokens: 1801, context_limit: 8192,
                tokens_truncated: 42, context_truncated: true,
            }],
            modelStats: [{
                model: 'gpt-4', provider: 'openai', total_requests: 12,
                truncated_count: 2, truncation_rate: 16, avg_context_limit: 8192,
            }],
            modelTokenStats: [{
                model: 'gpt-4', provider: 'openai', min_prompt: 100,
                avg_prompt: 1200, max_prompt: 2000, avg_response_time_ms: 250,
            }],
            truncated: [{
                timestamp: '2026-01-02T03:04:05Z', research_id: 'research-123',
                research_query: 'quantum computing', model: 'gpt-4',
                prompt_tokens: 1234, context_limit: 8192, tokens_truncated: 42,
            }],
        }));

        expect(document.querySelectorAll('#requests-tbody tr')).toHaveLength(1);
        expect(document.querySelector('#requests-tbody').textContent).toContain('1,234');
        expect(document.querySelector('#requests-tbody').textContent).toContain('1,801');
        expect(document.querySelector('#model-stats').textContent).toContain('1,200');
        expect(document.querySelector('#truncated-list').textContent).toContain('8,192');
        expectNoLiveMarkup();
    });
});