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.7 KB
Raw Blame
/**
 * Regression tests for SemanticSearch.renderSnippet's sanitize ordering.
 *
 * renderSnippet() highlights query terms by injecting <mark> into an HTML
 * string with a tag-splitting regex. That surgery MUST happen before the
 * DOMPurify pass, never after: HTML serialization does not escape ">" inside
 * attribute values, so an attribute containing ">" desynchronizes the
 * highlighter's view of the markup from the real parser's, and the <mark> it
 * injects at that point supplies the quote that closes the attribute for real.
 *
 * Running the highlighter on sanitizer OUTPUT therefore reintroduces markup
 * DOMPurify had already neutralized. Four sinks assign renderSnippet output
 * straight to innerHTML: components/history.js (twice),
 * components/library_search_ui.js, and createSemanticResultCard() in
 * components/semantic_search.js itself — whose cards are appended verbatim,
 * with no re-sanitize of their own, by six callers: the library tier-3 list
 * and library search (components/library_search_ui.js,
 * components/library_search.js), both news search paths and its own tier-3
 * list (pages/news.js), collection-details search results
 * (collection_details.js), and history search (components/history_search.js).
 * A third tier-3 list, in components/history.js, does not call
 * createSemanticResultCard() at all — it builds its own card, which is one
 * of the two components/history.js sinks counted above. The only re-sanitize
 * on the news page happens in renderNewsItems() (its safeRenderHTML() sink),
 * which is reached from many call sites, not from a single "load" path.
 *
 * WHY THE PAYLOAD LOOKS INERT: marked escapes ">" to "&gt;" when it builds the
 * title attribute, so the string handed to the sanitizer contains no raw ">".
 * The round trip THROUGH DOMPurify is what creates one: parsing "&gt;" yields a
 * ">" character in the attribute VALUE, and HTML serialization does not escape
 * "<" or ">" inside attribute values (only "&", U+00A0 and the quote). Verified
 * directly:
 *
 *   in  : <span title="A&gt;budget&lt;img src=x onerror=BOOM()&gt;B">hi</span>
 *   out : <span title="A>budget<img src=x onerror=BOOM()>B">hi</span>
 *
 * So the exposure exists only when highlighting runs AFTER sanitizing, which is
 * exactly what these tests pin. Do not "simplify" the payload to a literal raw
 * ">" — marked would escape it and the test would pass vacuously.
 *
 * WHY THE PAYLOAD STARTS WITH "**x** ": that leading <strong> is a sacrificial
 * node, the same workaround tests/js/services/ui.test.js uses (see its
 * "renders math alongside other markdown" case). DOMPurify >=3.4.8 reads
 * element names through the cached Node.prototype.nodeName getter, and
 * happy-dom's getter returns "" when invoked that way, so the FIRST top-level
 * node of a sanitized fragment is treated as an unknown tag and unwrapped —
 * tags dropped, text kept. Upstream: capricorn86/happy-dom#2182. Without the
 * prefix the payload's <a> is the lone top-level node, gets unwrapped, and BOTH
 * orderings collapse to "hi" — the tests would pass on vulnerable code. Real
 * browsers are unaffected; this is a test-env artifact only. Do not remove it.
 *
 * WHAT THIS ENVIRONMENT CAN AND CANNOT SEE: the same happy-dom defect means
 * DOMPurify's tag/attribute allow-lists are NOT enforced on the remaining
 * top-level nodes here — only the first one is inspected. So nothing below can
 * observe ALLOWED_TAGS/ALLOWED_ATTR; those are pinned from source instead, in
 * tests/security/test_client_side_xss_sinks.py. What these tests DO observe is
 * the property the fix actually turns on: DOMPurify re-parses and re-serializes
 * the string, so a tag boundary the highlighter broke is repaired before the
 * value reaches a caller — and running the highlighter afterwards instead
 * breaks it again, unrepaired.
 *
 * ALSO NOTE: on correct code the literal text "onerror" DOES appear in the
 * output, inert, inside the title attribute value. Assertions here are made at
 * the DOM level (re-parse, then look for event-handler attributes and
 * elements), never with a substring match on the serialized string.
 *
 * Each test below names the revert it catches.
 */

import DOMPurify from 'dompurify';
import { marked } from 'marked';
import '@js/components/semantic_search.js';

const SS = window.SemanticSearch;

/**
 * A title attribute whose value contains ">" and a would-be element, behind a
 * sacrificial leading node (see the happy-dom note in the file header).
 */
const ATTR_BOUNDARY_MD =
    '**x** [hi](https://example.com "A>budget<img src=x onerror=BOOM()>B")';

beforeAll(() => {
    window.DOMPurify = DOMPurify;
    window.marked = marked;
});

/** Re-parse an HTML string and return the host element. */
function parse(html) {
    const host = document.createElement('div');
    // eslint-disable-next-line no-unsanitized/property -- test code
    host.innerHTML = html;
    return host;
}

/** Every event-handler attribute that survived a re-parse, as "TAG@attr". */
function eventHandlers(html) {
    return Array.from(parse(html).querySelectorAll('*')).flatMap((el) =>
        Array.from(el.attributes)
            .filter((a) => /^on/i.test(a.name))
            .map((a) => `${el.tagName}@${a.name}`),
    );
}

describe('renderSnippet sanitizes after highlighting', () => {
    // CATCHES: moving the highlightTerms() call back after the
    // DOMPurify.sanitize() call in renderSnippet(). Under the old order this
    // output re-parses to <a title="A><mark ...>budget</mark><img src=x
    // onerror=BOOM()>B"> — a live IMG@onerror.
    it('does not let a ">" inside an attribute value smuggle an event handler', () => {
        const out = SS.renderSnippet(ATTR_BOUNDARY_MD, 'budget');

        // DOM-level, not a substring match: "onerror" legitimately appears as
        // inert text inside the title attribute value on correct code.
        expect(eventHandlers(out)).toEqual([]);
    });

    // CATCHES: the same revert, via the element rather than the attribute.
    it('never emits an <img> from attribute-boundary confusion', () => {
        const out = SS.renderSnippet(ATTR_BOUNDARY_MD, 'budget');
        expect(parse(out).querySelector('img')).toBeNull();
    });

    // CATCHES: dropping the highlightTerms() call, or gating it off. It does
    // NOT catch the ordering revert — on an ordinary snippet both orders
    // produce the same <mark>; the two payload tests above are what pin order.
    it('still highlights matching terms in ordinary snippets', () => {
        const out = SS.renderSnippet('the budget report', 'budget');
        expect(out).toMatch(/<mark[^>]*>budget<\/mark>/i);
    });

    // CATCHES: changing or dropping the highlight class that
    // css/components/semantic-search.css styles. It does NOT catch removing
    // 'mark'/'class' from the sanitizer allow-lists — see the note in the file
    // header on why this environment cannot observe them; that revert is
    // caught from source in tests/security/test_client_side_xss_sinks.py.
    it('emits the highlight class the stylesheet keys off', () => {
        const out = SS.renderSnippet('the budget report', 'budget');
        expect(out).toMatch(/<mark class="ldr-search-highlight">/);
    });

    // CATCHES: reintroducing highlightTerms in the no-sanitizer fallback,
    // where there is no pass afterwards to repair a broken boundary.
    it('escapes wholesale and does not highlight when DOMPurify is absent', () => {
        const saved = window.DOMPurify;
        window.DOMPurify = undefined;
        try {
            const out = SS.renderSnippet('<b>budget</b>', 'budget');
            expect(out).not.toMatch(/<mark/i);
            expect(out).not.toMatch(/<b>/i);
        } finally {
            window.DOMPurify = saved;
        }
    });
});