~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 tests for services/socket.js — socket-event -> DOM surface.
*
* socket.js registers handlers for server-pushed Socket.IO events whose
* payloads are indirectly untrusted (engine names and progress/log messages
* originate from web-search results and LLM output). This file fires each
* handler with hostile payloads and asserts inertness at the real sink.
*
* Sink map (verified against socket.js source):
* - 'search_engine_selected' (:116) -> window.addConsoleLog (:127)
* - 'research_progress_<id>' (:203) -> handleProgressUpdate ->
* window.addConsoleLog at :302 (engine path), :372 (progress_log
* items), :425 (log_entry), :451 (message/status path)
* - 'connect_error' (:100), 'error' (:156), 'disconnect' (:138),
* 'reconnect' (:147), 'reconnect_attempt' (:152) -> SafeLogger only;
* no DOM writes (asserted below as payload-received + untouched DOM).
*
* In production window.addConsoleLog is logpanel.js's function. This suite
* isolates socket.js's standalone DOM fallback by clearing its routing
* connector. With the socket's own connector still installed, current main
* deliberately drops entries instead of recursing. Clearing it here lets the
* real fallback sink run so its output encoding can be checked directly.
*/
// Static import is safe: module scope only schedules a 100ms auto-init that
// no-ops off research pages. Globals below are installed before anything
// fires. Mirrors the import style of tests/js/services/api.test.js.
import '@js/services/socket.js';
// Registered socket event handlers, captured by the fake socket.io client.
const registeredHandlers = new Map();
let fakeSocket;
// window.escapeHtml — identical implementation to escapeHtmlFallback in
// services/ui.js (:75 there), which production pages provide globally.
const escapeHtml =
(str) => String(str).replace(/[&<>"']/g, (m) => ({'&':'&','<':'<','>':'>','"':'"',"'":'''})[m]);
const originalLocation = window.location;
/** Fire a captured socket handler with a payload. */
function emit(event, payload) {
const handler = registeredHandlers.get(event);
if (typeof handler !== 'function') {
throw new Error(`no handler registered for '${event}'`);
}
handler(payload);
}
/**
* Assert the last log entry landed inert: no img/script elements anywhere,
* and the message text present as escaped, non-parsed content.
*/
function expectInertLogEntry(rawPayloadSubstring) {
const entries = document.querySelectorAll('.ldr-console-log-entry');
expect(entries.length).toBeGreaterThan(0); // sink actually ran
expect(document.querySelectorAll('img')).toHaveLength(0);
expect(document.querySelectorAll('script')).toHaveLength(0);
expect(document.querySelectorAll('[onerror]')).toHaveLength(0);
const message = entries[entries.length - 1].querySelector('.ldr-log-message');
expect(message).not.toBeNull();
// Raw payload visible as inert text...
expect(message.textContent).toContain(rawPayloadSubstring);
// ...and stored escaped, never as live markup.
expect(message.innerHTML).not.toMatch(/<(img|script)\b/i);
expect(message.innerHTML).toContain('<');
}
beforeAll(() => {
vi.spyOn(SafeLogger, 'log');
vi.spyOn(SafeLogger, 'warn');
vi.spyOn(SafeLogger, 'error');
window.escapeHtml = escapeHtml;
// Bare globals socket.js resolves at call time.
globalThis.ResearchStates = { logLevel: () => 'info', isTerminal: () => false };
// Fake socket.io-client: records .on(event, cb) registrations.
fakeSocket = {
connected: true,
on: vi.fn((event, cb) => registeredHandlers.set(event, cb)),
off: vi.fn(),
emit: vi.fn(),
disconnect: vi.fn(),
};
globalThis.io = vi.fn(() => fakeSocket);
// Reach the standalone DOM sink rather than the missing-logpanel guard.
delete window._socketAddLogEntry;
// Research page so initializeSocket() actually creates the socket.
delete window.location;
window.location = {
href: 'http://localhost:5000/progress/xss-test',
protocol: 'http:',
host: 'localhost:5000',
pathname: '/progress/xss-test',
};
window.socket.init();
window.socket.subscribeToResearch('r-xss');
expect(registeredHandlers.get('search_engine_selected')).toBeTypeOf('function');
expect(registeredHandlers.get('research_progress_r-xss')).toBeTypeOf('function');
});
afterAll(() => {
window.socket.disconnect();
window.location = originalLocation;
});
beforeEach(() => {
// A prior error/disconnect case may already own the polling fallback.
// Begin each scenario with the real connected-state reset.
emit('connect');
vi.clearAllMocks();
window._processedSocketMessages = new Map();
window.pollResearchStatus = vi.fn();
// Log-panel markup WITHOUT the entry template and WITHOUT logpanel.js:
// this is exactly the configuration socket.js's fallback DOM sink serves.
document.body.innerHTML =
'<div id="console-log-container"></div><span id="log-indicator">0</span>';
});
describe('socket events -> DOM XSS surface', () => {
it('sanity: the inertness detector is not vacuous (unescaped payload does create an img)', () => {
// Detached node: setting innerHTML never fires load/error events,
// this only proves querySelectorAll('img') would find a real leak.
const scratch = document.createElement('div');
scratch.innerHTML = '<img src=x onerror=alert(1)>';
expect(scratch.querySelectorAll('img')).toHaveLength(1);
});
it('search_engine_selected: engine name with <img onerror> markup lands inert', () => {
emit('search_engine_selected', {
engine: '<img src=x onerror=alert(1)>',
result_count: 3,
});
expectInertLogEntry('Search engine selected: <img src=x onerror=alert(1)> (found 3 results)');
// Sink really executed: the indicator counted the entry.
expect(document.getElementById('log-indicator').textContent).toBe('1');
});
it('search_engine_selected: script-injection engine name lands inert', () => {
emit('search_engine_selected', {
engine: '"><script>alert(1)</script>',
result_count: 0,
});
expectInertLogEntry('"><script>alert(1)</script>');
expect(document.querySelectorAll('script')).toHaveLength(0);
expect(document.body.innerHTML).not.toContain('<script>');
});
it('research_progress_<id>: hostile message string lands inert at the addConsoleLog sink', () => {
emit('research_progress_r-xss', {
status: 'in_progress',
message: '<img src=x onerror=alert(1)>',
});
expectInertLogEntry('<img src=x onerror=alert(1)>');
// status only feeds ResearchStates.logLevel in socket.js — it must
// never appear in the DOM by itself.
expect(document.body.textContent).not.toContain('in_progress<script>');
});
it('research_progress_<id>: hostile log_entry.message lands inert', () => {
emit('research_progress_r-xss', {
log_entry: {
time: '2026-01-01T00:00:00Z',
type: 'error',
message: '<img src=x onerror=alert(1)>',
},
});
expectInertLogEntry('<img src=x onerror=alert(1)>');
expect(document.querySelectorAll('.ldr-log-error')).toHaveLength(1);
});
it('research_progress_<id>: hostile messages inside progress_log JSON land inert', () => {
emit('research_progress_r-xss', {
progress_log: JSON.stringify([
{ time: 't-xss-1', message: '"><script>alert(1)</script>' },
{ time: 't-xss-2', message: '<img src=x onerror=alert(1)>' },
]),
});
expect(document.querySelectorAll('.ldr-console-log-entry')).toHaveLength(2);
expect(document.querySelectorAll('img')).toHaveLength(0);
expect(document.querySelectorAll('script')).toHaveLength(0);
const messages = [...document.querySelectorAll('.ldr-log-message')];
expect(messages[0].textContent).toContain('"><script>alert(1)</script>');
expect(messages[1].textContent).toContain('<img src=x onerror=alert(1)>');
expect(messages.every((m) => m.innerHTML.includes('<'))).toBe(true);
});
it('connect_error: hostile error is logged to SafeLogger only — no DOM writes', () => {
// Handler (:100) has no DOM sink: SafeLogger.warn + attempt counting.
emit('connect_error', { message: '<img src=x onerror=alert(1)>' });
expect(SafeLogger.warn).toHaveBeenCalled();
const warnArg = SafeLogger.warn.mock.calls.find(
(call) => call[0] === 'Socket connection error:'
)[1];
expect(warnArg.message).toBe('<img src=x onerror=alert(1)>');
// Lock in "no DOM sink": nothing materialized.
expect(document.getElementById('console-log-container').children).toHaveLength(0);
expect(document.querySelectorAll('img, script')).toHaveLength(0);
});
it('error: hostile error is logged and polling fallback invoked — no DOM writes', () => {
emit('error', { message: '"><script>alert(1)</script>' });
const errorArg = SafeLogger.error.mock.calls.find(
(call) => call[0] === 'Socket error:'
)[1];
expect(errorArg.message).toBe('"><script>alert(1)</script>');
expect(window.pollResearchStatus).toHaveBeenCalledWith('r-xss');
expect(document.getElementById('console-log-container').children).toHaveLength(0);
expect(document.querySelectorAll('img, script')).toHaveLength(0);
});
it('disconnect/reconnect/reconnect_attempt: hostile payloads stay in SafeLogger, DOM untouched', () => {
emit('disconnect', '<img src=x onerror=alert(1)>');
emit('reconnect', '"><script>');
emit('reconnect_attempt', '<img src=x onerror=alert(1)>');
expect(SafeLogger.log).toHaveBeenCalledWith('Socket disconnected:', '<img src=x onerror=alert(1)>');
expect(SafeLogger.log).toHaveBeenCalledWith('Socket reconnected after', '"><script>', 'attempts');
expect(SafeLogger.log).toHaveBeenCalledWith('Socket reconnection attempt:', '<img src=x onerror=alert(1)>');
expect(document.getElementById('console-log-container').children).toHaveLength(0);
expect(document.querySelectorAll('img, script')).toHaveLength(0);
});
});