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.

18.8 KB
Raw Blame
/**
 * Defensive output-encoding coverage for the new-research/follow-up flow.
 *
 * innerHTML / insertAdjacentHTML interpolation map (current source lines):
 *
 * components/research.js
 *   :259   static rerun-notice markup; no interpolated values.
 *   :463   static empty string (model dropdown clear).
 *   :535   static empty string (search-engine dropdown clear).
 *   :1176  static empty string (provider select clear). Provider value/label are
 *          subsequently assigned through option.value / option.textContent at
 *          :1181-1182, so neither is an HTML interpolation.
 *
 * research_form.js
 *   :272   static empty string (warning clear).
 *   :338   warningsHtml assembled at :279-334:
 *          :291 actionUrl -> escapeHtml in a double-quoted href attribute.
 *          :292 actionLabel -> escapeHtml (text context).
 *          :301 dismissKey -> escapeHtml in a data attribute; a delegated click
 *               listener reads the decoded value without executable markup.
 *          :313 alertClass -> hardcoded; warning.type -> escapeHtml in class.
 *          :321 warning.icon -> escapeHtml (text context).
 *          :324 warning.title -> escapeHtml (text context).
 *          :327 warning.message -> escapeHtml (text context).
 *          :329/:331 actionHtml/dismissHtml -> derived fragments above.
 *          No numeric values are interpolated.
 *
 * followup.js
 *   :91    modalHtml -> raw, trusted same-origin static template response.
 *   :201   static CSS literal; no interpolated values.
 *          API-displayed parent_summary / available_sources use textContent at
 *          :265-266 rather than either HTML sink.
 */

import '@js/config/urls.js';
import '@js/utils/alert-helpers.js';
import '@js/security/xss-protection.js';
import '@js/utils/form-validation.js';
import '@js/components/custom_dropdown.js';
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest';

const IMG_PAYLOAD = '<img src=x onerror=alert(1)>';
const SCRIPT_PAYLOAD = '<script>alert(1)</script>';
const SVG_PAYLOAD = '"><svg onload=alert(1)>';
const ATTR_PAYLOAD = 'x" onmouseover="alert(1)';
const JAVASCRIPT_URL = 'javascript:alert(1)';
const SPECIAL_VALUES = [
    IMG_PAYLOAD,
    SCRIPT_PAYLOAD,
    SVG_PAYLOAD,
    ATTR_PAYLOAD,
    JAVASCRIPT_URL,
];
const BENIGN_PROVIDER_LABEL = 'Ollama (benign control)';
const START_RESEARCH_URL = '/api/start_research';

const providerOptions = [
    ...SPECIAL_VALUES.map((label, index) => ({
        value: `TEST_PROVIDER_${index}`,
        label,
        is_cloud: false,
    })),
    {
        value: 'OLLAMA',
        label: BENIGN_PROVIDER_LABEL,
        is_cloud: false,
    },
];

const defaultApiBody = {
    status: 'error',
    message: 'stubbed response',
    settings: {},
    providers: {},
    provider_options: providerOptions,
    engine_options: [],
    engines: {},
};

let fetchMock;
let FollowUpResearch;

function jsonResponse(responseBody) {
    return Promise.resolve({
        ok: true,
        status: 200,
        json: () => Promise.resolve(responseBody),
        text: () => Promise.resolve(''),
    });
}

function installResearchPage() {
    document.body.innerHTML = `
        <form id="research-form">
            <div id="research-alert" role="alert" style="display:none"></div>
            <div id="research-error-alert" style="display:none"></div>
            <textarea id="query" name="query"></textarea>
            <div class="ldr-privacy-panel" data-scope="adaptive">
                <i id="ldr-privacy-panel-icon"></i>
                <select id="policy_egress_scope" name="policy_egress_scope">
                    <option value="adaptive" selected>Adaptive</option>
                </select>
                <input type="checkbox" id="llm_require_local_endpoint">
                <input type="checkbox" id="embeddings_require_local">
            </div>
            <label class="ldr-mode-option" data-mode="quick">
                <input type="radio" name="research_mode" value="quick" checked>
            </label>
            <button type="button" class="ldr-advanced-options-toggle ldr-open" aria-expanded="true">
                <i class="fas fa-chevron-up"></i><span class="sr-only"></span>
            </button>
            <div class="ldr-advanced-options-panel ldr-expanded" id="advanced-options-panel">
                <select id="model_provider" data-initial-value="OLLAMA">
                    <option value="OLLAMA" selected>Ollama</option>
                </select>
                <div class="form-group">
                    <div id="model-dropdown">
                        <input type="text" id="model">
                        <div id="model-dropdown-list"></div>
                    </div>
                </div>
                <input type="hidden" id="model_hidden" value="safe-model">
                <button type="button" id="model-refresh"></button>
                <div id="search-engine-dropdown">
                    <input type="text" id="search_engine">
                    <div id="search-engine-dropdown-list"></div>
                </div>
                <input type="hidden" id="search_engine_hidden" value="searxng">
                <button type="button" id="search_engine-refresh"></button>
                <select id="strategy"><option value="source-based">Source based</option></select>
                <input id="iterations" value="2">
                <input id="questions_per_iteration" value="3">
            </div>
            <button type="submit" id="start-research-btn"><span>Start Research</span></button>
        </form>
    `;
}

function installWarningContainer() {
    document.body.innerHTML = '<div id="research-alert" style="display:none"></div>';
}

function installParentContext() {
    document.body.innerHTML = `
        <section id="parentContext" style="display:none">
            <div id="parentSummary"></div>
            <div id="parentSources"></div>
        </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) {
    expect(root.querySelectorAll('script, img, svg')).toHaveLength(0);
    expect(eventHandlerAttributes(root)).toEqual([]);
    for (const link of root.querySelectorAll('a[href]')) {
        expect(link.getAttribute('href') ?? '').not.toMatch(/^\s*javascript:/i);
    }
}

function expectNonEmpty(container) {
    expect(container).not.toBeNull();
    expect(container.textContent.trim().length).toBeGreaterThan(0);
}

async function renderResearchStatus(message) {
    const query = document.getElementById('query');
    const model = document.getElementById('model_hidden');
    const button = document.getElementById('start-research-btn');
    const topAlert = document.getElementById('research-alert');
    const anchoredAlert = document.getElementById('research-error-alert');

    query.value = 'Benign research query';
    model.value = 'safe-model';
    button.disabled = false;
    topAlert.replaceChildren();
    anchoredAlert.replaceChildren();
    topAlert.style.display = 'none';
    anchoredAlert.style.display = 'none';
    document.querySelectorAll('.ldr-loading-overlay').forEach((overlay) => overlay.remove());

    fetchMock.mockImplementation((requestUrl) => {
        if (requestUrl === START_RESEARCH_URL) {
            return jsonResponse({ status: 'error', message });
        }
        return jsonResponse(defaultApiBody);
    });

    document.getElementById('research-form').dispatchEvent(
        new Event('submit', { bubbles: true, cancelable: true })
    );

    await vi.waitFor(() => expect(topAlert.textContent).toContain(message));
    return { topAlert, anchoredAlert };
}

async function renderParentContext(summary, sources) {
    installParentContext();
    const followUp = new FollowUpResearch();
    followUp.parentResearchId = 'parent-research-1';
    fetchMock.mockImplementation(() => jsonResponse({
        success: true,
        parent_summary: summary,
        available_sources: sources,
    }));

    await followUp.loadParentContext();
    return document.getElementById('parentContext');
}

beforeAll(async () => {
    fetchMock = vi.fn(() => jsonResponse(defaultApiBody));
    globalThis.fetch = fetchMock;
    window.api = { getCsrfToken: () => 'test-csrf' };
    window.RESEARCH_STATUS = { QUEUED: 'queued', IN_PROGRESS: 'in_progress' };

    await import('@js/research_form.js');
    await import('@js/followup.js');
    FollowUpResearch = window.FollowUpResearch;
});

afterAll(() => {
    sessionStorage.clear();
    document.head.querySelector('#followup-modal-styles')?.remove();
    delete window.api;
    delete window.RESEARCH_STATUS;
    delete globalThis.fetch;
});

describe('components/research.js rendering', () => {
    let pageListeners;

    beforeEach(async () => {
        installResearchPage();
        fetchMock.mockImplementation(() => jsonResponse(defaultApiBody));
        sessionStorage.setItem('rerunConfig', JSON.stringify({ query: IMG_PAYLOAD }));
        vi.resetModules();
        pageListeners = [];
        const initializers = [];
        const addEventListener = document.addEventListener.bind(document);
        const listenerSpy = vi.spyOn(document, 'addEventListener').mockImplementation(
            (type, listener, options) => {
                if (type === 'DOMContentLoaded') {
                    initializers.push(listener);
                    return;
                }
                pageListeners.push([type, listener, options]);
                addEventListener(type, listener, options);
            }
        );
        try {
            // Reload the page's closure as a navigation would. Only run its
            // initializer; the other page modules have separate fixtures.
            await import('@js/components/research.js');
            expect(initializers.length).toBeGreaterThan(0);
            for (const initialize of initializers) {
                initialize.call(document, new Event('DOMContentLoaded'));
            }
            await vi.waitFor(() => {
                expect(document.querySelectorAll('#model_provider option')).toHaveLength(
                    providerOptions.length
                );
                expect(document.getElementById('research-alert').textContent)
                    .toContain('Re-running previous research');
            });
        } finally {
            listenerSpy.mockRestore();
        }
    });

    afterEach(() => {
        for (const [type, listener, options] of pageListeners) {
            document.removeEventListener(type, listener, options);
        }
    });

    test('keeps rerun metadata out of its static innerHTML notice', () => {
        const alertContainer = document.getElementById('research-alert');

        expectNonEmpty(alertContainer);
        expect(alertContainer.textContent).toContain('Re-running previous research');
        expect(document.getElementById('query').value).toBe(IMG_PAYLOAD);
        expect(alertContainer.textContent).not.toContain(IMG_PAYLOAD);
        expectNoLiveMarkup(document.body);
    });

    test('renders special-character provider labels through textContent', () => {
        const providerSelect = document.getElementById('model_provider');

        expectNonEmpty(providerSelect);
        for (const specialValue of SPECIAL_VALUES) {
            expect(providerSelect.textContent).toContain(specialValue);
        }
        expect(providerSelect.textContent).toContain(BENIGN_PROVIDER_LABEL);
        expectNoLiveMarkup(document.body);
    });

    test.each(SPECIAL_VALUES)(
        'renders a server status value as inert visible text: %s',
        async (specialValue) => {
            const { topAlert, anchoredAlert } = await renderResearchStatus(specialValue);

            expectNonEmpty(topAlert);
            expectNonEmpty(anchoredAlert);
            expect(topAlert.textContent).toContain(specialValue);
            expect(anchoredAlert.textContent).toContain(specialValue);
            expectNoLiveMarkup(document.body);
        }
    );

    test('preserves a benign server status message', async () => {
        const benignMessage = 'Research could not start; choose another engine.';
        const { topAlert, anchoredAlert } = await renderResearchStatus(benignMessage);

        expectNonEmpty(topAlert);
        expectNonEmpty(anchoredAlert);
        expect(topAlert.textContent).toContain(benignMessage);
        expect(anchoredAlert.textContent).toContain(benignMessage);
        expectNoLiveMarkup(document.body);
    });
});

describe('research_form.js warning rendering', () => {
    beforeEach(() => {
        installWarningContainer();
    });

    test('renders warning text and an internal quote-bearing href without live markup', () => {
        const internalUrl = `/${ATTR_PAYLOAD}`;
        window.displayWarnings([{
            type: 'configuration_warning',
            icon: IMG_PAYLOAD,
            title: SCRIPT_PAYLOAD,
            message: SVG_PAYLOAD,
            actionUrl: internalUrl,
            actionLabel: ATTR_PAYLOAD,
            dismissKey: null,
        }]);

        const alertContainer = document.getElementById('research-alert');
        const action = alertContainer.querySelector('a.ldr-alert-action');
        expectNonEmpty(alertContainer);
        expect(alertContainer.textContent).toContain(IMG_PAYLOAD);
        expect(alertContainer.textContent).toContain(SCRIPT_PAYLOAD);
        expect(alertContainer.textContent).toContain(SVG_PAYLOAD);
        expect(alertContainer.textContent).toContain(ATTR_PAYLOAD);
        expect(action.getAttribute('href')).toBe(internalUrl);
        expectNoLiveMarkup(alertContainer);
    });

    test('keeps a quote-bearing warning type inside the class attribute', () => {
        window.displayWarnings([{
            type: ATTR_PAYLOAD,
            icon: '!',
            title: 'Attribute boundary control',
            message: ATTR_PAYLOAD,
            dismissKey: null,
        }]);

        const alertContainer = document.getElementById('research-alert');
        const banner = alertContainer.querySelector('.warning-banner');
        expectNonEmpty(alertContainer);
        expect(alertContainer.textContent).toContain(ATTR_PAYLOAD);
        expect(banner.getAttribute('class')).toContain(`warning-${ATTR_PAYLOAD}`);
        expect(banner.hasAttribute('onmouseover')).toBe(false);
        expectNoLiveMarkup(alertContainer);
    });

    test('rejects javascript action URLs while retaining visible warning text', () => {
        window.displayWarnings([{
            type: 'configuration_warning',
            icon: '!',
            title: 'Unsafe destination rejected',
            message: JAVASCRIPT_URL,
            actionUrl: JAVASCRIPT_URL,
            actionLabel: 'Open destination',
            dismissKey: null,
        }]);

        const alertContainer = document.getElementById('research-alert');
        expectNonEmpty(alertContainer);
        expect(alertContainer.textContent).toContain(JAVASCRIPT_URL);
        expect(alertContainer.querySelector('a')).toBeNull();
        expectNoLiveMarkup(alertContainer);
    });

    // Regression coverage: dismissKey stays in a data attribute and is handled
    // by a delegated click listener instead of executable inline JavaScript.
    test('does not create event-handler attributes for a hostile dismiss key', () => {
        window.displayWarnings([{
            type: 'configuration_warning',
            icon: '!',
            title: 'Dismissible warning',
            message: ATTR_PAYLOAD,
            dismissKey: ATTR_PAYLOAD,
        }]);

        const alertContainer = document.getElementById('research-alert');
        const dismissButton = alertContainer.querySelector('button');
        expectNonEmpty(alertContainer);
        expect(alertContainer.textContent).toContain(ATTR_PAYLOAD);
        expect(dismissButton).not.toBeNull();
        expectNoLiveMarkup(alertContainer);
    });

    test('preserves a benign warning and internal action', () => {
        window.displayWarnings([{
            type: 'searxng_recommendation',
            icon: 'i',
            title: 'Use a local search engine',
            message: 'SearXNG is available for private searches.',
            actionUrl: '/settings',
            actionLabel: 'Open settings',
            dismissKey: null,
        }]);

        const alertContainer = document.getElementById('research-alert');
        const action = alertContainer.querySelector('a.ldr-alert-action');
        expectNonEmpty(alertContainer);
        expect(alertContainer.textContent).toContain('Use a local search engine');
        expect(action.textContent).toContain('Open settings');
        expect(action.getAttribute('href')).toBe('/settings');
        expectNoLiveMarkup(alertContainer);
    });
});

describe('followup.js displayed parent context', () => {
    test.each(SPECIAL_VALUES)(
        'renders parent summary and source values as inert visible text: %s',
        async (specialValue) => {
            const parentContext = await renderParentContext(specialValue, specialValue);
            const summary = document.getElementById('parentSummary');
            const sources = document.getElementById('parentSources');

            expectNonEmpty(parentContext);
            expect(summary.textContent).toBe(specialValue);
            expect(sources.textContent).toBe(specialValue);
            expect(parentContext.style.display).toBe('block');
            expectNoLiveMarkup(parentContext);
        }
    );

    test('preserves benign parent context values', async () => {
        const parentContext = await renderParentContext(
            'A benign parent summary.',
            '12 sources'
        );

        expectNonEmpty(parentContext);
        expect(document.getElementById('parentSummary').textContent)
            .toBe('A benign parent summary.');
        expect(document.getElementById('parentSources').textContent).toBe('12 sources');
        expectNoLiveMarkup(parentContext);
    });

    test('loads the trusted static modal template and hardcoded styles normally', async () => {
        document.body.replaceChildren();
        document.head.querySelector('#followup-modal-styles')?.remove();
        fetchMock.mockImplementation(() => Promise.resolve({
            ok: true,
            status: 200,
            text: () => Promise.resolve(`
                <div id="followUpModal">
                    <p>Benign follow-up form</p>
                    <button id="startFollowUpBtn" type="button">Start follow-up</button>
                </div>
            `),
        }));
        const followUp = new FollowUpResearch();

        await followUp.createModal();

        const modal = document.getElementById('followUpModal');
        expectNonEmpty(modal);
        expect(modal.textContent).toContain('Benign follow-up form');
        expect(document.getElementById('followup-modal-styles')).not.toBeNull();
        expectNoLiveMarkup(document.body);
    });
});