~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
/**
* PDF export must render report content under the SAME hardened policy as
* the on-screen report view (services/ui.js renderMarkdown): raw
* author-supplied HTML is escaped to literal text, and the output is
* sanitized to a reparse fixpoint under the restrictive allowlist. These
* tests run the REAL marked + DOMPurify + XSSProtection + ui.renderMarkdown
* pipeline; only jsPDF / html2canvas are boundary fakes, and the text handed
* to jsPDF is what the PDF would actually contain.
*/
import createDOMPurify from 'dompurify';
import { installDomPurifyHappyDomShims } from '../happy-dom-dompurify-shim.js';
import { installMathMlNamespaceApproximation } from '../happy-dom-mathml-namespace-approximation.js';
import '@js/security/url-validator.js';
import '@js/security/xss-protection.js';
import '@js/services/ui.js';
import '@js/services/pdf.js';
const { generatePdf } = window.pdfService;
let pdf;
class FakePdf {
constructor() {
this.internal = {
pageSize: { getWidth: () => 612, getHeight: () => 792 },
};
for (const name of [
'setFontSize', 'setTextColor', 'text', 'setFont', 'addPage',
'setFillColor', 'rect', 'textWithLink', 'setDrawColor',
'setLineWidth', 'line', 'addImage',
]) {
this[name] = vi.fn();
}
this.splitTextToSize = vi.fn((text) => String(text).split('\n'));
this.getTextWidth = vi.fn((text) => String(text).length * 5);
this.output = vi.fn(() => new Blob(['pdf']));
pdf = this;
}
}
beforeAll(async () => {
installDomPurifyHappyDomShims();
installMathMlNamespaceApproximation();
const { Marked } = await import('marked');
const markedKatex = (await import('marked-katex-extension')).default;
const instance = new Marked();
instance.use(markedKatex({ throwOnError: false, errorColor: 'currentColor' }));
globalThis.marked = instance;
// Capture the opted-in namespace accessor in this instance. The default
// instance was created by the static import before the shim was installed.
globalThis.DOMPurify = createDOMPurify(window);
});
afterAll(() => {
delete globalThis.marked;
delete globalThis.DOMPurify;
});
beforeEach(() => {
document.body.replaceChildren();
pdf = undefined;
globalThis.jsPDF = FakePdf;
globalThis.html2canvas = vi.fn().mockResolvedValue({
width: 10,
height: 10,
toDataURL: () => 'data:image/png;base64,x',
});
globalThis.URLValidator = {
...(globalThis.URLValidator || {}),
safeAssign: vi.fn((el, prop, value) => { el[prop] = value; }),
};
window.pwned = undefined;
});
afterEach(() => {
vi.restoreAllMocks();
delete globalThis.jsPDF;
delete globalThis.html2canvas;
});
function pdfText() {
return [...pdf.text.mock.calls, ...pdf.textWithLink.mock.calls]
.map((call) => String(call[0]))
.join('\n');
}
describe('generatePdf() uses the hardened rendering pipeline', () => {
test('shows raw author HTML as literal text, matching the screen view', async () => {
const markdown = '# Report\n\n<img src=x onerror="window.pwned=true">\n\n'
+ '<script>window.pwned = true</script>\n\nPlain paragraph';
// What the screen path displays for the same content.
const screen = document.createElement('div');
// eslint-disable-next-line no-unsanitized/property -- test-only: parsing renderMarkdown()'s sanitized output to compare with the PDF body.
screen.innerHTML = window.ui.renderMarkdown(markdown);
expect(screen.querySelector('img, script')).toBeNull();
const screenText = screen.textContent;
expect(screenText).toContain('<img src=x onerror=');
expect(screenText).toContain('<script>');
await generatePdf('Title', markdown);
const text = pdfText();
expect(text).toContain('Plain paragraph');
// Raw HTML is interpreted as markup on neither surface: it reaches
// the PDF as visible text rather than being dropped or executed.
expect(text).toContain('<img src=x onerror=');
expect(text).toContain('<script>');
expect(window.pwned).toBeUndefined();
expect(document.querySelector('.ldr-pdf-content')).toBeNull();
});
test('never lets default-profile SVG/MathML foreign content through', async () => {
const markdown = '<svg><animate onbegin="window.pwned=true" attributeName="x" dur="1s"></svg>\n\n'
+ '<math><mtext><table><mglyph><style><img src=x onerror="window.pwned=true"></style></mglyph></table></mtext></math>\n\n'
+ 'After';
await generatePdf('Title', markdown);
expect(window.pwned).toBeUndefined();
expect(pdfText()).toContain('After');
expect(document.querySelector('svg, math')).toBeNull();
});
test('does not call DOMPurify directly with the permissive default profile', async () => {
const sanitize = vi.spyOn(globalThis.DOMPurify, 'sanitize');
await generatePdf('Title', 'Some *text*');
// Every call must carry an explicit config (the restrictive
// allowlist); a bare sanitize(html) or an ADD_TAGS-only call is the
// default profile this change removes.
expect(sanitize).toHaveBeenCalled();
for (const call of sanitize.mock.calls) {
expect(call[1]).toBeDefined();
expect(call[1].ALLOWED_TAGS).toBeDefined();
expect(call[1].ADD_TAGS).toBeUndefined();
}
});
test('keeps KaTeX math readable as LaTeX in the PDF', async () => {
await generatePdf('Title', 'Inline $x^2$ here\n\n$$\ny = mx + b\n$$\n');
const text = pdfText();
expect(text).toContain('$x^2$');
expect(text).toContain('$$y = mx + b$$');
});
test('keeps ordinary markdown structure and safe links', async () => {
await generatePdf(
'Title',
'# Heading\n\n- item one\n- item two\n\n[the source](https://example.test/source)\n\n'
+ '[bad](javascript:window.pwned=true)',
);
const text = pdfText();
expect(text).toContain('Heading');
expect(text).toContain('item one');
expect(pdf.textWithLink).toHaveBeenCalledWith(
expect.stringContaining('the source'),
expect.any(Number),
expect.any(Number),
{ url: 'https://example.test/source' },
);
const urls = pdf.textWithLink.mock.calls.map((call) => call[3]?.url);
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));
});
});