~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
"""Sink-leak tests for the direct-PDF download path's log events.
``DirectPDFDownloader.download_with_result`` -> ``_download_pdf`` ->
``BaseDownloader._download_pdf`` log the raw fetch URL — which can carry
credentials — and previously used ``logger.exception``, attaching the caught
error (whose message often embeds the same URL) to every sink. These tests
pin the redacted + no-attachment shape of those events and the redacted
rate-limit engine key.
"""
from __future__ import annotations
from io import StringIO
from unittest.mock import Mock
import pytest
import requests
from loguru import logger
import local_deep_research.research_library.downloaders.direct_pdf as direct_pdf_mod
from local_deep_research.research_library.downloaders.base import (
BaseDownloader,
rate_limit_authority,
)
from local_deep_research.research_library.downloaders.direct_pdf import (
ContentType,
DirectPDFDownloader,
)
_SECRET = "credential-do-not-log"
_EXCEPTION_TEXT = "network-error-do-not-log"
_URL = (
f"https://alice:{_SECRET}@origin.example:8443/private/%2Fencoded"
f"?token={_SECRET}&next=%2Felsewhere#fragment"
)
_ORIGIN = "https://origin.example:8443"
_CLOSE_ERROR_TEXT = "connection pool already released"
_FORBIDDEN = (
"alice",
_SECRET,
"/private/",
"%2Fencoded",
"token=",
"next=",
"fragment",
_EXCEPTION_TEXT,
"Traceback",
)
@pytest.fixture
def rendered_log():
output = StringIO()
logger.enable("local_deep_research")
sink_id = logger.add(
output,
level="DEBUG",
format="{message}\n{exception}",
backtrace=True,
diagnose=False,
)
try:
yield output
finally:
logger.remove(sink_id)
logger.disable("local_deep_research")
def _assert_safe_event(output, event, require_origin=True):
lines = [line for line in output.getvalue().splitlines() if event in line]
assert len(lines) == 1
if require_origin:
assert lines[0].count(_ORIGIN) == 1
for marker in _FORBIDDEN:
assert marker not in output.getvalue()
def _pdf_response():
response = Mock()
response.status_code = 200
response.content = b"%PDF-1.4 fake-bytes"
response.headers = {"content-type": "application/pdf"}
return response
def _make_downloader(monkeypatch, session_get):
downloader = DirectPDFDownloader()
monkeypatch.setattr(downloader.session, "get", session_get)
monkeypatch.setattr(
downloader.rate_tracker, "apply_rate_limit", lambda _engine: 0.0
)
monkeypatch.setattr(downloader.rate_tracker, "record_outcome", Mock())
return downloader
def test_text_path_success_renders_safe_events(rendered_log, monkeypatch):
downloader = _make_downloader(
monkeypatch, lambda *_a, **_k: _pdf_response()
)
monkeypatch.setattr(
DirectPDFDownloader,
"extract_text_from_pdf",
staticmethod(lambda _content: "text"),
)
result = downloader.download_with_result(_URL, ContentType.TEXT)
assert result.is_success
assert result.content == b"text"
_assert_safe_event(rendered_log, "Downloading PDF directly from:")
_assert_safe_event(rendered_log, "Downloading PDF from")
_assert_safe_event(rendered_log, "Successfully downloaded PDF from")
def test_pdf_path_success_renders_safe_events(rendered_log, monkeypatch):
downloader = _make_downloader(
monkeypatch, lambda *_a, **_k: _pdf_response()
)
result = downloader.download_with_result(_URL)
assert result.is_success
_assert_safe_event(rendered_log, "Attempting direct PDF download from")
_assert_safe_event(
rendered_log, "Successfully downloaded PDF directly from"
)
@pytest.mark.parametrize(
("exc", "event"),
[
(
requests.exceptions.RequestException(f"{_EXCEPTION_TEXT}: {_URL}"),
"Request error downloading from",
),
(
ValueError(f"{_EXCEPTION_TEXT}: {_URL}"),
"Unexpected error downloading from",
),
],
)
def test_immediate_failures_render_safe_events(
rendered_log, monkeypatch, exc, event
):
def _raise(*_a, **_k):
raise exc
downloader = _make_downloader(monkeypatch, _raise)
assert downloader._download_pdf(_URL) is None
_assert_safe_event(rendered_log, event)
def test_connection_error_exhaustion_renders_safe_events(
rendered_log, monkeypatch
):
def _raise(*_a, **_k):
raise requests.exceptions.ConnectionError(f"{_EXCEPTION_TEXT}: {_URL}")
downloader = _make_downloader(monkeypatch, _raise)
assert downloader._download_pdf(_URL) is None
output = rendered_log.getvalue()
final = [line for line in output.splitlines() if "after 3 attempts" in line]
assert len(final) == 1
assert final[0].count(_ORIGIN) == 1
attempts = [
line for line in output.splitlines() if "downloading from" in line
]
assert len(attempts) == 3
for line in attempts:
assert line.count(_ORIGIN) == 1
for marker in _FORBIDDEN:
assert marker not in output
def test_http_error_status_renders_safe_event(rendered_log, monkeypatch):
response = Mock()
response.status_code = 404
response.content = b""
response.headers = {}
downloader = _make_downloader(monkeypatch, lambda *_a, **_k: response)
assert downloader._download_pdf(_URL) is None
_assert_safe_event(rendered_log, "Failed to download from")
def test_http_rate_limited_exhaustion_renders_safe_events(
rendered_log, monkeypatch
):
response = Mock()
response.status_code = 429
response.content = b""
response.headers = {}
downloader = _make_downloader(monkeypatch, lambda *_a, **_k: response)
assert downloader._download_pdf(_URL) is None
output = rendered_log.getvalue()
final = [line for line in output.splitlines() if "after 3 attempts" in line]
assert len(final) == 1
assert final[0].count(_ORIGIN) == 1
attempts = [line for line in output.splitlines() if "HTTP 429 from" in line]
assert len(attempts) == 3
for line in attempts:
assert line.count(_ORIGIN) == 1
for marker in _FORBIDDEN:
assert marker not in output
def test_decode_failure_renders_safe_event(rendered_log, monkeypatch):
downloader = _make_downloader(monkeypatch, Mock())
monkeypatch.setattr(
downloader, "download", lambda _url, _content_type: b"\xff\xfe"
)
assert downloader.download_text(_URL) is None
_assert_safe_event(rendered_log, "Failed to decode text content from")
def test_can_handle_parse_error_renders_safe_event(rendered_log, monkeypatch):
def _raise(_url):
raise ValueError(_EXCEPTION_TEXT)
monkeypatch.setattr(direct_pdf_mod, "urlparse", _raise)
assert DirectPDFDownloader().can_handle(_URL) is False
_assert_safe_event(rendered_log, "Error parsing URL")
@pytest.mark.parametrize(
("authority", "expected_authority"),
[
("origin.example", "origin.example"),
("origin.example:8443", "origin.example:8443"),
("[2001:db8::1]", "[2001:db8::1]"),
("[2001:db8::1]:8443", "[2001:db8::1]:8443"),
],
)
def test_rate_limit_engine_key_omits_userinfo_and_preserves_authority(
monkeypatch, authority, expected_authority
):
captured = []
url = f"https://alice:secret@{authority}/file.pdf"
def _capture(engine_type):
captured.append(engine_type)
return 0.0
response = Mock()
response.status_code = 404
response.content = b""
response.headers = {}
downloader = DirectPDFDownloader()
monkeypatch.setattr(downloader.session, "get", lambda *_a, **_k: response)
monkeypatch.setattr(downloader.rate_tracker, "apply_rate_limit", _capture)
monkeypatch.setattr(downloader.rate_tracker, "record_outcome", Mock())
assert downloader._download_pdf(url) is None
assert captured == [f"pdf_download_{expected_authority}"]
assert "alice" not in captured[0]
assert "secret" not in captured[0]
@pytest.mark.parametrize(
"url",
[
f"https://alice:{_SECRET}@origin.example:bad/file.pdf",
f"https://alice:{_SECRET}@origin.example:99999/file.pdf",
f"http://alice:{_SECRET}@[/file.pdf",
],
)
def test_malformed_authority_reaches_normal_download_failure(
rendered_log, monkeypatch, url
):
captured = []
response = Mock(status_code=404, content=b"", headers={})
downloader = _make_downloader(monkeypatch, lambda *_a, **_k: response)
monkeypatch.setattr(
downloader.rate_tracker,
"apply_rate_limit",
lambda engine_type: captured.append(engine_type) or 0.0,
)
assert downloader._download_pdf(url) is None
assert captured == ["pdf_download_invalid_authority"]
assert "alice" not in captured[0]
assert _SECRET not in captured[0]
_assert_safe_event(
rendered_log, "Downloading PDF from", require_origin=False
)
_assert_safe_event(
rendered_log, "Failed to download from", require_origin=False
)
assert url not in rendered_log.getvalue()
def test_extract_text_from_pdf_failure_attaches_no_exception(rendered_log):
assert BaseDownloader.extract_text_from_pdf(b"not a pdf") is None
_assert_safe_event(
rendered_log, "Failed to extract text from PDF", require_origin=False
)
# Same middle ground as ``close``: pypdf's own message ("Stream has
# ended unexpectedly") is the whole diagnostic and cannot carry a URL --
# the frame holds only the PDF bytes -- so it is kept, without a
# traceback.
rendered = rendered_log.getvalue()
event = next(
line
for line in rendered.splitlines()
if "Failed to extract text from PDF" in line
)
assert event.split("Failed to extract text from PDF:", 1)[1].strip()
assert "Traceback" not in rendered
def test_close_failure_attaches_no_exception(rendered_log, monkeypatch):
downloader = DirectPDFDownloader()
monkeypatch.setattr(
downloader.session,
"close",
Mock(side_effect=RuntimeError(_CLOSE_ERROR_TEXT)),
)
downloader.close()
_assert_safe_event(
rendered_log, "Error closing downloader session", require_origin=False
)
# The scrubbed type+message survives -- "something failed" is not a
# usable event -- while the traceback (and every frame-local loguru's
# ``diagnose`` would render with it) stays out. Safe here because this
# handler's frames hold no URL: the only value in scope is the session.
assert _CLOSE_ERROR_TEXT in rendered_log.getvalue()
assert "Traceback" not in rendered_log.getvalue()
@pytest.mark.parametrize(
("url", "expected"),
[
("https://origin.example/paper.pdf", "origin.example"),
(
f"https://alice:{_SECRET}@origin.example:8443/p?token={_SECRET}",
"origin.example:8443",
),
# Leading whitespace: ``urlsplit`` lstrips C0-control-or-space and so
# does ``requests`` when it prepares the request, so the URL really
# is fetched and its per-host bucket must survive. Deriving the key
# from ``redact_url_for_log(url)`` instead yielded "" here and
# merged every such host into one adaptive rate-limit bucket.
(" https://slow-host.example/paper", "slow-host.example"),
("\thttps://tab-host.example/paper", "tab-host.example"),
("http://[2001:db8::1]:8080/paper.pdf", "[2001:db8::1]:8080"),
("http://[2001:db8::1]/paper.pdf", "[2001:db8::1]"),
(f"https://alice:{_SECRET}@origin.example:bad/p", "invalid_authority"),
(
f"https://alice:{_SECRET}@origin.example:99999/p",
"invalid_authority",
),
(f"http://alice:{_SECRET}@[/paper.pdf", "invalid_authority"),
# Whitespace inside the host is not a legal RFC 3986 reg-name and is
# percent-encoded by ``requests`` before the request leaves, so it is
# not the host contacted either; the tracker renders the key into its
# own log messages, so it must stay one printable token.
("https://exa mple.com/paper.pdf", "invalid_authority"),
# No authority at all -- one degenerate bucket, as on main.
("/relative/paper.pdf", ""),
("", ""),
],
)
def test_rate_limit_authority_matrix(url, expected):
authority = rate_limit_authority(url)
assert authority == expected
assert "alice" not in authority
assert _SECRET not in authority
assert not any(ch.isspace() or not ch.isprintable() for ch in authority)
def test_rate_limit_authority_ignores_an_oversized_url():
url = "https://origin.example:8443/" + "a" * 100_000
assert rate_limit_authority(url) == "origin.example:8443"
def test_rate_limit_authority_keeps_distinct_hosts_in_distinct_buckets():
keys = {
rate_limit_authority(f" https://{host}/paper")
for host in ("slow-host.example", "fast-host.example")
}
assert keys == {"slow-host.example", "fast-host.example"}