~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
fix(security): redact blocked URLs in the PDF fetcher's log and exception (#6521)
LearningCircuite7364e57de97
diff --git a/changelog.d/6521.security.md b/changelog.d/6521.security.mdnew file mode 100644index 0000000000..37195151ad--- /dev/null+++ b/changelog.d/6521.security.md@@ -0,0 +1 @@+The PDF renderer's SSRF guard now logs and raises blocked URLs in redacted form (scheme://host:port only): a rejected URL is adversarial-shaped and may still carry credentials in its userinfo or secret query tokens, which previously landed in server logs and exception chains.diff --git a/src/local_deep_research/web/services/pdf_service.py b/src/local_deep_research/web/services/pdf_service.pyindex e2de5762a6..2e381c9335 100644--- a/src/local_deep_research/web/services/pdf_service.py+++ b/src/local_deep_research/web/services/pdf_service.py@@ -16,7 +16,7 @@ from typing import Optional, Dict, Anyimport markdown # type: ignore[import-untyped]from loguru import logger-from ...security import validate_url+from ...security import redact_url_for_log, validate_url# WeasyPrint pulls in Pango/Cairo/fontTools — a heavy, multi-second import.@@ -110,9 +110,14 @@ _EMPTY_RESOURCE_MIME_TYPE = "application/octet-stream"def _safe_url_fetcher(url):"""WeasyPrint url_fetcher that blocks SSRF targets (GHSA-fj2m-qvh9-jq4q)."""if not validate_url(url):- logger.warning(f"Blocked unsafe URL in PDF rendering: {url}")+ # A rejected URL is adversarial-shaped and may carry the+ # operator's real credentials (RFC 3986 userinfo) or secret+ # query tokens; log and raise only scheme://host:port, the+ # same discipline every ssrf_validator site follows.+ redacted = redact_url_for_log(url)+ logger.warning(f"Blocked unsafe URL in PDF rendering: {redacted}")raise UnsafePDFResourceURLError(- f"Blocked unsafe URL in PDF rendering: {url}"+ f"Blocked unsafe URL in PDF rendering: {redacted}")_ensure_weasyprint()return _URL_FETCHER.fetch(url)@@ -130,7 +135,12 @@ def _skipping_url_fetcher(url):try:return _safe_url_fetcher(url)except Exception:- logger.warning(f"Skipping unavailable resource in PDF rendering: {url}")+ # Same redaction discipline as the fetcher's own block path:+ # a refused/unavailable URL is adversarial-shaped and may carry+ # credentials in userinfo or secret query tokens.+ logger.warning(+ f"Skipping unavailable resource in PDF rendering: {redact_url_for_log(url)}"+ )if URLFetcherResponse is not None:return URLFetcherResponse(url,diff --git a/tests/security/test_pdf_fetcher_blocked_url_redaction.py b/tests/security/test_pdf_fetcher_blocked_url_redaction.pynew file mode 100644index 0000000000..c9ffe64b87--- /dev/null+++ b/tests/security/test_pdf_fetcher_blocked_url_redaction.py@@ -0,0 +1,90 @@+"""Redaction contracts for blocked-URL logging in the PDF fetcher.++``_safe_url_fetcher`` is the SSRF seam of the PDF render path. When it+blocks a URL it logs the value at warning level **and** embeds it in+``UnsafePDFResourceURLError``, which propagates through WeasyPrint's+wrapped tracebacks. A rejected URL is by definition adversarial-shaped+— and may carry the operator's real credentials (RFC 3986 userinfo) or+secret query tokens if a misconfiguration produced it.++Every ``ssrf_validator`` call site redacts with ``redact_url_for_log``+(``scheme://host:port`` only); the PDF fetcher must not be the one+seam that leaks the full URL into logs and exception chains.+"""++from __future__ import annotations++import re+from pathlib import Path++import pytest+from loguru import logger++from local_deep_research.web.services import pdf_service+++#: A blocked-shaped URL whose userinfo and query carry unmistakable+#: secrets; loopback is blocked by the SSRF validator regardless.+BLOCKED_URL = (+ "https://operator:[email protected]:8443/x?api_token=T0PSECRET"+)+++def captured_logs():+ """Capture the fetcher's warnings.++ The package silences its own loguru namespace in library mode+ (``logger.disable("local_deep_research")`` in ``__init__``), so the+ namespace must be re-enabled to observe what operators would see+ when logging is turned on.+ """+ messages: list[str] = []+ handler_id = logger.add(lambda m: messages.append(str(m)), level="DEBUG")+ logger.enable("local_deep_research")+ try:+ yield messages+ finally:+ logger.disable("local_deep_research")+ logger.remove(handler_id)+++class TestBlockedUrlRedaction:+ def test_blocked_url_secrets_never_reach_the_log(self, captured_logs):+ with pytest.raises(pdf_service.UnsafePDFResourceURLError):+ pdf_service._safe_url_fetcher(BLOCKED_URL)++ joined = "\n".join(captured_logs)+ assert "hunter2-secret" not in joined, (+ "userinfo credentials leaked into logs"+ )+ assert "T0PSECRET" not in joined, "query token leaked into logs"+ # Arrival control: the blocked fetch was actually logged (the+ # assertions above are not vacuous) — in redacted form.+ assert "https://127.0.0.1:8443" in joined, joined[-400:]++ def test_blocked_url_secrets_never_reach_the_exception(self):+ with pytest.raises(pdf_service.UnsafePDFResourceURLError) as excinfo:+ pdf_service._safe_url_fetcher(BLOCKED_URL)++ message = str(excinfo.value)+ assert "hunter2-secret" not in message, (+ "userinfo credentials leaked into the exception chain"+ )+ assert "T0PSECRET" not in message+ assert "https://127.0.0.1:8443" in message, message++ def test_the_fetcher_still_blocks(self):+ """Redaction must not weaken the block itself."""+ with pytest.raises(pdf_service.UnsafePDFResourceURLError):+ pdf_service._safe_url_fetcher(BLOCKED_URL)++ def test_no_raw_url_interpolation_remains_in_the_module(self):+ """Structural: no f-string in pdf_service interpolates *url*+ directly into a log line or exception message."""+ source = Path(pdf_service.__file__).read_text(encoding="utf-8")+ pattern = re.compile(+ r'(?:logger\.\w+|UnsafePDFResourceURLError)\s*\(\s*f"[^"]*\{url\}'+ )+ hits = pattern.findall(source)+ assert hits == [], f"raw URL interpolated into log/exception: {hits}"