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(downloader): build GenericDownloader fallback URL via urlunparse (#6717)

S69c97ee396f7
diff --git a/changelog.d/6717.bugfix.md b/changelog.d/6717.bugfix.md
new file mode 100644
index 0000000000..6eb29fae2e
--- /dev/null
+++ b/changelog.d/6717.bugfix.md
@@ -0,0 +1,19 @@
+Fixed `GenericDownloader` building malformed fallback URLs when the
+`.pdf` suffix retry kicks in. A bare-host URL like
+`https://pmc.ncbi.nlm.nih.gov/` used to be turned into
+`https://pmc.ncbi.nlm.nih.gov.pdf` (`.pdf` appended to the hostname);
+the SSRF validator correctly rejected the unresolvable hostname, the
+download was logged at `ERROR`, and the URL was logged as
+`URL failed security validation (possible SSRF)` even though the fallback
+is a normal part of the download path. A URL with a query string used to
+land `.pdf` after the query separator (`?q=1.pdf`), producing a malformed
+URL. The fallback now routes through `urlunparse(parsed._replace(path=...))`
+so the suffix lands in the path component and scheme / netloc / query /
+fragment / userinfo are preserved verbatim; a bare-host URL becomes
+`https://host/index.pdf`.
+
+Also hardened the "already has a `.pdf` suffix" guard: it now operates
+on the trailing-slashes-stripped path and is case-insensitive, so paths
+like `paper.pdf/`, `paper.PDF`, and `paper.Pdf/` are all recognised as
+already-suffixed instead of producing `paper.pdf.pdf` / `paper.PDF.pdf`
+on the second pass.
diff --git a/src/local_deep_research/research_library/downloaders/generic.py b/src/local_deep_research/research_library/downloaders/generic.py
index 10ad940580..3dd5c2ab55 100644
--- a/src/local_deep_research/research_library/downloaders/generic.py
+++ b/src/local_deep_research/research_library/downloaders/generic.py
@@ -4,7 +4,7 @@ Generic PDF Downloader for unspecified sources
from typing import Dict, Optional
import requests
-from urllib.parse import urlparse
+from urllib.parse import urlparse, urlunparse
from loguru import logger
from ...security.ssrf_validator import redact_url_for_log
@@ -14,6 +14,51 @@ from .base import BaseDownloader, ContentType, DownloadResult
HTML_NOT_PDF_REASON = "Source is an HTML page, not a PDF"
+def _with_pdf_suffix(url: str) -> Optional[str]:
+ """Return ``url`` with ``.pdf`` appended to its path, or ``None`` if
+ no suffix is needed / the URL cannot be parsed.
+
+ Three edge cases the previous implementation got wrong:
+
+ 1. **Bare host.** A URL like ``https://pmc.ncbi.nlm.nih.gov/`` has an
+ empty path. Naive string concatenation
+ (``url.rstrip("/") + ".pdf"``) produced
+ ``https://pmc.ncbi.nlm.nih.gov.pdf`` -- ``.pdf`` ended up appended
+ to the hostname, the SSRF validator rejected it as an unresolvable
+ hostname, and the URL was logged at ``ERROR`` even though the
+ fallback was a normal part of the download path.
+ 2. **Trailing slash on a path.** ``https://example.com/paper/`` ended
+ up as ``https://example.com/paper.pdf`` by accident; that worked,
+ but only because ``rstrip("/")`` happened to delete exactly the
+ slash before appending.
+ 3. **Query string.** ``https://example.com/paper?q=1`` became
+ ``https://example.com/paper?q=1.pdf``, putting ``.pdf`` after the
+ query separator and corrupting the URL.
+
+ We rebuild the URL via ``urlunparse(parsed._replace(path=...))`` so
+ the suffix lands in the path component and scheme/netloc/query/
+ fragment/userinfo are preserved verbatim. An empty path becomes
+ ``/index.pdf`` -- a deterministic placeholder that still describes
+ a resource at the host root.
+
+ The ``.pdf`` guard is case-insensitive and runs on the trailing
+ slashes-stripped path, so ``paper.pdf/``, ``paper.PDF``, and
+ ``paper.Pdf/`` are all recognised as already-suffixed and short-
+ circuit to ``None`` instead of producing ``paper.pdf.pdf``.
+ """
+ try:
+ parsed = urlparse(url)
+ except ValueError:
+ return None
+
+ stripped_path = parsed.path.rstrip("/") if parsed.path else ""
+ if stripped_path.lower().endswith(".pdf"):
+ return None
+
+ new_path = (stripped_path or "/index") + ".pdf"
+ return urlunparse(parsed._replace(path=new_path))
+
+
class GenericDownloader(BaseDownloader):
"""Generic downloader for any URL - attempts basic PDF download."""
@@ -68,18 +113,13 @@ class GenericDownloader(BaseDownloader):
return DownloadResult(content=pdf_content, is_success=True)
# If the URL doesn't end with .pdf, try adding it
- try:
- parsed = urlparse(url)
- if not parsed.path.endswith(".pdf"):
- pdf_url = url.rstrip("/") + ".pdf"
- logger.debug(
- f"Trying with .pdf extension: {redact_url_for_log(pdf_url)}"
- )
- pdf_content = super()._download_pdf(pdf_url)
- else:
- pdf_content = None
- except (ValueError, AttributeError):
- # urlparse can raise ValueError for malformed URLs
+ pdf_url = _with_pdf_suffix(url)
+ if pdf_url is not None:
+ logger.debug(
+ f"Trying with .pdf extension: {redact_url_for_log(pdf_url)}"
+ )
+ pdf_content = super()._download_pdf(pdf_url)
+ else:
pdf_content = None
if pdf_content:
@@ -179,18 +219,13 @@ class GenericDownloader(BaseDownloader):
return pdf_content
# If the URL doesn't end with .pdf, try adding it
- try:
- parsed = urlparse(url)
- if not parsed.path.endswith(".pdf"):
- pdf_url = url.rstrip("/") + ".pdf"
- logger.debug(
- f"Trying with .pdf extension: {redact_url_for_log(pdf_url)}"
- )
- pdf_content = super()._download_pdf(pdf_url)
- else:
- pdf_content = None
- except (ValueError, AttributeError):
- # urlparse can raise ValueError for malformed URLs
+ pdf_url = _with_pdf_suffix(url)
+ if pdf_url is not None:
+ logger.debug(
+ f"Trying with .pdf extension: {redact_url_for_log(pdf_url)}"
+ )
+ pdf_content = super()._download_pdf(pdf_url)
+ else:
pdf_content = None
if pdf_content:
diff --git a/tests/research_library/downloaders/test_generic_downloader_pdf_suffix.py b/tests/research_library/downloaders/test_generic_downloader_pdf_suffix.py
new file mode 100644
index 0000000000..4611780d5c
--- /dev/null
+++ b/tests/research_library/downloaders/test_generic_downloader_pdf_suffix.py
@@ -0,0 +1,330 @@
+"""Regression tests for the ``GenericDownloader`` ``.pdf`` fallback URL builder.
+
+Background
+----------
+``GenericDownloader._download_pdf`` and ``download_with_result`` both
+have a "fall back to a URL with ``.pdf`` appended" branch for sources
+that serve the PDF under a slightly different path than the HTML
+landing page (the canonical case is a publisher that exposes
+``https://example.com/paper`` for the HTML and
+``https://example.com/paper.pdf`` for the PDF).
+
+The original implementation was::
+
+ pdf_url = url.rstrip("/") + ".pdf"
+
+That works for the common case (``https://example.com/paper`` becomes
+``https://example.com/paper.pdf``) but breaks in three silent ways:
+
+1. **Bare host** -- ``https://pmc.ncbi.nlm.nih.gov/`` becomes
+ ``https://pmc.ncbi.nlm.nih.gov.pdf``. ``.pdf`` ends up appended to
+ the hostname, the SSRF validator (``safe_requests.ssrf_validator``)
+ rejects the unresolvable hostname with
+ ``URL failed security validation (possible SSRF)``, and the failure
+ is logged at ``ERROR`` even though the fallback was a normal part of
+ the download path. This was the failure mode that surfaced in
+ production Docker logs on 2026-09-21.
+2. **Trailing slash on a path** -- ``https://example.com/paper/``
+ happens to work because ``rstrip("/")`` deletes exactly the slash
+ before appending. Fragile.
+3. **Query string** -- ``https://example.com/paper?q=1`` becomes
+ ``https://example.com/paper?q=1.pdf``. ``.pdf`` ends up after the
+ query separator and the URL is malformed.
+
+The fix routes the URL through ``urlunparse(parsed._replace(path=...))``
+so the suffix lands in the path component and scheme / netloc / query /
+fragment / userinfo are preserved verbatim. An empty path becomes
+``/index.pdf`` -- a deterministic placeholder that still describes a
+resource at the host root.
+
+These tests pin every URL shape the production scheduler feeds into the
+download service, plus the historical shape that the OLD string
+concatenation would have corrupted silently.
+"""
+
+import inspect
+from contextlib import contextmanager
+from unittest.mock import patch
+
+import pytest
+
+from local_deep_research.research_library.downloaders.generic import (
+ GenericDownloader,
+ _with_pdf_suffix,
+)
+
+
+# ---------------------------------------------------------------------------
+# Pure unit tests on the URL builder
+# ---------------------------------------------------------------------------
+
+
+class TestWithPdfSuffix:
+ """The helper is the single source of truth for how a URL becomes
+ its ``.pdf`` suffix variant. Pin its behaviour against every URL
+ shape the production download scheduler produces."""
+
+ @pytest.mark.parametrize(
+ "input_url,expected",
+ [
+ # The canonical case the original code happened to get right.
+ (
+ "https://example.com/paper",
+ "https://example.com/paper.pdf",
+ ),
+ # URL already ends in .pdf -> no change needed, helper returns
+ # None to signal "skip the fallback".
+ ("https://example.com/paper.pdf", None),
+ # The production bug: bare host with trailing slash. The old
+ # implementation turned this into
+ # ``https://pmc.ncbi.nlm.nih.gov.pdf`` -- a URL whose
+ # ``.pdf`` was bolted onto the hostname. The helper now
+ # synthesises an ``/index.pdf`` resource at the host root.
+ (
+ "https://pmc.ncbi.nlm.nih.gov/",
+ "https://pmc.ncbi.nlm.nih.gov/index.pdf",
+ ),
+ # Bare host without trailing slash. ``urlparse`` normalises
+ # the empty path the same way.
+ (
+ "https://example.com",
+ "https://example.com/index.pdf",
+ ),
+ # Trailing slash on a real path. Used to work by accident
+ # (``rstrip("/")`` + ``".pdf"``); the helper now does it
+ # structurally.
+ (
+ "https://example.com/paper/",
+ "https://example.com/paper.pdf",
+ ),
+ # Latent bug: a query string used to land before ``.pdf``
+ # in the rebuilt URL. The helper keeps the query intact.
+ (
+ "https://example.com/paper?q=1",
+ "https://example.com/paper.pdf?q=1",
+ ),
+ # Query string with multiple parameters and a fragment.
+ (
+ "https://example.com/paper?q=1&v=2#section",
+ "https://example.com/paper.pdf?q=1&v=2#section",
+ ),
+ # userinfo is preserved.
+ (
+ "https://user:[email protected]/paper",
+ "https://user:[email protected]/paper.pdf",
+ ),
+ # Path with trailing slash AND query string.
+ (
+ "https://example.com/paper/?lang=en",
+ "https://example.com/paper.pdf?lang=en",
+ ),
+ # A path that already happens to end with a non-``.pdf``
+ # extension. The helper appends ``.pdf`` after the existing
+ # extension, which is the documented "best effort fallback"
+ # semantic.
+ (
+ "https://example.com/paper.html",
+ "https://example.com/paper.html.pdf",
+ ),
+ ],
+ )
+ def test_url_shapes(self, input_url, expected):
+ assert _with_pdf_suffix(input_url) == expected
+
+ @pytest.mark.parametrize(
+ "input_url",
+ [
+ # Trailing slash on an already-.pdf path -- the OLD guard
+ # only checked ``endswith(".pdf")`` on the unstripped path,
+ # so the helper used to fall through and produce
+ # ``paper.pdf.pdf``.
+ "https://example.com/paper.pdf/",
+ "https://example.com/paper.pdf//",
+ # Uppercase / mixed-case ``.pdf`` extension. The OLD guard
+ # was case-sensitive (``paper.PDF`` slipped through) and
+ # produced ``paper.PDF.pdf``. Both shapes are recognised
+ # as already-suffixed and short-circuit to ``None``.
+ "https://example.com/paper.PDF",
+ "https://example.com/paper.Pdf",
+ "https://example.com/paper.pDf",
+ # Uppercase + trailing slash.
+ "https://example.com/paper.PDF/",
+ "https://example.com/paper.Pdf/",
+ ],
+ )
+ def test_already_suffixed_url_short_circuits(self, input_url):
+ """A path that already ends in ``.pdf`` -- case-insensitive,
+ with or without trailing slashes -- must NOT trigger a second
+ ``.pdf`` fallback. Pinned against the
+ ``paper.pdf.pdf`` / ``paper.PDF.pdf`` doubling the OLD guard
+ produced."""
+ assert _with_pdf_suffix(input_url) is None
+
+ def test_bare_host_does_not_corrupt_hostname(self):
+ """Production reproduction. The OLD implementation turned
+ ``https://pmc.ncbi.nlm.nih.gov/`` into
+ ``https://pmc.ncbi.nlm.nih.gov.pdf`` -- ``.pdf`` appended to the
+ hostname. The host portion of the rebuilt URL must still be
+ ``pmc.ncbi.nlm.nih.gov``.
+ """
+ from urllib.parse import urlparse
+
+ rebuilt = _with_pdf_suffix("https://pmc.ncbi.nlm.nih.gov/")
+ assert rebuilt is not None
+ parsed = urlparse(rebuilt)
+ assert parsed.hostname == "pmc.ncbi.nlm.nih.gov", (
+ f"hostname must not absorb the .pdf suffix; got {parsed.hostname!r}"
+ )
+ assert parsed.path == "/index.pdf"
+ assert ".pdf" not in parsed.hostname
+
+ def test_unparseable_url_returns_none(self):
+ """``urlparse`` is permissive, but the helper still has to be
+ defensive about pathological inputs. A URL that triggers a
+ ``ValueError`` short-circuits to ``None`` -- the same fail-soft
+ contract the original try/except block aimed at."""
+ # An unparseable scheme-side fragment -- urlparse does not
+ # raise on this in practice, so we use a synthetic ValueError
+ # via a mocked urlparse to pin the branch.
+ with patch(
+ "local_deep_research.research_library.downloaders.generic.urlparse",
+ side_effect=ValueError("synthetic"),
+ ):
+ assert _with_pdf_suffix("https://example.com/paper") is None
+
+
+# ---------------------------------------------------------------------------
+# Integration with _download_pdf and download_with_result
+# ---------------------------------------------------------------------------
+
+
+class TestDownloaderUsesCorrectFallbackUrl:
+ """The two callers of ``_with_pdf_suffix`` (in ``_download_pdf`` and
+ ``download_with_result``) must pass the rebuilt URL to the parent
+ class's ``_download_pdf`` -- and must NOT call it with the
+ hostname-corrupted URL the OLD implementation produced."""
+
+ @pytest.fixture
+ def downloader(self):
+ return GenericDownloader(timeout=30)
+
+ @contextmanager
+ def _spy_downloads(self, downloader):
+ """Record every URL the parent ``_download_pdf`` is called with."""
+ urls_called = []
+
+ def fake_download(url, headers=None):
+ urls_called.append(url)
+ return # every attempt "fails" so the fallback runs
+
+ # Patch the inherited ``BaseDownloader._download_pdf`` so we can
+ # observe the URL the helper built and the parent fetched.
+ with patch.object(
+ GenericDownloader.__bases__[0],
+ "_download_pdf",
+ side_effect=fake_download,
+ ):
+ yield urls_called
+
+ def test_download_pdf_uses_index_pdf_for_bare_host(self, downloader):
+ """Pin the production bug fix at the public API boundary: a
+ bare-host URL must produce a ``/index.pdf`` request, not a
+ hostname that ends in ``.pdf``."""
+ with self._spy_downloads(downloader) as urls_called:
+ downloader._download_pdf("https://pmc.ncbi.nlm.nih.gov/")
+
+ assert urls_called == [
+ "https://pmc.ncbi.nlm.nih.gov/", # direct attempt
+ "https://pmc.ncbi.nlm.nih.gov/index.pdf", # rebuilt fallback
+ ]
+
+ def test_download_pdf_preserves_query_string(self, downloader):
+ with self._spy_downloads(downloader) as urls_called:
+ downloader._download_pdf("https://example.com/paper?id=42")
+
+ # ``.pdf`` must land BEFORE the query, not after it.
+ assert urls_called == [
+ "https://example.com/paper?id=42",
+ "https://example.com/paper.pdf?id=42",
+ ]
+
+ def test_download_pdf_does_not_double_extension(self, downloader):
+ """A URL already ending in ``.pdf`` must NOT trigger a second
+ fallback attempt (``paper.pdf.pdf``). Pinned against the
+ existing happy-path contract."""
+ with self._spy_downloads(downloader) as urls_called:
+ downloader._download_pdf("https://example.com/paper.pdf")
+
+ # The direct attempt fails (we stubbed every call to None), so
+ # the fallback branch is reached, but ``_with_pdf_suffix``
+ # returns ``None`` for an already-``.pdf`` URL -- so the helper
+ # makes only ONE call.
+ assert urls_called == ["https://example.com/paper.pdf"]
+ assert not any(".pdf.pdf" in u for u in urls_called)
+
+ def test_download_with_result_uses_index_pdf_for_bare_host(
+ self, downloader
+ ):
+ """Same fix, but on the ``download_with_result`` code path.
+ That branch has its own try/except block that used to build
+ the URL with the same broken string concatenation."""
+
+ # The diagnostic GET inside ``download_with_result`` also fires
+ # when the fallback fails. A plain object works because the
+ # route only reads ``status_code`` and ``headers`` and uses it
+ # inside ``with ... as response:`` -- a regular class with
+ # ``__enter__``/``__exit__`` is the most truthful stand-in.
+ class _DiagnosticResp:
+ def __init__(self):
+ self.status_code = 200
+ self.headers = {"content-type": "text/html"}
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, exc_type, exc, tb):
+ return False
+
+ with self._spy_downloads(downloader) as urls_called:
+ with patch.object(
+ downloader.session, "get", return_value=_DiagnosticResp()
+ ):
+ result = downloader.download_with_result(
+ "https://pmc.ncbi.nlm.nih.gov/"
+ )
+
+ # The route must have tried the rebuilt URL, not a
+ # hostname-with-``.pdf`` URL.
+ assert urls_called == [
+ "https://pmc.ncbi.nlm.nih.gov/",
+ "https://pmc.ncbi.nlm.nih.gov/index.pdf",
+ ]
+ # And the result is the HTML-detected response, not a crash.
+ assert result.is_success is False
+
+
+# ---------------------------------------------------------------------------
+# Dogfood: the helper should not be exported as part of the module API
+# by accident -- it's an internal detail. Document that here so a
+# future refactor that promotes it has to update the test.
+# ---------------------------------------------------------------------------
+
+
+def test_helper_is_module_level_and_private():
+ """``_with_pdf_suffix`` is the single chokepoint for the URL
+ transform. If a future refactor duplicates the logic in the two
+ callers instead of routing through this helper, the regression
+ coverage in ``TestDownloaderUsesCorrectFallbackUrl`` would still
+ pass -- so pin the helper's existence here."""
+ import local_deep_research.research_library.downloaders.generic as mod
+
+ assert hasattr(mod, "_with_pdf_suffix"), (
+ "_with_pdf_suffix must remain a module-level helper so the "
+ "two callers (download_with_result and _download_pdf) cannot "
+ "drift in their URL-rebuild semantics"
+ )
+ assert callable(mod._with_pdf_suffix)
+ # And it's a pure function -- no reliance on session, rate_tracker,
+ # or anything else that would force tests to set up a downloader.
+ sig = inspect.signature(mod._with_pdf_suffix)
+ assert list(sig.parameters) == ["url"]