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.

5.4 KB
Raw Blame
/** Runtime checks for the trusted template event boundary. */
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { Window } from 'happy-dom';

const SOURCE = readFileSync(resolve(
    __dirname, '../../../src/local_deep_research/web/static/js/security/template-events.js',
), 'utf8');
const TOKEN = 'a'.repeat(43);
const NONCE = 'b'.repeat(43);
let page;

function load(token = TOKEN, nonce = NONCE) {
    page = new Window();
    const script = page.document.createElement('script');
    script.setAttribute('data-template-token', token);
    script.nonce = nonce;
    Object.defineProperty(page.document, 'currentScript', {value: script});
    page.eval(SOURCE);
    page.document.dispatchEvent(new page.Event('DOMContentLoaded'));
    return page.LDRTemplateEvents;
}

async function flush() {
    await new Promise(resolveTick => setTimeout(resolveTick, 0));
}

function button(id, token = TOKEN) {
    const element = page.document.createElement('button');
    element.setAttribute('data-ldr-onclick', id);
    if (token !== null) element.setAttribute('data-ldr-template-token', token);
    page.document.body.appendChild(element);
    return element;
}

afterEach(async () => {
    await page?.happyDOM.close();
});

it('binds newly inserted controls once and keeps the element receiver', async () => {
    load();
    const parent = page.document.createElement('div');
    page.document.body.appendChild(parent);
    const close = button('ldr-d7e9771cf24b');
    parent.appendChild(close);
    await flush();
    page.document.dispatchEvent(new page.Event('DOMContentLoaded'));
    close.setAttribute('data-ldr-onclick', 'ldr-d7e9771cf24b');
    await flush();
    close.click();
    expect(parent.style.display).toBe('none');
});

it.each([null, 'c'.repeat(43), ''])('ignores a control with a different authorization (%s)', async token => {
    load();
    page.clearAllFilters = vi.fn();
    const control = button('ldr-579596525a9b', token);
    await flush();
    control.click();
    expect(page.clearAllFilters).not.toHaveBeenCalled();
});

it('rechecks authorization at dispatch and can recover after it is restored', async () => {
    load();
    page.clearAllFilters = vi.fn();
    const control = button('ldr-579596525a9b');
    await flush();
    control.setAttribute('data-ldr-template-token', 'c'.repeat(43));
    control.click();
    expect(page.clearAllFilters).not.toHaveBeenCalled();
    control.setAttribute('data-ldr-template-token', TOKEN);
    await flush();
    control.click();
    expect(page.clearAllFilters).toHaveBeenCalledOnce();
});

it('carries ordinary punctuation as data without adding attributes or nodes', async () => {
    const events = load();
    page.goToPage = vi.fn();
    const value = 'A "quoted" <label> & 50%';
    const template = page.document.createElement('template');
    // eslint-disable-next-line no-unsanitized/property -- exercises the real attribute encoder with synthetic punctuation, then inspects the parsed DOM.
    template.innerHTML = `<button ${events.attributes('ldr-a88460f4b0fb', [value])}></button>`;
    const control = template.content.firstElementChild;
    page.document.body.appendChild(control);
    await flush();
    control.click();
    expect(page.goToPage).toHaveBeenCalledExactlyOnceWith(value);
    expect([...control.attributes].map(attribute => attribute.name).sort()).toEqual([
        'data-ldr-onclick', 'data-ldr-onclick-args', 'data-ldr-template-token',
    ]);
    expect(control.childNodes).toHaveLength(0);
});

it.each(['%', '%7B%7D', 'null'])('does not dispatch malformed or non-array argument data (%s)', async data => {
    load();
    page.goToPage = vi.fn();
    const control = button('ldr-a88460f4b0fb');
    control.setAttribute('data-ldr-onclick-args', data);
    await flush();
    control.click();
    expect(page.goToPage).not.toHaveBeenCalled();
});

it('keeps keyboard activation and return-false link behavior', async () => {
    load();
    page.showSchedulerInfo = vi.fn();
    const control = button('ldr-8b7c364b086d');
    control.setAttribute('data-ldr-onkeydown', 'ldr-19b1027885b7');
    await flush();
    const keyboard = new page.KeyboardEvent('keydown', {key: 'Enter', cancelable: true});
    control.dispatchEvent(keyboard);
    expect(keyboard.defaultPrevented).toBe(true);
    expect(page.showSchedulerInfo).toHaveBeenCalledOnce();
    const click = new page.MouseEvent('click', {cancelable: true});
    control.dispatchEvent(click);
    expect(click.defaultPrevented).toBe(true);
    expect(page.showSchedulerInfo).toHaveBeenCalledTimes(2);
});

it('rejects unknown action names, duplicate registration, and unsupported events', () => {
    const events = load();
    expect(() => events.attributes('missing')).toThrow('Invalid template action arguments');
    expect(() => events.attributes('ldr-579596525a9b', {})).toThrow('Invalid template action arguments');
    expect(() => events.register('ldr-579596525a9b', 'click', () => {})).toThrow();
    expect(() => events.register('ldr-000000000000', 'load', () => {})).toThrow();
    expect(Object.isFrozen(events)).toBe(true);
    expect(events.styleNonce).toBe(NONCE);
});

it('leaves bindings inert when the loader authorization is invalid', async () => {
    const events = load('invalid', 'invalid');
    page.clearAllFilters = vi.fn();
    const control = button('ldr-579596525a9b', 'invalid');
    await flush();
    control.click();
    expect(page.clearAllFilters).not.toHaveBeenCalled();
    expect(events.styleNonce).toBe('');
});