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.

17.0 KB
Raw Blame
/**
 * Security regression coverage for components/context-overflow.js.
 *
 * The component is an IIFE, so these tests use its real public path:
 * contextOverflowController.loadContextData() -> fetch -> displayContextData().
 * Chart.js is stubbed because its output is canvas-only; every DOM renderer in
 * context-overflow.js still runs unchanged.
 *
 * innerHTML occurrence/interpolation map (current source lines):
 *   :49    escapeHtml serializer read after textContent assignment; not a write sink.
 *   :154   static empty string.
 *   :210   static empty string.
 *   :216   static page scaffold; no data interpolation.
 *   :412   searchTerm selects one of two hardcoded empty-table messages.
 *   :457   request rows assembled at :437-452:
 *            :439 timestamp -> Date(...).toLocaleString() (derived text)
 *            :440 research_id -> encodeURIComponent in href, escapeHtml in text
 *            :441 model -> escapeHtmlAttribute in title, escapeHtml in text
 *            :442 provider -> escapeHtml
 *            :443 research_phase -> escapeHtml
 *            :444-447 token counts/context limit -> formatNumber (numeric coercion)
 *            :448 truncation badge -> hardcoded branch
 *            :449 class -> hardcoded branch; tokens lost -> formatNumber
 *            :450 research_query -> escapeHtmlAttribute in title, escapeHtml in text
 *   :476   static no-model message.
 *   :485   static empty string.
 *   :506   model card assembled at :506-571:
 *            :509 model -> escapeHtmlAttribute in title, escapeHtml in text
 *            :510 provider -> escapeHtml
 *            :513 class -> one of three hardcoded classes
 *            :514 truncation_rate -> finite numeric coercion
 *            :524/:528/:532 request counts/context limit -> formatNumber/static
 *            :536/:550 max_prompt only selects a template; min/avg/max at
 *              :540/:544/:548 -> formatNumber
 *            :552/:561 hasUtil only selects a template; :556/:559 utilPct is
 *              arithmetic-derived and utilColor is a hardcoded CSS variable
 *            :564 truncation progress width -> Math.min numeric coercion
 *            :567/:570 response-time presence only selects a template
 *            :569 avg_response_time_ms -> finite numeric coercion and formatting
 *   :582   static no-truncated-requests message.
 *   :592   static empty string.
 *   :597   recent-truncation table assembled at :597-628:
 *            :610/:626 map/join only; :612 timestamp -> Date locale string
 *            :614 query and :616 model -> escapeHtml
 *            :617-619 token metrics -> formatNumber
 *            :621 research_id -> encodeURIComponent in href
 *   :895   static no-phase-data message.
 *   :930   static no-phase-data message.
 *   :1089  phase summary assembled at :1089-1110:
 *            :1100/:1107 map/join only; :1102 phase -> escapeHtml;
 *            :1103-1105 counts/tokens -> formatNumber.
 *   :1265  static load-error message.
 *
 * There are 16 innerHTML tokens in the file: 15 assignments plus the escaped
 * serializer read at :49. There are no insertAdjacentHTML calls.
 */

import { afterAll, afterEach, beforeAll, describe, expect, test, vi } from 'vitest';

const IMG_PAYLOAD = '<img src=x onerror=alert(1)>';
const MODEL_PAYLOAD = '<script>';
const SCRIPT_PAYLOAD = '<script>alert(2)</script>';
const SVG_PAYLOAD = '"><svg onload=alert(3)>';
const ATTR_PAYLOAD = 'x" onmouseover="alert(1)';
const JAVASCRIPT_URL = 'javascript:alert(1)';

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

afterEach(() => {
    vi.restoreAllMocks();
});

afterAll(() => {
    delete window.__VITEST_TEST__;
    delete globalThis.Chart;
    delete globalThis.URLValidator;
    delete globalThis.fetch;
});

function buildPayload() {
    const timestamp = '2026-08-20T12:00:00Z';
    return {
        status: 'success',
        overview: {
            truncation_rate: 25,
            truncated_requests: 1,
            requests_with_context_data: 2,
            total_requests: 2,
            avg_tokens_truncated: 256,
        },
        token_summary: { total_requests: 2 },
        model_stats: [{
            model: 'gpt-4',
            provider: 'openai',
            total_requests: 2,
            truncated_count: 1,
            truncation_rate: 25,
            avg_context_limit: 8192,
        }],
        model_token_stats: [{
            model: 'gpt-4',
            provider: 'openai',
            min_prompt: 512,
            avg_prompt: 4096,
            max_prompt: 7000,
            avg_response_time_ms: 1200,
        }],
        recent_truncated: [{
            timestamp,
            research_query: 'Benign truncated query',
            model: 'gpt-4',
            prompt_tokens: 8000,
            context_limit: 8192,
            tokens_truncated: 256,
            research_id: 'research-1',
        }],
        chart_data: [{
            timestamp,
            original_prompt_tokens: 4096,
            prompt_tokens: 4096,
            context_limit: 8192,
            research_phase: 'search',
            research_id: 'research-1',
            model: 'gpt-4',
            provider: 'openai',
            tokens_truncated: 0,
            response_time_ms: 1200,
            truncated: false,
        }],
        context_limits: [8192],
        phase_breakdown: [{
            phase: 'search',
            count: 2,
            total_tokens: 8192,
            avg_tokens: 4096,
        }],
        current_context_window: 4096,
        all_requests: [{
            timestamp,
            research_id: 'research-1',
            model: 'gpt-4',
            provider: 'openai',
            research_phase: 'search',
            prompt_tokens: 4096,
            completion_tokens: 512,
            total_tokens: 4608,
            context_limit: 8192,
            context_truncated: false,
            tokens_truncated: 0,
            research_query: 'Benign request query',
        }],
        pagination: { page: 1, total_pages: 1 },
    };
}

function installPageDom() {
    document.body.innerHTML = `
        <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>
    `;
}

async function renderPayload(payload) {
    installPageDom();
    globalThis.fetch = vi.fn(async () => ({
        ok: true,
        json: async () => payload,
    }));

    await window.contextOverflowController.loadContextData('30d');
    expect(globalThis.fetch).toHaveBeenCalledTimes(1);
    return document.getElementById('context-overflow-section');
}

function eventHandlerAttributes(root) {
    const found = [];
    for (const element of root.querySelectorAll('*')) {
        for (const name of element.getAttributeNames()) {
            if (/^on/i.test(name)) found.push(`${element.tagName}[${name}]`);
        }
    }
    return found;
}

function expectNoLiveMarkup(root = document.body) {
    expect(root.querySelectorAll('script, img, svg')).toHaveLength(0);
    expect(eventHandlerAttributes(root)).toEqual([]);
}

describe('context-overflow text and attribute contexts', () => {
    test('renders model, provider, query, phase, and overview percentage payloads as inert text', async () => {
        const payload = buildPayload();
        payload.overview.truncation_rate = SCRIPT_PAYLOAD;

        payload.model_stats[0].model = MODEL_PAYLOAD;
        payload.model_stats[0].provider = IMG_PAYLOAD;
        payload.model_token_stats[0].model = MODEL_PAYLOAD;
        payload.model_token_stats[0].provider = IMG_PAYLOAD;

        payload.recent_truncated[0].model = MODEL_PAYLOAD;
        payload.recent_truncated[0].research_query = IMG_PAYLOAD;

        payload.chart_data[0].model = MODEL_PAYLOAD;
        payload.chart_data[0].provider = IMG_PAYLOAD;
        payload.chart_data[0].research_phase = SVG_PAYLOAD;
        payload.phase_breakdown[0].phase = SVG_PAYLOAD;

        payload.all_requests[0].model = MODEL_PAYLOAD;
        payload.all_requests[0].provider = IMG_PAYLOAD;
        payload.all_requests[0].research_phase = SVG_PAYLOAD;
        payload.all_requests[0].research_query = IMG_PAYLOAD;

        const section = await renderPayload(payload);
        const modelCard = section.querySelector('.ldr-model-card');
        const requestRow = document.querySelector('#requests-tbody tr');
        const phaseRow = document.querySelector('#phase-summary-table tbody tr');

        expect(modelCard).not.toBeNull();
        expect(requestRow).not.toBeNull();
        expect(phaseRow).not.toBeNull();
        expect(modelCard.querySelector('.ldr-model-name').textContent).toBe(MODEL_PAYLOAD);
        expect(modelCard.querySelector('.ldr-model-provider').textContent).toBe(IMG_PAYLOAD);
        expect(requestRow.children[2].textContent).toBe(MODEL_PAYLOAD);
        expect(requestRow.children[3].textContent).toBe(IMG_PAYLOAD);
        expect(requestRow.children[4].textContent).toBe(SVG_PAYLOAD);
        expect(requestRow.children[11].textContent).toBe(IMG_PAYLOAD);
        expect(document.getElementById('truncated-list').textContent).toContain(IMG_PAYLOAD);
        expect(phaseRow.children[0].textContent).toBe(SVG_PAYLOAD);
        expect(document.getElementById('truncation-rate').textContent)
            .toBe(`${SCRIPT_PAYLOAD}%`);
        expectNoLiveMarkup();
    });

    test('encodes javascript-like research IDs as inert detail-path segments', async () => {
        const payload = buildPayload();
        payload.recent_truncated[0].research_id = JAVASCRIPT_URL;
        payload.all_requests[0].research_id = JAVASCRIPT_URL;

        const section = await renderPayload(payload);
        const detailLinks = document.querySelectorAll(
            '#truncated-list a[href], #requests-tbody a[href]'
        );

        expect(section.querySelector('.ldr-model-card')).not.toBeNull();
        expect(detailLinks).toHaveLength(2);
        for (const link of detailLinks) {
            expect(link.getAttribute('href')).toBe('/details/javascript%3Aalert(1)');
            expect(link.getAttribute('href')).not.toMatch(/^javascript:/i);
        }
        expectNoLiveMarkup();
    });

    // Regression: all_requests[].model titles must keep quotes inside the attribute value.
    test('prevents quote breakouts in request-model title attributes', async () => {
        const payload = buildPayload();
        payload.all_requests[0].model = ATTR_PAYLOAD;

        await renderPayload(payload);
        const modelCell = document.querySelector('#requests-tbody tr').children[2];

        expect(modelCell).not.toBeNull();
        expect(modelCell.hasAttribute('onmouseover')).toBe(false);
        expect(modelCell.getAttribute('title')).toBe(ATTR_PAYLOAD);
    });

    // Regression: all_requests[].research_query titles must keep quotes inside the attribute value.
    test('prevents quote breakouts in request-query title attributes', async () => {
        const payload = buildPayload();
        payload.all_requests[0].research_query = ATTR_PAYLOAD;

        await renderPayload(payload);
        const queryCell = document.querySelector('#requests-tbody tr').children[11];

        expect(queryCell).not.toBeNull();
        expect(queryCell.hasAttribute('onmouseover')).toBe(false);
        expect(queryCell.getAttribute('title')).toBe(ATTR_PAYLOAD);
    });

    // Regression: model_stats[].model titles must keep quotes inside the attribute value.
    test('prevents quote breakouts in model-card title attributes', async () => {
        const payload = buildPayload();
        payload.model_stats[0].model = ATTR_PAYLOAD;
        payload.model_token_stats[0].model = ATTR_PAYLOAD;

        const section = await renderPayload(payload);
        const modelName = section.querySelector('.ldr-model-name');

        expect(modelName).not.toBeNull();
        expect(modelName.hasAttribute('onmouseover')).toBe(false);
        expect(modelName.getAttribute('title')).toBe(ATTR_PAYLOAD);
    });
});

describe('context-overflow numeric interpolation', () => {
    test('coerces hostile count and token metrics to zero instead of markup', async () => {
        const payload = buildPayload();
        payload.overview.truncated_requests = IMG_PAYLOAD;
        payload.overview.avg_tokens_truncated = IMG_PAYLOAD;
        payload.overview.total_requests = IMG_PAYLOAD;

        payload.model_stats[0].total_requests = IMG_PAYLOAD;
        payload.model_stats[0].truncated_count = IMG_PAYLOAD;
        payload.model_stats[0].avg_context_limit = 0;
        payload.model_token_stats[0].min_prompt = IMG_PAYLOAD;
        payload.model_token_stats[0].avg_prompt = IMG_PAYLOAD;
        payload.model_token_stats[0].max_prompt = IMG_PAYLOAD;

        payload.recent_truncated[0].prompt_tokens = IMG_PAYLOAD;
        payload.recent_truncated[0].context_limit = IMG_PAYLOAD;
        payload.recent_truncated[0].tokens_truncated = IMG_PAYLOAD;

        payload.phase_breakdown[0].count = IMG_PAYLOAD;
        payload.phase_breakdown[0].total_tokens = IMG_PAYLOAD;
        payload.phase_breakdown[0].avg_tokens = IMG_PAYLOAD;

        payload.all_requests[0].prompt_tokens = IMG_PAYLOAD;
        payload.all_requests[0].completion_tokens = IMG_PAYLOAD;
        payload.all_requests[0].total_tokens = IMG_PAYLOAD;
        payload.all_requests[0].context_limit = IMG_PAYLOAD;
        payload.all_requests[0].tokens_truncated = IMG_PAYLOAD;

        const section = await renderPayload(payload);
        const recentCells = document.querySelector('#truncated-list tbody tr').children;
        const requestCells = document.querySelector('#requests-tbody tr').children;
        const phaseCells = document.querySelector('#phase-summary-table tbody tr').children;

        expect(section.querySelector('.ldr-model-card')).not.toBeNull();
        expect(document.getElementById('truncated-count').textContent).toBe('0');
        expect(document.getElementById('avg-tokens-lost').textContent).toBe('0');
        expect([recentCells[3].textContent, recentCells[4].textContent, recentCells[5].textContent])
            .toEqual(['0', '0', '0']);
        expect([
            requestCells[5].textContent,
            requestCells[6].textContent,
            requestCells[7].textContent,
            requestCells[8].textContent,
            requestCells[10].textContent,
        ]).toEqual(['0', '0', '0', '0', '0']);
        expect([phaseCells[1].textContent, phaseCells[2].textContent, phaseCells[3].textContent])
            .toEqual(['0', '0', '0']);
        expect(document.body.textContent).not.toContain(IMG_PAYLOAD);
        expect(document.body.textContent).not.toContain('NaN');
        expectNoLiveMarkup();
    });

    // Regression: model_stats[].truncation_rate must be numerically coerced before interpolation.
    test('rejects markup supplied as a model truncation percentage', async () => {
        const payload = buildPayload();
        payload.model_stats[0].truncation_rate = IMG_PAYLOAD;

        const section = await renderPayload(payload);
        const modelCard = section.querySelector('.ldr-model-card');

        expect(modelCard).not.toBeNull();
        expect(modelCard.querySelector('img')).toBeNull();
        expect(eventHandlerAttributes(modelCard)).toEqual([]);
    });

    // Regression: model_token_stats[].avg_response_time_ms must be numerically coerced.
    test('rejects markup supplied as average response time', async () => {
        const payload = buildPayload();
        payload.model_token_stats[0].avg_response_time_ms = SCRIPT_PAYLOAD;

        const section = await renderPayload(payload);
        const modelCard = section.querySelector('.ldr-model-card');

        expect(modelCard).not.toBeNull();
        expect(modelCard.querySelector('.fa-clock')).not.toBeNull();
        expect(modelCard.querySelector('script')).toBeNull();
    });
});

test('benign control renders all four data-bearing HTML surfaces intact', async () => {
    const section = await renderPayload(buildPayload());
    const modelCard = section.querySelector('.ldr-model-card');
    const recentRow = document.querySelector('#truncated-list tbody tr');
    const requestRow = document.querySelector('#requests-tbody tr');
    const phaseRow = document.querySelector('#phase-summary-table tbody tr');

    expect(modelCard).not.toBeNull();
    expect(recentRow).not.toBeNull();
    expect(requestRow).not.toBeNull();
    expect(phaseRow).not.toBeNull();
    expect(modelCard.textContent).toContain('gpt-4');
    expect(modelCard.textContent).toContain('OpenAI');
    expect(recentRow.textContent).toContain('Benign truncated query');
    expect(requestRow.textContent).toContain('Benign request query');
    expect(phaseRow.textContent).toContain('search');
    expect(document.querySelectorAll('a[href="/details/research-1"]')).toHaveLength(2);
    expectNoLiveMarkup();
});