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.7 KB
Raw Blame
import { readdirSync, readFileSync } from 'node:fs';
import { relative, resolve } from 'node:path';

import { describe, expect, it, vi } from 'vitest';

const STATIC_JS_ROOT = resolve(
    __dirname,
    '../../../src/local_deep_research/web/static/js'
);
const VETTED_MESSAGING_SITES = new Set();
const MESSAGING_PATTERNS = [
    ['postMessage', /\bpostMessage\s*\(/g],
    ['message listener', /\baddEventListener\s*\(\s*['"]message['"]/g],
    ['onmessage', /\bonmessage\b/g],
];

const KNOWN_BENIGN_KEYS = new Set([
    'active_news_research',
    'advancedMenuOpen',
    'expanded_news_ids',
    'lastUsedProvider',
    'mobileNavState',
    'news_last_visit',
    'news_read_ids',
    'news_seen_ids',
    'rerunConfig',
    'saved_news_ids',
    'saved_news_items',
]);
const SECRET_VALUE_SHAPES = [
    ['csrf-token', /\b(?:x[-_])?csrf[-_]?token\b/i],
    ['sk-prefix', /sk-/i],
    ['bearer-token', /Bearer\s+/i],
    ['api-key', /api[_-]?key/i],
    ['password-field', /["']?password["']?\s*[:=]/i],
    ['long-base62-token', /[a-z0-9]{41,}/i],
];

function scrubJsComments(source) {
    const scrubbed = [...source];
    let quote = null;

    for (let index = 0; index < source.length; index += 1) {
        const char = source[index];
        const next = source[index + 1];

        if (quote !== null) {
            if (char === '\\') {
                index += 1;
            } else if (char === quote) {
                quote = null;
            }
            continue;
        }
        if (char === "'" || char === '"' || char === '`') {
            quote = char;
            continue;
        }
        if (char === '/' && next === '/') {
            while (index < source.length && !['\n', '\r'].includes(source[index])) {
                scrubbed[index] = ' ';
                index += 1;
            }
            index -= 1;
        } else if (char === '/' && next === '*') {
            while (index < source.length) {
                if (!['\n', '\r'].includes(source[index])) scrubbed[index] = ' ';
                if (source[index] === '*' && source[index + 1] === '/') {
                    scrubbed[index + 1] = ' ';
                    index += 1;
                    break;
                }
                index += 1;
            }
        }
    }
    return scrubbed.join('');
}

function scanMessagingSource(source, file = 'synthetic.js') {
    const scrubbed = scrubJsComments(source);
    return MESSAGING_PATTERNS.flatMap(([kind, pattern]) =>
        [...scrubbed.matchAll(pattern)].map(match => ({
            file,
            line: scrubbed.slice(0, match.index).split('\n').length,
            kind,
        }))
    );
}

function listJsFiles(directory) {
    return readdirSync(directory, { withFileTypes: true }).flatMap(entry => {
        const path = resolve(directory, entry.name);
        if (entry.isDirectory()) return listJsFiles(path);
        return entry.isFile() && entry.name.endsWith('.js') ? [path] : [];
    });
}

function isKnownBenignKey(key) {
    return KNOWN_BENIGN_KEYS.has(key) || key.startsWith('ldr_panel_collapsed_');
}

function storageViolations(writes) {
    return writes.flatMap(({ area, key: rawKey, value: rawValue }) => {
        const key = String(rawKey);
        const value = String(rawValue);
        const normalizedKey = key.toLowerCase().replace(/[^a-z0-9]/g, '');
        const shapes = [];

        if (['token', 'secret', 'password', 'apikey'].some(part => normalizedKey.includes(part))) {
            shapes.push('secret-bearing-key');
        }
        if (!isKnownBenignKey(key)) shapes.push('unapproved-key');
        for (const [shape, pattern] of SECRET_VALUE_SHAPES) {
            if (pattern.test(value)) shapes.push(shape);
        }
        return shapes.map(shape => ({ area, key, shape }));
    });
}

function formatStorageViolations(violations) {
    return violations
        .map(({ area, key, shape }) => `  ${area}:${key} -> value shape: ${shape}`)
        .join('\n');
}

function installFlowDom() {
    document.body.innerHTML = `
        <div class="ldr-page-header"></div>
        <input id="subscription-search"><select id="status-filter"></select>
        <select id="frequency-filter"></select><button id="add-folder-btn"></button>
        <button id="create-subscription-btn"></button><div id="subscriptions-grid"></div>
        <span id="total-subscriptions"></span><span id="active-subscriptions"></span>
        <span id="paused-subscriptions"></span><span id="updates-today"></span>
        <span id="status-indicator"></span><span id="status-text"></span>
        <div id="scheduler-details"></div>
        <nav class="ldr-folder-list">
            <div class="ldr-folder-item" data-folder="all"><span class="ldr-folder-count"></span></div>
            <div class="ldr-folder-item" data-folder="uncategorized"><span class="ldr-folder-count"></span></div>
        </nav>

        <div class="ldr-feed-header"><h2></h2></div><div id="news-feed-content"></div>
        <input id="table-view-toggle" type="checkbox"><tbody id="news-table-body"></tbody>
        <input id="news-search"><button id="search-btn"></button><button id="run-template-btn"></button>
        <input id="impact-filter" type="range" value="0"><span class="ldr-impact-value"></span>
        <div id="sidebar-subscriptions-list"></div><div id="news-semantic-results"></div>
        <div id="news-query"></div><div id="newsSubscriptionModal"></div>
        <input id="news-subscription-query"><div class="ldr-time-filter-group"></div>
        <input id="auto-refresh" type="checkbox"><button id="refresh-feed-btn"></button>
        <div id="recent-searches"></div><div id="trending-topics"></div>
        <div id="priority-status"><span id="priority-message"></span></div>

        <form id="research-form">
            <div id="research-alert"></div><div id="research-error-alert"></div>
            <textarea id="query"></textarea>
            <div class="ldr-privacy-panel" data-scope="adaptive">
                <i id="ldr-privacy-panel-icon"></i>
                <select id="policy_egress_scope"><option value="adaptive">Adaptive</option></select>
                <input id="llm_require_local_endpoint" type="checkbox">
                <input id="embeddings_require_local" type="checkbox">
            </div>
            <label class="ldr-mode-option"><input name="research_mode" value="quick" type="radio" checked></label>
            <button class="ldr-advanced-options-toggle ldr-open" type="button" aria-expanded="true"></button>
            <div class="ldr-advanced-options-panel ldr-expanded" id="advanced-options-panel">
                <select id="model_provider"><option value="OLLAMA">Ollama</option></select>
                <div id="model-dropdown"><input id="model"><div id="model-dropdown-list"></div></div>
                <input id="model_hidden" type="hidden"><button id="model-refresh" type="button"></button>
                <div id="search-engine-dropdown"><input id="search_engine"><div id="search-engine-dropdown-list"></div></div>
                <input id="search_engine_hidden" type="hidden" value="searxng">
                <button id="search_engine-refresh" type="button"></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 id="start-research-btn" type="submit"><span>Start</span></button>
        </form>

        <form id="settings-form">
            <input id="settings-search"><button class="ldr-settings-tab active" data-tab="all" type="button">All</button>
            <div id="settings-alert"></div><div id="settings-content"></div>
        </form>
    `;
}

function jsonResponse(body) {
    return Promise.resolve({
        ok: true,
        status: 200,
        statusText: 'OK',
        json: () => Promise.resolve(body),
        text: () => Promise.resolve(JSON.stringify(body)),
    });
}

describe('WSTG-CLNT-11 Web Messaging lock', () => {
    it('finds no unvetted messaging sites in source JavaScript', () => {
        const findings = listJsFiles(STATIC_JS_ROOT).flatMap(path => {
            const file = relative(STATIC_JS_ROOT, path);
            return scanMessagingSource(readFileSync(path, 'utf8'), file);
        });
        const unvetted = findings.filter(({ file, line }) =>
            !VETTED_MESSAGING_SITES.has(`${file}:${line}`)
        );
        const report = unvetted.map(({ file, line, kind }) => `  ${file}:${line} -> ${kind}`).join('\n');

        expect(unvetted, `New Web Messaging sites require origin-check review:\n${report}`).toEqual([]);
    });

    it('detects synthetic messaging APIs while ignoring comments and clean code', () => {
        const active = scanMessagingSource([
            "window.addEventListener('message', receiveMessage);",
            "window.postMessage('payload', 'https://example.test');",
            'worker.onmessage = receiveMessage;',
        ].join('\n'));
        const clean = scanMessagingSource(`
            // window.postMessage('commented', '*');
            /* window.addEventListener("message", commentedHandler);
               worker.onmessage = commentedHandler; */
            const eventName = 'storage';
        `);

        expect(active.map(({ kind }) => kind)).toEqual([
            'postMessage',
            'message listener',
            'onmessage',
        ]);
        expect(clean).toEqual([]);
    });
});

describe('WSTG-CLNT-12 Browser Storage secret-leak guard', () => {
    it('rejects synthetic forbidden keys and every protected value shape', () => {
        const violations = storageViolations([
            { area: 'localStorage', key: 'access_token', value: 'ordinary' },
            { area: 'localStorage', key: 'news_seen_ids', value: 'csrf_token=abcdefgh' },
            { area: 'localStorage', key: 'saved_news_ids', value: 'sk-example' },
            { area: 'sessionStorage', key: 'rerunConfig', value: 'Bearer credential' },
            { area: 'localStorage', key: 'saved_news_items', value: 'api_key=example' },
            { area: 'localStorage', key: 'active_news_research', value: '{"password":"example"}' },
            { area: 'localStorage', key: 'news_read_ids', value: 'Ab3'.repeat(14) },
        ]);
        const benign = storageViolations([
            { area: 'localStorage', key: 'news_seen_ids', value: '["news-1"]' },
            { area: 'localStorage', key: 'ldr_panel_collapsed_help', value: 'true' },
            { area: 'sessionStorage', key: 'rerunConfig', value: '{"query":"safe"}' },
        ]);

        expect(violations.map(({ shape }) => shape)).toEqual(expect.arrayContaining([
            'secret-bearing-key',
            'unapproved-key',
            'csrf-token',
            'sk-prefix',
            'bearer-token',
            'api-key',
            'password-field',
            'long-base62-token',
        ]));
        expect(benign).toEqual([]);
    });

    it('allows only non-secret writes while real subscription, news, research, and settings flows run', async () => {
        localStorage.clear();
        sessionStorage.clear();
        installFlowDom();

        const storageSetItem = vi.spyOn(window.Storage.prototype, 'setItem');
        const localSetItem = vi.spyOn(localStorage, 'setItem');
        const sessionSetItem = vi.spyOn(sessionStorage, 'setItem');
        const subscription = {
            id: 'subscription-1',
            name: 'Benign subscription',
            query: 'benign query',
            is_active: true,
            refresh_minutes: 60,
            total_runs: 1,
            folder_id: null,
        };
        const settingsPayload = {
            status: 'success',
            settings: {
                'app.guard_setting': {
                    key: 'app.guard_setting',
                    name: 'Guard Setting',
                    description: 'Benign settings-load control',
                    value: 'enabled',
                    ui_element: 'text',
                    editable: true,
                    visible: true,
                    category: 'app_parameters',
                },
            },
        };
        const fetchMock = vi.fn((url, options = {}) => {
            const requestUrl = String(url);
            if (requestUrl === '/news/api/subscriptions/current') {
                return jsonResponse({ subscriptions: [subscription] });
            }
            if (requestUrl === '/news/api/subscription/folders') return jsonResponse([]);
            if (requestUrl === '/news/api/scheduler/status') {
                return jsonResponse({ scheduler_available: false });
            }
            if (requestUrl.startsWith('/news/api/feed?')) return jsonResponse({ news_items: [] });
            if (requestUrl === '/history/api') return jsonResponse({ items: [] });
            if (requestUrl === '/library/api/research-history/collection') {
                return jsonResponse({ success: false });
            }
            if (requestUrl.startsWith('/settings/api/available-models')) {
                return jsonResponse({ providers: {}, provider_options: [], models: [] });
            }
            if (requestUrl.startsWith('/settings/api/available-search-engines')) {
                return jsonResponse({ engine_options: [], engines: [] });
            }
            if (requestUrl === '/settings/api') return jsonResponse(settingsPayload);
            if (requestUrl === '/news/api/subscriptions/subscription-1' &&
                ['PUT', 'DELETE'].includes(options.method)) {
                return jsonResponse({ success: true });
            }
            return Promise.reject(new Error(`Unexpected fetch: ${requestUrl}`));
        });

        vi.stubGlobal('fetch', fetchMock);
        vi.stubGlobal('confirm', vi.fn(() => true));
        vi.stubGlobal('bootstrap', { Modal: vi.fn() });
        window.RESEARCH_STATUS = { QUEUED: 'queued', IN_PROGRESS: 'in_progress' };
        window.RESEARCH_TERMINAL_STATES = new Set(['completed', 'failed', 'cancelled']);
        window.ui = { showAlert: vi.fn() };
        window.DOMPurify = {
            addHook: vi.fn(),
            sanitize: html => {
                const template = document.createElement('template');
                // eslint-disable-next-line no-unsanitized/property -- fixed benign API fixtures pass through this test-only DOMPurify stand-in.
                template.innerHTML = html;
                return template.content;
            },
        };

        try {
            await import('@js/config/constants.js');
            await import('@js/config/urls.js');
            await import('@js/utils/alert-helpers.js');
            await import('@js/security/xss-protection.js');
            await import('@js/utils/form-validation.js');
            await import('@js/components/custom_dropdown.js');
            await import('@js/utils/provider-options.js');
            await import('@js/utils/value-helpers.js');
            await import('@js/services/api.js');
            Object.assign(window.api, {
                fetchWithErrorHandling: vi.fn(() => Promise.resolve(settingsPayload)),
                getCsrfToken: () => 'test-csrf',
            });

            await import('@js/pages/subscriptions.js');
            await import('@js/pages/news.js');
            await import('@js/components/research.js');
            await import('@js/components/settings.js');
            document.dispatchEvent(new Event('DOMContentLoaded'));

            await vi.waitFor(() => {
                expect(document.querySelectorAll('.ldr-subscription-card')).toHaveLength(1);
                expect(document.getElementById('settings-content').textContent).toContain('Guard Setting');
                expect(fetchMock.mock.calls.some(([url]) => String(url).startsWith('/news/api/feed?'))).toBe(true);
                expect(fetchMock.mock.calls.some(([url]) => url === '/history/api')).toBe(true);
            });

            document.querySelector('#subscriptions-grid [data-subscription-action="toggle"]').click();
            await vi.waitFor(() => expect(fetchMock.mock.calls.some(([url, options]) =>
                url === '/news/api/subscriptions/subscription-1' && options.method === 'PUT'
            )).toBe(true));
            document.querySelector('#subscriptions-grid [data-subscription-action="delete"]').click();
            await vi.waitFor(() => expect(fetchMock.mock.calls.some(([url, options]) =>
                url === '/news/api/subscriptions/subscription-1' && options.method === 'DELETE'
            )).toBe(true));

            document.querySelector('.ldr-advanced-options-toggle').click();
            window.dispatchEvent(new Event('beforeunload'));
            await new Promise(resolvePromise => setTimeout(resolvePromise, 350));

            const writes = [
                ...localSetItem.mock.calls.map(([key, value]) => ({ area: 'localStorage', key, value })),
                ...sessionSetItem.mock.calls.map(([key, value]) => ({ area: 'sessionStorage', key, value })),
            ];
            const violations = storageViolations(writes);
            const observedKeys = writes.map(({ key }) => String(key));

            expect(storageSetItem).toHaveBeenCalled();
            expect(observedKeys).toEqual(expect.arrayContaining([
                'advancedMenuOpen',
                'news_last_visit',
                'news_read_ids',
                'news_seen_ids',
            ]));
            expect(
                violations,
                `Browser storage secret leak(s):\n${formatStorageViolations(violations)}`
            ).toEqual([]);
        } finally {
            window.dispatchEvent(new Event('pagehide'));
            sessionSetItem.mockRestore();
            localSetItem.mockRestore();
            storageSetItem.mockRestore();
            vi.unstubAllGlobals();
            delete window.DOMPurify;
            delete window.RESEARCH_STATUS;
            delete window.RESEARCH_TERMINAL_STATES;
            document.body.replaceChildren();
        }
    });
});