~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
/**
* Security regression tests for the XSS surface of components/results.js —
* the results page's primary render surface for LLM/web-derived report
* content, metadata and error strings.
*
* Interpolation map (results.js @ commit under test):
* :148 loading spinner innerHTML — static string, no interpolation
* :598 data.html via window.sanitizeHtml — DOMPurify SANITIZE_CONFIG
* (xss-protection.js:329)
* :602 data.html via window.escapeHtml — escaped (& < > " ' /)
* :606 data.html via textContent — inert by construction
* :632 content via window.ui.renderMarkdown — delegated to services/ui.js
* (marked -> DOMPurify allowlist ->
* hardening; covered end-to-end
* here with the real pipeline)
* :637/:644 fallback `<pre>${escaped}</pre>` — (window.escapeHtml ||
* escapeHtmlFallback @ :21)
* :665/:670 showError alert innerHTML — escaped message, hardcoded
* href="/" @ :673
* :216-:292, :373-:572 query/date/mode — textContent only (inert)
* :248-:259 duration — parseInt + isNaN guard (numeric)
* :857/:891/:909 pdf button innerHTML — static strings
* No data-driven raw innerHTML interpolation exists; URL writes all go
* through URLValidator.safeAssign with URLBuilder-built paths.
*
* Driving the real paths: results.js is an IIFE with no exports that calls
* initializeResults() at import time (happy-dom reports readyState
* 'complete'), so each mount installs fresh DOM + fetch mocks, resets the
* module registry, and dynamically imports the component. The REAL
* xss-protection.js escaper and the REAL services/ui.js renderMarkdown
* pipeline (marked + katex + DOMPurify) are installed so assertions exercise
* production escaping end-to-end, mirroring
* tests/js/security/markdown-render-xss.test.js.
*/
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
const RESEARCH_ID = 'research-123';
const RESULTS_MARKUP = `
<h1 id="result-query"></h1>
<p id="result-date"></p>
<p id="result-mode"></p>
<div id="results-content"></div>
<button id="export-markdown-btn" disabled></button>
<button id="download-pdf-btn" disabled></button>
<button id="view-metrics-btn"></button>
`;
const TEXT_PAYLOADS = [
'<img src=x onerror=alert(1)>',
'<script>alert(2)</script>',
'"><svg onload=alert(3)>',
];
const ATTR_PAYLOAD = 'x" onmouseover="alert(1)';
let realUi;
let realEscapeHtml;
let realSanitizeHtml;
beforeAll(async () => {
// --- happy-dom shims required by DOMPurify -------------------------------
// Copied from tests/js/security/markdown-render-xss.test.js: happy-dom's
// NodeIterator truncates on removal, its Node.prototype#nodeName base
// getter returns "" for every tag, and parsed <math> never gets the
// MathML namespace. All three are captured by dompurify at module-eval
// time, so the shims must be installed BEFORE the dynamic import below.
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 MATHML_TAG_NAMES = new Set([
'math', 'semantics', 'annotation', 'annotation-xml', 'maction',
'maligngroup', 'malignmark', 'menclose', 'merror', 'mfenced',
'mfrac', 'mglyph', 'mi', 'mlabeledtr', 'mlongdiv', 'mmultiscripts',
'mn', 'mo', 'mover', 'mpadded', 'mphantom', 'mprescripts', 'mroot',
'mrow', 'ms', 'mscarries', 'msgroup', 'msline', 'mspace', 'msqrt',
'msrow', 'mstack', 'mstyle', 'msub', 'msubsup', 'msup', 'mtable',
'mtd', 'mtext', 'mtr', 'munder', 'munderover', 'none',
]);
const MATHML_NAMESPACE_URI = 'http://www.w3.org/1998/Math/MathML';
const originalNamespaceURI = Object.getOwnPropertyDescriptor(
window.Element.prototype, 'namespaceURI',
);
Object.defineProperty(window.Element.prototype, 'namespaceURI', {
configurable: true,
get() {
if (typeof this.tagName === 'string'
&& MATHML_TAG_NAMES.has(this.tagName.toLowerCase())) {
return MATHML_NAMESPACE_URI;
}
return originalNamespaceURI && originalNamespaceURI.get
? originalNamespaceURI.get.call(this)
: undefined;
},
});
// Stubs for the URL globals results.js reaches at init/click time,
// assigned before any await (require-atomic-updates).
window.URLBuilder = {
extractResearchIdFromPattern: () => RESEARCH_ID,
detailsPage: (id) => `/research/details/${id}`,
};
window.URLValidator = {
safeAssign: (obj, prop, value) => { obj[prop] = value; },
};
window.URLS = { PAGES: { HISTORY: '/history' } };
// --- production globals --------------------------------------------------
// Real escaper/sanitizer (window.escapeHtml, window.sanitizeHtml) and the
// real renderMarkdown pipeline (window.ui), as loaded by base.html.
await import('@js/security/xss-protection.js');
const { Marked } = await import('marked');
const markedKatex = (await import('marked-katex-extension')).default;
const DOMPurify = (await import('dompurify')).default;
const markedInstance = new Marked();
markedInstance.use(markedKatex({
throwOnError: false,
errorColor: 'currentColor',
}));
globalThis.marked = markedInstance;
globalThis.DOMPurify = DOMPurify;
await import('@js/services/ui.js');
realUi = window.ui;
realEscapeHtml = window.escapeHtml;
realSanitizeHtml = window.sanitizeHtml;
});
afterAll(() => {
delete globalThis.marked;
delete globalThis.DOMPurify;
});
afterEach(() => {
// Tests delete/undefine these to isolate fallback paths; restore the
// production globals for the next test.
window.ui = realUi;
window.escapeHtml = realEscapeHtml;
window.sanitizeHtml = realSanitizeHtml;
});
function okResponse(data) {
return { ok: true, json: () => Promise.resolve(data) };
}
/**
* Mount the real results component against a fresh DOM and a fetch mock that
* serves `payload` from /api/report/:id (or rejects with `error`), then wait
* for the loading spinner to be replaced by rendered output.
*/
async function mountResults({ payload, error } = {}) {
// eslint-disable-next-line no-unsanitized/property -- test fixture: hardcoded static template with no interpolated values.
document.body.innerHTML = RESULTS_MARKUP;
globalThis.fetch = vi.fn((url) => {
if (url === `/api/report/${RESEARCH_ID}`) {
return error ? Promise.reject(error) : Promise.resolve(okResponse(payload));
}
if (url === `/metrics/api/research/${RESEARCH_ID}/context-overflow`) {
return Promise.resolve({ ok: false, status: 404 });
}
return Promise.reject(new Error(`Unexpected fetch: ${url}`));
});
// happy-dom reports readyState 'complete', so the IIFE runs
// initializeResults() synchronously at import: reset the registry and
// re-import so each mount captures this test's DOM and fetch mock.
vi.resetModules();
await import('@js/components/results.js');
const container = document.getElementById('results-content');
await vi.waitFor(() => {
expect(container.innerHTML).not.toContain('fa-spinner');
});
return container;
}
/** Names of any inline event-handler attributes (on*) anywhere under root. */
function eventHandlerAttributes(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;
}
/** Non-vacuity guard: the container must actually contain rendered output. */
function expectRendered(container) {
expect(container.textContent.trim().length).toBeGreaterThan(0);
}
describe('results.js renderResults() — fallback plaintext path (renderMarkdown unavailable)', () => {
it('escapes text-context payloads in report content as visible text', async () => {
window.ui = undefined;
for (const payload of TEXT_PAYLOADS) {
const container = await mountResults({ payload: { content: `Report intro.\n\n${payload}` } });
expect(container.querySelectorAll('img, script, svg, iframe')).toHaveLength(0);
expect(eventHandlerAttributes(container)).toEqual([]);
expect(container.querySelector('pre')).not.toBeNull();
expect(container.textContent).toContain(payload);
expect(container.innerHTML).toContain('<');
expectRendered(container);
}
});
it('escapes hostile payloads arriving through every findAndRenderContent() field', async () => {
window.ui = undefined;
const p = TEXT_PAYLOADS[0];
const shapes = [
{ markdown: p }, // renderResults(data.markdown)
{ text: p }, // renderResults(data.text)
{ summary: p }, // renderResults(data.summary)
{ report: p }, // findAndRenderContent: data.report
{ results: [p, 'second'] }, // renderResults: array join
{ results: p }, // renderResults: string results
{ research: { content: p } }, // nested research.content
{ results: { content: p } }, // results.content
{ output: p }, // contentProps fallback loop
{ unknown_field: p }, // last resort: JSON.stringify(data)
p, // plain-string API response
];
for (const payload of shapes) {
const container = await mountResults({ payload });
expect(container.querySelectorAll('img, script, svg, iframe')).toHaveLength(0);
expect(eventHandlerAttributes(container)).toEqual([]);
expect(container.textContent).toContain(p);
expectRendered(container);
}
});
it('stays safe when only the local escapeHtmlFallback is available (xss-protection.js failed to load)', async () => {
window.ui = undefined;
delete window.escapeHtml;
delete window.sanitizeHtml;
const container = await mountResults({
payload: { content: `Notes: ${ATTR_PAYLOAD} and <img src=x onerror=alert(1)>` },
});
// results.js:637 resolves escapeHtmlFallback (results.js:21), which
// escapes & < > " ' — the quote escaping keeps the payload inert.
expect(container.querySelectorAll('img, script, svg, iframe')).toHaveLength(0);
expect(eventHandlerAttributes(container)).toEqual([]);
expect(container.textContent).toContain('<img src=x onerror=alert(1)>');
expect(container.textContent).toContain(ATTR_PAYLOAD);
expectRendered(container);
});
it('keeps attribute-breakout payloads inert (results.js renders no title/tooltip/aria interpolations)', async () => {
window.ui = undefined;
const container = await mountResults({ payload: { content: `Research notes: ${ATTR_PAYLOAD}` } });
expect(container.querySelector('[onmouseover]')).toBeNull();
expect(eventHandlerAttributes(container)).toEqual([]);
expect(container.textContent).toContain(ATTR_PAYLOAD);
expectRendered(container);
});
it('escapes hostile error messages in showError() on the fetch-rejection path', async () => {
window.ui = undefined;
const container = await mountResults({
error: new Error('HTTP error <img src=x onerror=alert(1)><script>alert(2)</script>'),
});
expect(container.querySelector('.alert-danger')).not.toBeNull();
expect(container.querySelectorAll('img, script, svg')).toHaveLength(0);
expect(eventHandlerAttributes(container)).toEqual([]);
expect(container.textContent).toContain('<img src=x onerror=alert(1)>');
// The only link in the error state is the hardcoded safe one.
const back = container.querySelector('a');
expect(back.getAttribute('href')).toBe('/');
expectRendered(container);
});
});
describe('results.js renderResults() — data.html branches', () => {
it('sanitizes data.html through the real window.sanitizeHtml (DOMPurify allowlist)', async () => {
const container = await mountResults({
payload: {
html: '<p>keep this</p><img src=x onerror=alert(1)><script>alert(2)</script>'
+ '<a href="javascript:alert(1)" onmouseover="alert(1)">link</a>',
},
});
expect(container.querySelectorAll('img, script, svg, iframe')).toHaveLength(0);
expect(eventHandlerAttributes(container)).toEqual([]);
for (const a of container.querySelectorAll('a')) {
// A stripped href (null) is the safe outcome — coerce before matching.
expect(a.getAttribute('href') ?? '').not.toMatch(/^(javascript|data|vbscript):/i);
}
expect(container.textContent).toContain('keep this');
expectRendered(container);
});
it('keeps template-literal-shaped text intact instead of blanking it (SAFE_FOR_TEMPLATES: false)', async () => {
// Regression test: SANITIZE_CONFIG's shared default is
// SAFE_FOR_TEMPLATES: true, which makes DOMPurify blank out any
// text node matching mustache/ERB/template-literal syntax
// (`}}`, `${...}`, `%>`). results.js's call site overrides that
// with { SAFE_FOR_TEMPLATES: false } (services/ui.js's safeSetHTML
// does too) specifically so report content quoting JSON or a shell
// `${VAR}` example survives. Reverting that override to the shared
// default's SAFE_FOR_TEMPLATES: true would blank the payload below
// but pass every other test in this describe block, since none of
// them assert on `}}`-shaped text.
const container = await mountResults({
payload: { html: '<p>see the {{ config }} block</p>' },
});
expect(container.textContent).toContain('{{ config }}');
expectRendered(container);
});
it('escapes data.html as text when only window.escapeHtml is available', async () => {
delete window.sanitizeHtml;
const container = await mountResults({
payload: { html: '<img src=x onerror=alert(1)><b>bold</b>' },
});
expect(container.querySelectorAll('img, b')).toHaveLength(0);
expect(container.innerHTML).toContain('<img');
expect(container.textContent).toContain('<img src=x onerror=alert(1)>');
expectRendered(container);
});
it('renders data.html via textContent when no sanitizer is available at all', async () => {
delete window.sanitizeHtml;
delete window.escapeHtml;
const container = await mountResults({ payload: { html: TEXT_PAYLOADS[0] } });
expect(container.querySelectorAll('img, script, svg')).toHaveLength(0);
expect(container.children).toHaveLength(0); // pure text node, no elements
expect(container.textContent).toBe(TEXT_PAYLOADS[0]);
});
});
describe('results.js renderResults() — markdown path (real marked + DOMPurify pipeline)', () => {
it('sanitizes raw hostile HTML embedded in markdown content', async () => {
const container = await mountResults({
payload: {
content: '# Heading ok\n\n<img src=x onerror=alert(1)>\n\n<script>alert(2)</script>'
+ '\n\n"><svg onload=alert(3)>\n\nBody text.',
},
});
expect(container.querySelectorAll('img, script, svg, iframe')).toHaveLength(0);
expect(eventHandlerAttributes(container)).toEqual([]);
expect(container.querySelector('.ldr-markdown-content h1').textContent).toBe('Heading ok');
expectRendered(container);
});
it('never renders javascript:/data:/vbscript: hrefs from markdown content', async () => {
const container = await mountResults({
payload: {
content: [
'[js](javascript:alert(1))',
'[data](data:text/html,<script>alert(1)</script>)',
'[vb](vbscript:alert(1))',
'<a href="javascript:alert(1)">raw</a>',
'[ok](https://example.com/safe)',
].join('\n'),
},
});
const anchors = container.querySelectorAll('a[href]');
expect(anchors.length).toBeGreaterThan(0);
for (const a of anchors) {
expect(a.getAttribute('href')).not.toMatch(/^(javascript|data|vbscript):/i);
}
expect(container.querySelector('a[href="https://example.com/safe"]')).not.toBeNull();
expectRendered(container);
});
it('renders a benign report intact with clickable safe links and populated metadata', async () => {
const container = await mountResults({
payload: {
content: '# Quarterly AI Report\n\n**Key finding**: models improved.'
+ '\n\n[Read the source](https://example.com/paper).',
metadata: {
query: 'Latest AI advances',
mode: 'quick',
created_at: '2026-01-02T03:04:05Z',
},
},
});
expect(container.querySelector('.ldr-markdown-content h1').textContent).toBe('Quarterly AI Report');
const link = container.querySelector('a[href="https://example.com/paper"]');
expect(link).not.toBeNull();
expect(link.getAttribute('target')).toBe('_blank');
expect(link.getAttribute('rel')).toContain('noopener');
expect(document.getElementById('result-query').textContent).toBe('Latest AI advances');
expect(document.getElementById('result-mode').textContent).toBe('Quick Summary');
expect(document.getElementById('result-date').textContent).not.toBe('');
// Real load path completed: export buttons were re-enabled.
expect(document.getElementById('export-markdown-btn').disabled).toBe(false);
expectRendered(container);
});
});
describe('results.js metadata population — textContent-only surface', () => {
it('renders hostile API metadata (query/mode/title/duration) as inert text', async () => {
window.ui = undefined;
const container = await mountResults({
payload: {
content: 'benign report body',
metadata: {
query: TEXT_PAYLOADS[0],
title: '<script>alert(2)</script>',
mode: '"><svg onload=alert(3)>',
created_at: '<img src=x onerror=alert(4)>',
duration: '<script>alert(5)</script>',
},
},
});
const query = document.getElementById('result-query');
expect(query.textContent).toBe(TEXT_PAYLOADS[0]);
expect(query.children).toHaveLength(0);
const mode = document.getElementById('result-mode');
expect(mode.children).toHaveLength(0);
expect(mode.textContent).toContain('<svg onload=alert(3)>');
const date = document.getElementById('result-date');
expect(date.children).toHaveLength(0);
expect(date.textContent).not.toContain('<img');
// Hostile duration fails parseInt -> NaN -> no suffix is appended.
expect(document.querySelectorAll('#result-query img, #result-query script, #result-mode svg, #result-date img')).toHaveLength(0);
expectRendered(container);
});
it('keeps the query extracted from a hostile content heading inert (populateMetadata fallback)', async () => {
window.ui = undefined;
const container = await mountResults({
payload: { content: `# ${TEXT_PAYLOADS[0]}\n\nbody text` },
});
const query = document.getElementById('result-query');
expect(query.textContent).toBe(TEXT_PAYLOADS[0]);
expect(query.children).toHaveLength(0);
expectRendered(container);
});
});