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.

7.0 KB
Raw Blame
/**
 * Regression tests for the XSS hardening in pages/news.js (PR #3095).
 *
 * The render path runs every template literal through safeRenderHTML ->
 * window.DOMPurify.sanitize, so DOMPurify is the runtime XSS barrier.
 * The Number()/escapeHtml() wrappers added in PR #3095 are defense-in-depth:
 * they stop a payload from reaching DOMPurify in an executable shape —
 * notably inside `style="width: ..."` (where escapeHtml would not help) and
 * inside text nodes that DOMPurify would technically allow through if the
 * surrounding markup looked valid.
 *
 * To assert the *source-level* escaping rather than DOMPurify's output, we
 * spy on DOMPurify.sanitize and inspect the pre-sanitized htmlString that
 * the template produced. If the literal `<script>` substring shows up there,
 * a future edit has dropped one of the Number()/escapeHtml() wrappers.
 *
 * Scope: the fields touched by PR #3095 — impact_score (style attribute and
 * text node), upvotes/downvotes (text node), and time_ago (text node wrapped
 * by escapeHtml that previously only covered one branch of a || chain).
 *
 * The shared render scaffold lives in helpers/news-render-harness.js.
 */

import {
    installNewsRenderHarness,
    makeNewsItem as makeItem,
} from './helpers/news-render-harness.js';

const PAYLOAD = '<script>alert(1)</script>';

let harness;

beforeAll(async () => {
    harness = await installNewsRenderHarness();
});

async function renderWith(items) {
    return harness.renderWith(items);
}

function renderedHtmlForHeadline(headline) {
    return harness.capturedHtmlFor(headline) || '';
}

describe('pages/news.js — XSS regression for PR #3095', () => {
    it('does not leak raw <script> from impact_score into the style attribute', async () => {
        const html = await renderWith([makeItem({
            id: 'impact-payload',
            headline: 'IMPACT_CANARY',
            impact_score: PAYLOAD,
        })]).then(() => renderedHtmlForHeadline('IMPACT_CANARY'));

        expect(html).not.toBe('');
        // Style-attribute injection: a payload inside style="width:..."
        // would let an attacker inject e.g. `1;background:url(javascript:...)`.
        // Number() coerces the payload to NaN, so the literal <script>
        // substring must never appear in the produced HTML.
        expect(html).not.toContain(PAYLOAD);
        // The "/10" text node is interpolated from the same field and must
        // also be free of the payload.
        expect(html).not.toMatch(new RegExp(PAYLOAD.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '\\s*/10'));
    });

    it('does not leak raw <script> from upvotes/downvotes into vote buttons', async () => {
        const html = await renderWith([makeItem({
            id: 'vote-payload',
            headline: 'VOTE_CANARY',
            upvotes: PAYLOAD,
            downvotes: PAYLOAD,
        })]).then(() => renderedHtmlForHeadline('VOTE_CANARY'));

        expect(html).not.toBe('');
        expect(html).not.toContain(PAYLOAD);
    });

    it('does not leak raw <script> from time_ago into the date span', async () => {
        // time_ago only interpolates when formatNewsDate(item.created_at) is
        // falsy. Send no created_at so the right side of the || chain fires
        // — that's the branch PR #3095 wrapped in escapeHtml().
        const html = await renderWith([makeItem({
            id: 'time-payload',
            headline: 'TIME_CANARY',
            created_at: null,
            time_ago: PAYLOAD,
        })]).then(() => renderedHtmlForHeadline('TIME_CANARY'));

        expect(html).not.toBe('');
        // escapeHtml turns <script> into &lt;script&gt;. The literal payload
        // substring must not survive.
        expect(html).not.toContain(PAYLOAD);
        expect(html).toContain('&lt;script&gt;');
    });
});

describe('pages/news.js — safeHref open redirect (frontend-security-xss-hardening gate defect (4))', () => {
    // safeHref() used to short-circuit on `startsWith('/')`, so
    // `//evil.com` and `/\evil.com` reached the "View Full Report" href
    // unchanged -- browsers resolve both as protocol-relative, i.e. an
    // open redirect off the current origin. isSafeHref
    // (security/xss-protection.js) rejects this form outright; safeHref
    // now does too, via an explicit check rather than relying on
    // URLValidator.isSafeUrl (which accepts a bare "//evil.com" absent a
    // trustedDomains restriction). This is narrower than an open redirect
    // to any host: safeHref, like isSafeUrl, still accepts a plain
    // "https://evil.com" source_url -- only the disguised "//"/"/\"
    // spelling that used to be misclassified as an internal path is now
    // rejected.
    //
    // Parse the captured pre-sanitized markup and read the href back off the
    // anchor, rather than regexing the raw attribute text. safeHref returns
    // escapeHtml(trimmed) for an accepted internal path, so the raw attribute
    // reads "&#x2F;results&#x2F;42"; getAttribute() gives the decoded value
    // the browser would navigate to, which is what these cases are about.
    // That keeps the assertions stable if the escaping (or the attribute
    // order in the template) ever changes.
    function viewFullReportHref(html) {
        const holder = document.createElement('template');
        // eslint-disable-next-line no-unsanitized/property -- test-only: `html` is the pre-sanitized template output captured from the DOMPurify spy, parsed here purely to read the href back; runs only under Vitest, never in production.
        holder.innerHTML = html;
        const link = holder.content.querySelector(
            'a[data-news-action="mark-read-on-click"]',
        );
        return link ? link.getAttribute('href') : null;
    }

    it.each([
        ['protocol-relative', '//evil.com'],
        ['slash-backslash', '/\\evil.com'],
        // A control character (tab) between the two slashes: browsers
        // strip C0 controls before parsing a URL, so "/\t/evil.com"
        // resolves exactly like "//evil.com". safeHref's `probe` strips
        // control characters before the protocol-relative check for
        // exactly this reason -- this exercises that stripping, not just
        // the already-normalized forms above.
        ['control-character', '/\t/evil.com'],
    ])('rejects a %s source_url on the View Full Report link', async (_label, sourceUrl) => {
        const html = await renderWith([makeItem({
            id: 'redirect-payload',
            headline: 'REDIRECT_CANARY',
            source_url: sourceUrl,
        })]).then(() => renderedHtmlForHeadline('REDIRECT_CANARY'));

        expect(html).not.toBe('');
        const href = viewFullReportHref(html);
        expect(href).toBe('#');
    });

    it('still allows a normal internal path source_url through', async () => {
        const html = await renderWith([makeItem({
            id: 'benign-link',
            headline: 'BENIGN_LINK_CANARY',
            source_url: '/results/42',
        })]).then(() => renderedHtmlForHeadline('BENIGN_LINK_CANARY'));

        expect(html).not.toBe('');
        expect(viewFullReportHref(html)).toBe('/results/42');
    });
});