~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
"""Custom-CSS fetcher contracts for the PDF export path.
The rendered HTML already resolves every resource through the
SSRF-guarded ``_safe_url_fetcher`` (GHSA-fj2m-qvh9-jq4q). Caller-supplied
``custom_css`` is parsed by a separate ``CSS()`` construction, and
WeasyPrint fetches a stylesheet's ``@import`` and ``@color-profile src``
while parsing it — so that construction must resolve through the same
guarded fetcher, not WeasyPrint's default. (Property-level ``url()``
values, e.g. ``background-image``, are resolved later, at render time,
through the HTML document's fetcher, which is already guarded.)
"""
from __future__ import annotations
from unittest.mock import patch
import pytest
pytest.importorskip("weasyprint")
from local_deep_research.web.services import pdf_service as pdf_module # noqa: E402
class TestCustomCssUsesTheGuardedFetcher:
def test_custom_css_is_parsed_with_the_ssrf_guarded_fetcher(self):
custom_css = "p { color: rebeccapurple; }"
service = pdf_module.get_pdf_service()
# CSS is populated lazily by _ensure_weasyprint(), so capture the
# real callable only after the service has initialised it.
real_css = pdf_module.CSS
recorded: list[dict] = []
def recording_css(*args, **kwargs):
recorded.append(kwargs)
return real_css(*args, **kwargs)
with patch.object(pdf_module, "CSS", recording_css):
service.markdown_to_pdf(
"# Title\n\nBody text.\n", title="Title", custom_css=custom_css
)
custom_calls = [kw for kw in recorded if kw.get("string") == custom_css]
assert custom_calls, "custom_css must be parsed through CSS()"
assert all(
kw.get("url_fetcher") is pdf_module._safe_url_fetcher
for kw in custom_calls
)
class TestColorProfileRefusalKeepsRendering:
"""A refused ``@color-profile`` fetch must not abort the export.
WeasyPrint resolves an ``@color-profile`` rule's ``src`` outside the
try/except that guards ``@import``, so an unwrapped
``CSS(string=custom_css, ...)`` lets the guarded fetcher's refusal
surface as ``URLFetchingError`` and abort the whole render. The
export must instead drop the custom stylesheet and complete with
default styling — and the probe must confirm the fetch never left
the process ungated.
"""
def test_custom_css_color_profile_refusal_does_not_abort_the_render(
self,
):
import threading
import urllib.request
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
hits: list[str] = []
class _Probe(BaseHTTPRequestHandler):
def do_GET(self):
hits.append(self.path)
self.send_response(200)
self.send_header("Content-Type", "text/css")
self.end_headers()
self.wfile.write(b"body { color: blue; }")
def log_message(self, *args):
pass
server = ThreadingHTTPServer(("127.0.0.1", 0), _Probe)
port = server.server_address[1]
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
try:
# Environment canary: prove loopback HTTP works here, so a
# broken sandbox cannot turn the assertion below into a
# silent pass.
with urllib.request.urlopen(
f"http://127.0.0.1:{port}/canary", timeout=5
) as response:
assert response.status == 200
assert hits == ["/canary"]
hits.clear()
service = pdf_module.get_pdf_service()
pdf_bytes = service.markdown_to_pdf(
"# Probe",
# The payload must keep its ``--`` prefixed name and a
# ``src: url(...)`` token: WeasyPrint drops an unprefixed
# profile or a profile without ``src`` before any fetch,
# and the assertion would then pass under a revert of the
# guarded fetcher.
custom_css=(
"@color-profile --p { "
f"src: url('http://127.0.0.1:{port}/p.icc'); "
"components: 3; }"
),
)
assert pdf_bytes, (
"render must still complete when the color-profile "
"fetch is refused"
)
finally:
server.shutdown()
server.server_close()
thread.join(timeout=5)
assert hits == [], (
f"custom_css @color-profile reached the network ungated "
f"(requests: {hits}); the stylesheet fetch bypassed "
"_safe_url_fetcher"
)
class TestUnparseableCustomCssKeepsRendering:
"""A custom stylesheet WeasyPrint cannot even parse must not abort
the export either -- the same widened ``except`` that covers a
refused fetch must also cover a relative ``@import`` WeasyPrint
cannot resolve without a base URL (a ``ValueError``).
A relative ``@import url(theme.css);`` cannot resolve: ``custom_css``
is parsed via ``CSS(string=custom_css, ...)`` with no ``base_url``,
so WeasyPrint raises ``InvalidValues`` (a ``ValueError``) before any
fetch is attempted -- ``URLFetchingError`` is never involved. An
``except URLFetchingError`` alone would let that ``ValueError``
escape and abort the export; the render must instead drop the
custom stylesheet and complete with default styling, exactly like a
refused fetch.
"""
def test_unparseable_custom_css_does_not_abort_the_render(self):
service = pdf_module.get_pdf_service()
seen_css_lists: list[list] = []
real_render_pdf = service._render_pdf
def recording_render_pdf(html_content, html_doc, css_list):
seen_css_lists.append(list(css_list))
return real_render_pdf(html_content, html_doc, css_list)
with patch.object(service, "_render_pdf", recording_render_pdf):
pdf_bytes = service.markdown_to_pdf(
"# Title\n\nBody text.\n",
title="Title",
custom_css="@import url(theme.css);",
)
assert pdf_bytes.startswith(b"%PDF"), (
"render must still complete when the custom stylesheet "
"cannot be parsed"
)
assert seen_css_lists == [[service.minimal_css]], (
"the unparseable custom stylesheet must be dropped, "
"leaving only the default styling"
)