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.

14.3 KB
Raw Blame
import '../helpers/safe-fetch-with-auth-to-fetch.js';

/**
 * Security tests for components/logpanel.js — research-log -> DOM surface.
 *
 * logpanel.js is the REAL log-panel renderer used by progress.js and
 * results.js. Its inputs are indirectly untrusted: research-progress log
 * messages originate from web-search result content and LLM output, and
 * they reach this module both live (socket.js -> window.addConsoleLog /
 * window._socketAddLogEntry, registered at logpanel.js:491/:497) and via
 * the persisted API (loadLogsForResearch -> /api/research/<id>/logs).
 * tests/js/security/socket-events-xss.test.js covered only socket.js's
 * internal fallback sink; this file covers logpanel.js's own sinks.
 *
 * Interpolation map (verified against logpanel.js source):
 *   - :1228-1230  template path — .textContent sinks for timeStr /
 *     badge / message (structurally inert)
 *   - :1249-1253  no-template fallback — element.innerHTML with
 *     escapeHtml(timeStr) / escapeHtml(badge) / escapeHtml(message)
 *   - :1108       loadLogsForResearch error path — innerHTML with
 *     escapeHtml(error.message)
 *   - :183/:300/:783/:999  static placeholder innerHTML — constant
 *     strings, no interpolation
 *   - :1203-1224 / :1235-1245  dataset.logType / logId / logMessage /
 *     engine writes — property assignment, not markup
 *
 * window.escapeHtml is deliberately left UNDEFINED before import so the
 * module exercises its own quote-safe fallback (logpanel.js:9-10) — the
 * weakest escaper any real page can run it with. (base.html normally
 * provides an equivalent global.)
 *
 * Non-XSS observation: logEntry.id / logEntry.time are interpolated into
 * a querySelector attribute selector at :1316. A hostile server payload
 * with embedded double quotes can throw there (selector injection /
 * DoS), but it cannot execute markup — hostile fields used in these
 * tests are therefore kept quote-free to isolate the XSS surface.
 */

let logPanel;
let emptyCounts;

beforeAll(async () => {
    // logpanel.js destructures window.LdrLogHelpers at IIFE-time.
    await import('@js/utils/log-helpers.js');
    emptyCounts = window.LdrLogHelpers.emptyCounts;

    // Stubs the IIFE expects to find on window. window.escapeHtml is
    // intentionally NOT defined — see the file header.
    window.URLBuilder = {
        researchLogs: (id, limit) =>
            `/api/research/${id}/logs${limit ? `?limit=${limit}` : ''}`,
        historyLogCount: (id) => `/api/research/${id}/log_count`,
        researchLogsExport: (id) => `/api/research/${id}/logs/export`,
    };

    Object.defineProperty(window, 'location', {
        configurable: true,
        value: { ...window.location, pathname: '/', search: '', hash: '' },
    });

    await import('@js/components/logpanel.js');
    logPanel = window.logPanel;
});

/**
 * Build the panel DOM. `withTemplate` mirrors production pages (which ship
 * #console-log-entry-template, so createLogEntryElement takes the
 * textContent path); without it the fallback innerHTML path (:1249) runs —
 * exactly the escapeHtml-dependent sink.
 */
function setupDom({ withTemplate = true } = {}) {
    const template = withTemplate
        ? `
        <template id="console-log-entry-template">
            <div class="ldr-console-log-entry">
                <span class="ldr-log-timestamp"></span>
                <span class="ldr-log-badge"></span>
                <span class="ldr-log-message"></span>
            </div>
        </template>`
        : '';
    // eslint-disable-next-line no-unsanitized/property -- test fixture: hardcoded static template with no interpolated values.
    document.body.innerHTML = `
        <div class="ldr-collapsible-log-panel">
            <div class="ldr-log-panel-header" id="log-panel-toggle">
                <i class="fas fa-chevron-right ldr-toggle-icon"></i>
            </div>
            <div class="ldr-log-panel-content collapsed" id="log-panel-content">
                <div class="ldr-console-log" id="console-log-container"></div>
            </div>
        </div>
        ${template}
    `;
}

beforeEach(() => {
    setupDom({ withTemplate: true });

    // Reset shared state between tests (mirrors logpanel.test.js).
    if (window._logPanelState) {
        window._logPanelState.queuedLogs = [];
        window._logPanelState.expanded = true; // route addLog straight to the panel
        window._logPanelState.logCount = 0;
        window._logPanelState.counts = emptyCounts();
        window._logPanelState.currentFilter = 'all';
        window._logPanelState.autoscroll = true;
        window._logPanelState.renderedIds = new Set();
        window._logPanelState.initialized = false;
        window._logPanelState.connectedResearchId = null;
        window._logPanelState.totalLogs = null;
        window._logPanelState.fetchedLogs = null;
        window._logPanelState.renderedLimit = null;
    }
});

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

const IMG_ONERROR = '<img src=x onerror=alert(1)>';
const QUOTE_SCRIPT = '"><script>alert(1)</script>';
const SPAN_BREAKOUT = '</span><img src=x onerror=alert(1)><span>';
const HOSTILE_TYPE = '<img/src=x/onerror=alert(1)>'; // no whitespace: classList token stays valid

/** Assert no live markup materialized under `root` (or under it). */
function expectTreeInert(root) {
    expect(root.querySelectorAll('img')).toHaveLength(0);
    expect(root.querySelectorAll('script')).toHaveLength(0);
    expect(root.querySelectorAll('[onerror]')).toHaveLength(0);
    for (const el of Array.from(root.querySelectorAll('*'))) {
        for (const attr of Array.from(el.attributes)) {
            expect(attr.name.toLowerCase().startsWith('on')).toBe(false);
        }
    }
}

/** Mock the /logs API (loadLogs fetches log_count first, then logs). */
function mockLogsResponse(entries) {
    globalThis.fetch = vi.fn(() =>
        Promise.resolve({ json: () => Promise.resolve(entries) })
    );
}

describe('logpanel inertness detector sanity', () => {
    it('is not vacuous: an unescaped payload would create a real img element', () => {
        const scratch = document.createElement('div');
        // eslint-disable-next-line no-unsanitized/property -- test-only: detached scratch node proving the detector is not vacuous.
        scratch.innerHTML = IMG_ONERROR;
        expect(scratch.querySelectorAll('img')).toHaveLength(1);
        expect(scratch.querySelectorAll('[onerror]')).toHaveLength(1);
    });
});

describe('addConsoleLog message — template path (textContent sinks :1228-1230)', () => {
    it('img/onerror message lands inert and visible as text', () => {
        logPanel.addLog(IMG_ONERROR, 'info');

        const container = document.getElementById('console-log-container');
        const entries = container.querySelectorAll('.ldr-console-log-entry');
        expect(entries).toHaveLength(1); // sink actually ran

        expectTreeInert(document.body);
        const message = entries[0].querySelector('.ldr-log-message');
        expect(message).not.toBeNull();
        expect(message.textContent).toBe(IMG_ONERROR); // payload arrived...
        expect(message.innerHTML).toContain('&lt;img'); // ...and stored escaped
        expect(message.innerHTML).not.toMatch(/<(img|script)\b/i);
    });

    it('quote-breakout + script payload lands inert', () => {
        logPanel.addLog(QUOTE_SCRIPT, 'info');

        const container = document.getElementById('console-log-container');
        const entries = container.querySelectorAll('.ldr-console-log-entry');
        expect(entries).toHaveLength(1);

        expectTreeInert(document.body);
        const message = entries[0].querySelector('.ldr-log-message');
        expect(message.textContent).toBe(QUOTE_SCRIPT);
        expect(message.innerHTML).toContain('&lt;script&gt;');
        // dataset.logMessage (:1224) stores the raw payload as a data-*
        // attribute value; serialization keeps it inside the quoted value,
        // so no live script element can exist (asserted structurally).
        expect(document.querySelectorAll('script')).toHaveLength(0);
    });

    it('benign control entry renders the expected structure', () => {
        logPanel.addLog('research started', 'info');

        const entry = document.querySelector('.ldr-console-log-entry');
        expect(entry).not.toBeNull(); // non-vacuity: renderer produced output
        expect(entry.dataset.logType).toBe('info');
        expect(entry.querySelector('.ldr-log-timestamp').textContent).not.toBe('');
        expect(entry.querySelector('.ldr-log-badge').textContent).toBe('Info');
        expect(entry.querySelector('.ldr-log-message').textContent).toBe('research started');
        expectTreeInert(document.body);
    });
});

describe('hostile entry metadata — via loadLogs batch render (template path)', () => {
    it('hostile type / timestamp / metadata.engine render inert', async () => {
        // id is quote-free and hostile fields avoid double quotes so the
        // :1316 attribute-selector lookup stays parseable (see header note).
        mockLogsResponse([
            {
                id: 'xss-meta-1',
                timestamp: IMG_ONERROR, // hostile time (flows into new Date())
                message: `meta hostile ${IMG_ONERROR}`,
                log_type: HOSTILE_TYPE, // hostile severity
                metadata: {
                    phase: 'engine_selected',
                    engine: `${QUOTE_SCRIPT}<img src=x onerror=alert(1)>`,
                },
            },
        ]);

        await logPanel.loadLogs('xss-meta-rid');

        const container = document.getElementById('console-log-container');
        const entries = container.querySelectorAll('.ldr-console-log-entry');
        expect(entries).toHaveLength(1); // sink actually ran

        expectTreeInert(document.body);
        const entry = entries[0];
        // Hostile type: rendered as badge text, never markup.
        const badge = entry.querySelector('.ldr-log-badge');
        expect(badge.textContent).toBe(HOSTILE_TYPE);
        expect(badge.innerHTML).not.toMatch(/<(img|script)\b/i);
        // Hostile message still inert next to the hostile metadata.
        expect(entry.querySelector('.ldr-log-message').textContent)
            .toBe(`meta hostile ${IMG_ONERROR}`);
        // metadata.engine -> dataset property write (:1217): present as a
        // data-* attribute value, never as an on* handler or live element.
        expect(entry.dataset.engine).toContain('<img src=x onerror=alert(1)>');
        expect(entry.getAttribute('data-engine')).toContain(QUOTE_SCRIPT);
        // Hostile timestamp: destroyed by Date parsing (renders "Invalid
        // Date"), and the raw string never reaches markup.
        expect(entry.querySelector('.ldr-log-timestamp').textContent)
            .not.toContain('<img');
    });
});

describe('addConsoleLog message — no-template fallback (innerHTML :1249-1253)', () => {
    beforeEach(() => {
        setupDom({ withTemplate: false });
    });

    it('img/onerror message is escaped into innerHTML and visible as text', () => {
        logPanel.addLog(IMG_ONERROR, 'error');

        const entries = document.querySelectorAll('.ldr-console-log-entry');
        expect(entries).toHaveLength(1); // sink actually ran

        expectTreeInert(document.body);
        const message = entries[0].querySelector('.ldr-log-message');
        expect(message).not.toBeNull();
        expect(message.innerHTML).toContain('&lt;img src=x onerror=alert(1)&gt;');
        expect(message.innerHTML).not.toMatch(/<(img|script)\b/i);
        expect(message.textContent).toBe(IMG_ONERROR);
    });

    it('span-closing payload cannot break out of the message span', () => {
        logPanel.addLog(SPAN_BREAKOUT, 'info');

        const entries = document.querySelectorAll('.ldr-console-log-entry');
        expect(entries).toHaveLength(1);

        expectTreeInert(document.body);
        const entry = entries[0];
        const message = entry.querySelector('.ldr-log-message');
        expect(message.textContent).toBe(SPAN_BREAKOUT);
        // The closing tag was neutralized, so the message node still holds
        // the full payload and the entry gained no extra element children.
        expect(entry.querySelectorAll('.ldr-log-message')).toHaveLength(1);
        expect(message.innerHTML).toContain('&lt;/span&gt;');
    });

    it('hostile type renders an escaped badge', () => {
        logPanel.addLog('fallback hostile type', HOSTILE_TYPE);

        const entries = document.querySelectorAll('.ldr-console-log-entry');
        expect(entries).toHaveLength(1);

        expectTreeInert(document.body);
        const badge = entries[0].querySelector('.ldr-log-badge');
        expect(badge).not.toBeNull();
        expect(badge.textContent).toBe(HOSTILE_TYPE);
        expect(badge.innerHTML).toContain('&lt;img');
        expect(badge.innerHTML).not.toMatch(/<(img|script)\b/i);
    });
});

describe('loadLogsForResearch error path (:1108)', () => {
    it('hostile error.message lands escaped and inert', async () => {
        globalThis.fetch = vi.fn(() =>
            Promise.reject(new Error(IMG_ONERROR))
        );

        await logPanel.loadLogs('xss-error-rid');

        const container = document.getElementById('console-log-container');
        const err = container.querySelector('.ldr-error-message');
        expect(err).not.toBeNull(); // sink actually ran

        expectTreeInert(document.body);
        expect(err.innerHTML).toContain('&lt;img src=x onerror=alert(1)&gt;');
        expect(err.innerHTML).not.toMatch(/<(img|script)\b/i);
        expect(err.textContent).toBe(`Error loading logs: ${IMG_ONERROR}`);
    });

    it('quote-breakout error.message lands escaped and inert', async () => {
        globalThis.fetch = vi.fn(() =>
            Promise.reject(new Error(QUOTE_SCRIPT))
        );

        await logPanel.loadLogs('xss-error-rid-2');

        const container = document.getElementById('console-log-container');
        const err = container.querySelector('.ldr-error-message');
        expect(err).not.toBeNull();

        expectTreeInert(document.body);
        expectTreeInert(document.body);
        // innerHTML read-back re-serializes the parsed text node, so the
        // escaped &quot; becomes a bare quote character again; assert on the
        // inert, fully tag-escaped serialization instead.
        expect(err.innerHTML).toContain('&gt;&lt;script&gt;');
        expect(err.innerHTML).not.toMatch(/<(img|script)\b/i);
        expect(err.textContent).toBe(`Error loading logs: ${QUOTE_SCRIPT}`);
    });
});