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.

16.9 KB
Raw Blame
/**
 * Security regression tests for the localStorage re-hydration XSS surface of
 * pages/news.js — the paths that survive a page reload and are attacker-
 * writable via any earlier XSS or direct store poisoning.
 *
 * Traced paths (src/local_deep_research/web/static/js/pages/news.js):
 *
 *   saved_news_items:
 *     loadSavedNewsFeed (news.js:1383) reads + JSON.parse's the key (:1384,
 *     UNGUARDED) -> newsItems -> renderNewsItems (:864) builds the card
 *     template (:944-1079: headline escapeHtml :1011, summary escapeHtml :993,
 *     id escapeHtml :1007, source_url via safeHref :1066, links via
 *     renderSourceLinks :1801 -> safeHref/escapeHtml :1821-1823) ->
 *     safeRenderHTML (:5) -> window.DOMPurify.sanitize (:26).
 *     Entry: loadNewsFeed (:783) saved branch (:798-799), reached via
 *     window.selectSubscription('saved') (:775).
 *
 *   active_news_research:
 *     checkActiveNewsResearch (news.js:2568) reads + JSON.parse's the key
 *     (:2571, :2574 — inside try/catch) -> on in-progress status calls
 *     monitorResearch(researchId, query) (:2594) -> progress card via DIRECT
 *     newCard.innerHTML assignment (:2468-2485). DOMPurify is NOT on this
 *     path — escapeHtml is the only barrier: query :2472
 *     (escapeHtml(query.substring(0, 60))), researchId :2476, status message
 *     :2483, progress coerced via Number() :2480-2481.
 *     Entry: DOMContentLoaded (:141) -> initializeNewsPage (:146) ->
 *     await checkActiveNewsResearch() (:191).
 *
 *   saved_news_ids / expanded_news_ids:
 *     loadSavedItems (:1301) / loadExpandedState (:1317) read + parse inside
 *     try/catch (malformed -> empty Set). The stored strings never reach
 *     innerHTML directly — they only feed `Set.has(item.id)` boolean checks
 *     (:1004, :1069-1070). Hydrated via initializeNewsPage (:159, :162).
 *
 * Unlike news-xss.test.js / news-search-history-xss.test.js (server-response
 * data), these tests poison localStorage directly, then drive the REAL
 * re-hydration triggers: window.loadNewsFeed() for the saved feed and a
 * re-dispatched DOMContentLoaded for the page-load paths.
 */

// Backend-injected constants (base.html injects these in production; stub
// before importing config/constants.js, which freezes window.ResearchStates
// around them — same approach as tests/js/config/constants.test.js).
window.RESEARCH_STATUS = {
    IN_PROGRESS: 'in_progress',
    COMPLETED: 'completed',
    FAILED: 'failed',
    SUSPENDED: 'suspended',
    CANCELLED: 'cancelled',
    QUEUED: 'queued',
    PENDING: 'pending',
    ERROR: 'error',
};
window.RESEARCH_TERMINAL_STATES = new Set([
    'completed', 'suspended', 'failed', 'error', 'cancelled',
]);

const IMG_ONERROR = '<img src=x onerror=alert(1)>';
const QUOTE_BREAK = '"><a href="javascript:alert(1)">click</a>';
const SCRIPT_PAYLOAD = '<script>alert(1)</script>';
// Attribute-context breakout aimed at item.id, the ONLY field that reached
// the card's inline handlers. escapeAttr backslash-escaped the quote, which
// the HTML parser discards, leaving onmouseover as a genuine handler.
const ATTR_BREAK = '" onmouseover="alert(1)';

let sanitizeSpy;
// fetchResponses is keyed by URL substring (search-history harness pattern);
// '/news/api/feed' and '/api/research/' (URLBuilder.researchStatus shape:
// '/api/research/{id}/status') are the endpoints the re-hydration paths hit.
let fetchResponses;
let statusFetches = 0;

function feedContainer() {
    return document.getElementById('news-feed-content');
}

function flush(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms));
}

/** Names of any inline event-handler attributes (on*) anywhere under root. */
function onHandlerAttributes(root) {
    const found = [];
    for (const el of root.querySelectorAll('*')) {
        for (const name of el.getAttributeNames()) {
            if (/^on/i.test(name)) found.push(`${el.tagName}[${name}]`);
        }
    }
    return found;
}

/** on* attributes whose value carries the attacker payload marker.
 *
 * This used to carry a note declaring the card template's static onclick
 * handlers legitimate. They were not: they interpolated item.id through
 * escapeAttr (a JavaScript-string escaper) into an HTML attribute, so a
 * quote in the id closed onclick and the remainder tokenized as real
 * attributes. The card template now ships NO inline handlers at all --
 * ids ride in data-* attributes and are read back by delegation -- so any
 * on* attribute under a rendered card is a finding. */
function payloadHandlerAttributes(root) {
    const found = [];
    for (const el of root.querySelectorAll('*')) {
        for (const name of el.getAttributeNames()) {
            if (/^on/i.test(name) && (el.getAttribute(name) || '').includes('alert(')) {
                found.push(`${el.tagName}[${name}]`);
            }
        }
    }
    return found;
}

/** Any anchor whose href uses the javascript: scheme. */
function javascriptHrefs(root) {
    return [...root.querySelectorAll('a[href]')]
        .map((a) => (a.getAttribute('href') || '').trim())
        .filter((href) => href.toLowerCase().startsWith('javascript:'));
}

function inertContainerAssertions(root) {
    expect(root.querySelector('script')).toBeNull();
    expect(payloadHandlerAttributes(root)).toEqual([]);
    expect(javascriptHrefs(root)).toEqual([]);
}

beforeAll(async () => {
    // Real escaper first — window.escapeHtml from security/xss-protection.js
    // is the production barrier on the innerHTML sink at news.js:2468.
    await import('@js/security/xss-protection.js');
    // window.ResearchStates + window.URLBuilder/URLS, mirroring the base.html
    // script order news.js depends on as bare globals.
    await import('@js/config/constants.js');
    await import('@js/config/urls.js');

    sanitizeSpy = vi.fn((htmlString) => {
        const template = document.createElement('template');
        // eslint-disable-next-line no-unsanitized/property -- test-only spy: htmlString is the pre-sanitized template output we explicitly capture for assertions; runs only under Vitest, never in production.
        template.innerHTML = htmlString;
        return template.content;
    });
    window.DOMPurify = { isSupported: true, sanitize: sanitizeSpy };

    fetchResponses = {
        default: {},
        '/news/api/feed': { news_items: [] },
        '/api/research/': { status: 'in_progress', progress: 45, message: 'Analyzing' },
    };
    globalThis.fetch = vi.fn((url) => {
        const urlStr = String(url);
        let body = fetchResponses.default;
        for (const [key, value] of Object.entries(fetchResponses)) {
            if (key !== 'default' && urlStr.includes(key)) {
                body = value;
                if (key === '/api/research/') statusFetches += 1;
                break;
            }
        }
        return Promise.resolve({
            ok: true,
            status: 200,
            json: () => Promise.resolve(body),
            text: () => Promise.resolve(''),
        });
    });

    document.body.innerHTML = `
        <div id="news-feed-content"></div>
        <input id="table-view-toggle" type="checkbox">
        <tbody id="news-table-body"></tbody>
        <input id="news-search" type="text">
        <button id="search-btn"></button>
        <button id="create-subscription-btn"></button>
        <button id="run-template-btn"></button>
        <input id="impact-filter" type="range" min="0" max="10" 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>
        <div id="news-subscription-query"></div>
        <div class="ldr-time-filter-group"><button class="ldr-filter-btn"></button></div>
        <input id="auto-refresh" type="checkbox">
        <button id="refresh-feed-btn"></button>
        <div id="recent-searches"></div>
        <div id="trending-topics"></div>
    `;

    localStorage.clear();
    await import('@js/pages/news.js');

    // Pin the module to the saved view — loadNewsFeed's saved branch
    // (news.js:798-799) is the only reader of saved_news_items.
    window.selectSubscription('saved');
});

beforeEach(() => {
    localStorage.clear();
    sanitizeSpy.mockClear();
    statusFetches = 0;
    fetchResponses['/news/api/feed'] = { news_items: [] };
    fetchResponses['/api/research/'] = { status: 'in_progress', progress: 45, message: 'Analyzing' };
    feedContainer().innerHTML = '';
});

/** The pre-sanitized htmlString (news.js:1082 -> safeRenderHTML) containing
 * `substring`, or null — same capture approach as news-render-harness.js. */
function capturedHtmlFor(substring) {
    for (const [htmlString] of sanitizeSpy.mock.calls) {
        if (typeof htmlString === 'string' && htmlString.includes(substring)) {
            return htmlString;
        }
    }
    return null;
}

describe('pages/news.js — saved_news_items localStorage re-hydration XSS', () => {
    it('renders a hostile saved article as inert text (no on* handlers, no <script>, no javascript: hrefs)', async () => {
        localStorage.setItem('saved_news_items', JSON.stringify({
            'xss-saved-1': {
                // item.id is the field that reached the card's action
                // buttons. The corpus used to poison headline/source_url/links
                // but never this one, routing around the detector below.
                id: `xss-saved-1${ATTR_BREAK}`,
                headline: `${IMG_ONERROR}${QUOTE_BREAK}${SCRIPT_PAYLOAD}`,
                summary: `${QUOTE_BREAK} ${IMG_ONERROR}`,
                source_url: 'javascript:alert(1)',
                links: [{ url: 'javascript:alert(1)', title: SCRIPT_PAYLOAD }],
                topics: [IMG_ONERROR],
                impact_score: 5,
                created_at: new Date().toISOString(),
                savedAt: new Date().toISOString(),
            },
        }));

        // Real trigger: saved branch of loadNewsFeed -> loadSavedNewsFeed
        // (news.js:1383) reads the poisoned key and renders it.
        await window.loadNewsFeed();
        await flush(10); // let post-render vote fetches settle

        const container = feedContainer();
        expect(container.textContent).toContain('alert(1)'); // something rendered

        // Pre-sanitized template output: payloads must arrive escaped.
        const html = capturedHtmlFor('xss-saved-1');
        expect(html).not.toBeNull();
        expect(html).not.toContain('<img src=x');
        expect(html).not.toContain('<script');
        expect(html).not.toContain('href="javascript:');
        expect(html).toContain('&lt;img src=x onerror=alert(1)&gt;');

        // The hostile id must not have produced an attribute breakout. A
        // string check is not enough here -- the id is escapeHtml'd into a
        // data-* attribute, so the literal text " onmouseover=" survives
        // inside the attribute VALUE while being completely inert. Only the
        // parsed DOM can tell the two apart, and the card template now ships
        // no inline handlers of its own, so any on* attribute under a card is
        // a finding.
        //
        // Both cards and their sibling bulk controls use registered listeners.
        const cards = [...container.querySelectorAll('.ldr-news-item')];
        expect(cards.length).toBeGreaterThan(0);
        for (const cardEl of cards) {
            expect(onHandlerAttributes(cardEl)).toEqual([]);
        }

        // Keep the non-card controls free of executable attributes too.
        const nonCardHandlerValues = [];
        for (const el of container.querySelectorAll('*')) {
            if (el.closest('.ldr-news-item')) continue;
            for (const name of el.getAttributeNames()) {
                if (/^on/i.test(name)) nonCardHandlerValues.push(el.getAttribute(name));
            }
        }
        expect(nonCardHandlerValues).toEqual([]);

        // Post-parse DOM: nothing executable, payload visible as text.
        inertContainerAssertions(container);
        expect(container.textContent).toContain(IMG_ONERROR);
        expect(container.textContent).toContain('javascript:alert(1)');
    });
});

describe('pages/news.js — active_news_research localStorage re-hydration XSS', () => {
    it('renders a hostile query/status through checkActiveNewsResearch -> monitorResearch as inert text', async () => {
        localStorage.setItem('active_news_research', JSON.stringify({
            researchId: 'research-xss-canary',
            query: `${IMG_ONERROR}${QUOTE_BREAK}${SCRIPT_PAYLOAD}`,
            startTime: new Date().toISOString(),
        }));
        // Server-side fields rendered on the same card (escapeHtml :2483,
        // Number() :2480-2481) — poisoned to prove the card sinks hold.
        fetchResponses['/api/research/'] = {
            status: 'in_progress',
            progress: SCRIPT_PAYLOAD,
            message: IMG_ONERROR,
        };

        // Real trigger: page-load re-hydration.
        document.dispatchEvent(new Event('DOMContentLoaded'));
        await vi.waitFor(() => {
            if (!document.querySelector('.ldr-active-research-card')) {
                throw new Error('progress card has not rendered yet');
            }
        });

        // NOTE: this card is a DIRECT newCard.innerHTML assignment
        // (news.js:2468) — DOMPurify is not on this path, escapeHtml is the
        // only barrier, so assert on the parsed DOM itself.
        const card = document.querySelector('.ldr-active-research-card');
        inertContainerAssertions(card);
        // The progress-card template (news.js:2468-2485) ships no static
        // handlers of its own, so ANY on* attribute here would be payload.
        expect(onHandlerAttributes(card)).toEqual([]);
        // Payload survived as inert text (query is truncated at 60 chars,
        // which still covers the full <img ...> payload).
        expect(card.textContent).toContain(IMG_ONERROR);
        expect(card.innerHTML).toContain('&lt;img src=x onerror=alert(1)&gt;');
    });

    it('does not crash or leak when the stored researchId itself is hostile', async () => {
        localStorage.setItem('active_news_research', JSON.stringify({
            researchId: `${QUOTE_BREAK}${IMG_ONERROR}`,
            query: 'benign query',
            startTime: new Date().toISOString(),
        }));

        document.dispatchEvent(new Event('DOMContentLoaded'));
        // The current renderer compares dataset values directly, so a
        // quote-bearing ID remains data and still gets a progress card.
        await vi.waitFor(() => expect(statusFetches).toBeGreaterThanOrEqual(2));
        await flush(50);

        const card = document.querySelector('.ldr-active-research-card');
        expect(card).not.toBeNull();
        expect(card.dataset.researchId).toBe(`${QUOTE_BREAK}${IMG_ONERROR}`);
        expect(onHandlerAttributes(card)).toEqual([]);
        inertContainerAssertions(feedContainer());
    });
});

describe('pages/news.js — malformed localStorage JSON on re-hydration', () => {
    it('saved_news_items: unguarded JSON.parse rejects the load (documented actual behavior)', async () => {
        // news.js:1384 parses this key with NO try/catch. loadNewsFeed is
        // async, so the SyntaxError surfaces as a rejection. On a real page
        // reload initializeNewsPage awaits this call (news.js:173) with no
        // surrounding catch, leaving an unhandled rejection and the feed stuck
        // on the loading spinner — a robustness gap, but NOT an XSS: nothing
        // from the malformed value reaches a sink. Assert the actual behavior
        // rather than guessing a cleanup that does not exist.
        localStorage.setItem('saved_news_items', '{not-valid-json');
        await expect(window.loadNewsFeed()).rejects.toThrow(SyntaxError);
    });

    it('saved_news_ids / expanded_news_ids: malformed JSON is caught and the feed still renders', async () => {
        localStorage.setItem('saved_news_ids', 'not-json-at-all');
        localStorage.setItem('expanded_news_ids', '[[[');

        document.dispatchEvent(new Event('DOMContentLoaded'));
        // loadSavedItems (:1307) and loadExpandedState (:1323) swallow the
        // SyntaxError and reset to empty Sets, so initializeNewsPage completes
        // and renders the (empty) saved feed.
        await vi.waitFor(() => {
            expect(feedContainer().textContent).toContain('No saved items');
        });
        inertContainerAssertions(feedContainer());
    });

    it('active_news_research: malformed JSON is caught and the poisoned key is cleared', async () => {
        localStorage.setItem('active_news_research', '{broken');

        document.dispatchEvent(new Event('DOMContentLoaded'));
        // checkActiveNewsResearch's catch (news.js:2606-2609) removes the key.
        await vi.waitFor(() => {
            expect(localStorage.getItem('active_news_research')).toBeNull();
        });
        expect(document.querySelector('.ldr-active-research-card')).toBeNull();
        inertContainerAssertions(feedContainer());
    });
});