~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
test(security): pin the PDF render path's parser posture (#6516)
LearningCircuite60844fab64f
diff --git a/tests/security/test_pdf_render_parser_posture.py b/tests/security/test_pdf_render_parser_posture.pynew file mode 100644index 0000000000..da6190d499--- /dev/null+++ b/tests/security/test_pdf_render_parser_posture.py@@ -0,0 +1,142 @@+"""Parser-posture contracts for the PDF render path.++Two defenses of the report-rendering pipeline currently hold only by+architecture, and nothing in the test tree pins them:++1. **Pillow's decompression-bomb guard stays armed.** WeasyPrint decodes+ images referenced from report HTML; the only thing standing between a+ hostile ``<img>`` and a multi-gigapixel decode is Pillow's default+ ``MAX_IMAGE_PIXELS``. The classic operational "fix" for a user's+ oversized-image complaint — ``Image.MAX_IMAGE_PIXELS = None`` — would+ silently reopen decode bombs; no test would notice.+2. **Inline XML DTDs in report bodies are inert.** Python-Markdown+ passes raw HTML blocks through, so a report body can carry an SVG+ with a DOCTYPE and entity declarations. The HTML5 parsing pipeline+ never processes XML DTDs, so entities neither expand (billion+ laughs) nor resolve (local-file read). A pipeline swap to an+ XML-based renderer would resurrect both classes silently.++These contracts pin today's posture so either regression goes red.+"""++from __future__ import annotations++import io+import re+import struct+import zlib+from pathlib import Path++import pytest++pytest.importorskip("weasyprint")++from local_deep_research.web.services import pdf_service # noqa: E402+++def _extract_pdf_text(pdf_bytes: bytes) -> str:+ from pypdf import PdfReader++ reader = PdfReader(io.BytesIO(pdf_bytes))+ return "\n".join(page.extract_text() or "" for page in reader.pages)+++def _bomb_png(dim: int = 40_000) -> bytes:+ """A PNG declaring ``dim x dim`` 1-bit pixels with trivial data."""++ def chunk(tag: bytes, data: bytes) -> bytes:+ return (+ struct.pack(">I", len(data))+ + tag+ + data+ + struct.pack(">I", zlib.crc32(tag + data) & 0xFFFFFFFF)+ )++ ihdr = struct.pack(">IIBBBBB", dim, dim, 1, 0, 0, 0, 0)+ raw = b"\x00" + b"\x00" * 16 # partial first row; decoders size from IHDR+ return (+ b"\x89PNG\r\n\x1a\n"+ + chunk(b"IHDR", ihdr)+ + chunk(b"IDAT", zlib.compress(raw))+ + chunk(b"IEND", b"")+ )+++class TestPillowBombGuardStaysArmed:+ """Nothing in the codebase may defuse Pillow's decode-bomb guard."""++ def test_no_assignment_disables_max_image_pixels(self):+ src_root = (+ Path(pdf_service.__file__).resolve().parents[3]+ / "local_deep_research"+ )+ offenders: list[str] = []+ # (?!=) so equality READS (== comparisons) are not flagged.+ pattern = re.compile(r"MAX_IMAGE_PIXELS\s*=(?!=)")+ for module_path in sorted(src_root.rglob("*.py")):+ source = module_path.read_text(encoding="utf-8")+ if pattern.search(source):+ offenders.append(str(module_path.relative_to(src_root)))+ assert offenders == [], (+ f"code assigns MAX_IMAGE_PIXELS (the decode-bomb guard): {offenders}"+ )++ def test_the_guard_fires_in_the_render_process(self):+ """Importing the pdf pipeline leaves the guard armed, and a+ bomb-shaped image is refused — not decoded."""+ from PIL import Image++ with pytest.raises(Image.DecompressionBombError):+ Image.open(io.BytesIO(_bomb_png())).load()+++class TestInlineDTDsAreInert:+ """Entity declarations in report bodies must not expand or resolve."""++ HOSTILE_BODY = """<?xml version="1.0"?>+<!DOCTYPE r [+ <!ENTITY leaf "Z0">+ <!ENTITY branch "&leaf;&leaf;&leaf;&leaf;&leaf;&leaf;&leaf;&leaf;">+ <!ENTITY tree "&branch;&branch;&branch;&branch;&branch;&branch;&branch;&branch;">+ <!ENTITY xxe SYSTEM "file://{sentinel_path}">+]>+<svg xmlns="http://www.w3.org/2000/svg"><text>&tree;&xxe;</text></svg>++Normal report body text with the ARRIVAL-CONTROL marker.+"""++ def test_dtd_entities_neither_expand_nor_read_files(self, tmp_path):+ sentinel = tmp_path / "sentinel.txt"+ sentinel.write_text("SENTINEL-FILE-CONTENT-9QW", encoding="utf-8")++ markdown = self.HOSTILE_BODY.format(sentinel_path=sentinel)+ pdf_bytes = pdf_service.get_pdf_service().markdown_to_pdf(+ markdown, title="Posture probe"+ )++ assert pdf_bytes, "render must complete on a DTD-bearing body"++ text = _extract_pdf_text(pdf_bytes)+ # Arrival control: the benign marker made it into the render,+ # so the absence assertions below are not vacuous.+ assert "ARRIVAL-CONTROL" in text, text[:400]+ # No billion-laughs expansion (declaration text contains+ # "&leaf;", never the literal "Z0Z0Z0").+ assert "Z0Z0Z0" not in text, "internal entity expanded in render"+ # No external-entity file read.+ assert "SENTINEL-FILE-CONTENT-9QW" not in text, (+ "external entity resolved during render"+ )++ def test_the_hostile_block_actually_reaches_the_renderer(self, tmp_path):+ """Guard against the payload being dropped before render: the+ markdown-to-HTML conversion passes the SVG block through."""+ sentinel = tmp_path / "sentinel.txt"+ sentinel.write_text("x", encoding="utf-8")++ html = pdf_service.get_pdf_service()._markdown_to_html(+ self.HOSTILE_BODY.format(sentinel_path=sentinel),+ title="Pass-through probe",+ )++ assert "<svg" in html, "SVG block no longer passes through markdown"