~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
"""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"
)
@pytest.fixture
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}"