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.

13.1 KB
Raw Blame
/**
 * Exercise the settings dashboard's real API-to-DOM render path with hostile
 * stored metadata. Adapted from the settings regression suite in PR #5632.
 */

const WINDOW_KEYS = [
    'escapeHtml', 'escapeHtmlAttribute', 'ui', 'modelProvidersRequestInProgress',
    'searchEnginesRequestInProgress', 'modelDropdownsInitialized',
    'searchEngineDropdownInitialized', '__LDR_MOBILE_BREAKPOINT_PX',
    'setupSettingsDropdowns', 'LdrValueHelpers', 'LdrProviderOptions', 'URLS',
];
const ORIGINAL_WINDOW_STATE = new Map(
    WINDOW_KEYS.map(key => [key, { owned: Object.hasOwn(window, key), value: window[key] }])
);
const ORIGINAL_HEAD_NODES = [...document.head.childNodes].map(node => node.cloneNode(true));
const ORIGINAL_BODY_NODES = [...document.body.childNodes].map(node => node.cloneNode(true));

const CATEGORY = 'X"onmouseover="alert(1)';
const SETTING_KEY = 'app.x"onfocus="alert(2)';
const JSON_SETTING_KEY = 'app.json"onfocus="alert(3)';
const JSON_PROPERTY = 'name"][onclick="alert(4)';
const SPECIAL_JSON_SETTING_KEY = 'app.json_original.extra';
const SPECIAL_JSON_PROPERTY = '__proto__';
const REDACTED_KEY = 'app.x"onfocus="alert(5).api_key';
const COLLIDING_DOTTED_KEY = 'app.collision.probe';
const COLLIDING_DASHED_KEY = 'app.collision-probe';
const TOP_LEVEL_SETTING_KEY = 'local_search_chunk_size';
const OPTION_LABEL = '<img src=x onerror=alert(7)>';
const RAW_SECRET = 'sk-test-LEAKED-SECRET-DO-NOT-RENDER-9d8f7a';

function setting(name, value, uiElement = 'text', extra = {}) {
    return {
        name, value, ui_element: uiElement, category: 'app_parameters',
        description: 'A setting', editable: true, visible: true, ...extra,
    };
}

function payload() {
    return {
        status: 'success',
        settings: {
            'app.benign_control': setting('Benign Control', 'benign'),
            'app.hostile_category': setting('Hostile Category Surface', 'safe', 'text', {
                category: CATEGORY,
            }),
            [SETTING_KEY]: setting('Hostile Key', 1, 'number', {
                min_value: CATEGORY, max_value: CATEGORY, step: CATEGORY,
            }),
            [REDACTED_KEY]: setting('Redacted Key', '[REDACTED]'),
            [JSON_SETTING_KEY]: setting('JSON Key', JSON.stringify({
                [JSON_PROPERTY]: 'old value',
                "x') ;alert(1)//": 'iteration',
                '<img src=x onerror=alert(6)>': true,
            }), 'json'),
            [SPECIAL_JSON_SETTING_KEY]: setting('Special JSON Key', JSON.stringify({
                [SPECIAL_JSON_PROPERTY]: 'old value',
                keep: 'unchanged',
            }), 'json'),
            [COLLIDING_DOTTED_KEY]: setting('Dotted Key', 'dotted'),
            [COLLIDING_DASHED_KEY]: setting('Dashed Key', 'dashed'),
            [TOP_LEVEL_SETTING_KEY]: setting('Top-level key', 512, 'number'),
            'app.choose_engine': setting('Hostile Engine Label', CATEGORY, 'select', {
                options: [
                    { value: CATEGORY, label: OPTION_LABEL },
                    { value: 'searxng', label: 'SearXNG (control)' },
                ],
            }),
            'llm.api_key_redacted': setting('API Key (masked)', '[REDACTED]', 'password', {
                category: 'llm_parameters',
            }),
            'llm.api_key_raw': setting('API Key (raw)', RAW_SECRET, 'password', {
                category: 'llm_parameters',
            }),
            'constructor.alpha': setting('Prototype Prefix A', 'a', 'text', {
                category: 'general',
            }),
            'constructor.beta': setting('Prototype Prefix B', 'b', 'text', {
                category: 'general',
            }),
        },
    };
}

function onAttributes(root) {
    const found = [];
    root.querySelectorAll('*').forEach(element => {
        for (const attribute of element.attributes) {
            if (/^on/i.test(attribute.name)) found.push(attribute.name);
        }
    });
    return found;
}

async function renderSettings({ searchValue } = {}) {
    document.body.innerHTML = `
        <form id="settings-form">
            <input id="settings-search" type="text">
            <button type="button" class="ldr-settings-tab" data-tab="all">All</button>
            <button type="button" class="ldr-settings-tab" data-tab="app">App</button>
            <div id="settings-alert"></div>
            <div id="settings-content"></div>
            <div id="raw-config" style="display: none">
                <textarea id="raw-config-editor"></textarea>
            </div>
        </form>
    `;
    await import('@js/config/urls.js');
    await import('@js/services/api.js');
    await import('@js/utils/alert-helpers.js');
    await import('@js/utils/provider-options.js');
    await import('@js/utils/value-helpers.js');
    await import('@js/security/xss-protection.js');
    expect(typeof window.escapeHtml).toBe('function');
    window.api.fetchWithErrorHandling = vi.fn(async () => payload());
    vi.stubGlobal('fetch', vi.fn(async () => new Response('{}', { status: 200 })));

    vi.useFakeTimers();
    try {
        vi.resetModules();
        await import('@js/components/settings.js');
        document.dispatchEvent(new Event('DOMContentLoaded'));
        await vi.advanceTimersByTimeAsync(400);
        if (searchValue !== undefined) {
            const search = document.getElementById('settings-search');
            search.value = searchValue;
            search.dispatchEvent(new Event('input'));
            await vi.advanceTimersByTimeAsync(300);
        }
    } finally {
        vi.useRealTimers();
    }
    const content = document.getElementById('settings-content');
    expect(content.innerHTML).not.toBe('');
    return content;
}

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    vi.useRealTimers();
    document.head.replaceChildren(...ORIGINAL_HEAD_NODES.map(node => node.cloneNode(true)));
    document.body.replaceChildren(...ORIGINAL_BODY_NODES.map(node => node.cloneNode(true)));
    ORIGINAL_WINDOW_STATE.forEach((state, key) => {
        if (state.owned) window[key] = state.value;
        else delete window[key];
    });
});

it.each([
    ['initial', undefined],
    ['search', 'hostile category surface'],
])('keeps stored metadata inert in the %s render', async (_label, searchValue) => {
    const content = await renderSettings({ searchValue });
    const title = Array.from(content.querySelectorAll('.ldr-settings-section-title'))
        .find(element => element.textContent.trim() === CATEGORY);
    expect(title).toBeDefined();
    expect(title.getAttribute('title')).toBe(CATEGORY);
    expect(onAttributes(content)).toEqual([]);
    expect(content.querySelector('img, svg, script')).toBeNull();
    const header = title.closest('.ldr-settings-section-header');
    expect(header.dataset.target).toMatch(/^[a-z][\w-]*$/i);
    expect(document.getElementById(header.dataset.target)).not.toBeNull();
});

it('round-trips hostile keys as data with distinct selector-safe IDs', async () => {
    const content = await renderSettings();
    const inputs = Array.from(content.querySelectorAll('input.ldr-settings-input'));
    const hostile = inputs.find(input => input.name === SETTING_KEY);
    const redacted = inputs.find(input => input.name === REDACTED_KEY);
    const dotted = inputs.find(input => input.name === COLLIDING_DOTTED_KEY);
    const dashed = inputs.find(input => input.name === COLLIDING_DASHED_KEY);
    const benign = document.getElementById('setting-app-benign_control');
    const topLevel = inputs.find(input => input.name === TOP_LEVEL_SETTING_KEY);
    expect(hostile).toBeDefined();
    expect(hostile.closest('.ldr-settings-item').dataset.key).toBe(SETTING_KEY);
    for (const attribute of ['min', 'max', 'step']) {
        expect(hostile.getAttribute(attribute)).toBe(CATEGORY);
    }
    expect(redacted?.value).toBe('');
    expect(redacted?.dataset.redacted).toBe('true');
    expect(dotted?.id).not.toBe(dashed?.id);
    expect(benign?.value).toBe('benign');
    expect(topLevel?.id).toBe(`setting-${TOP_LEVEL_SETTING_KEY}`);
    for (const input of [hostile, redacted, dotted, dashed]) {
        expect(input.id).toMatch(/^[a-z][\w-]*$/i);
    }
    expect(new Set(Array.from(content.querySelectorAll('[id]'), element => element.id)).size)
        .toBe(content.querySelectorAll('[id]').length);
    expect(onAttributes(content)).toEqual([]);
});

it('edits JSON properties under quote-bearing setting keys without selector injection', async () => {
    const content = await renderSettings();
    const control = Array.from(content.querySelectorAll('.ldr-json-property-control'))
        .find(input => input.dataset.parentKey === JSON_SETTING_KEY
            && input.dataset.property === JSON_PROPERTY);
    expect(control).toBeDefined();
    expect(control.name).toBe(`${JSON_SETTING_KEY}_${JSON_PROPERTY}`);
    const original = control.closest('.ldr-settings-item')
        .querySelector('input[id$="_original"]');
    expect(original).not.toBeNull();
    expect(onAttributes(content)).toEqual([]);

    vi.useFakeTimers();
    control.value = 'new value';
    control.dispatchEvent(new Event('input', { bubbles: true }));
    expect(JSON.parse(original.value)[JSON_PROPERTY]).toBe('new value');

    const saves = [];
    window.api.fetchWithErrorHandling = vi.fn(async (url, options) => {
        saves.push({ url, options });
        return { status: 'success', settings: {} };
    });
    document.getElementById('settings-form')
        .dispatchEvent(new Event('submit', { bubbles: true, cancelable: true }));
    await Promise.resolve();
    await Promise.resolve();
    const save = saves.find(request => request.url === URLS.SETTINGS_API.SAVE_ALL_SETTINGS);
    expect(save).toBeDefined();
    expect(JSON.parse(save.options.body)[JSON_SETTING_KEY][JSON_PROPERTY])
        .toBe('new value');
});

it('preserves prototype-shaped JSON properties under suffix-bearing setting keys', async () => {
    const content = await renderSettings();
    const control = Array.from(content.querySelectorAll('.ldr-json-property-control'))
        .find(input => input.dataset.parentKey === SPECIAL_JSON_SETTING_KEY
            && input.dataset.property === SPECIAL_JSON_PROPERTY);
    expect(control).toBeDefined();
    const original = control.closest('.ldr-settings-item')
        .querySelector('input[id$="_original"]');
    expect(original).not.toBeNull();

    vi.useFakeTimers();
    control.value = 'new value';
    control.dispatchEvent(new Event('input', { bubbles: true }));
    const updated = JSON.parse(original.value);
    expect(Object.hasOwn(updated, SPECIAL_JSON_PROPERTY)).toBe(true);
    expect(updated[SPECIAL_JSON_PROPERTY]).toBe('new value');
    expect(updated.keep).toBe('unchanged');

    const saves = [];
    window.api.fetchWithErrorHandling = vi.fn(async (url, options) => {
        saves.push({ url, options });
        return { status: 'success', settings: {} };
    });
    document.getElementById('settings-form')
        .dispatchEvent(new Event('submit', { bubbles: true, cancelable: true }));
    await Promise.resolve();
    await Promise.resolve();
    const save = saves.find(request => request.url === URLS.SETTINGS_API.SAVE_ALL_SETTINGS);
    expect(save).toBeDefined();
    const saved = JSON.parse(save.options.body)[SPECIAL_JSON_SETTING_KEY];
    expect(Object.hasOwn(saved, SPECIAL_JSON_PROPERTY)).toBe(true);
    expect(saved[SPECIAL_JSON_PROPERTY]).toBe('new value');
    expect(saved.keep).toBe('unchanged');
});

it('treats prototype-like legacy prefixes as setting metadata', async () => {
    const content = await renderSettings();
    const first = content.querySelector('input[name="constructor.alpha"]');
    const second = content.querySelector('input[name="constructor.beta"]');
    expect(first).not.toBeNull();
    expect(second).not.toBeNull();
    expect(first.closest('.ldr-settings-section').contains(second)).toBe(true);
    expect(first.closest('.ldr-settings-section')
        .querySelector('.ldr-settings-section-title').textContent.trim())
        .toBe('Constructor General');
});


it('renders hostile select option labels and values as inert text', async () => {
    const content = await renderSettings();
    const select = document.getElementById('setting-app-choose_engine');
    expect(select?.tagName).toBe('SELECT');
    const options = Array.from(select.options);
    expect(options.map(option => [option.value, option.textContent])).toEqual([
        [CATEGORY, OPTION_LABEL],
        ['searxng', 'SearXNG (control)'],
    ]);
    expect(select.querySelector('img, svg, script')).toBeNull();
    expect(onAttributes(content)).toEqual([]);
});

it('renders password inputs empty for both redacted and raw stored values', async () => {
    await renderSettings();
    for (const id of ['setting-llm-api_key_redacted', 'setting-llm-api_key_raw']) {
        const input = document.getElementById(id);
        expect(input).not.toBeNull();
        expect(input.type).toBe('password');
        expect(input.value).toBe('');
        expect(input.getAttribute('value')).toBe('');
        expect(input.getAttribute('placeholder')).toBe('(saved — type to change)');
        expect(input.getAttribute('autocomplete')).toBe('new-password');
        expect(input.outerHTML).not.toContain('[REDACTED]');
    }
    expect(document.body.innerHTML).not.toContain(RAW_SECRET);
});