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.

21.2 KB
Raw Blame
/**
 * Defensive output-encoding coverage for the notes/news component scripts.
 *
 * Interpolation map (line numbers in the sources under test):
 *
 * notes_shared.js
 *   :67-83  no innerHTML/insertAdjacentHTML sink. note.id is passed through
 *           encodeURIComponent before href assignment; title and stripped
 *           content_preview are assigned with textContent.
 *
 * research_notes.js
 *   :91-96  no parser sink. API note rows are rendered by
 *           NotesShared.renderNoteRow (the safe path above).
 *
 * annotation_surface.js
 *   no innerHTML/insertAdjacentHTML sink. Dynamic display values use DOM APIs:
 *   :189 note_id -> dataset (raw property assignment)
 *   :432 selected quote -> textContent
 *   :490-491 comment_preview/note_title -> textContent
 *   :497 note_id -> encodeURIComponent -> href property
 *
 * news.js
 *   :58-64  icon -> esc() in class=""; message -> esc() in text
 *   :70-75  message -> esc() in text
 *   :81-87  message -> esc() in text
 *
 * subscription-manager.js
 *   :129-134 folder.name -> escapeHtml in data-folder and text; folder.icon ->
 *            escapeHtml in text; item_count -> finite integer coercion
 *   :166-171 static empty-state markup only
 *   :177     joins renderSubscriptionCard output into innerHTML
 *   :188     subscription.id -> escapeHtml in data-subscription-id
 *   :191     query_or_topic -> escapeHtml in text
 *   :193     refresh_interval_minutes -> finite integer coercion
 *   :194     timeUntil -> locally derived numeric duration string
 *   :195     folder -> escapeHtml in text
 *   :197     notes -> escapeHtml in text
 *   :201/:205/:209 subscription.id -> escapeHtml in data attributes
 *   :205-206 status -> strict-comparison-selected static title/icon literals
 *   :241-246 refresh interval -> strict-comparison-selected static "selected"
 *   :253-256 folder name -> escapeHtml in option value/text; selected is static
 *   :279     insertAdjacentHTML receives the modal assembled from those values;
 *            notes are assigned afterward through textarea.value (:281)
 *   :410-416 static loading markup only
 */

import {
    afterAll,
    beforeAll,
    beforeEach,
    describe,
    expect,
    it,
    test,
    vi,
} from 'vitest';

const IMAGE_PAYLOAD = '<img src=x onerror=alert(1)>';
const SCRIPT_PAYLOAD = '<script>alert(1)</script>';
const SVG_PAYLOAD = '"><svg onload=alert(1)>';
const ATTRIBUTE_PAYLOAD = 'x" onmouseover="alert(1)';
const JAVASCRIPT_URL = 'javascript:alert(1)';

const SUBSCRIPTION_FIXTURE = `
    <ul id="folderTabs">
        <li><button class="nav-link" data-folder="all" type="button">All</button></li>
        <li><button class="nav-link" data-folder="Unfiled" type="button">Unfiled</button></li>
        <li><button id="create-folder-btn" type="button">Create</button></li>
    </ul>
    <div id="subscriptions-list"></div>
`;

let researchNotesHook;
let subscriptionManager;
let modalShow;

function installSubscriptionFixture() {
    // eslint-disable-next-line no-unsanitized/property -- hardcoded test scaffold; no runtime values are interpolated.
    document.body.innerHTML = SUBSCRIPTION_FIXTURE;
}

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

function allElements(root) {
    const elements = [...root.querySelectorAll('*')];
    if (root instanceof Element) elements.unshift(root);
    return elements;
}

function eventHandlerAttributes(root) {
    const found = [];
    for (const element of allElements(root)) {
        for (const name of element.getAttributeNames()) {
            if (/^on/i.test(name)) found.push(`${element.tagName}[${name}]`);
        }
    }
    return found;
}

function javascriptHrefs(root) {
    return allElements(root)
        .filter((element) => element.hasAttribute('href'))
        .map((element) => element.getAttribute('href'))
        .filter((href) => /^\s*javascript:/i.test(href || ''));
}

function expectNoActiveMarkup(root) {
    expect(root.querySelectorAll('script, img, svg')).toHaveLength(0);
    expect(eventHandlerAttributes(root)).toEqual([]);
    expect(javascriptHrefs(root)).toEqual([]);
}

function expectInertText(root, payloads) {
    expect(root).not.toBeNull();
    expect(root.textContent).not.toBe('');
    expectNoActiveMarkup(root);
    for (const payload of payloads) {
        expect(root.textContent).toContain(payload);
    }
}

function makeSubscription(overrides = {}) {
    return {
        id: 'subscription-1',
        query_or_topic: 'Quantum updates',
        refresh_interval_minutes: 60,
        next_refresh: new Date(Date.now() + 60 * 60 * 1000).toISOString(),
        folder: 'Reading',
        notes: 'Review weekly',
        status: 'active',
        ...overrides,
    };
}

function renderSubscriptionCards(subscriptions) {
    installSubscriptionFixture();
    subscriptionManager.currentFolder = 'all';
    subscriptionManager.subscriptions = { Reading: subscriptions };
    subscriptionManager.renderSubscriptions();
    return document.getElementById('subscriptions-list');
}

async function renderResearchNotes(notes) {
    const list = document.createElement('div');
    list.id = 'research-notes-list';
    const empty = document.createElement('div');
    empty.id = 'research-notes-empty';
    document.body.replaceChildren(list, empty);

    researchNotesHook.setResearchId('research-1');
    globalThis.safeFetch.mockImplementation(() => jsonResponse({ success: true, notes }));
    await researchNotesHook.loadResearchNotes();
    return list;
}

async function renderAnnotation(annotation, reportText) {
    const container = document.createElement('div');
    container.id = 'annotation-xss-report';
    container.textContent = reportText;
    document.body.replaceChildren(container);

    globalThis.safeFetch.mockImplementation(() => jsonResponse({
        success: true,
        annotations: [annotation],
    }));
    window.LDRAnnotationSurface.init({
        containerId: container.id,
        endpoints: {
            list: '/notes/api/annotations',
            create: '/notes/api/annotations',
            deleteFor: (noteId) => `/notes/api/annotations/${encodeURIComponent(noteId)}`,
        },
    });

    await vi.waitFor(() => {
        expect(container.querySelectorAll('mark.ldr-research-annotation').length)
            .toBeGreaterThan(0);
    });
    const mark = container.querySelector('mark.ldr-research-annotation');
    mark.click();
    const popover = document.querySelector('.ldr-annotation-popover');
    expect(popover).not.toBeNull();
    return { container, mark, popover };
}

beforeAll(async () => {
    window.__VITEST_TEST__ = true;
    globalThis.safeFetch = vi.fn(() => jsonResponse({ success: true, annotations: [] }));

    await import('@js/security/xss-protection.js');
    await import('@js/components/annotation_surface.js');

    document.body.replaceChildren();
    await import('@js/components/research_notes.js');
    researchNotesHook = window.__researchNotesTest;

    await import('@js/components/news.js');

    installSubscriptionFixture();
    await import('@js/components/subscription-manager.js');
    if (!window.subscriptionManager) {
        document.dispatchEvent(new Event('DOMContentLoaded'));
    }
    subscriptionManager = window.subscriptionManager;
});

afterAll(() => {
    delete window.__VITEST_TEST__;
});

beforeEach(() => {
    document.body.replaceChildren();
    globalThis.safeFetch.mockReset();
    globalThis.safeFetch.mockImplementation(() => jsonResponse({ success: true, annotations: [] }));
    modalShow = vi.fn();
    globalThis.bootstrap = {
        Modal: vi.fn(function() {
            this.show = modalShow;
            this.hide = vi.fn();
        }),
    };
    subscriptionManager.folders = [];
    subscriptionManager.subscriptions = {};
    subscriptionManager.currentFolder = 'all';
});

describe('notes_shared.js output encoding', () => {
    it('renders a benign linked-note row through the production helper', () => {
        const row = window.NotesShared.renderNoteRow({
            id: 'note one',
            title: 'Benign title',
            content_preview: '**Benign preview**',
        });
        document.body.appendChild(row);

        expect(row.classList.contains('ldr-research-note-row')).toBe(true);
        expect(row.getAttribute('href')).toBe('/notes/note%20one');
        expect(row.querySelector('.ldr-research-note-title').textContent).toBe('Benign title');
        expect(row.querySelector('.ldr-research-note-preview').textContent).toBe('Benign preview');
        expectNoActiveMarkup(row);
    });

    it('keeps hostile note titles/previews as text and encodes href IDs', () => {
        const title = `${IMAGE_PAYLOAD} ${ATTRIBUTE_PAYLOAD}`;
        const preview = `${SCRIPT_PAYLOAD} ${SVG_PAYLOAD}`;
        const row = window.NotesShared.renderNoteRow({
            id: JAVASCRIPT_URL,
            title,
            content_preview: preview,
        });
        document.body.appendChild(row);

        expect(row.querySelector('.ldr-research-note-title')).not.toBeNull();
        expect(row.querySelector('.ldr-research-note-preview')).not.toBeNull();
        expect(row.getAttribute('href')).toBe(`/notes/${encodeURIComponent(JAVASCRIPT_URL)}`);
        expectInertText(row, [title, preview]);
    });
});

describe('research_notes.js output encoding', () => {
    it('renders a benign API note through the real load path', async () => {
        const list = await renderResearchNotes([{
            id: 'research-note-1',
            title: 'Linked research note',
            content_preview: 'A safe preview',
        }]);

        const row = list.querySelector('.ldr-research-note-row');
        expect(list.children).toHaveLength(1);
        expect(row.getAttribute('href')).toBe('/notes/research-note-1');
        expect(row.textContent).toContain('Linked research note');
        expect(row.textContent).toContain('A safe preview');
        expectNoActiveMarkup(list);
    });

    it('keeps every hostile API note display field inert through loadResearchNotes', async () => {
        const title = `${IMAGE_PAYLOAD} ${SVG_PAYLOAD}`;
        const preview = `${SCRIPT_PAYLOAD} ${ATTRIBUTE_PAYLOAD}`;
        const list = await renderResearchNotes([{
            id: JAVASCRIPT_URL,
            title,
            content_preview: preview,
        }]);

        const row = list.querySelector('.ldr-research-note-row');
        expect(list.children).toHaveLength(1);
        expect(row).not.toBeNull();
        expect(row.getAttribute('href')).toBe(`/notes/${encodeURIComponent(JAVASCRIPT_URL)}`);
        expectInertText(list, [title, preview]);
    });
});

describe('annotation_surface.js output encoding', () => {
    it('renders a benign annotation highlight and popover through init/reload', async () => {
        const { container, mark, popover } = await renderAnnotation({
            note_id: 'note-1',
            quote: 'benign highlighted quote',
            prefix: '',
            suffix: '',
            comment_preview: 'Benign comment',
        }, 'Report with a benign highlighted quote for review.');

        expect(mark.textContent).toBe('benign highlighted quote');
        expect(popover.querySelector('.ldr-annotation-popover-comment').textContent)
            .toBe('Benign comment');
        expect(popover.querySelector('a').getAttribute('href')).toBe('/notes/note-1');
        expectNoActiveMarkup(container);
        expectNoActiveMarkup(popover);
    });

    it('keeps hostile quotes/comments inert and percent-encodes note hrefs', async () => {
        const quote = `${IMAGE_PAYLOAD} ${SCRIPT_PAYLOAD} ${SVG_PAYLOAD}`;
        const comment = `${ATTRIBUTE_PAYLOAD} ${SCRIPT_PAYLOAD}`;
        const { container, mark, popover } = await renderAnnotation({
            note_id: JAVASCRIPT_URL,
            quote,
            prefix: '',
            suffix: '',
            comment_preview: comment,
        }, `Report prefix ${quote} report suffix.`);

        expect(mark.dataset.noteId).toBe(JAVASCRIPT_URL);
        expect(container.textContent).toContain(quote);
        expect(popover.querySelector('.ldr-annotation-popover-comment')).not.toBeNull();
        expect(popover.querySelector('a').getAttribute('href'))
            .toBe(`/notes/${encodeURIComponent(JAVASCRIPT_URL)}`);
        expectInertText(popover, [comment]);
        expectNoActiveMarkup(container);
    });

    it('keeps a hostile note_title fallback as visible popover text', async () => {
        const noteTitle = `${SCRIPT_PAYLOAD} ${SVG_PAYLOAD} ${ATTRIBUTE_PAYLOAD}`;
        const { popover } = await renderAnnotation({
            note_id: ATTRIBUTE_PAYLOAD,
            quote: 'title fallback target',
            prefix: '',
            suffix: '',
            comment_preview: '',
            note_title: noteTitle,
        }, 'A title fallback target is present in this report.');

        const body = popover.querySelector('.ldr-annotation-popover-comment');
        expect(body).not.toBeNull();
        expect(popover.querySelector('a').getAttribute('href'))
            .toBe(`/notes/${encodeURIComponent(ATTRIBUTE_PAYLOAD)}`);
        expectInertText(popover, [noteTitle]);
    });
});

describe('components/news.js output encoding', () => {
    it('renders benign empty/loading/error states and tags intact', () => {
        const empty = document.createElement('div');
        const loading = document.createElement('div');
        const error = document.createElement('div');
        const tag = window.createTag('Benign tag', 'ldr-safe-tag');
        document.body.append(empty, loading, error, tag);

        window.showEmptyState(empty, 'Nothing here', 'fas fa-box-open');
        window.showLoadingState(loading, 'Loading feed');
        window.showErrorState(error, 'Could not load feed');

        expect(empty.querySelector('.ldr-empty-state')).not.toBeNull();
        expect(empty.querySelector('p').textContent).toBe('Nothing here');
        expect(loading.querySelector('p').textContent).toBe('Loading feed');
        expect(error.querySelector('p').textContent).toBe('Could not load feed');
        expect(tag.textContent).toBe('Benign tag');
        expect(tag.className).toBe('ldr-safe-tag');
        expectNoActiveMarkup(document.body);
    });

    it('escapes state messages and contains an icon quote-breakout in class', () => {
        const empty = document.createElement('div');
        const loading = document.createElement('div');
        const error = document.createElement('div');
        document.body.append(empty, loading, error);

        window.showEmptyState(empty, IMAGE_PAYLOAD, ATTRIBUTE_PAYLOAD);
        window.showLoadingState(loading, SCRIPT_PAYLOAD);
        window.showErrorState(error, SVG_PAYLOAD);

        expect(empty.querySelector('.ldr-empty-state')).not.toBeNull();
        expect(loading.querySelector('.ldr-loading-placeholder')).not.toBeNull();
        expect(error.querySelector('.ldr-error-state')).not.toBeNull();
        expect(empty.querySelector('i').getAttribute('class')).toBe(ATTRIBUTE_PAYLOAD);
        expectInertText(empty, [IMAGE_PAYLOAD]);
        expectInertText(loading, [SCRIPT_PAYLOAD]);
        expectInertText(error, [SVG_PAYLOAD]);
    });

    it('uses DOM properties for hostile tag text and class values', () => {
        const text = `${IMAGE_PAYLOAD} ${SCRIPT_PAYLOAD} ${SVG_PAYLOAD}`;
        const tag = window.createTag(text, ATTRIBUTE_PAYLOAD);
        document.body.appendChild(tag);

        expect(tag.tagName).toBe('SPAN');
        expect(tag.getAttribute('class')).toBe(ATTRIBUTE_PAYLOAD);
        expectInertText(tag, [text]);
    });
});

describe('subscription-manager.js output encoding', () => {
    it('renders benign folder tabs and subscription cards intact', () => {
        installSubscriptionFixture();
        subscriptionManager.folders = [{
            name: 'Reading',
            icon: 'folder',
            item_count: 2,
        }];
        subscriptionManager.renderFolderTabs();

        subscriptionManager.subscriptions = { Reading: [makeSubscription()] };
        subscriptionManager.renderSubscriptions();

        const tab = [...document.querySelectorAll('#folderTabs .nav-link')]
            .find((element) => element.dataset.folder === 'Reading');
        const card = document.querySelector('.ldr-subscription-card');
        expect(tab).not.toBeUndefined();
        expect(tab.textContent).toContain('Reading');
        expect(tab.textContent).toContain('2');
        expect(card).not.toBeNull();
        expect(card.dataset.subscriptionId).toBe('subscription-1');
        expect(card.textContent).toContain('Quantum updates');
        expect(card.textContent).toContain('Every 60 min');
        expect(card.textContent).toContain('Review weekly');
        expect(card.querySelectorAll('.ldr-subscription-actions button')).toHaveLength(3);
        expectNoActiveMarkup(document.body);
    });

    it('escapes hostile folder names/icons in text and data attributes', () => {
        installSubscriptionFixture();
        const folderName = `${IMAGE_PAYLOAD} ${ATTRIBUTE_PAYLOAD}`;
        const folderIcon = `${SCRIPT_PAYLOAD} ${SVG_PAYLOAD}`;
        subscriptionManager.folders = [{
            name: folderName,
            icon: folderIcon,
            item_count: 7,
        }];
        subscriptionManager.renderFolderTabs();

        const tab = [...document.querySelectorAll('#folderTabs .nav-link')]
            .find((element) => element.dataset.folder === folderName);
        expect(tab).not.toBeUndefined();
        expect(tab.getAttribute('data-folder')).toBe(folderName);
        expectInertText(tab, [folderIcon, folderName]);
    });

    // Regression coverage: folder.item_count is coerced to a finite integer.
    test('does not materialize markup from a hostile folder item_count', () => {
        installSubscriptionFixture();
        subscriptionManager.folders = [{
            name: 'Count canary',
            icon: 'folder',
            item_count: IMAGE_PAYLOAD,
        }];
        subscriptionManager.renderFolderTabs();

        const tab = [...document.querySelectorAll('#folderTabs .nav-link')]
            .find((element) => element.dataset.folder === 'Count canary');
        expect(tab).not.toBeUndefined();
        expect(tab.textContent).toContain('Count canary');
        expectNoActiveMarkup(document.getElementById('folderTabs'));
    });

    it('escapes hostile subscription text/IDs and safely derives status/time output', () => {
        const cardId = ATTRIBUTE_PAYLOAD;
        const query = IMAGE_PAYLOAD;
        const folder = SCRIPT_PAYLOAD;
        const notes = SVG_PAYLOAD;
        const container = renderSubscriptionCards([makeSubscription({
            id: cardId,
            query_or_topic: query,
            folder,
            notes,
            next_refresh: IMAGE_PAYLOAD,
            status: ATTRIBUTE_PAYLOAD,
        })]);

        const card = container.querySelector('.ldr-subscription-card');
        expect(container.children).toHaveLength(1);
        expect(card).not.toBeNull();
        expect(card.dataset.subscriptionId).toBe(cardId);
        const actions = card.querySelectorAll('.ldr-subscription-actions button');
        expect(actions).toHaveLength(3);
        for (const action of actions) {
            expect(action.dataset.subscriptionId).toBe(cardId);
        }
        expect(card.querySelector('.pause-subscription-btn').title).toBe('Resume');
        expect(card.textContent).toContain('Next: NaNm');
        expectInertText(card, [query, folder, notes]);
    });

    // Regression coverage: refresh_interval_minutes is coerced to a finite integer.
    test('does not materialize markup from a hostile refresh interval', () => {
        const container = renderSubscriptionCards([makeSubscription({
            query_or_topic: 'Interval canary',
            refresh_interval_minutes: SCRIPT_PAYLOAD,
        })]);

        const card = container.querySelector('.ldr-subscription-card');
        expect(container.children).toHaveLength(1);
        expect(card).not.toBeNull();
        expect(card.textContent).toContain('Interval canary');
        expectNoActiveMarkup(container);
    });

    it('keeps hostile edit-modal folder text and textarea notes inert', async () => {
        installSubscriptionFixture();
        const folderName = `${ATTRIBUTE_PAYLOAD} ${SVG_PAYLOAD}`;
        const notes = `${IMAGE_PAYLOAD} ${SCRIPT_PAYLOAD}`;
        const subscription = makeSubscription({
            id: 'edit-subscription',
            refresh_interval_minutes: ATTRIBUTE_PAYLOAD,
            folder: folderName,
            notes,
        });
        subscriptionManager.folders = [{ name: folderName }];
        subscriptionManager.subscriptions = { Reading: [subscription] };

        await subscriptionManager.editSubscription(subscription.id);

        const modal = document.getElementById('editSubscriptionModal');
        expect(modal).not.toBeNull();
        expect(modal.querySelectorAll('#edit-folder option').length).toBeGreaterThan(1);
        const option = [...modal.querySelectorAll('#edit-folder option')]
            .find((element) => element.value === folderName);
        expect(option).not.toBeUndefined();
        expect(option.textContent).toBe(folderName);
        expect(option.value).toBe(folderName);
        expect(document.getElementById('edit-notes').value).toBe(notes);
        expect(globalThis.bootstrap.Modal).toHaveBeenCalledWith(modal);
        expect(modalShow).toHaveBeenCalledTimes(1);
        expectInertText(modal, [folderName]);
    });
});