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): remove unsanitized HTML-insertion sinks in pdf service (#7736)

LearningCircuitf09a9c9109cc
diff --git a/changelog.d/+xss-pdf-service-sinks.security.md b/changelog.d/+xss-pdf-service-sinks.security.md
new file mode 100644
index 0000000000..b7cfe6997e
--- /dev/null
+++ b/changelog.d/+xss-pdf-service-sinks.security.md
@@ -0,0 +1 @@
+PDF export no longer uses the flagged dynamic-HTML-insertion sinks in `static/js/services/pdf.js` ([#9752](https://github.com/LearningCircuit/local-deep-research/security/code-scanning/9752) and [#9754](https://github.com/LearningCircuit/local-deep-research/security/code-scanning/9754)): `wrapTopLevelText()` now wraps top-level text nodes with `insertBefore`/`remove` and the PDF body is populated with `removeChild`/`appendChild` node moves, so the only remaining sink is the intentional parse of `ui.renderMarkdown()`'s fixed-point-sanitized output into an inert `<template>`, which now carries a documented `bearer:disable` rationale (alert [#9753](https://github.com/LearningCircuit/local-deep-research/security/code-scanning/9753)).
diff --git a/src/local_deep_research/web/static/js/services/pdf.js b/src/local_deep_research/web/static/js/services/pdf.js
index b7991e3df5..b76a3395f1 100644
--- a/src/local_deep_research/web/static/js/services/pdf.js
+++ b/src/local_deep_research/web/static/js/services/pdf.js
@@ -64,7 +64,8 @@ function wrapTopLevelText(container) {
}
const p = document.createElement('p');
p.textContent = node.textContent.trim();
- node.replaceWith(p);
+ container.insertBefore(p, node);
+ node.remove();
}
}
@@ -241,14 +242,26 @@ async function generatePdf(title, content, _metadata = {}) {
// Parse the already-sanitized string in an inert <template>
// (no script execution, no resource loading) and move its nodes
// into the live container, so no HTML string is ever assigned
- // to a live-document element's innerHTML.
+ // to a live-document element's innerHTML. renderedHtml is the
+ // output of ui.renderMarkdown(): escaped at renderer.html, then
+ // sanitizeStable() under the restrictive allowlist to a reparse
+ // fixpoint. Turning that string into a DOM must parse it
+ // somewhere; the inert template is that parse, so the sink is
+ // intentional and suppressed below.
const template = document.createElement('template');
+ // bearer:disable javascript_lang_dangerous_insert_html
// eslint-disable-next-line no-unsanitized/property -- renderedHtml is the output of ui.renderMarkdown(): escaped at renderer.html, then sanitizeStable() under the restrictive allowlist; parsed here only into an inert template.
template.innerHTML = renderedHtml;
// The PDF walker iterates contentDiv.children, so unwrap the
- // .ldr-markdown-content wrapper renderMarkdown() adds.
+ // .ldr-markdown-content wrapper renderMarkdown() adds. Move the
+ // already-parsed nodes one by one (no HTML strings re-parsed).
const wrapper = template.content.firstElementChild;
- pdfBodyDiv.replaceChildren(...wrapper.childNodes);
+ while (pdfBodyDiv.firstChild) {
+ pdfBodyDiv.removeChild(pdfBodyDiv.firstChild);
+ }
+ while (wrapper.firstChild) {
+ pdfBodyDiv.appendChild(wrapper.firstChild);
+ }
wrapTopLevelText(pdfBodyDiv);
replaceKatexWithLatex(pdfBodyDiv);
} else {
diff --git a/tests/js/services/pdf-sanitization.test.js b/tests/js/services/pdf-sanitization.test.js
index 6b244399ef..15e1e2668a 100644
--- a/tests/js/services/pdf-sanitization.test.js
+++ b/tests/js/services/pdf-sanitization.test.js
@@ -169,4 +169,24 @@ describe('generatePdf() uses the hardened rendering pipeline', () => {
expect(urls.every((url) => !String(url).startsWith('javascript:'))).toBe(true);
expect(window.pwned).toBeUndefined();
});
+
+ test('moves every body node into the PDF in document order', async () => {
+ // Escaped author HTML becomes a bare top-level text node, which
+ // wrapTopLevelText() must wrap and the body-population move must
+ // keep in place between the surrounding blocks.
+ const markdown = '# Heading\n\n<aside>escaped aside</aside>\n\n'
+ + 'First paragraph\n\nSecond paragraph';
+
+ await generatePdf('Title', markdown);
+
+ const text = pdfText();
+ const positions = [
+ 'Heading',
+ '<aside>escaped aside</aside>',
+ 'First paragraph',
+ 'Second paragraph',
+ ].map((needle) => text.indexOf(needle));
+ expect(positions.every((position) => position >= 0)).toBe(true);
+ expect(positions).toEqual([...positions].sort((a, b) => a - b));
+ });
});