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.

fix(security): guard details metrics and links (#7051)

LearningCircuitf091c6a20b8c
diff --git a/changelog.d/+details-metrics-rendering.security.md b/changelog.d/+details-metrics-rendering.security.md
new file mode 100644
index 0000000000..81a613193b
--- /dev/null
+++ b/changelog.d/+details-metrics-rendering.security.md
@@ -0,0 +1 @@
+Reject nonnumeric API metrics before rendering research details, and allow only HTTP(S) resource links even when the sanitizer is unavailable.
diff --git a/src/local_deep_research/web/static/js/components/details.js b/src/local_deep_research/web/static/js/components/details.js
index 6288ab239f..e9cb6b4b89 100644
--- a/src/local_deep_research/web/static/js/components/details.js
+++ b/src/local_deep_research/web/static/js/components/details.js
@@ -146,15 +146,27 @@
// takes the fallback branch and bypasses real escaping
// when the global helper hasn't loaded yet.
const escapeAttr = window.escapeHtmlAttribute || escapeHtml;
- const resourceHtml = data.resources.map(resource => `
+ const resourceHtml = data.resources.map(resource => {
+ let safeHref = '#';
+ try {
+ const rawUrl = typeof resource.url === 'string' ? resource.url.trim() : '';
+ const parsed = rawUrl ? new URL(rawUrl, window.location.href) : null;
+ if (parsed && (parsed.protocol === 'http:' || parsed.protocol === 'https:')) {
+ safeHref = escapeAttr(parsed.href);
+ }
+ } catch (_) {
+ // Malformed URLs remain visible as text, without a live link.
+ }
+ return `
<div style="padding: 0.75rem; border-bottom: 1px solid var(--border-color);">
<div style="font-weight: 500; margin-bottom: 0.25rem;">${escapeHtml(resource.title)}</div>
- <a href="${escapeAttr(resource.url)}" target="_blank" rel="noopener noreferrer" style="color: var(--primary-color); text-decoration: none; font-size: 0.875rem; word-break: break-all;">
+ <a href="${safeHref}" target="_blank" rel="noopener noreferrer" style="color: var(--primary-color); text-decoration: none; font-size: 0.875rem; word-break: break-all;">
${escapeHtml(resource.url)}
</a>
${resource.preview ? `<div style="color: var(--text-secondary); font-size: 0.875rem; margin-top: 0.5rem;">${escapeHtml(resource.preview)}</div>` : ''}
</div>
- `).join('');
+ `;
+ }).join('');
if (window.sanitizeHtml) {
// bearer:disable javascript_lang_dangerous_insert_html
resourceSample.innerHTML = window.sanitizeHtml(resourceHtml);
@@ -605,7 +617,7 @@
<div class="ldr-search-engine-name">${escapeHtml(engine.engine || 'Unknown')}</div>
</div>
<div class="ldr-search-engine-stats">
- <span>${engine.call_count ?? 0} searches</span>
+ <span>${formatNumber(engine.call_count ?? 0)} searches</span>
${engine.skipped_count ? `<span class="ldr-search-status-skipped">${Number(engine.skipped_count)} skipped (engine unavailable)</span>` : ''}
<span>${(engine.success_rate ?? 0).toFixed(1)}% success</span>
<span>${((engine.avg_response_time ?? 0) / 1000).toFixed(2)}s avg</span>
@@ -686,7 +698,7 @@
${escapeHtml(item.calling_function || 'Unknown Function')}
</div>
<div style="font-size: 0.875rem; color: var(--text-secondary);">
- ${timestamp} \u2014 ${item.prompt_tokens || 0} in + ${item.completion_tokens || 0} out = ${item.tokens || 0} tokens, ${item.response_time_ms || 0}ms
+ ${timestamp} \u2014 ${formatNumber(item.prompt_tokens || 0)} in + ${formatNumber(item.completion_tokens || 0)} out = ${formatNumber(item.tokens || 0)} tokens, ${formatNumber(item.response_time_ms || 0)}ms
</div>
</div>
<div style="font-family: 'Courier New', monospace; font-size: 0.75rem; background: var(--bg-tertiary); padding: 0.5rem; border-radius: 4px; color: var(--text-secondary); overflow-x: auto; margin-bottom: 0.5rem;">
diff --git a/src/local_deep_research/web/static/js/services/formatting.js b/src/local_deep_research/web/static/js/services/formatting.js
index 48970f9e2d..053c2e9952 100644
--- a/src/local_deep_research/web/static/js/services/formatting.js
+++ b/src/local_deep_research/web/static/js/services/formatting.js
@@ -107,8 +107,12 @@ function capitalizeFirstLetter(string) {
* @returns {string} Formatted number
*/
function formatNumber(num) {
- if (num === null || num === undefined) return '0';
- return num.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
+ // Metrics arrive as JSON, so never pass a string through to an HTML sink.
+ const numeric = typeof num === 'number' || typeof num === 'string'
+ ? Number(num)
+ : NaN;
+ if (!Number.isFinite(numeric)) return '0';
+ return numeric.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
}
/**
diff --git a/tests/js/security/details-render-xss.test.js b/tests/js/security/details-render-xss.test.js
new file mode 100644
index 0000000000..d15efdbe78
--- /dev/null
+++ b/tests/js/security/details-render-xss.test.js
@@ -0,0 +1,889 @@
+/**
+ * Exercise real research-details rendering with API payloads, including
+ * numeric strings in HTML sinks and resource links when the sanitizer is
+ * unavailable. The browser-DOM fixture loads production escapers and starts
+ * the page through its DOMContentLoaded listener.
+ */
+
+import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
+
+const RESEARCH_ID = 'research-123';
+
+const DETAILS_MARKUP = `
+ <div id="loading"></div>
+ <div id="error" style="display: none;"></div>
+ <div id="details-content" style="display: none;">
+ <h1 id="research-query"></h1>
+ <p id="research-mode"></p>
+ <p id="research-date"></p>
+ <p id="research-strategy"></p>
+ <div id="detail-progress-fill"></div>
+ <span id="detail-progress-percentage"></span>
+ <span id="total-tokens"></span>
+ <span id="prompt-tokens"></span>
+ <span id="completion-tokens"></span>
+ <span id="llm-calls"></span>
+ <span id="avg-response-time"></span>
+ <span id="model-used"></span>
+ <span id="total-cost"></span>
+ <div id="phase-breakdown"></div>
+ <span id="total-searches"></span>
+ <span id="total-search-results"></span>
+ <span id="avg-search-response-time"></span>
+ <span id="search-success-rate"></span>
+ <div id="search-engine-breakdown"></div>
+ <div id="search-engine-performance"></div>
+ <div id="search-timeline"></div>
+ <div id="call-stack-card"><div id="call-stack-traces"></div></div>
+ <div><canvas id="timeline-chart"></canvas></div>
+ <div><canvas id="search-chart"></canvas></div>
+ </div>
+ <div id="source-distribution-section">
+ <div class="ldr-card-content"><canvas id="source-type-chart"></canvas></div>
+ </div>
+ <div id="link-analytics-section">
+ <span id="total-links"></span>
+ <span id="unique-domains"></span>
+ <span>Category A</span><span id="academic-sources"></span>
+ <span>Category B</span><span id="news-sources"></span>
+ <div id="domain-list"></div>
+ <div id="resource-sample"></div>
+ </div>
+ <div id="context-overflow-section">
+ <span id="co-total-tokens"></span>
+ <span id="co-context-limit"></span>
+ <span id="co-max-tokens"></span>
+ <span id="co-truncation-status"></span>
+ <div id="co-phase-breakdown"></div>
+ <table><tbody id="co-requests-table"></tbody></table>
+ <canvas id="co-usage-chart"></canvas>
+ <div id="co-performance-warning"></div>
+ </div>
+`;
+
+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 realEscapeHtml;
+let realEscapeHtmlAttribute;
+let realSanitizeHtml;
+
+beforeAll(async () => {
+ // --- happy-dom shims required by DOMPurify -------------------------------
+ // Copied from tests/js/security/results-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 at module-eval time, so the
+ // shims must be installed BEFORE the dynamic imports 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 globals details.js reaches at init time ---------------
+ window.URLBuilder = {
+ extractResearchIdFromPattern: () => RESEARCH_ID,
+ historyDetails: (id) => `/history/api/research/${id}`,
+ build: (base, id) => `${base}/${id}`,
+ resultsPage: (id) => `/results/${id}`,
+ journalQualityPage: (id) => `/metrics/journals?research_id=${id}`,
+ };
+ window.URLS = {
+ METRICS_API: {
+ RESEARCH: '/metrics/api/metrics/research',
+ RESEARCH_TIMELINE: '/metrics/api/metrics/research-timeline',
+ RESEARCH_SEARCH: '/metrics/api/metrics/research-search',
+ },
+ PAGES: { HISTORY: '/history' },
+ };
+ // Chart.js is canvas-rendered (no innerHTML surface); details.js calls
+ // `new Chart(...)` unguarded on the search-chart path, so a no-op stub
+ // keeps the real render flow running to completion. happy-dom ships no
+ // canvas implementation, so getContext is shimmed to return null (the
+ // Chart stub ignores it).
+ globalThis.Chart = class Chart {
+
+ destroy() {}
+ };
+ window.HTMLCanvasElement.prototype.getContext = function() { return null; };
+
+ // --- production globals --------------------------------------------------
+ // Real escaper/sanitizer (window.escapeHtml, window.escapeHtmlAttribute,
+ // window.sanitizeHtml) as loaded by base.html.
+ const DOMPurify = (await import('dompurify')).default;
+ globalThis.DOMPurify = DOMPurify;
+ await import('@js/security/xss-protection.js');
+ // Real truncation-badge helper that details.js:1109 delegates to.
+ await import('@js/components/context-overflow-shared.js');
+
+
+ realEscapeHtml = window.escapeHtml;
+ realEscapeHtmlAttribute = window.escapeHtmlAttribute;
+ realSanitizeHtml = window.sanitizeHtml;
+});
+
+afterAll(() => {
+ delete globalThis.DOMPurify;
+ delete globalThis.Chart;
+});
+
+afterEach(() => {
+ // Tests delete these to isolate degraded-mode branches; restore the
+ // production globals for the next test.
+ window.escapeHtml = realEscapeHtml;
+ window.escapeHtmlAttribute = realEscapeHtmlAttribute;
+ window.sanitizeHtml = realSanitizeHtml;
+});
+
+function respond(payload) {
+ if (payload === null || payload === undefined) {
+ return { ok: false, status: 404 };
+ }
+ return { ok: true, json: () => Promise.resolve(payload) };
+}
+
+/**
+ * Mount the real details component against a fresh DOM and a fetch mock
+ * serving the six endpoints it polls, then dispatch the DOMContentLoaded
+ * event its IIFE-only listener waits for, and wait for the primary render
+ * (details-content shown, domain list populated, co badge when served).
+ */
+async function mountDetails(responses = {}) {
+ const cfg = {
+ details: {
+ query: 'benign query',
+ mode: 'quick',
+ created_at: '2026-01-02T03:04:05Z',
+ strategy: 'focused-scan',
+ progress: 50,
+ },
+ metrics: { status: 'success', metrics: { total_tokens: 1234, model_usage: [] } },
+ timeline: null,
+ search: null,
+ links: { status: 'success', data: {} },
+ contextOverflow: null,
+ ...responses,
+ };
+
+ // eslint-disable-next-line no-unsanitized/property -- test fixture: hardcoded static template with no interpolated values.
+ document.body.innerHTML = DETAILS_MARKUP;
+ globalThis.fetch = vi.fn((url) => {
+ if (url === `/history/api/research/${RESEARCH_ID}`) {
+ return Promise.resolve(respond(cfg.details));
+ }
+ if (url === `/metrics/api/metrics/research/${RESEARCH_ID}`) {
+ return Promise.resolve(respond(cfg.metrics));
+ }
+ if (url === `/metrics/api/metrics/research-timeline/${RESEARCH_ID}`) {
+ return Promise.resolve(respond(cfg.timeline));
+ }
+ if (url === `/metrics/api/metrics/research-search/${RESEARCH_ID}`) {
+ return Promise.resolve(respond(cfg.search));
+ }
+ if (url === `/metrics/api/metrics/research/${RESEARCH_ID}/links`) {
+ return Promise.resolve(respond(cfg.links));
+ }
+ if (url === `/api/research/${RESEARCH_ID}/context-overflow`) {
+ return Promise.resolve(respond(cfg.contextOverflow));
+ }
+ return Promise.reject(new Error(`Unexpected fetch: ${url}`));
+ });
+ // The details page may use the shared JSON client for history data.
+ // Keep this rendering fixture on the same response payload while the
+ // separate API-client tests cover negotiation and session redirects.
+ window.api = {
+ fetchWithErrorHandling: async (url) => {
+ const response = await globalThis.fetch(url);
+ if (!response.ok) throw new Error(`Request failed: ${response.status}`);
+ return response.json();
+ },
+ };
+
+ // details.js registers a bare DOMContentLoaded listener (details.js:1298)
+ // with no readyState fast-path, so re-import and fire the real event.
+ vi.resetModules();
+ await import('@js/components/details.js');
+ document.dispatchEvent(new Event('DOMContentLoaded'));
+
+ await vi.waitFor(() => {
+ expect(document.getElementById('details-content').style.display).toBe('block');
+ expect(document.getElementById('domain-list').innerHTML).not.toBe('');
+ if (cfg.contextOverflow) {
+ expect(document.getElementById('co-truncation-status').innerHTML).not.toBe('');
+ }
+ });
+ return document.getElementById('details-content');
+}
+
+/** 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;
+}
+
+/** Whole-document guard: no live injection elements, no on* attributes. */
+function expectNoLiveMarkup() {
+ expect(document.querySelectorAll('img, script, svg, iframe')).toHaveLength(0);
+ expect(eventHandlerAttributes(document.body)).toEqual([]);
+}
+
+describe('details.js link analytics rendering (loadLinkAnalytics)', () => {
+ it('escapes hostile domain names/counts/percentages as inert text', async () => {
+ await mountDetails({
+ links: {
+ status: 'success',
+ data: {
+ total_links: 2,
+ unique_domains: 1,
+ domains: [{
+ domain: TEXT_PAYLOADS[0],
+ count: TEXT_PAYLOADS[1],
+ percentage: TEXT_PAYLOADS[2],
+ }],
+ },
+ },
+ });
+
+ const domainList = document.getElementById('domain-list');
+ expect(domainList.children.length).toBeGreaterThan(0);
+ expectNoLiveMarkup();
+ for (const payload of TEXT_PAYLOADS) {
+ expect(domainList.textContent).toContain(payload);
+ }
+ });
+
+ it('keeps quote-breakout and javascript: URLs out of resource hrefs via the real sanitizeHtml pipeline', async () => {
+ await mountDetails({
+ links: {
+ status: 'success',
+ data: {
+ resources: [
+ { title: 'Paper', url: `https://example.com/${ATTR_PAYLOAD}`, preview: 'p' },
+ { title: 'Bad scheme', url: 'javascript:alert(1)', preview: 'p' },
+ { title: 'Data scheme', url: 'data:text/html,<script>alert(2)</script>', preview: 'p' },
+ ],
+ },
+ },
+ });
+
+ const sample = document.getElementById('resource-sample');
+ const anchors = sample.querySelectorAll('a');
+ expect(anchors.length).toBeGreaterThan(0); // non-vacuous: resources rendered
+ for (const a of anchors) {
+ // A stripped href (null) is the safe outcome — coerce before matching.
+ expect(a.getAttribute('href') ?? '').not.toMatch(/^(javascript|data|vbscript):/i);
+ expect(a.getAttribute('rel')).toContain('noopener');
+ }
+ expect(sample.querySelector('[onmouseover]')).toBeNull();
+ expectNoLiveMarkup();
+ expect(sample.textContent).toContain(ATTR_PAYLOAD);
+ });
+
+ it('escapes hostile resource titles/previews and category labels as text', async () => {
+ await mountDetails({
+ links: {
+ status: 'success',
+ data: {
+ total_links: 3,
+ domain_categories: { [TEXT_PAYLOADS[0]]: 7 },
+ domains: [{ domain: 'arxiv.org', count: 2, percentage: 50 }],
+ resources: [{
+ title: TEXT_PAYLOADS[1],
+ url: 'https://example.com/paper',
+ preview: TEXT_PAYLOADS[2],
+ }],
+ },
+ },
+ });
+
+ const sample = document.getElementById('resource-sample');
+ expect(sample.textContent).toContain(TEXT_PAYLOADS[1]);
+ expect(sample.textContent).toContain(TEXT_PAYLOADS[2]);
+ // Category label is written via textContent (details.js:95) — inert.
+ const label = document.querySelector('#academic-sources').previousElementSibling;
+ expect(label.textContent).toContain(TEXT_PAYLOADS[0]);
+ expectNoLiveMarkup();
+ });
+
+ // The URL scheme check must remain effective if the sanitizer and shared
+ // escaping helpers are unavailable while the details page renders.
+ it('keeps non-HTTP resource hrefs inert when the sanitizer is unavailable', async () => {
+ delete window.sanitizeHtml;
+ delete window.escapeHtml;
+ delete window.escapeHtmlAttribute;
+
+ await mountDetails({
+ links: {
+ status: 'success',
+ data: {
+ resources: [
+ { title: 'Bad scheme', url: 'javascript:alert(1)', preview: 'p' },
+ { title: 'Data scheme', url: 'data:text/html,<svg onload=alert(2)>', preview: 'p' },
+ { title: 'Good scheme', url: 'https://example.com/paper', preview: 'p' },
+ ],
+ },
+ },
+ });
+
+ const sample = document.getElementById('resource-sample');
+ const anchors = sample.querySelectorAll('a');
+ expect(anchors).toHaveLength(3);
+ // The no-domain placeholder still renders in degraded mode.
+ expect(document.getElementById('domain-list').textContent)
+ .toBe('No domain data available');
+ expectNoLiveMarkup();
+ expect([...anchors].map(anchor => anchor.getAttribute('href')))
+ .toEqual(['#', '#', 'https://example.com/paper']);
+ });
+});
+
+describe('details.js research details metadata (displayResearchDetails)', () => {
+ it('renders hostile query/mode/strategy/date/progress as inert text', async () => {
+ const content = await mountDetails({
+ details: {
+ query: TEXT_PAYLOADS[0],
+ mode: TEXT_PAYLOADS[2],
+ created_at: TEXT_PAYLOADS[1],
+ strategy: TEXT_PAYLOADS[0],
+ progress: TEXT_PAYLOADS[1],
+ },
+ });
+
+ const query = document.getElementById('research-query');
+ expect(query.textContent).toBe(TEXT_PAYLOADS[0]);
+ expect(query.children).toHaveLength(0);
+
+ const mode = document.getElementById('research-mode');
+ expect(mode.children).toHaveLength(0);
+ expect(mode.textContent).toContain('<svg onload=alert(3)>');
+
+ // Hostile created_at → Invalid Date string, still a text node.
+ const date = document.getElementById('research-date');
+ expect(date.children).toHaveLength(0);
+ expect(date.textContent).not.toContain('<img');
+ expect(date.textContent).not.toBe('');
+
+ expect(document.getElementById('research-strategy').textContent)
+ .toBe(TEXT_PAYLOADS[0]);
+
+ // progress → style.width (CSSOM, not HTML) + textContent percentage.
+ expect(document.getElementById('detail-progress-percentage').textContent)
+ .toContain('alert(2)');
+ expectNoLiveMarkup();
+ expect(content.textContent.trim().length).toBeGreaterThan(0);
+ });
+});
+
+describe('details.js timeline metrics (displayTimelineMetrics / displayCallStackTraces)', () => {
+ it('escapes hostile phase names in the phase breakdown', async () => {
+ await mountDetails({
+ timeline: {
+ status: 'success',
+ metrics: {
+ research_details: {
+ query: TEXT_PAYLOADS[0],
+ mode: 'quick',
+ created_at: '2026-01-02T03:04:05Z',
+ },
+ summary: { avg_response_time: 1500 },
+ phase_stats: { [`plan ${TEXT_PAYLOADS[1]}`]: { tokens: 1200, count: 2 } },
+ },
+ },
+ });
+
+ const breakdown = document.getElementById('phase-breakdown');
+ expect(breakdown.querySelectorAll('.ldr-phase-stat-item').length).toBeGreaterThan(0);
+ expect(breakdown.textContent).toContain('alert(2)');
+ expect(breakdown.textContent).toContain('1,200 tokens'); // formatted numerics intact
+ // research_details.query rewrite is textContent — inert.
+ expect(document.getElementById('research-query').children).toHaveLength(0);
+ expectNoLiveMarkup();
+ });
+
+ // Regression (fixed): details.js:544-545 — formatNumber() now coerces via
+ // Number()/isFinite (formatting.js), so a hostile string in phase_stats
+ // token/count fields renders as '0' instead of live markup in
+ // item.innerHTML.
+ it('renders hostile phase token/count strings as 0/blank, never as markup (numeric coercion)', async () => {
+ await mountDetails({
+ timeline: {
+ status: 'success',
+ metrics: {
+ phase_stats: {
+ planning: { tokens: TEXT_PAYLOADS[0], count: TEXT_PAYLOADS[0] },
+ malformed: {
+ tokens: { toString: null, valueOf: null },
+ count: 1,
+ },
+ },
+ },
+ },
+ });
+
+ const breakdown = document.getElementById('phase-breakdown');
+ expect(breakdown.querySelectorAll('.ldr-phase-stat-item').length).toBeGreaterThan(0);
+ expect(breakdown.textContent).toContain('malformed');
+ expectNoLiveMarkup();
+ });
+
+ it('escapes call-stack function/file/phase/model/status/stack fields; hostile timestamps degrade to an em-dash', async () => {
+ await mountDetails({
+ timeline: {
+ status: 'success',
+ metrics: {
+ timeline: [{
+ calling_function: TEXT_PAYLOADS[0],
+ call_stack: TEXT_PAYLOADS[1],
+ calling_file: `/app/${TEXT_PAYLOADS[2]}.py`,
+ research_phase: TEXT_PAYLOADS[2],
+ model_name: TEXT_PAYLOADS[0],
+ success_status: TEXT_PAYLOADS[1],
+ timestamp: '<img src=x onerror=alert(9)>',
+ prompt_tokens: 400,
+ completion_tokens: 120,
+ tokens: 520,
+ response_time_ms: 250,
+ }],
+ },
+ },
+ });
+
+ const traces = document.getElementById('call-stack-traces');
+ expect(traces.textContent).toContain(TEXT_PAYLOADS[0]);
+ expect(traces.textContent).toContain(TEXT_PAYLOADS[1]);
+ expect(traces.textContent).toContain(TEXT_PAYLOADS[2]);
+ expect(traces.textContent).toContain('\u2014'); // invalid date fallback
+ expect(traces.textContent).toContain('520 tokens'); // numeric fields intact
+ expect(document.getElementById('call-stack-card').style.display).toBe('block');
+ expectNoLiveMarkup();
+ });
+
+ // Regression (fixed): details.js:691 — the call-stack header now routes
+ // `${item.prompt_tokens || 0} in + ... ${item.response_time_ms || 0}ms`
+ // through formatNumber(); a truthy hostile STRING degrades to '0' instead
+ // of flowing into trace.innerHTML as markup.
+ it('renders hostile call-stack token counts as 0/blank, never as markup (numeric coercion)', async () => {
+ await mountDetails({
+ timeline: {
+ status: 'success',
+ metrics: {
+ timeline: [{
+ calling_function: 'generate_queries',
+ prompt_tokens: TEXT_PAYLOADS[0],
+ completion_tokens: TEXT_PAYLOADS[0],
+ tokens: TEXT_PAYLOADS[0],
+ response_time_ms: TEXT_PAYLOADS[0],
+ }],
+ },
+ },
+ });
+
+ const traces = document.getElementById('call-stack-traces');
+ expect(traces.textContent).toContain('generate_queries');
+ expectNoLiveMarkup();
+ });
+});
+
+describe('details.js search metrics (displaySearchMetrics / displaySearchEnginePerformance / displaySearchTimeline)', () => {
+ it('escapes hostile engine names and queries in breakdown and timeline; hostile timestamps degrade', async () => {
+ await mountDetails({
+ search: {
+ status: 'success',
+ metrics: {
+ total_searches: 3,
+ total_results: 27,
+ avg_response_time: 1200,
+ success_rate: 66.7,
+ search_calls: [
+ { engine: TEXT_PAYLOADS[0], query: TEXT_PAYLOADS[1], results_count: 9, response_time_ms: 800, timestamp: '2026-01-02T03:04:05Z', success_status: 'success' },
+ { engine: TEXT_PAYLOADS[2], query: TEXT_PAYLOADS[0], results_count: 4, response_time_ms: 300, timestamp: TEXT_PAYLOADS[1], success_status: 'failed' },
+ ],
+ engine_stats: [],
+ },
+ },
+ });
+
+ const breakdown = document.getElementById('search-engine-breakdown');
+ expect(breakdown.querySelectorAll('.ldr-search-engine-item').length).toBe(2);
+ expect(breakdown.textContent).toContain(TEXT_PAYLOADS[0]);
+ expect(breakdown.textContent).toContain(TEXT_PAYLOADS[1]);
+
+ const timeline = document.getElementById('search-timeline');
+ expect(timeline.textContent).toContain('Success');
+ expect(timeline.textContent).toContain('Failed');
+ expect(timeline.textContent).toContain(TEXT_PAYLOADS[2]);
+ expect(timeline.textContent).toContain('\u2014'); // hostile timestamp fallback
+
+ // Summary cards are textContent sinks — hostile-free numerics intact.
+ expect(document.getElementById('total-search-results').textContent).toBe('27');
+ expectNoLiveMarkup();
+ });
+
+ // Regression (fixed): details.js:612 — `<span>${formatNumber(engine.call_count ?? 0)} searches</span>`
+ // numeric-coerces hostile strings from the search-metrics API to '0'
+ // before they reach container.innerHTML (details.js:621).
+ it('renders hostile engine_stats call_count as 0/blank, never as markup (numeric coercion)', async () => {
+ await mountDetails({
+ search: {
+ status: 'success',
+ metrics: {
+ total_searches: 1,
+ total_results: 5,
+ avg_response_time: 500,
+ success_rate: 100,
+ search_calls: [],
+ engine_stats: [{
+ engine: 'wikipedia',
+ call_count: TEXT_PAYLOADS[0],
+ success_rate: 90,
+ avg_response_time: 1500,
+ total_results: 42,
+ }],
+ },
+ },
+ });
+
+ const performance = document.getElementById('search-engine-performance');
+ expect(performance.textContent).toContain('wikipedia'); // non-vacuous
+ expectNoLiveMarkup();
+ });
+
+ // Regression (fixed): details.js:583 and :645 — formatNumber() now
+ // numeric-coerces call.results_count, so hostile strings render as '0'
+ // in the engine-breakdown and search-timeline innerHTML sinks
+ // (formatting.js coercion).
+ it('renders hostile search results_count as 0/blank, never as markup (numeric coercion)', async () => {
+ await mountDetails({
+ search: {
+ status: 'success',
+ metrics: {
+ total_searches: 1,
+ total_results: 5,
+ avg_response_time: 500,
+ success_rate: 100,
+ search_calls: [{
+ engine: 'wikipedia',
+ query: 'quantum computing',
+ results_count: TEXT_PAYLOADS[0],
+ response_time_ms: 500,
+ timestamp: '2026-01-02T03:04:05Z',
+ success_status: 'success',
+ }],
+ engine_stats: [],
+ },
+ },
+ });
+
+ const breakdown = document.getElementById('search-engine-breakdown');
+ expect(breakdown.textContent).toContain('quantum computing'); // non-vacuous
+ expectNoLiveMarkup();
+ });
+});
+
+describe('details.js context overflow tables (displayContextOverflow)', () => {
+ it('escapes hostile phase/function names; truncation badge is numeric-coerced', async () => {
+ await mountDetails({
+ contextOverflow: {
+ status: 'success',
+ data: {
+ overview: {
+ total_tokens: 5000,
+ context_limit: 8000,
+ max_tokens_used: 4100,
+ truncation_occurred: true,
+ truncated_count: TEXT_PAYLOADS[0],
+ },
+ phase_stats: {
+ [`analysis ${TEXT_PAYLOADS[1]}`]: {
+ count: 2,
+ prompt_tokens: 1234,
+ completion_tokens: 567,
+ total_tokens: 1801,
+ truncated_count: 0,
+ },
+ },
+ requests: [{
+ timestamp: '2026-01-02T03:04:05Z',
+ phase: TEXT_PAYLOADS[2],
+ calling_function: TEXT_PAYLOADS[0],
+ prompt_tokens: 100,
+ completion_tokens: 50,
+ total_tokens: 150,
+ context_limit: 8000,
+ context_truncated: false,
+ response_time_ms: 42,
+ }],
+ },
+ },
+ });
+
+ // renderTruncationBadge Number()-coerces the hostile count (safe).
+ expect(document.getElementById('co-truncation-status').textContent)
+ .toBe('No truncation');
+
+ const phases = document.getElementById('co-phase-breakdown');
+ expect(phases.querySelector('table')).not.toBeNull();
+ expect(phases.textContent).toContain('alert(2)');
+ expect(phases.textContent).toContain('1,234'); // formatted numerics intact
+
+ const requests = document.getElementById('co-requests-table');
+ expect(requests.querySelectorAll('tr').length).toBe(1);
+ expect(requests.textContent).toContain('<svg onload=alert(3)>');
+ expect(requests.textContent).toContain('<img src=x onerror=alert(1)>');
+ expectNoLiveMarkup();
+ });
+
+ // Regression (fixed): details.js:1161-1163 — the context-overflow phase
+ // table interpolates formatNumber(stats.prompt_tokens / completion_tokens /
+ // total_tokens), which now numeric-coerces; hostile strings render as '0'
+ // in container.innerHTML (:1173).
+ it('renders hostile co-phase token counts as 0/blank, never as markup (numeric coercion)', async () => {
+ await mountDetails({
+ contextOverflow: {
+ status: 'success',
+ data: {
+ overview: { total_tokens: 100, truncation_occurred: false },
+ phase_stats: {
+ analysis: {
+ count: 1,
+ prompt_tokens: TEXT_PAYLOADS[0],
+ completion_tokens: TEXT_PAYLOADS[0],
+ total_tokens: TEXT_PAYLOADS[0],
+ truncated_count: 0,
+ },
+ },
+ requests: [],
+ },
+ },
+ });
+
+ const phases = document.getElementById('co-phase-breakdown');
+ expect(phases.textContent).toContain('analysis'); // non-vacuous
+ expectNoLiveMarkup();
+ });
+
+ // Regression (fixed): details.js:1198-1201 — the requests table interpolates
+ // formatNumber(req.prompt_tokens / completion_tokens / total_tokens /
+ // context_limit); hostile strings now coerce to '0' before tbody.innerHTML.
+ it('renders hostile requests-table token counts as 0/blank, never as markup (numeric coercion)', async () => {
+ await mountDetails({
+ contextOverflow: {
+ status: 'success',
+ data: {
+ overview: { total_tokens: 100, truncation_occurred: false },
+ phase_stats: {},
+ requests: [{
+ timestamp: '2026-01-02T03:04:05Z',
+ phase: 'analysis',
+ calling_function: 'call_llm',
+ prompt_tokens: TEXT_PAYLOADS[0],
+ completion_tokens: TEXT_PAYLOADS[0],
+ total_tokens: TEXT_PAYLOADS[0],
+ context_limit: TEXT_PAYLOADS[0],
+ context_truncated: false,
+ response_time_ms: 42,
+ }],
+ },
+ },
+ });
+
+ const requests = document.getElementById('co-requests-table');
+ expect(requests.textContent).toContain('call_llm'); // non-vacuous
+ expectNoLiveMarkup();
+ });
+});
+
+describe('details.js benign control rendering', () => {
+ it('renders a full benign research session intact end-to-end', async () => {
+ await mountDetails({
+ details: {
+ query: 'What is quantum computing?',
+ mode: 'quick',
+ created_at: '2026-01-02T03:04:05Z',
+ strategy: 'focused-scan',
+ progress: 75,
+ },
+ metrics: {
+ status: 'success',
+ metrics: {
+ total_tokens: 123456,
+ total_calls: 12,
+ model_usage: [{ model: 'qwen3.6', prompt_tokens: 1000, completion_tokens: 500, calls: 4 }],
+ },
+ },
+ timeline: {
+ status: 'success',
+ metrics: {
+ summary: { avg_response_time: 1500 },
+ phase_stats: {
+ planning: { tokens: 1200, count: 2 },
+ analysis: { tokens: 3400, count: 5 },
+ },
+ timeline: [{
+ calling_function: 'generate_search_queries',
+ call_stack: 'main \u2192 plan',
+ calling_file: '/app/processor.py',
+ research_phase: 'planning',
+ model_name: 'qwen3.6',
+ success_status: 'success',
+ timestamp: '2026-01-02T03:04:05Z',
+ prompt_tokens: 400,
+ completion_tokens: 120,
+ tokens: 520,
+ response_time_ms: 900,
+ }],
+ },
+ },
+ search: {
+ status: 'success',
+ metrics: {
+ total_searches: 4,
+ total_results: 36,
+ avg_response_time: 900,
+ success_rate: 75,
+ search_calls: [{
+ engine: 'wikipedia',
+ query: 'quantum computing',
+ results_count: 12,
+ response_time_ms: 700,
+ timestamp: '2026-01-02T03:04:05Z',
+ success_status: 'success',
+ }],
+ engine_stats: [{
+ engine: 'wikipedia',
+ call_count: 4,
+ success_rate: 75,
+ avg_response_time: 700,
+ total_results: 36,
+ }],
+ },
+ },
+ links: {
+ status: 'success',
+ data: {
+ total_links: 12,
+ unique_domains: 4,
+ domain_categories: { academic: 7, news: 5 },
+ domains: [{ domain: 'arxiv.org', count: 1234, percentage: 42.5 }],
+ resources: [{
+ title: 'Quantum paper',
+ url: 'https://example.com/paper',
+ preview: 'A study of quantum systems.',
+ }],
+ },
+ },
+ });
+
+ // Header metadata (textContent sinks).
+ expect(document.getElementById('research-query').textContent)
+ .toBe('What is quantum computing?');
+ expect(document.getElementById('research-strategy').textContent).toBe('focused-scan');
+ expect(document.getElementById('detail-progress-percentage').textContent).toBe('75%');
+ expect(document.getElementById('research-date').textContent).not.toBe('');
+
+ // Summary cards.
+ expect(document.getElementById('total-tokens').textContent).toBe('123,456');
+ expect(document.getElementById('model-used').textContent).toBe('qwen3.6');
+ expect(document.getElementById('avg-response-time').textContent).toBe('1.5s');
+
+ // Phase breakdown + call stack.
+ expect(document.getElementById('phase-breakdown').textContent).toContain('1,200 tokens');
+ const traces = document.getElementById('call-stack-traces');
+ expect(traces.textContent).toContain('processor.py');
+ expect(traces.textContent).toContain('520 tokens');
+
+ // Search surfaces.
+ expect(document.getElementById('search-engine-breakdown').textContent)
+ .toContain('quantum computing');
+ expect(document.getElementById('search-timeline').textContent).toContain('Success');
+ expect(document.getElementById('search-engine-performance').textContent)
+ .toContain('4 searches');
+
+ // Link analytics.
+ const domainList = document.getElementById('domain-list');
+ expect(domainList.textContent).toContain('arxiv.org');
+ expect(domainList.textContent).toContain('1234 links');
+ expect(domainList.textContent).toContain('42.5%');
+ const anchor = document.querySelector('#resource-sample a');
+ expect(anchor.getAttribute('href')).toBe('https://example.com/paper');
+ expect(document.getElementById('total-links').textContent).toBe('12');
+ expect(document.querySelector('#academic-sources').previousElementSibling.textContent)
+ .toBe('academic');
+ expect(document.getElementById('classify-domains-btn')).not.toBeNull();
+
+ expectNoLiveMarkup();
+ });
+});
diff --git a/tests/theme_tests/test_accent_ink_contrast.py b/tests/theme_tests/test_accent_ink_contrast.py
index 1c9f252038..aaa4cfc5d7 100644
--- a/tests/theme_tests/test_accent_ink_contrast.py
+++ b/tests/theme_tests/test_accent_ink_contrast.py
@@ -965,8 +965,8 @@ DYNAMIC_INLINE_STYLES = {
(NUMBER,),
None,
),
- # details.js L700: an inline ternary of two literals.
- ("static/js/components/details.js", 700, "color"): (
+ # details.js L712: an inline ternary of two literals.
+ ("static/js/components/details.js", 712, "color"): (
"item.success_status === 'success' ? 'var(--success-color)' "
": 'var(--error-color)'",
("var(--success-color)", "var(--error-color)"),
@@ -1280,7 +1280,7 @@ def test_inline_style_scan_finds_the_known_expressions():
("templates/pages/journal_quality.html", 723, "background"),
("templates/pages/journal_quality.html", 831, "background"),
("static/js/components/context-overflow.js", 574, "background"),
- ("static/js/components/details.js", 700, "color"),
+ ("static/js/components/details.js", 712, "color"),
("templates/pages/metrics.html", 975, "color"),
} <= set(found), sorted(found)
expanded = {