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.1 KB
Raw Blame
/**
 * Output-encoding regression coverage for small, otherwise isolated render
 * surfaces. Every production module below is reachable from a page template:
 * fallback/ui.js (results.html), embedding_settings.js (embedding_settings.html),
 * confirmation_modal.js (library.html and collection_details.html),
 * mobile-navigation.js and services/theme.js (base.html), and services/pdf.js
 * (results.html).
 *
 * Interpolation map (file:line -> value -> status):
 * - components/fallback/ui.js:36 -> escapedMessage -> escaped by its local
 *   escapeHtmlFallback/window.escapeHtml; :74, :121 -> escapedMessage -> escaped.
 *   This fallback has no DOMPurify configuration: unlike services/ui.js, it uses
 *   a self-contained five-character HTML escaper before its three HTML sinks.
 * - embedding_settings.js:205,209,213,217,221,225,229 -> saved setting fields ->
 *   escapeHtml (numeric fields are String-coerced first); :645 -> provider label
 *   -> escapeHtml; :796-797,826,831 -> numeric counts / escaped provider label;
 *   :896-900,909,920,941,964,987 -> escaped values. Other innerHTML writes are
 *   static templates or clearing assignments.
 * - deletion/confirmation_modal.js:142 -> details item -> escapeHtml.
 * - mobile-navigation.js:122-133 -> fixed tab/URLS constants; :150-229 -> fixed
 *   URLS constants plus getUsername(), which strips non [\\w\\[email protected]] characters.
 * - services/theme.js:281 -> metadata icon (raw class interpolation); theme names
 *   and labels themselves use DOM APIs/textContent at :204-211 and setAttribute
 *   at :286-287. This suite locks the server-provided theme-name display path.
 * - services/pdf.js -> ui.renderMarkdown output (escaped raw HTML + sanitizeStable
 *   under the restrictive allowlist); the remaining writes are static CSS/detached
 *   text extraction/static loading markup respectively.
 */
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';

const IMG_PAYLOAD = '<img src=x onerror=alert(1)>';
const SVG_PAYLOAD = '"><svg onload=alert(1)>';
const ATTRIBUTE_PAYLOAD = 'x" onmouseover="alert(1)';
const JAVASCRIPT_PAYLOAD = 'javascript:alert(1)';
let productionEscapeHtml;
let productionDOMPurify;

// Sync helper: assigning globalThis/window properties in a non-async scope
// keeps eslint's require-atomic-updates aliasing analysis out of the way.
function installDOMPurifyGlobal(instance) {
    globalThis.DOMPurify = instance;
    window.DOMPurify = instance;
}

function expectInert(root) {
    expect(root).not.toBeNull();
    expect(root.querySelectorAll('script, img, svg, iframe, object, embed').length).toBe(0);
    for (const element of root.querySelectorAll('*')) {
        expect(element.getAttributeNames().some(name => /^on/i.test(name))).toBe(false);
    }
    for (const anchor of root.querySelectorAll('[href]')) {
        expect(anchor.getAttribute('href')).not.toMatch(/^javascript:/i);
    }
}

function response(payload) {
    return { ok: true, json: async () => payload };
}

function resetBody() {
    document.head.replaceChildren();
    document.body.replaceChildren();
}

beforeAll(async () => {
    // happy-dom's NodeIterator stops yielding nodes as soon as the current
    // node is removed, and its Node.prototype#nodeName base getter returns
    // "" for every node. DOMPurify (which lists happy-dom as unsupported)
    // captures both at module-evaluation time: without repair its traversal
    // silently truncates after the first removal and every tag name
    // mis-resolves, so event handlers and javascript: URLs sail through
    // sanitize(). Install the same spec-correct shims as
    // tests/js/security/markdown-render-xss.test.js BEFORE the first
    // dompurify import so every suite below exercises real sanitizer
    // behavior instead of a broken harness path.
    document.createNodeIterator = function(root, whatToShow) {
        const mask = whatToShow === undefined || whatToShow === null
            ? 0xffffffff
            : whatToShow;
        const nodes = [];
        (function collect(node) {
            if (mask & (1 << (node.nodeType - 1))) {
                nodes.push(node);
            }
            for (let i = 0; i < node.childNodes.length; i++) {
                collect(node.childNodes[i]);
            }
        })(root);
        let index = 0;
        return {
            nextNode() {
                return index < nodes.length ? nodes[index++] : null;
            },
            previousNode() {
                return index > 0 ? nodes[--index] : null;
            },
        };
    };
    Object.defineProperty(window.Node.prototype, 'nodeName', {
        configurable: true,
        get() {
            switch (this.nodeType) {
                case 1: return this.tagName;
                case 2: return this.name;
                case 3: return '#text';
                case 4: return '#cdata-section';
                case 7: return this.target;
                case 8: return '#comment';
                case 9: return '#document';
                case 10: return '#document-type';
                case 11: return '#document-fragment';
                default: return '';
            }
        },
    });

    const DOMPurify = (await import('dompurify')).default;
    installDOMPurifyGlobal(DOMPurify);
    productionDOMPurify = DOMPurify;
    await import('@js/security/xss-protection.js');
    productionEscapeHtml = window.escapeHtml;
});

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    window.escapeHtml = productionEscapeHtml;
    resetBody();
    delete window.ui;
    delete window.DeleteConfirmation;
    delete window.MobileNavigation;
    delete window.mobileNav;
    delete window.themeService;
    delete window.LDR_THEME_METADATA;
    delete window.URLS;
    delete window.URLValidator;
    delete window.pdfService;
});

describe('components/fallback/ui.js output encoding', () => {
    beforeEach(async () => {
        vi.resetModules();
        delete window.ui;
        await import('@js/components/fallback/ui.js');
    });

    it('uses its own local escaper to render spinner, error, and success messages as text', () => {
        window.escapeHtml = undefined;
        const notifications = document.createElement('div');
        notifications.className = 'ldr-notifications-container';
        const spinnerTarget = document.createElement('div');
        document.body.append(notifications, spinnerTarget);

        window.ui.showSpinner(spinnerTarget, IMG_PAYLOAD);
        window.ui.showError(SVG_PAYLOAD);
        window.ui.showMessage(ATTRIBUTE_PAYLOAD);

        expect(spinnerTarget.querySelector('.spinner-message').textContent).toBe(IMG_PAYLOAD);
        expect(notifications.querySelectorAll('.ldr-notification')).toHaveLength(2);
        expect(notifications.textContent).toContain(SVG_PAYLOAD);
        expect(notifications.textContent).toContain(ATTRIBUTE_PAYLOAD);
        expectInert(document.body);
    });

    it('renders a benign fallback message (non-vacuity)', () => {
        const notifications = document.createElement('div');
        notifications.className = 'ldr-notifications-container';
        document.body.appendChild(notifications);

        window.ui.showMessage('Saved successfully');

        expect(notifications.querySelector('.ldr-notification').textContent).toContain('Saved successfully');
    });
});

describe('embedding_settings.js output encoding', () => {
    function buildDom() {
        document.body.innerHTML = `
            <select id="embedding-provider"></select><select id="embedding-model"></select>
            <input id="chunk-size"><input id="chunk-overlap"><select id="splitter-type"></select>
            <select id="distance-metric"></select><select id="index-type"></select>
            <input id="normalize-vectors" type="checkbox"><textarea id="text-separators"></textarea>
            <input id="ollama-url"><input id="ollama-num-ctx"><input id="openai-embedding-batch-size">
            <div id="openai-embedding-batch-size-error"></div><div id="openai-embedding-batch-size-group"></div>
            <div id="ollama-url-group"></div><div id="ollama-num-ctx-group"></div>
            <div id="saved-default-settings"></div><div id="provider-info"></div>
            <span id="model-description"></span><button id="test-config-btn"></button><div id="test-result"></div>
            <div class="ldr-library-container"></div>`;
    }

    it('escapes server-provided saved defaults and provider labels through the real load path', async () => {
        vi.resetModules();
        buildDom();
        globalThis.safeFetchWithAuth = vi.fn((url) => {
            if (url === '/library/api/rag/models') {
                return Promise.resolve(response({
                    success: true,
                    provider_options: [{ value: 'hostile', label: IMG_PAYLOAD, available: true }],
                    providers: { hostile: [{ value: SVG_PAYLOAD, label: ATTRIBUTE_PAYLOAD, is_embedding: true }] },
                }));
            }
            if (url === '/library/api/rag/settings') {
                return Promise.resolve(response({
                    success: true,
                    settings: {
                        embedding_provider: 'hostile', embedding_model: SVG_PAYLOAD,
                        chunk_size: ATTRIBUTE_PAYLOAD, chunk_overlap: IMG_PAYLOAD,
                        splitter_type: SVG_PAYLOAD, distance_metric: ATTRIBUTE_PAYLOAD,
                        index_type: IMG_PAYLOAD,
                    },
                }));
            }
            return Promise.resolve(response({ value: null }));
        });
        window.api = { getCsrfToken: () => '' };

        await import('@js/embedding_settings.js');
        document.dispatchEvent(new Event('DOMContentLoaded'));
        await new Promise(resolve => setTimeout(resolve, 0));
        await new Promise(resolve => setTimeout(resolve, 0));

        const defaults = document.getElementById('saved-default-settings');
        const providerInfo = document.getElementById('provider-info');
        expect(defaults.textContent).toContain(SVG_PAYLOAD);
        expect(defaults.textContent).toContain(ATTRIBUTE_PAYLOAD);
        expect(defaults.textContent).toContain(IMG_PAYLOAD);
        expect(providerInfo.textContent).toContain(IMG_PAYLOAD);
        expect(providerInfo.querySelector('.ldr-stat-card')).not.toBeNull();
        expectInert(defaults);
        expectInert(providerInfo);
    });
});

describe('deletion/confirmation_modal.js output encoding', () => {
    beforeEach(async () => {
        vi.resetModules();
        document.body.innerHTML = `
            <div id="deleteConfirmModal"><h2 id="deleteModalTitle"></h2><p id="deleteModalMessage"></p>
            <div id="deleteModalDetails"><ul id="deleteDetailsList"></ul></div>
            <div id="deleteModalWarning"><span id="deleteWarningText"></span></div>
            <button id="deleteConfirmBtn"><span id="deleteConfirmBtnText"></span></button></div>`;
        globalThis.bootstrap = { Modal: class { show() {} static getInstance() { return { hide() {} }; } } };
        await import('@js/deletion/confirmation_modal.js');
    });

    it('renders hostile confirmation details as literal list text', () => {
        window.DeleteConfirmation.show({
            action: 'deleteDocument', details: [IMG_PAYLOAD, SVG_PAYLOAD, ATTRIBUTE_PAYLOAD],
        });

        const modal = document.getElementById('deleteConfirmModal');
        expect(modal.querySelectorAll('#deleteDetailsList li')).toHaveLength(3);
        expect(modal.textContent).toContain(IMG_PAYLOAD);
        expect(modal.textContent).toContain(SVG_PAYLOAD);
        expect(modal.textContent).toContain(ATTRIBUTE_PAYLOAD);
        expectInert(modal);
    });

    it('renders benign confirmation details (non-vacuity)', () => {
        window.DeleteConfirmation.show({ action: 'deleteDocument', details: ['Quarterly report'] });
        expect(document.querySelector('#deleteDetailsList li').textContent).toBe('Quarterly report');
    });
});

describe('mobile-navigation.js output encoding', () => {
    beforeEach(async () => {
        vi.resetModules();
        window.URLS = { PAGES: {
            HOME: '/', HISTORY: '/history', LIBRARY: '/library', NEWS: '/news', NEWS_SUBSCRIPTIONS: '/news/subscriptions',
            COLLECTIONS: '/collections', METRICS: '/metrics', BENCHMARK: '/benchmark', BENCHMARK_RESULTS: '/benchmark-results',
            EMBEDDING_SETTINGS: '/embedding-settings', SETTINGS: '/settings',
        } };
        window.URLValidator = { safeAssign: vi.fn() };
        Object.defineProperty(window, 'innerWidth', { configurable: true, value: 1024 });
        await import('@js/mobile-navigation.js');
    });

    it('keeps special characters from the displayed username inert in the sheet menu', () => {
        const payload = `User ${SVG_PAYLOAD}`;
        const userInfo = document.createElement('div');
        userInfo.className = 'ldr-user-info';
        userInfo.textContent = payload;
        document.body.appendChild(userInfo);
        const nav = new window.MobileNavigation({ persistState: false });
        nav.createSheetMenu();

        const sheet = nav.elements.sheet;
        const usernameLabel = [...sheet.querySelectorAll('.ldr-mobile-sheet-label')]
            .find(label => label.textContent.includes('User'));
        expect(usernameLabel.textContent).toBe('User svg onloadalert1');
        expectInert(sheet);
    });
});

describe('services/theme.js output encoding', () => {
    it('renders server-provided special-character theme names as option text, not markup', async () => {
        vi.resetModules();
        const hostileName = `Theme ${IMG_PAYLOAD}`;
        window.LDR_THEME_METADATA = {
            hostile: { label: hostileName, icon: 'fa-palette', group: 'core' },
            system: { label: 'System', icon: 'fa-desktop', group: 'system' },
        };
        globalThis.fetch = vi.fn(() => Promise.resolve({ ok: false, status: 404 }));
        window.api = { getCsrfToken: () => '' };
        await import('@js/services/theme.js');

        const select = document.createElement('select');
        window.themeService.populateThemeDropdown(select);

        const hostileOption = [...select.options].find(option => option.value === 'hostile');
        expect(hostileOption).toBeDefined();
        expect(hostileOption.textContent).toBe(hostileName);
        expectInert(select);
        expect(select.options.length).toBe(2);
    });
});

describe('services/pdf.js DOM-facing output', () => {
    // pdf.js renders the report body through ui.renderMarkdown -- the same
    // hardened pipeline as the on-screen report view: raw author HTML is
    // escaped at marked's renderer.html hook, then sanitizeStable() applies
    // the restrictive allowlist. (It previously ran marked output through
    // DOMPurify's default profile, which kept defanged <img>/<svg>; see
    // tests/js/services/pdf-sanitization.test.js for the screen/PDF parity
    // checks.) Hostile markup must therefore be inert AND absent: the
    // payloads below reach the preview DOM only as literal text.
    it('sanitizes hostile markdown before it reaches the temporary preview DOM', async () => {
        vi.resetModules();
        const previewContainers = [];
        const appendChild = document.body.appendChild.bind(document.body);
        vi.spyOn(document.body, 'appendChild').mockImplementation((node) => {
            if (node.classList?.contains('ldr-pdf-content')) previewContainers.push(node);
            return appendChild(node);
        });
        const { Marked } = await import('marked');
        globalThis.marked = new Marked();
        globalThis.DOMPurify = productionDOMPurify;
        window.DOMPurify = productionDOMPurify;
        await import('@js/security/url-validator.js');
        await import('@js/security/xss-protection.js');
        await import('@js/services/ui.js');
        const pdf = {
            internal: { pageSize: { getWidth: () => 612, getHeight: () => 792 } }, output: () => new Blob(),
            setFontSize() {}, setTextColor() {}, text() {}, splitTextToSize: text => [text], setFont() {},
            setFillColor() {}, rect() {}, addPage() {}, setDrawColor() {}, setLineWidth() {}, line() {},
        };
        globalThis.jsPDF = class { constructor() { return pdf; } };
        globalThis.html2canvas = vi.fn();
        await import('@js/services/pdf.js');

        const markdown = `${IMG_PAYLOAD}${SVG_PAYLOAD}\n\n[bad](${JAVASCRIPT_PAYLOAD})`;
        await window.pdfService.generatePdf('Report', markdown);

        expect(previewContainers).toHaveLength(1);
        const preview = previewContainers[0].querySelector('#pdf-body-content');
        expect(preview.textContent).toContain('bad');
        // Raw HTML is escaped, so none of it may exist as an element.
        expect(preview.querySelectorAll('script, img, svg, iframe, object, embed')).toHaveLength(0);
        for (const element of preview.querySelectorAll('*')) {
            expect(element.getAttributeNames().some(name => /^on/i.test(name))).toBe(false);
        }
        for (const anchor of preview.querySelectorAll('[href]')) {
            expect(anchor.getAttribute('href')).not.toMatch(/^javascript:/i);
        }
        // ...and it is visible as text, exactly as on the screen path.
        expect(preview.textContent).toContain('<img src=x onerror=alert(1)>');
    });
});