~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
/**
* OWASP DOM Clobbering Prevention Cheat Sheet and WSTG-INV-22 regression
* coverage for the browser code.
*
* Vitest's happy-dom 20.11.2 realm does not expose connected `id` or `name`
* elements as Window named properties. The harness test records that limitation;
* the sanitizer/config tests are therefore authoritative for browser behavior.
* The NodeIterator/nodeName repairs below are installed before dompurify is
* imported, matching the misc-surface/markdown security harnesses.
*
* Source recon performed for prototype-pollution sinks:
* - grep for recursive merge/clone/path-assignment helper declarations in the
* static JS source found none. The only `merge(...)` aliases are array/result
* aggregation through SemanticSearch.buildTieredResults in notes.js and
* unified_search.js, not recursive object mutation.
* - settings.js dynamic JSON writes/merges are exercised through its real
* load -> render -> blur/autosave -> POST path below.
*
* The markdown hardening pass preserves benign named attributes while
* namespacing collisions with sensitive built-ins and app globals.
*/
import { readFileSync, readdirSync } from 'node:fs';
import { join, relative, resolve } from 'node:path';
import {
afterAll,
afterEach,
beforeAll,
beforeEach,
describe,
expect,
it,
test,
vi,
} from 'vitest';
const STATIC_JS_ROOT = resolve(
__dirname,
'../../../src/local_deep_research/web/static/js',
);
const XSS_PROTECTION_PATH = join(STATIC_JS_ROOT, 'security/xss-protection.js');
const SETTINGS_KEY = 'app.prototype_pollution_probe';
const SENSITIVE_NAMED_PROPS = new Set([
'location', 'document', 'name', 'opener', 'top', 'close', 'frames',
]);
const STANDARD_CLOBBERING_CORPUS = [
'<a id="api" name="api">api probe</a>',
'<img name="socket" src="x">',
'<div id="URLBuilder">URL builder probe</div>',
...[...SENSITIVE_NAMED_PROPS].map(
(propertyName) => `<a id="${propertyName}" name="${propertyName}">${propertyName}</a>`,
),
];
function collectJavaScriptFiles(directoryPath) {
return readdirSync(directoryPath, { withFileTypes: true }).flatMap((directoryEntry) => {
const entryPath = join(directoryPath, directoryEntry.name);
if (directoryEntry.isDirectory()) {
return collectJavaScriptFiles(entryPath);
}
return directoryEntry.isFile() && directoryEntry.name.endsWith('.js')
? [entryPath]
: [];
});
}
const SOURCE_RECORDS = collectJavaScriptFiles(STATIC_JS_ROOT).map((filePath) => ({
filePath,
relativePath: relative(STATIC_JS_ROOT, filePath),
contents: readFileSync(filePath, 'utf8'),
}));
function extractAppGlobals() {
const names = new Set();
const assignmentPattern = /\bwindow\.([A-Za-z_$][\w$]*)\s*=(?!=)/g;
for (const sourceRecord of SOURCE_RECORDS) {
for (const assignmentMatch of sourceRecord.contents.matchAll(assignmentPattern)) {
names.add(assignmentMatch[1]);
}
}
return names;
}
const APP_GLOBALS = extractAppGlobals();
const ORIGINAL_CREATE_NODE_ITERATOR = document.createNodeIterator;
const ORIGINAL_NODE_NAME = Object.getOwnPropertyDescriptor(window.Node.prototype, 'nodeName');
const WINDOW_KEYS = [
'api', 'socket', 'URLBuilder', 'URLS', 'ui', 'DOMPurify', 'marked',
'XSSProtection', 'escapeHtml', 'sanitizeHtml', 'LdrAlertHelpers',
'LdrProviderOptions', 'LdrValueHelpers', 'setupSettingsDropdowns',
];
const ORIGINAL_WINDOW_STATE = new Map(WINDOW_KEYS.map((propertyName) => [propertyName, {
owned: Object.hasOwn(window, propertyName),
value: window[propertyName],
}]));
const ORIGINAL_GLOBAL_MARKED = {
owned: Object.hasOwn(globalThis, 'marked'),
value: globalThis.marked,
};
const ORIGINAL_GLOBAL_DOMPURIFY = {
owned: Object.hasOwn(globalThis, 'DOMPurify'),
value: globalThis.DOMPurify,
};
let baselineWindowState;
let productionDOMPurify;
let productionMarked;
let productionLogHelpers;
function installDOMPurifyHappyDomShims() {
document.createNodeIterator = function createNodeIterator(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 index = 0; index < node.childNodes.length; index++) {
collect(node.childNodes[index]);
}
})(root);
let iteratorIndex = 0;
return {
nextNode() {
return iteratorIndex < nodes.length ? nodes[iteratorIndex++] : null;
},
previousNode() {
return iteratorIndex > 0 ? nodes[--iteratorIndex] : 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 '';
}
},
});
}
function restoreProperty(target, propertyName, savedState) {
if (savedState.owned) {
target[propertyName] = savedState.value;
} else {
delete target[propertyName];
}
}
function parseMarkup(markup) {
const container = document.createElement('div');
// eslint-disable-next-line no-unsanitized/property -- test-only: parsing sanitizer output is the assertion surface.
container.innerHTML = markup;
return container;
}
function renderThenReparse(markdown) {
const firstParse = parseMarkup(window.ui.renderMarkdown(markdown));
return parseMarkup(firstParse.innerHTML);
}
function namedValues(root) {
return [...root.querySelectorAll('[id], [name]')].flatMap((element) =>
['id', 'name']
.map((attributeName) => element.getAttribute(attributeName))
.filter(Boolean),
);
}
function collidingNamedValues(root, collisionNames) {
return [...new Set(namedValues(root).filter((value) => collisionNames.has(value)))].sort();
}
function assertPrototypeClean() {
expect(({}).polluted).toBeUndefined();
expect(Object.prototype.polluted).toBeUndefined();
}
function deliberatelyUnsafeRecursiveMerge(target, source) {
for (const [propertyName, propertyValue] of Object.entries(source)) {
if (propertyValue && typeof propertyValue === 'object') {
if (!target[propertyName]) {
target[propertyName] = {};
}
deliberatelyUnsafeRecursiveMerge(target[propertyName], propertyValue);
} else {
target[propertyName] = propertyValue;
}
}
return target;
}
beforeAll(async () => {
installDOMPurifyHappyDomShims();
const { Marked } = await import('marked');
productionMarked = new Marked();
productionDOMPurify = (await import('dompurify')).default;
globalThis.marked = productionMarked;
window.marked = productionMarked;
globalThis.DOMPurify = productionDOMPurify;
window.DOMPurify = productionDOMPurify;
await import('@js/config/urls.js');
await import('@js/services/api.js');
await import('@js/services/socket.js');
await import('@js/security/xss-protection.js');
await import('@js/services/ui.js');
await import('@js/utils/log-helpers.js');
productionLogHelpers = window.LdrLogHelpers;
baselineWindowState = new Map(WINDOW_KEYS.map((propertyName) => [propertyName, {
owned: Object.hasOwn(window, propertyName),
value: window[propertyName],
}]));
});
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
vi.useRealTimers();
document.body.replaceChildren();
baselineWindowState.forEach((savedState, propertyName) => {
restoreProperty(window, propertyName, savedState);
});
globalThis.marked = productionMarked;
globalThis.DOMPurify = productionDOMPurify;
});
afterAll(() => {
document.createNodeIterator = ORIGINAL_CREATE_NODE_ITERATOR;
if (ORIGINAL_NODE_NAME) {
Object.defineProperty(window.Node.prototype, 'nodeName', ORIGINAL_NODE_NAME);
}
ORIGINAL_WINDOW_STATE.forEach((savedState, propertyName) => {
restoreProperty(window, propertyName, savedState);
});
restoreProperty(globalThis, 'marked', ORIGINAL_GLOBAL_MARKED);
restoreProperty(globalThis, 'DOMPurify', ORIGINAL_GLOBAL_DOMPURIFY);
});
describe('DOM clobbering (OWASP DOM Clobbering Prevention Cheat Sheet)', () => {
it('documents the named-property behavior available in the happy-dom realm', () => {
const idProbe = document.createElement('a');
idProbe.id = '__owaspIdNamedAccessProbe';
const nameProbe = document.createElement('img');
nameProbe.name = '__owaspNameNamedAccessProbe';
document.body.append(idProbe, nameProbe);
expect(Object.hasOwn(window, '__owaspIdNamedAccessProbe')).toBe(false);
expect(Object.hasOwn(window, '__owaspNameNamedAccessProbe')).toBe(false);
});
it('renders benign markdown through the production pipeline (non-vacuity)', () => {
const root = renderThenReparse('## Safe heading\n\n**benign content**');
expect(root.querySelector('h2')?.textContent).toBe('Safe heading');
expect(root.querySelector('strong')?.textContent).toBe('benign content');
});
it('keeps real globals stable when hostile named elements are connected', () => {
const expectedGlobals = new Map([
['api', window.api],
['socket', window.socket],
['URLBuilder', window.URLBuilder],
]);
const host = parseMarkup(window.ui.renderMarkdown([
'<a id="api">api</a>',
'<img name="socket" src="x">',
'<div id="URLBuilder">builder</div>',
].join('\n')));
document.body.appendChild(host);
expect(host.textContent).toContain('api');
for (const [globalName, expectedValue] of expectedGlobals) {
expect(typeof window[globalName]).toBe(typeof expectedValue);
expect(window[globalName]).toBe(expectedValue);
}
});
it('locks the shared sanitizer DOM-clobbering configuration', () => {
const source = readFileSync(XSS_PROTECTION_PATH, 'utf8');
const configStart = source.indexOf('const SANITIZE_CONFIG = {');
const configEnd = source.indexOf('\n };', configStart);
const configSource = source.slice(configStart, configEnd);
expect(configStart).toBeGreaterThan(-1);
expect(configEnd).toBeGreaterThan(configStart);
expect(configSource).toMatch(/\bSANITIZE_DOM:\s*true\b/);
expect(configSource).toMatch(/\bSANITIZE_NAMED_PROPS:\s*true\b/);
});
it('sanitizeHtml inert-izes collisions with sensitive Window/Document built-ins', () => {
const dirty = [...SENSITIVE_NAMED_PROPS]
.map((propertyName) => `<a id="${propertyName}" name="${propertyName}">${propertyName}</a>`)
.join('');
const root = parseMarkup(window.XSSProtection.sanitizeHtml(dirty));
expect(root.querySelectorAll('a')).toHaveLength(SENSITIVE_NAMED_PROPS.size);
expect(root.textContent).toContain('location');
expect(collidingNamedValues(root, SENSITIVE_NAMED_PROPS)).toEqual([]);
});
// App-defined globals need filtering beyond DOMPurify's built-in collision protection.
//
// NOTE ON WHAT THIS DOES AND DOES NOT COVER: this asserts the *outcome*
// -- no element reaching the DOM carries an app-global id/name -- not the
// MARKDOWN_PROHIBITED_NAMED_PROPS namespacing loop in ui.js. Markdown
// never delivers an author-chosen id/name that far: renderMarkdown's
// `renderer.html` hook escapes raw author HTML to text, and `name` is not
// in MARKDOWN_ALLOWED_ATTR, so the payload below arrives as literal text
// and the loop sees nothing. Deleting that loop would NOT fail this test.
// See the comment above MARKDOWN_PROHIBITED_NAMED_PROPS in
// services/ui.js: it is defence-in-depth with no revert-fails test, and
// the registry is a best-effort snapshot rather than a checked invariant.
test('markdown named attributes cannot collide with the source-derived app-global registry', () => {
expect(APP_GLOBALS.size).toBeGreaterThan(50);
for (const expectedGlobal of [
'api', 'socket', 'ui', 'URLBuilder', 'URLValidator', 'SafeLogger',
]) {
expect(APP_GLOBALS.has(expectedGlobal)).toBe(true);
}
const payload = [...APP_GLOBALS]
.map((globalName) => `<div id="${globalName}" name="${globalName}">${globalName}</div>`)
.join('\n');
const root = renderThenReparse(payload);
expect(collidingNamedValues(root, APP_GLOBALS)).toEqual([]);
});
// The collision boundary must survive the production serialize/reparse path.
test('rendered-then-reparsed hostile corpus has no app-global or sensitive built-in names', () => {
const collisionNames = new Set([...APP_GLOBALS, ...SENSITIVE_NAMED_PROPS]);
const root = renderThenReparse(STANDARD_CLOBBERING_CORPUS.join('\n'));
expect(root.textContent).toContain('api probe');
expect(collidingNamedValues(root, collisionNames)).toEqual([]);
});
});
async function exerciseSettingsJsonSave(jsonBody) {
document.body.innerHTML = `
<form id="settings-form">
<input id="settings-search">
<button type="button" class="ldr-settings-tab active" data-tab="all">All</button>
<div id="settings-alert"></div>
<div id="settings-content"></div>
<div id="raw-config" style="display: none">
<textarea id="raw_config_editor"></textarea>
</div>
</form>`;
vi.useFakeTimers();
vi.resetModules();
await import('@js/config/urls.js');
await import('@js/services/api.js');
await import('@js/utils/alert-helpers.js');
await import('@js/utils/provider-options.js');
await import('@js/utils/value-helpers.js');
await import('@js/security/xss-protection.js');
const settingDescriptor = {
name: 'Prototype pollution probe',
description: 'JSON editor security probe',
value: [],
ui_element: 'json',
editable: true,
visible: true,
category: 'app_parameters',
};
const apiRequests = vi.fn(async (requestUrl, requestOptions = {}) => {
if (requestUrl === window.URLS.SETTINGS_API.SAVE_ALL_SETTINGS) {
const savedSettings = JSON.parse(requestOptions.body);
return {
status: 'success',
settings: {
[SETTINGS_KEY]: {
...settingDescriptor,
value: savedSettings[SETTINGS_KEY],
},
},
};
}
if (requestUrl === window.URLS.SETTINGS_API.BASE) {
return {
status: 'success',
settings: { [SETTINGS_KEY]: { ...settingDescriptor } },
};
}
if (requestUrl === '/settings/api/data-location') {
return { data_directory: '/tmp/ldr-test', security_notice: { encrypted: true } };
}
if (requestUrl === window.URLS.SETTINGS_API.BACKUP_STATUS) {
return { enabled: false, count: 0, backups: [] };
}
return { status: 'success' };
});
window.api.fetchWithErrorHandling = apiRequests;
vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({
providers: {},
provider_options: [],
engine_options: [],
}), { status: 200 })));
await import('@js/components/settings.js');
if (!apiRequests.mock.calls.some(([requestUrl]) =>
requestUrl === window.URLS.SETTINGS_API.BASE)) {
document.dispatchEvent(new Event('DOMContentLoaded'));
}
await vi.advanceTimersByTimeAsync(400);
const editor = document.getElementById('setting-app-prototype_pollution_probe');
expect(editor).not.toBeNull();
expect(editor.classList.contains('ldr-json-content')).toBe(true);
editor.value = jsonBody;
editor.dispatchEvent(new Event('input', { bubbles: true }));
editor.dispatchEvent(new Event('blur', { bubbles: true }));
await vi.advanceTimersByTimeAsync(1);
await Promise.resolve();
const saveCall = apiRequests.mock.calls.find(([requestUrl]) =>
requestUrl === window.URLS.SETTINGS_API.SAVE_ALL_SETTINGS);
expect(saveCall).toBeDefined();
return JSON.parse(saveCall[1].body)[SETTINGS_KEY];
}
describe('client-side prototype pollution (OWASP WSTG-INV-22)', () => {
let pollutedDescriptor;
beforeEach(() => {
pollutedDescriptor = Object.getOwnPropertyDescriptor(Object.prototype, 'polluted');
delete Object.prototype.polluted;
});
afterEach(() => {
delete Object.prototype.polluted;
if (pollutedDescriptor) {
Reflect.defineProperty(Object.getPrototypeOf({}), 'polluted', pollutedDescriptor);
}
});
it('keeps hostile category keys inert through LdrLogHelpers.bumpCount', () => {
const counts = productionLogHelpers.emptyCounts();
for (const hostileKey of ['__proto__', 'constructor.prototype', '__proto__[x]']) {
productionLogHelpers.bumpCount(counts, hostileKey, 1);
}
expect(counts).toEqual({ info: 0, milestone: 0, warning: 0, error: 0 });
expect(Object.getPrototypeOf(counts)).toBe(Object.prototype);
assertPrototypeClean();
});
it.each([
['__proto__', '{"__proto__":{"polluted":"yes"}}'],
['constructor.prototype', '{"constructor":{"prototype":{"polluted":"yes"}}}'],
])('keeps %s inert through the real settings JSON load/save flow', async (hostileKey, jsonBody) => {
const postedValue = await exerciseSettingsJsonSave(jsonBody);
if (hostileKey === '__proto__') {
expect(Object.hasOwn(postedValue, '__proto__')).toBe(true);
const ownProtoValue = Object.getOwnPropertyDescriptor(postedValue, '__proto__').value;
expect(ownProtoValue.polluted).toBe('yes');
} else {
expect(postedValue.constructor.prototype.polluted).toBe('yes');
}
assertPrototypeClean();
});
it('round-trips benign settings JSON through the same save path (non-vacuity)', async () => {
const benign = { theme: 'dark', nested: { enabled: true } };
const postedValue = await exerciseSettingsJsonSave(JSON.stringify(benign));
expect(postedValue).toEqual(benign);
assertPrototypeClean();
});
it('proves the prototype-clean detector trips on a deliberately unsafe merge', () => {
const syntheticPayload = JSON.parse('{"__proto__":{"polluted":"yes"}}');
try {
expect(() => {
deliberatelyUnsafeRecursiveMerge({}, syntheticPayload);
assertPrototypeClean();
}).toThrow();
} finally {
delete Object.prototype.polluted;
}
assertPrototypeClean();
});
it('records no hand-rolled recursive merge/clone/path helper at JSON boundaries', () => {
const jsonBoundaryPattern = /\bJSON\.parse\(|(?:local|session)Storage\.getItem\(|fetchWithErrorHandling\(/;
const helperName = '(?:merge|clone|deepMerge|recursiveMerge|mergeObjects?|deepClone|recursiveClone|cloneObjects?|setByPath|setNestedValue|assignPath|assignNestedValue)';
const helperDefinitionPattern = new RegExp(
`\\b(?:function\\s+${helperName}\\s*\\(|(?:const|let|var)\\s+${helperName}\\s*=\\s*(?:async\\s*)?(?:function\\b|\\([^)]*\\)\\s*=>|[a-z_$][\\w$]*\\s*=>))`,
'i',
);
const boundarySources = SOURCE_RECORDS.filter((sourceRecord) =>
jsonBoundaryPattern.test(sourceRecord.contents));
const candidates = SOURCE_RECORDS.flatMap((sourceRecord) =>
sourceRecord.contents.split('\n').flatMap((sourceLine, lineIndex) => (
helperDefinitionPattern.test(sourceLine)
? [`${sourceRecord.relativePath}:${lineIndex + 1}: ${sourceLine.trim()}`]
: []
)));
expect(SOURCE_RECORDS.length).toBeGreaterThan(50);
expect(boundarySources.length).toBeGreaterThan(10);
expect(candidates).toEqual([]);
});
});