~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
import '../helpers/safe-fetch-with-auth-to-fetch.js';
/**
* XSS regression coverage for the library/search/history render surfaces.
*
* Interpolation map (source lines at the revision under test):
* library_search.js:38-52,161-184 — result ids/titles/file types/dates/domains
* are passed to SemanticSearch; ids become dataset values, display fields are
* escaped, and the unencoded id is anchored below /library/document/.
* library_search_ui.js:313-327 — similarity uses dataset + escapeHtml and
* snippets use SemanticSearch.renderSnippet; :394-398 receives only static
* or array-length-derived notices from :225/:230/:267.
* semantic_search.js:23-42 — marked output is DOMPurify-sanitized (or escaped);
* :210 dataset id is a DOM-API sink; :216/:226/:228/:231/:233 escape text and
* attributes; :234 sanitizes snippets; :235-241 scheme-guards external URLs.
* history.js:549,578-602,630-666,701-733 — ids use dataset/escaped data attrs,
* titles/status/date/mode/counts use escapeHtml, queries are URI-encoded,
* and semantic snippets use renderSnippet.
* history_search.js:390-412 — delegates result rendering to SemanticSearch;
* progress/status API strings elsewhere use textContent.
* custom_dropdown.js:31,60,145-200,404-478 — group/reason/no-result text uses
* textContent or safeSetTextContent, values use setAttribute, and highlighted
* labels pass through safeSetInnerHTML/DOMPurify.
* save_to_collection.js:118-146,214-226 — collection ids/names use dataset and
* names/descriptions/counts use textContent-created DOM nodes.
* collection_details.js:113-174 — textContent; :187-240 escaped settings;
* :306-338 escaped document fields, numeric formatBytes, encoded href id;
* :375-395 escaped note fields + encoded href id; :578-669 textContent;
* :899-923 delegates cards or escapes the API error.
*/
import {
afterAll,
afterEach,
beforeAll,
beforeEach,
describe,
expect,
test,
vi,
} from 'vitest';
const IMG_PAYLOAD = '<img src=x onerror=alert(1)>';
const SVG_PAYLOAD = '"><svg onload=alert(2)>';
const ATTR_PAYLOAD = 'x" onmouseover="alert(3)';
const JS_URL = 'javascript:alert(4)';
const originalGlobals = {
api: window.api,
bootstrap: globalThis.bootstrap,
collectionId: globalThis.COLLECTION_ID,
domPurify: globalThis.DOMPurify,
fetch: globalThis.fetch,
historySearch: window.HistorySearch,
marked: window.marked,
researchStates: window.ResearchStates,
safeFetch: globalThis.safeFetch,
safeFetchWithAuth: globalThis.safeFetchWithAuth,
};
let realLibrarySearch;
let pageListeners;
let pageListenerSpy;
function setFixture(html) {
// eslint-disable-next-line no-unsanitized/property -- test-only helper; every caller passes a static fixture literal defined in this file.
document.body.innerHTML = html;
}
function eventHandlerAttributes(root) {
const found = [];
for (const element of [root, ...root.querySelectorAll('*')]) {
for (const name of element.getAttributeNames()) {
if (/^on/i.test(name)) found.push(name.toLowerCase());
}
}
return found;
}
function expectInert(root) {
expect(root.querySelectorAll('img, script, svg, iframe, object, embed')).toHaveLength(0);
// No exemption list here: an inline onclick (or any other `on*`)
// attribute anywhere under root must fail this check. An exemption
// parameter on this assertion is exactly the shape that let a reverted
// inline-onclick regression pass round 1 (see the comment at the
// click-driven regression test below).
expect(eventHandlerAttributes(root)).toEqual([]);
}
function expectSafeHrefs(root) {
const anchors = root.querySelectorAll('a[href]');
for (const anchor of anchors) {
expect(anchor.getAttribute('href')).not.toMatch(/^\s*(?:javascript|data|vbscript):/i);
}
}
function researchStatesStub() {
return {
formatStatus: (status) => String(status || ''),
isTerminal: (status) => ['completed', 'failed', 'cancelled', 'error'].includes(status),
isCompleted: (status) => status === 'completed',
isFailed: (status) => status === 'failed',
isCancelled: (status) => status === 'cancelled',
};
}
function installDomPurifyHappyDomShims() {
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 (const child of node.childNodes) collect(child);
})(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 mathMlTags = 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 originalNamespace = Object.getOwnPropertyDescriptor(
window.Element.prototype,
'namespaceURI'
);
Object.defineProperty(window.Element.prototype, 'namespaceURI', {
configurable: true,
get() {
if (typeof this.tagName === 'string'
&& mathMlTags.has(this.tagName.toLowerCase())) {
return 'http://www.w3.org/1998/Math/MathML';
}
return originalNamespace?.get
? originalNamespace.get.call(this)
: undefined;
},
});
}
beforeAll(async () => {
// DOMPurify captures these DOM primitives at import time; happy-dom's
// defaults do not expose enough node metadata for removal-based sanitizing.
installDomPurifyHappyDomShims();
const DOMPurify = (await import('dompurify')).default;
const { marked } = await import('marked');
globalThis.DOMPurify = DOMPurify;
window.DOMPurify = DOMPurify;
window.marked = marked;
await import('@js/security/xss-protection.js');
await import('@js/config/urls.js');
await import('@js/components/library_search.js');
await import('@js/components/custom_dropdown.js');
realLibrarySearch = window.LibrarySearch;
});
beforeEach(() => {
// Each scenario loads a different page into this shared document. Retire
// its initializer before another page dispatches DOMContentLoaded.
vi.resetModules();
pageListeners = [];
const addEventListener = document.addEventListener.bind(document);
pageListenerSpy = vi.spyOn(document, 'addEventListener').mockImplementation(
(type, listener, options) => {
pageListeners.push([type, listener, options]);
addEventListener(type, listener, options);
}
);
});
afterEach(() => {
for (const [type, listener, options] of pageListeners) {
document.removeEventListener(type, listener, options);
}
pageListenerSpy.mockRestore();
vi.useRealTimers();
window.LibrarySearch = realLibrarySearch;
window.HistorySearch = originalGlobals.historySearch;
window.api = originalGlobals.api;
window.ResearchStates = originalGlobals.researchStates;
globalThis.fetch = originalGlobals.fetch;
globalThis.safeFetch = originalGlobals.safeFetch;
globalThis.safeFetchWithAuth = originalGlobals.safeFetchWithAuth;
globalThis.bootstrap = originalGlobals.bootstrap;
window.bootstrap = originalGlobals.bootstrap;
document.body.replaceChildren();
});
afterAll(() => {
globalThis.COLLECTION_ID = originalGlobals.collectionId;
window.COLLECTION_ID = originalGlobals.collectionId;
globalThis.DOMPurify = originalGlobals.domPurify;
window.DOMPurify = originalGlobals.domPurify;
window.marked = originalGlobals.marked;
});
describe('semantic_search.js', () => {
test('renders hostile card fields and URLs inert while preserving a benign card', () => {
const root = document.createElement('div');
const hostile = window.SemanticSearch.createSemanticResultCard({
research_id: SVG_PAYLOAD,
research_title: IMG_PAYLOAD,
similarity: ATTR_PAYLOAD,
snippet: `Hostile snippet ${IMG_PAYLOAD} [bad link](${JS_URL})`,
url: JS_URL,
type: 'source',
research_created_at: '2026-01-02T03:04:05Z',
}, null, 'Hostile');
const benign = window.SemanticSearch.createSemanticResultCard({
research_id: 'research-benign',
research_title: 'Benign semantic result',
similarity: 87,
snippet: '**Benign snippet**',
url: 'https://example.com/source',
type: 'source',
research_created_at: '2026-01-02T03:04:05Z',
}, null, 'Benign');
root.append(hostile, benign);
const cards = root.querySelectorAll('.ldr-semantic-result');
expect(cards).toHaveLength(2);
expect(hostile.dataset.id).toBe(SVG_PAYLOAD);
expect(hostile.querySelector('.ldr-semantic-result-title').textContent.trim()).toBe(IMG_PAYLOAD);
expect(hostile.textContent).toContain(ATTR_PAYLOAD);
expect(hostile.querySelector('.ldr-semantic-result-source')).toBeNull();
expect(benign.textContent).toContain('Benign semantic result');
expect(benign.querySelector('.ldr-semantic-result-source a').getAttribute('href'))
.toBe('https://example.com/source');
expectInert(root);
expectSafeHrefs(root);
});
});
describe('library_search.js', () => {
test('drives renderSemanticResults with hostile document fields and a benign control', () => {
const container = document.createElement('div');
window.LibrarySearch.renderSemanticResults([
{
document_id: SVG_PAYLOAD,
title: IMG_PAYLOAD,
similarity: ATTR_PAYLOAD,
snippet: `Matched ${IMG_PAYLOAD}`,
file_type: ATTR_PAYLOAD,
domain: SVG_PAYLOAD,
url: JS_URL,
created_at: '2026-01-02T03:04:05Z',
},
{
document_id: 'doc-benign',
title: 'Benign library document',
similarity: 91,
snippet: 'Benign matched passage',
file_type: 'pdf',
domain: 'example.com',
created_at: '2026-01-02T03:04:05Z',
},
], container, 'Matched');
const cards = container.querySelectorAll('.ldr-semantic-result');
expect(cards).toHaveLength(2);
expect(cards[0].dataset.id).toBe(SVG_PAYLOAD);
expect(cards[0].querySelector('.ldr-semantic-result-title').textContent.trim()).toBe(IMG_PAYLOAD);
expect(cards[0].querySelector('.ldr-semantic-result-title a').getAttribute('href'))
.toBe(`/library/document/${SVG_PAYLOAD}`);
expect(cards[1].textContent).toContain('Benign library document');
expect(cards[1].querySelector('.ldr-semantic-result-title a').getAttribute('href'))
.toBe('/library/document/doc-benign');
expectInert(container);
expectSafeHrefs(container);
});
});
describe('library_search_ui.js', () => {
test('keeps hybrid similarity and snippet payloads inert on an existing benign card', async () => {
setFixture(`
<div id="documents-container">
<div class="ldr-document-card" data-doc-id="doc-ui" data-domain="" data-research="">
<div class="card-header"><span>Benign existing document</span></div>
<div class="card-body"></div>
</div>
</div>
<div id="semantic-results-container"></div>
<div id="library-search-notice"></div>
<select id="filter-collection"><option value="collection-ui" selected>Collection</option></select>
<select id="filter-domain"><option value="" selected>All</option></select>
<select id="filter-research"><option value="" selected>All</option></select>
<select id="filter-date"><option value="" selected>All</option></select>
<input id="search-documents">
`);
vi.useFakeTimers();
window.LibrarySearch = {
performSemanticSearch: vi.fn().mockResolvedValue({
success: true,
results: [{
document_id: 'doc-ui',
similarity: ATTR_PAYLOAD,
snippet: `Benign matched text ${IMG_PAYLOAD}`,
}],
}),
getIndexedCollectionIds: () => ['collection-ui'],
getLibraryCardConfig: () => realLibrarySearch.getLibraryCardConfig(),
};
await import('@js/components/library_search_ui.js');
const input = document.getElementById('search-documents');
input.value = 'Benign';
input.dispatchEvent(new Event('input', { bubbles: true }));
await vi.advanceTimersByTimeAsync(750);
const container = document.getElementById('documents-container');
const snippet = container.querySelector('.ldr-library-snippet');
const badge = container.querySelector('[data-similarity]');
expect(snippet).not.toBeNull();
expect(badge).not.toBeNull();
expect(badge.dataset.similarity).toBe(ATTR_PAYLOAD);
expect(badge.textContent).toContain(ATTR_PAYLOAD);
expect(snippet.textContent).toContain('Benign matched text');
expect(container.textContent).toContain('Benign existing document');
expectInert(container);
});
});
describe('history.js', () => {
test('loads and renders hostile history metadata inert beside a benign item', async () => {
setFixture(`
<div id="history-items"></div>
<input id="history-search">
<button id="clear-history-btn"></button>
<div id="history-empty-message"></div>
`);
const states = researchStatesStub();
window.ResearchStates = states;
globalThis.ResearchStates = states;
window.api = {
getCsrfToken: () => 'csrf',
getResearchHistory: vi.fn().mockResolvedValue({
items: [
{
id: SVG_PAYLOAD,
title: IMG_PAYLOAD,
query: ATTR_PAYLOAD,
mode: SVG_PAYLOAD,
status: 'completed',
document_count: 1,
created_at: '2026-01-02T03:04:05Z',
metadata: { is_news_search: true },
},
{
id: 'history-benign',
title: 'Benign history item',
query: 'benign query',
mode: 'quick',
status: 'completed',
document_count: 2,
created_at: '2026-01-01T03:04:05Z',
metadata: {},
},
],
}),
};
globalThis.fetch = vi.fn().mockResolvedValue({
ok: true,
json: () => Promise.resolve({ success: true, sessions: [] }),
});
window.HistorySearch = {
getSemanticCollectionId: () => 'history-collection',
semanticSearchHistory: vi.fn().mockResolvedValue([
{
research_id: 'history-benign',
similarity: ATTR_PAYLOAD,
snippet: `Tier one snippet ${IMG_PAYLOAD}`,
},
{
research_id: 'semantic-only-hostile',
research_title: SVG_PAYLOAD,
similarity: ATTR_PAYLOAD,
snippet: `Tier three snippet ${IMG_PAYLOAD}`,
},
]),
};
await import('@js/components/history.js');
document.dispatchEvent(new Event('DOMContentLoaded'));
const container = document.getElementById('history-items');
await vi.waitFor(() => {
expect(container.querySelectorAll('.ldr-history-item')).toHaveLength(2);
});
const hostile = Array.from(container.querySelectorAll('.ldr-history-item'))
.find((item) => item.dataset.id === SVG_PAYLOAD);
const subscribe = hostile.querySelector('.ldr-subscribe-btn');
expect(hostile.dataset.id).toBe(SVG_PAYLOAD);
expect(hostile.querySelector('.ldr-history-item-title').textContent).toBe(IMG_PAYLOAD);
expect(subscribe).not.toBeNull();
expect(subscribe.dataset.researchId).toBe(SVG_PAYLOAD);
expect(subscribe.dataset.query).toBe(encodeURIComponent(ATTR_PAYLOAD));
expect(container.textContent).toContain('Benign history item');
expect(container.textContent).toContain('Library (2)');
expectInert(container);
vi.useFakeTimers();
const search = document.getElementById('history-search');
search.value = 'Benign';
search.dispatchEvent(new Event('input', { bubbles: true }));
await vi.advanceTimersByTimeAsync(750);
const snippets = container.querySelectorAll('.ldr-history-item-snippet');
const badges = container.querySelectorAll('.ldr-ai-match-badge');
expect(snippets).toHaveLength(2);
expect(badges).toHaveLength(2);
expect(container.textContent).toContain('Tier one snippet');
expect(container.textContent).toContain('Tier three snippet');
expect(container.textContent).toContain(SVG_PAYLOAD);
expect(container.textContent).toContain('Benign history item');
expectInert(container);
});
});
describe('history_search.js', () => {
test('renders hostile API results through the real shared card path with a benign control', async () => {
const states = researchStatesStub();
window.ResearchStates = states;
globalThis.ResearchStates = states;
window.api = { getCsrfToken: () => 'csrf' };
globalThis.fetch = vi.fn((url) => Promise.resolve({
ok: true,
json: () => Promise.resolve(String(url).includes('research-history/collection')
? {
success: true,
collection_id: 'history-collection',
indexed_documents: 1,
total_documents: 1,
}
: { status: 'idle' }),
}));
await import('@js/components/history_search.js');
document.dispatchEvent(new Event('DOMContentLoaded'));
await new Promise((resolve) => setTimeout(resolve, 0));
setFixture('<div id="history-items"></div>');
window.HistorySearch.renderSemanticResults([
{
research_id: SVG_PAYLOAD,
research_title: IMG_PAYLOAD,
similarity: ATTR_PAYLOAD,
snippet: `History snippet ${IMG_PAYLOAD}`,
url: JS_URL,
},
{
research_id: 'history-semantic-benign',
research_title: 'Benign history semantic result',
similarity: 75,
snippet: 'Benign history snippet',
url: 'https://example.com/history',
},
], 'History');
const container = document.getElementById('history-items');
expect(container.querySelectorAll('.ldr-semantic-result')).toHaveLength(2);
expect(container.textContent).toContain(IMG_PAYLOAD);
expect(container.textContent).toContain('Benign history semantic result');
expectInert(container);
expectSafeHrefs(container);
});
});
describe('custom_dropdown.js', () => {
function mountDropdown() {
setFixture(`
<div id="dropdown-wrapper">
<input id="security-dropdown">
<input id="security-dropdown_hidden" type="hidden">
<div id="security-dropdown-list"></div>
</div>
`);
return {
input: document.getElementById('security-dropdown'),
list: document.getElementById('security-dropdown-list'),
};
}
test('sanitizes hostile option labels while keeping values and benign labels intact', () => {
const { input, list } = mountDropdown();
const options = [
{
value: ATTR_PAYLOAD,
label: `Hostile ${IMG_PAYLOAD} option`,
group_label: SVG_PAYLOAD,
disabled: true,
disabled_reason: IMG_PAYLOAD,
is_favorite: true,
},
{ value: 'benign', label: 'Benign option', group_label: 'Safe group' },
];
const dropdown = window.setupCustomDropdown(
input,
list,
() => options,
vi.fn(),
false,
'No results',
vi.fn()
);
input.click();
const items = list.querySelectorAll('.ldr-custom-dropdown-item');
const labels = list.querySelectorAll('.ldr-dropdown-item-label');
expect(items).toHaveLength(2);
expect(items[0].getAttribute('data-value')).toBe(ATTR_PAYLOAD);
expect(labels[0].textContent).toContain('Hostile');
expect(labels[0].textContent).toContain('option');
expect(labels[1].textContent).toBe('Benign option');
expect(list.querySelector('.ldr-custom-dropdown-group-header').textContent).toBe(SVG_PAYLOAD);
expect(list.querySelector('.ldr-dropdown-item-disabled-reason').textContent).toBe(IMG_PAYLOAD);
expectInert(list);
dropdown.destroy();
});
test('keeps updateDropdownOptions labels inert on the open-list render path', () => {
const { input, list } = mountDropdown();
const dropdown = window.setupCustomDropdown(
input,
list,
() => [{ value: 'initial', label: 'Initial benign option' }],
vi.fn()
);
input.click();
window.updateDropdownOptions(input, [
{ value: JS_URL, label: IMG_PAYLOAD, group_label: SVG_PAYLOAD },
{ value: 'benign-updated', label: 'Benign updated option', group_label: 'Safe group' },
]);
const items = list.querySelectorAll('.ldr-custom-dropdown-item');
expect(items).toHaveLength(2);
expect(items[0].getAttribute('data-value')).toBe(JS_URL);
expect(items[0].querySelector('.ldr-dropdown-item-label').textContent).toBe(IMG_PAYLOAD);
expect(items[1].textContent).toContain('Benign updated option');
expectInert(list);
dropdown.destroy();
});
});
describe('save_to_collection.js', () => {
test('keeps hostile collection names/descriptions inert through DOM-API construction', async () => {
setFixture(`
<button id="save-to-collection-btn"></button>
<div id="saveToCollectionModal"></div>
<div id="collection-list-loading"></div>
<div id="collection-list"></div>
<div id="collection-error"></div>
<div id="collection-success"></div>
<div id="collection-items"></div>
`);
const originalUrl = window.location.href;
window.history.pushState({}, '', '/results/research-save');
const show = vi.fn();
const bootstrapStub = {
Modal: { getOrCreateInstance: vi.fn(() => ({ show })) },
};
globalThis.bootstrap = bootstrapStub;
window.bootstrap = bootstrapStub;
globalThis.fetch = vi.fn().mockResolvedValue({
ok: true,
json: () => Promise.resolve({
success: true,
collections: [
{ id: ATTR_PAYLOAD, name: IMG_PAYLOAD, description: SVG_PAYLOAD },
{ id: 'collection-benign', name: 'Benign collection', description: 'Benign description' },
],
}),
});
try {
await import('@js/components/save_to_collection.js');
document.dispatchEvent(new Event('DOMContentLoaded'));
document.getElementById('save-to-collection-btn').click();
const items = document.getElementById('collection-items');
await vi.waitFor(() => {
expect(items.querySelectorAll('button')).toHaveLength(2);
});
const buttons = items.querySelectorAll('button');
expect(show).toHaveBeenCalledTimes(1);
expect(buttons[0].dataset.collectionId).toBe(ATTR_PAYLOAD);
expect(buttons[0].dataset.collectionName).toBe(IMG_PAYLOAD);
expect(buttons[0].querySelector('strong').textContent).toBe(IMG_PAYLOAD);
expect(buttons[0].querySelector('small').textContent).toBe(SVG_PAYLOAD);
expect(buttons[1].textContent).toContain('Benign collection');
expect(buttons[1].textContent).toContain('Benign description');
expectInert(items);
} finally {
window.history.pushState({}, '', originalUrl);
}
});
});
describe('collection_details.js', () => {
test('renders hostile collection/document/note metadata inert and encodes link ids', async () => {
setFixture(`
<button id="index-collection-btn"></button>
<button id="reindex-collection-btn"></button>
<button id="delete-collection-btn"></button>
<button id="cancel-indexing-btn"></button>
<input id="collection-is-public" type="checkbox">
<input id="collection-agent-enabled" type="checkbox">
<h1 id="collection-name"></h1>
<p id="collection-description"></p>
<span id="stat-total-docs"></span>
<span id="stat-indexed-docs"></span>
<span id="stat-unindexed-docs"></span>
<span id="stat-total-chunks"></span>
<div id="collection-embedding-info"></div>
<div id="documents-list"></div>
<div id="no-documents-message"></div>
<section id="notes-section"><div id="notes-list"></div></section>
<section id="collection-search-section" style="display:none">
<input id="collection-search-input">
<button id="collection-search-btn"></button>
<div id="collection-search-results"></div>
</section>
<div id="indexing-progress"></div>
<div id="progress-fill"></div>
<div id="progress-text"></div>
<div id="progress-log"></div>
<div id="indexing-spinner"></div>
`);
const states = researchStatesStub();
window.ResearchStates = states;
globalThis.ResearchStates = states;
globalThis.COLLECTION_ID = 'collection-security';
window.COLLECTION_ID = 'collection-security';
window.api = { getCsrfToken: () => 'csrf' };
const payload = {
success: true,
collection: {
name: IMG_PAYLOAD,
description: SVG_PAYLOAD,
embedding_model_type: IMG_PAYLOAD,
embedding_model: ATTR_PAYLOAD,
chunk_size: IMG_PAYLOAD,
chunk_overlap: ATTR_PAYLOAD,
embedding_dimension: SVG_PAYLOAD,
splitter_type: IMG_PAYLOAD,
distance_metric: ATTR_PAYLOAD,
index_type: SVG_PAYLOAD,
normalize_vectors: true,
index_file_size: IMG_PAYLOAD,
is_public: true,
agent_enabled: true,
is_protected: false,
},
documents: [
{
id: SVG_PAYLOAD,
filename: IMG_PAYLOAD,
indexed: true,
chunk_count: IMG_PAYLOAD,
file_size: IMG_PAYLOAD,
source_type: ATTR_PAYLOAD,
in_other_collections: true,
other_collections_count: ATTR_PAYLOAD,
last_indexed_at: SVG_PAYLOAD,
has_pdf: false,
has_text_db: false,
},
{
id: 'doc-benign',
filename: 'Benign document.pdf',
indexed: true,
chunk_count: 4,
file_size: 1024,
source_type: 'pdf',
in_other_collections: false,
last_indexed_at: '2026-01-02T03:04:05Z',
has_pdf: true,
has_text_db: true,
},
],
notes: [
{
id: JS_URL,
title: SVG_PAYLOAD,
indexed: true,
chunk_count: IMG_PAYLOAD,
updated_at: '2026-01-02T03:04:05Z',
pinned: true,
},
{
id: 'note-benign',
title: 'Benign note',
indexed: true,
chunk_count: 2,
updated_at: '2026-01-02T03:04:05Z',
pinned: false,
},
],
};
const safeFetchMock = vi.fn((url) => Promise.resolve({
ok: true,
json: () => Promise.resolve(String(url).endsWith('/documents')
? payload
: { status: 'idle' }),
}));
globalThis.safeFetch = safeFetchMock;
globalThis.safeFetchWithAuth = safeFetchMock;
await import('@js/collection_details.js');
document.dispatchEvent(new Event('DOMContentLoaded'));
await vi.waitFor(() => {
expect(document.querySelectorAll('#documents-list .ldr-document-item')).toHaveLength(2);
expect(document.querySelectorAll('#notes-list .ldr-document-item')).toHaveLength(2);
});
const documents = document.getElementById('documents-list');
const notes = document.getElementById('notes-list');
const embedding = document.getElementById('collection-embedding-info');
expect(document.getElementById('collection-name').textContent).toBe(IMG_PAYLOAD);
expect(document.getElementById('collection-description').textContent).toBe(SVG_PAYLOAD);
expect(document.getElementById('stat-total-docs').textContent).toBe('4');
expect(embedding.textContent).toContain(ATTR_PAYLOAD);
expect(documents.textContent).toContain(IMG_PAYLOAD);
expect(documents.textContent).toContain('Benign document.pdf');
expect(notes.textContent).toContain(SVG_PAYLOAD);
expect(notes.textContent).toContain('Benign note');
const documentLinks = documents.querySelectorAll('a.ldr-document-link');
const noteLinks = notes.querySelectorAll('a.ldr-document-link');
expect(documentLinks[0].getAttribute('href'))
.toBe(`/library/document/${encodeURIComponent(SVG_PAYLOAD)}`);
expect(documentLinks[1].getAttribute('href')).toBe('/library/document/doc-benign');
expect(noteLinks[0].getAttribute('href')).toBe(`/notes/${encodeURIComponent(JS_URL)}`);
expect(noteLinks[1].getAttribute('href')).toBe('/notes/note-benign');
// No `['onclick']` exemption here: an inline onclick attribute
// anywhere under body must fail this check. (See the dedicated
// click-driven regression below for why an exemption list on this
// assertion is not sufficient on its own.)
expectInert(document.body);
expectSafeHrefs(document.body);
});
// FIX (defect (2) in the frontend-security-xss-hardening gate report):
// the test above used to assert expectInert(document.body, ['onclick'])
// -- an EXEMPTION list. Reverting renderDocuments() to the pre-fix
// `onclick="…removeDocumentFromCollection('${escapeHtml(doc.id)}')"`
// form still PASSES that assertion, because the exemption tells
// expectInert to ignore onclick attributes rather than to require their
// absence -- it was never exercising the fix. This test drops the
// exemption (see above) and adds a click-driven regression modeled on
// tests/js/pages/subscriptions-xss.test.js:191-208 ("blocks hostile IDs
// from executing through card action handlers"): a hostile id whose
// escapeHtml() output (') the HTML parser decodes back to a bare
// quote BEFORE a reverted inline onclick string is compiled as
// JavaScript, closing the string early and letting the injected
// statement run on click. happy-dom actually compiles and executes
// inline event-handler attributes (ElementEventAttributeUtility), so
// this genuinely fails against that revert -- caught here by asserting
// no onclick attribute exists AND that clicking the action button never
// sets the sentinel. Against the current data-doc-action delegation,
// the id is only ever read back via `.dataset.documentId` as a plain
// string, never compiled as JS, so the sentinel stays unset.
test('blocks a hostile document id from executing through the remove/delete action buttons', async () => {
globalThis.confirm = vi.fn(() => false);
window.confirm = globalThis.confirm;
setFixture(`
<button id="index-collection-btn"></button>
<button id="reindex-collection-btn"></button>
<button id="delete-collection-btn"></button>
<button id="cancel-indexing-btn"></button>
<input id="collection-is-public" type="checkbox">
<input id="collection-agent-enabled" type="checkbox">
<h1 id="collection-name"></h1>
<p id="collection-description"></p>
<span id="stat-total-docs"></span>
<span id="stat-indexed-docs"></span>
<span id="stat-unindexed-docs"></span>
<span id="stat-total-chunks"></span>
<div id="collection-embedding-info"></div>
<div id="documents-list"></div>
<div id="no-documents-message"></div>
<section id="notes-section"><div id="notes-list"></div></section>
<section id="collection-search-section" style="display:none">
<input id="collection-search-input">
<button id="collection-search-btn"></button>
<div id="collection-search-results"></div>
</section>
<div id="indexing-progress"></div>
<div id="progress-fill"></div>
<div id="progress-text"></div>
<div id="progress-log"></div>
<div id="indexing-spinner"></div>
`);
const states = researchStatesStub();
window.ResearchStates = states;
globalThis.ResearchStates = states;
globalThis.COLLECTION_ID = 'collection-security';
window.COLLECTION_ID = 'collection-security';
window.api = { getCsrfToken: () => 'csrf' };
const hostileId = "doc-');window.__doc_action_onclick_xss=1;//";
// Reset the sentinel before any `await` in this test (assigning to
// it afterward trips eslint's require-atomic-updates) -- it only
// needs a known baseline before removeBtn.click() below, which is
// still well after this point.
window.__doc_action_onclick_xss = 0;
const payload = {
success: true,
collection: {
name: 'benign collection',
description: '',
is_public: false,
agent_enabled: false,
is_protected: false,
},
documents: [
{
id: hostileId,
filename: 'evil.pdf',
indexed: false,
chunk_count: 0,
file_size: 10,
source_type: 'pdf',
in_other_collections: false,
last_indexed_at: null,
has_pdf: true,
has_text_db: false,
},
],
notes: [],
};
const safeFetchMock = vi.fn((url) => Promise.resolve({
ok: true,
json: () => Promise.resolve(String(url).endsWith('/documents')
? payload
: { status: 'idle' }),
}));
globalThis.safeFetch = safeFetchMock;
globalThis.safeFetchWithAuth = safeFetchMock;
await import('@js/collection_details.js');
document.dispatchEvent(new Event('DOMContentLoaded'));
await vi.waitFor(() => {
expect(document.querySelectorAll('#documents-list .ldr-document-item')).toHaveLength(1);
});
const item = document.querySelector('#documents-list .ldr-document-item');
expect(item.dataset.documentId).toBe(hostileId);
const removeBtn = document.querySelector('#documents-list [data-doc-action="remove"]');
expect(removeBtn).not.toBeNull();
// No onclick attribute exists anywhere in the rendered list.
expect(document.querySelectorAll('#documents-list [onclick]')).toHaveLength(0);
removeBtn.click();
// The reverted inline-onclick form would set this synchronously,
// during attribute compilation, before confirm()/fetch ever run.
expect(window.__doc_action_onclick_xss).toBe(0);
});
test('keeps hostile indexing failure strings in textContent beside a benign error', async () => {
await import('@js/collection_details.js');
setFixture('<div id="progress-log"></div><div id="progress-text"></div>');
window.renderIndexingFailure({
error_message: `${IMG_PAYLOAD}.`,
result: {
errors: [
{ title: SVG_PAYLOAD, error: ATTR_PAYLOAD },
{ title: 'Benign document', error: 'Benign disk error' },
],
},
});
const log = document.getElementById('progress-log');
const text = document.getElementById('progress-text');
expect(log.querySelectorAll('.ldr-log-entry')).toHaveLength(3);
expect(log.textContent).toContain(IMG_PAYLOAD);
expect(log.textContent).toContain(SVG_PAYLOAD);
expect(log.textContent).toContain(ATTR_PAYLOAD);
expect(log.textContent).toContain('Benign document: Benign disk error');
expect(text.textContent).toBe(IMG_PAYLOAD);
expectInert(document.body);
});
});