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(pdf): migrate WeasyPrint fetcher off deprecated default_url_fetcher (#3622)

LearningCircuitc1c7148eb665
diff --git a/src/local_deep_research/web/services/pdf_service.py b/src/local_deep_research/web/services/pdf_service.py
index 3ae2a54fcf..961c5113a0 100644
--- a/src/local_deep_research/web/services/pdf_service.py
+++ b/src/local_deep_research/web/services/pdf_service.py
@@ -17,13 +17,14 @@ import markdown # type: ignore[import-untyped]
from loguru import logger
try:
- from weasyprint import HTML, CSS, default_url_fetcher
+ from weasyprint import HTML, CSS
+ from weasyprint.urls import URLFetcher
WEASYPRINT_AVAILABLE = True
except (OSError, ImportError) as _weasyprint_err:
HTML = None # type: ignore[assignment,misc]
CSS = None # type: ignore[assignment,misc]
- default_url_fetcher = None # type: ignore[assignment]
+ URLFetcher = None # type: ignore[assignment,misc]
WEASYPRINT_AVAILABLE = False
logger.warning("WeasyPrint not available — PDF export will be disabled")
@@ -39,26 +40,24 @@ class UnsafePDFResourceURLError(ValueError):
"""Subclasses ValueError so WeasyPrint skips the resource instead of aborting the render."""
-def _safe_url_fetcher(
- url,
- timeout=10,
- ssl_context=None,
- http_headers=None,
- allowed_protocols=None,
-):
+# Module-level URLFetcher preserves the allow_redirects=False posture that
+# default_url_fetcher hard-coded. Redirects disabled keeps the SSRF guard
+# airtight — validate_url only inspects the initial URL, so a 30x to the
+# AWS metadata endpoint (see ssrf_validator.AWS_METADATA_IP) would
+# otherwise slip past.
+_URL_FETCHER = (
+ URLFetcher(allow_redirects=False) if WEASYPRINT_AVAILABLE else None
+)
+
+
+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}")
raise UnsafePDFResourceURLError(
f"Blocked unsafe URL in PDF rendering: {url}"
)
- return default_url_fetcher(
- url,
- timeout=timeout,
- ssl_context=ssl_context,
- http_headers=http_headers,
- allowed_protocols=allowed_protocols,
- )
+ return _URL_FETCHER.fetch(url)
class MissingPDFDependencyError(RuntimeError):
diff --git a/tests/web/services/test_pdf_service.py b/tests/web/services/test_pdf_service.py
index 2d7e607a10..013c7310c3 100644
--- a/tests/web/services/test_pdf_service.py
+++ b/tests/web/services/test_pdf_service.py
@@ -10,7 +10,7 @@ Tests cover:
"""
import pytest
-from unittest.mock import patch
+from unittest.mock import Mock, patch
class TestPDFServiceInit:
@@ -405,8 +405,8 @@ class TestSafeUrlFetcher:
assert issubclass(UnsafePDFResourceURLError, ValueError)
- def test_validated_url_delegates_to_default_fetcher(self):
- """Validated URLs are forwarded to weasyprint's default_url_fetcher."""
+ def test_validated_url_delegates_to_url_fetcher(self):
+ """Validated URLs are forwarded to the module's URLFetcher instance."""
from local_deep_research.web.services import pdf_service as pdf_module
from local_deep_research.web.services.pdf_service import (
_safe_url_fetcher,
@@ -414,20 +414,35 @@ class TestSafeUrlFetcher:
with (
patch.object(pdf_module, "validate_url", return_value=True),
- patch.object(pdf_module, "default_url_fetcher") as mock_fetcher,
+ patch.object(pdf_module._URL_FETCHER, "fetch") as mock_fetch,
):
- mock_fetcher.return_value = {
- "string": b"fake",
- "mime_type": "image/png",
- }
+ mock_fetch.return_value = Mock(
+ status=200, url="https://example.com/image.png"
+ )
_safe_url_fetcher("https://example.com/image.png")
- mock_fetcher.assert_called_once()
- assert (
- mock_fetcher.call_args.args[0]
- == "https://example.com/image.png"
- )
+ mock_fetch.assert_called_once_with("https://example.com/image.png")
+
+ def test_url_fetcher_disables_redirects(self):
+ """The module-level URLFetcher must keep allow_redirects=False.
+
+ validate_url only inspects the initial URL string, so a 30x
+ response redirecting to the AWS metadata endpoint would bypass
+ the SSRF guard if URLFetcher were to follow redirects.
+ default_url_fetcher
+ hard-coded this; after migrating away from it, we assert the
+ posture explicitly.
+ """
+ from local_deep_research.web.services import pdf_service as pdf_module
+ from urllib.request import HTTPRedirectHandler
+
+ redirect_handlers = [
+ h
+ for h in pdf_module._URL_FETCHER.handlers
+ if isinstance(h, HTTPRedirectHandler)
+ ]
+ assert redirect_handlers == []
def test_pdf_html_passes_fetcher_to_weasyprint(self):
"""markdown_to_pdf must wire _safe_url_fetcher into HTML(...).