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): skip a refused resource for every at-rule, not just @import (#6503)

Lukasa1cca11d04ce
diff --git a/changelog.d/6466.bugfix.md b/changelog.d/6466.bugfix.md
new file mode 100644
index 0000000000..cb6aba4e4a
--- /dev/null
+++ b/changelog.d/6466.bugfix.md
@@ -0,0 +1 @@
+A report whose content carries a `@color-profile` at-rule pointing at a resource the SSRF guard refuses no longer fails PDF export with a generic 500. WeasyPrint skips a refused `@import` and completes the render, but lets the error from a refused `@color-profile` escape, so the export route answered 500 for a report that is otherwise fine. Report content is LLM- and web-derived, and raw HTML passes through the markdown step, so the at-rule is reachable. The render now retries once with unavailable resources skipped rather than raised, which restores for every at-rule the behaviour the others already had. The SSRF guard is unchanged: the retry still refuses the same URLs and still makes no request for one it refuses.
diff --git a/src/local_deep_research/web/services/pdf_service.py b/src/local_deep_research/web/services/pdf_service.py
index 2c2090f05f..e2de5762a6 100644
--- a/src/local_deep_research/web/services/pdf_service.py
+++ b/src/local_deep_research/web/services/pdf_service.py
@@ -44,13 +44,24 @@ def _ensure_weasyprint() -> None:
module-level guard did.
"""
global HTML, CSS, WEASYPRINT_AVAILABLE, _URL_FETCHER
+ global URLFetchingError, URLFetcherResponse
if WEASYPRINT_AVAILABLE is not None:
return
try:
from weasyprint import HTML as _HTML, CSS as _CSS
- from weasyprint.urls import URLFetcher
+ from weasyprint.urls import (
+ URLFetcher,
+ URLFetchingError as _URLFetchingError,
+ )
+
+ try:
+ from weasyprint.urls import URLFetcherResponse as _Response
+ except ImportError: # WeasyPrint < 68
+ _Response = None
HTML, CSS = _HTML, _CSS
+ URLFetchingError = _URLFetchingError
+ URLFetcherResponse = _Response
_URL_FETCHER = URLFetcher(allow_redirects=False)
WEASYPRINT_AVAILABLE = True
except (OSError, ImportError):
@@ -82,6 +93,18 @@ class UnsafePDFResourceURLError(ValueError):
# the initial URL, so a 30x to a cloud metadata endpoint (see
# ssrf_validator.ALWAYS_BLOCKED_METADATA_IPS) would otherwise slip past.
_URL_FETCHER = None
+# Populated alongside the fetcher; see `_render_pdf` for why it is needed.
+URLFetchingError = None
+# The response type WeasyPrint 68 introduced. A dict still works there and
+# warns, and stops working in 69, so the empty resource is built through this
+# when it exists and falls back to the dict for an older install.
+URLFetcherResponse = None
+
+# What the retry hands WeasyPrint in place of a resource it may not have. An
+# empty body of an opaque type: every consumer treats it as nothing usable and
+# moves on, which is the outcome the raising fetcher was already producing for
+# every at-rule but one.
+_EMPTY_RESOURCE_MIME_TYPE = "application/octet-stream"
def _safe_url_fetcher(url):
@@ -95,6 +118,32 @@ def _safe_url_fetcher(url):
return _URL_FETCHER.fetch(url)
+def _skipping_url_fetcher(url):
+ """As `_safe_url_fetcher`, but a refusal yields an empty resource.
+
+ Used only by the retry in `_render_pdf`. No fetch happens for a URL the
+ guard refuses -- `validate_url` still runs first and still decides -- so
+ the SSRF posture is byte for byte the one above. The difference is only
+ what WeasyPrint is told afterwards: nothing, rather than an exception it
+ handles inconsistently.
+ """
+ try:
+ return _safe_url_fetcher(url)
+ except Exception:
+ logger.warning(f"Skipping unavailable resource in PDF rendering: {url}")
+ if URLFetcherResponse is not None:
+ return URLFetcherResponse(
+ url,
+ body=b"",
+ headers={"Content-Type": _EMPTY_RESOURCE_MIME_TYPE},
+ )
+ return {
+ "string": b"",
+ "mime_type": _EMPTY_RESOURCE_MIME_TYPE,
+ "redirected_url": url,
+ }
+
+
class MissingPDFDependencyError(RuntimeError):
"""Raised when WeasyPrint system libraries are unavailable.
@@ -276,13 +325,7 @@ class PDFService:
css_list.append(CSS(string=custom_css))
# Generate PDF
- # Use BytesIO to get bytes instead of writing to file
- pdf_buffer = io.BytesIO()
- html_doc.write_pdf(pdf_buffer, stylesheets=css_list)
-
- # Get the PDF bytes
- pdf_bytes = pdf_buffer.getvalue()
- pdf_buffer.close()
+ pdf_bytes = self._render_pdf(html_content, html_doc, css_list)
logger.info(f"Generated PDF, size: {len(pdf_bytes)} bytes")
return pdf_bytes
@@ -291,6 +334,40 @@ class PDFService:
logger.exception("Error generating PDF")
raise
+ def _render_pdf(self, html_content, html_doc, css_list) -> bytes:
+ """Render, and retry once with refusals skipped rather than raised.
+
+ WeasyPrint does not treat a refused fetch the same way everywhere. An
+ `@import` it cannot fetch is skipped and the render completes; an
+ `@color-profile` it cannot fetch is not, because the `fetch()` call in
+ `weasyprint/css/__init__.py` sits outside the `try`, so the
+ `URLFetchingError` escapes `CSS.__init__`, escapes this method, and the
+ export route answers a generic 500 (#6466). Report content is LLM- and
+ web-derived, so a `<style>` block carrying that at-rule is reachable and
+ python-markdown passes raw HTML straight through.
+
+ The retry restores the behaviour the other at-rules already have,
+ rather than renaming the failure. Nothing about the SSRF guard moves:
+ `_skipping_url_fetcher` still calls `validate_url` first and still
+ performs no fetch for a URL it refuses. Only the normal path raises, so
+ an export that works today is rendered exactly once and unchanged.
+ """
+ pdf_buffer = io.BytesIO()
+ try:
+ html_doc.write_pdf(pdf_buffer, stylesheets=css_list)
+ except URLFetchingError:
+ logger.warning(
+ "PDF render aborted on an unavailable resource; retrying with "
+ "unavailable resources skipped"
+ )
+ pdf_buffer = io.BytesIO()
+ HTML(
+ string=html_content, url_fetcher=_skipping_url_fetcher
+ ).write_pdf(pdf_buffer, stylesheets=css_list)
+ pdf_bytes = pdf_buffer.getvalue()
+ pdf_buffer.close()
+ return pdf_bytes
+
def _markdown_to_html(
self,
markdown_content: str,
diff --git a/tests/web_services/test_pdf_service.py b/tests/web_services/test_pdf_service.py
index af35e23374..eb87a01788 100644
--- a/tests/web_services/test_pdf_service.py
+++ b/tests/web_services/test_pdf_service.py
@@ -396,3 +396,135 @@ class TestPDFServiceSingleton:
# Different instances after reset
assert service1 is not service2
+
+
+class TestRefusedResourceDoesNotAbortTheExport:
+ """A refused fetch must be skipped, whichever at-rule asked for it (#6466).
+
+ WeasyPrint does not treat them alike. An `@import` it cannot fetch is
+ skipped and the render completes; an `@color-profile` it cannot fetch is
+ not, because the `fetch()` call in `weasyprint/css/__init__.py` sits
+ outside the `try`, so the `URLFetchingError` escapes `CSS.__init__` and
+ then `markdown_to_pdf`, and the export route answers a generic 500.
+
+ Report content is LLM- and web-derived and python-markdown passes raw HTML
+ straight through, so a `<style>` block carrying that at-rule reaches the
+ renderer. The URL used below is the cloud metadata address the SSRF guard
+ always blocks, so the refusal comes from this service's own fetcher.
+ """
+
+ BLOCKED = "http://169.254.169.254/q.icc"
+
+ def _report(self, head: str) -> str:
+ return f"{head}\n\n# Report\n\nBody text.\n"
+
+ def test_a_refused_color_profile_still_exports(self):
+ from local_deep_research.web.services.pdf_service import PDFService
+
+ markdown = self._report(
+ "<style>@color-profile --q { src: url('"
+ + self.BLOCKED
+ + "'); components: 3; }</style>"
+ )
+
+ assert (
+ PDFService()
+ .markdown_to_pdf(markdown, title="t")
+ .startswith(b"%PDF")
+ )
+
+ def test_a_refused_import_still_exports(self):
+ """The control, and the reason the report calls this an asymmetry: this
+ shape already worked, so a fix that made both fail would satisfy a
+ weaker test than this one."""
+ from local_deep_research.web.services.pdf_service import PDFService
+
+ markdown = self._report(
+ "<style>@import url('http://169.254.169.254/x.css');</style>"
+ )
+
+ assert (
+ PDFService()
+ .markdown_to_pdf(markdown, title="t")
+ .startswith(b"%PDF")
+ )
+
+ def test_the_ordinary_report_is_rendered_once(self):
+ """Second control. The retry must be reached only by the render that
+ would otherwise have failed: a report with no external resource must
+ not pay for a second pass, and must not go through the fetcher that
+ turns refusals into empty resources.
+ """
+ from local_deep_research.web.services import pdf_service
+
+ calls = []
+ original = pdf_service.HTML
+
+ def counting_html(*args, **kwargs):
+ calls.append(kwargs.get("url_fetcher"))
+ return original(*args, **kwargs)
+
+ with patch.object(pdf_service, "HTML", counting_html):
+ pdf = pdf_service.PDFService().markdown_to_pdf(
+ "# Report\n\nBody text.\n", title="t"
+ )
+
+ assert pdf.startswith(b"%PDF")
+ assert calls == [pdf_service._safe_url_fetcher]
+
+ def test_the_skipping_fetcher_still_refuses_to_fetch_a_blocked_url(self):
+ """The security property, asserted rather than assumed. The retry
+ changes what WeasyPrint is TOLD about a refused URL, not whether the
+ request is made: `validate_url` still runs first and still decides, and
+ an empty body is returned without touching the network.
+ """
+ from local_deep_research.web.services import pdf_service
+
+ # Settle the lazy import first: which of the two shapes below comes
+ # back depends on it, and leaving that to whichever test ran earlier
+ # made this cell pass alone and fail in the file.
+ pdf_service._ensure_weasyprint()
+
+ # A fetcher that would happily serve the blocked URL, so the assertion
+ # is that it was never ASKED. Patching it to None instead proved
+ # nothing: a fetcher that bypassed the guard would raise on the None
+ # and be swallowed by the same `except`, and the cell stayed green.
+ fetched: list[str] = []
+
+ class _WouldServeAnything:
+ def fetch(self, url):
+ fetched.append(url)
+ return {"string": b"metadata", "mime_type": "text/plain"}
+
+ with patch.object(pdf_service, "_URL_FETCHER", _WouldServeAnything()):
+ resource = pdf_service._skipping_url_fetcher(self.BLOCKED)
+
+ assert fetched == [], "the guard must decide before any request is made"
+
+ # WeasyPrint 68 returns a response read through `read()`; older
+ # versions return a dict. Either way the body is empty and nothing was
+ # fetched.
+ body = (
+ resource["string"]
+ if isinstance(resource, dict)
+ else resource.read()
+ )
+ assert body == b""
+
+ def test_an_allowed_url_is_fetched_normally_by_the_skipping_fetcher(self):
+ """And the accept control for it: the retry fetcher is not a blanket
+ "return nothing", or the retried render would silently drop every
+ legitimate resource the report references."""
+ from local_deep_research.web.services import pdf_service
+
+ payload = {"string": b"body", "mime_type": "text/css"}
+
+ class _Fetcher:
+ def fetch(self, url):
+ return payload
+
+ with patch.object(pdf_service, "_URL_FETCHER", _Fetcher()):
+ assert (
+ pdf_service._skipping_url_fetcher("https://example.com/x.css")
+ is payload
+ )