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.

94.0 KB
Raw Blame
"""Redaction contracts for failure-path URL logging in the downloader stack.

Fetches are driven by web-influenced URLs (search results, crawled
page content), which routinely carry secrets in query strings —
presigned storage links, session tokens, password-reset URLs — or in
userinfo (``https://user:pass@host/``). When a fetch or parse *fails or
is rejected*, the operator-facing log must show only
``scheme://host:port`` (``redact_url_for_log``) — the discipline the
SSRF validator's own sites and the PDF fetcher (#6521) already follow.

Two leak channels are pinned here:

1. **The URL itself** interpolated into a log call.
2. **The exception text.** requests / urllib3 / SafeSession / Playwright
   exception messages embed the full URL, and loguru renders
   ``ExcType: str(e)`` for ``logger.exception`` even with
   ``diagnose=False``. Failure handlers therefore log
   ``type(e).__name__`` and the redacted URL — never ``str(e)`` and
   never a traceback.

What the structural contract covers (AST scan of ``SCANNED_RELATIVE``):

* every ``logger.<level>(...)`` call at *any* level (trace through
  critical, and ``log``) — including chained receivers such as
  ``logger.opt(...).warning`` and ``logger.bind(...).error`` — where any
  positional
  or keyword argument (f-string pieces, loguru brace-positional args,
  ``!r`` / ``=`` conversions, subscripts, ``str(...)`` wrappers, any
  quote style) references a URL-named identifier (``url``, ``*_url``,
  ``url_*``, ``urls``, ``uri``, ``href``, ``location``, as a bare name
  or attribute; ``*_type`` names such as ``url_type`` are not URLs) that
  is not wrapped in ``redact_url_for_log`` / ``rate_limit_authority``. The
  attempt/progress lines count: they run before every fetch, including
  the ones that then fail;
* every logger call *at any level* inside an ``except ... as e`` block
  that references ``e`` other than as ``type(e)`` / ``e.__class__``;
* every call that attaches a traceback — ``logger.exception`` and any
  ``logger.opt(exception=<anything but a literal False/None>)`` receiver
  (``True``, the exception variable, …) at any level including ``log``
  — in an ``except`` block whose ``try`` body makes a network-shaped
  call (``get``/``post``/``request``/``safe_get``/``download*``/
  ``_fetch*``/``_probe*``/…).

Not covered (documented residuals):

* a URL copied into a differently named variable, or a message string
  pre-built into a variable and then logged;
* an exception aliased to another name (``err = e`` then
  ``logger.error("{}", err)``): only the ``except ... as`` name is
  tracked;
* a fetch wrapped in a helper whose name does not match
  ``NETWORK_CALL``: the traceback rule keys on the called name, not on
  what the callee does;
* a traceback-attaching call in an ``except`` block whose ``try`` makes
  no network-shaped call (other persistence and extraction handlers;
  ``download_service`` is covered by the #6829 regression separately);
* modules outside ``SCANNED_RELATIVE``.

``ALLOWED`` lists the reviewed exceptions, each keyed to the exact call
text so that a new offending call in the same function is still
reported.

Exception messages raised by ``security/safe_requests.py`` are redacted
at source; ``TestSafeRequestsExceptionTexts`` pins each raise site.
"""

from __future__ import annotations

import ast
import re
from pathlib import Path
from unittest.mock import MagicMock, patch

import pytest
import requests
from loguru import logger

import local_deep_research


def _src_root() -> Path:
    return Path(local_deep_research.__file__).resolve().parent


#: Modules whose failure paths handle web-influenced URLs.
SCANNED_RELATIVE = [
    "research_library/downloaders",
    "research_library/services/download_service.py",
    "research_library/utils/__init__.py",
    "scheduler/background.py",
    "security/safe_requests.py",
    "web/routers/api.py",
    "web/routers/library.py",
    "web/routers/metrics.py",
]

#: Levels at which a raw URL must never be interpolated: all of them.
ALL_LEVELS = {
    "trace",
    "debug",
    "info",
    "success",
    "warning",
    "error",
    "exception",
    "critical",
    "log",
}

#: Calls whose result is safe to log even when given a URL.
SAFE_WRAPPERS = {"redact_url_for_log", "_redact", "rate_limit_authority"}

#: Identifiers that name a URL (token-delimited by ``_``).
URL_NAME = re.compile(
    r"(?:^|_)(?:urls?|uri|href|location)(?:_|$)", re.IGNORECASE
)

#: URL-named identifiers that hold no URL (``url_type`` is an enum).
NOT_URL_NAME = re.compile(r"_types?$", re.IGNORECASE)

#: Calls in a ``try`` body that can raise an exception whose text embeds
#: the requested URL.
NETWORK_CALL = re.compile(
    r"^(?:get|post|head|request|send|safe_get|safe_post|goto|arun"
    r"|raise_for_status|download\w*|_download\w*|fetch\w*|_fetch\w*"
    r"|_try_\w+|_probe\w*)$"
)

#: Reasons shared by the metrics-router entries in ``ALLOWED``.
_METRICS_DB_ONLY = (
    "the 'get' calls are dict / query-param .get; the try is database, "
    "settings and local computation work, no fetch"
)
_METRICS_PRICING_ONLY = (
    "the 'get' calls are query-param and dict .get; CostCalculator reads "
    "cached or static pricing, no fetch"
)
_METRICS_JOURNAL_FETCH = (
    "download_journal_data fetches only the app-fixed journal data "
    "sources (journal_quality/data_sources, e.g. OpenAlex, DOAJ), never a "
    "web-sourced URL, and catches each source's fetch error itself "
    "(_fetch_one); what escapes to this handler is settings, disk and "
    "DB-build work"
)

#: Reviewed exceptions: (module relpath, qualified function, rule,
#: whitespace-normalised call text) -> why the flagged call cannot carry
#: a request URL. Keying on the call text means only the reviewed call
#: is exempt; a new offending call in the same function is reported.
ALLOWED = {
    (
        "scheduler/background.py",
        "BackgroundJobScheduler._reload_config",
        "exception-after-network",
        'logger.exception("Error reloading configuration")',
    ): (
        "the matched 'get' is self.config.get; the try re-reads scheduler "
        "settings and holds no URL or credential"
    ),
    (
        "research_library/services/download_service.py",
        "DownloadService._build_egress_context",
        "exc-text",
        'logger.bind(policy_audit=True).warning( "DownloadService policy '
        'unavailable; locking out " "every URL check", reason=str(exc), )',
    ): "policy-construction error text (settings snapshot), no URL",
    (
        "web/routers/api.py",
        "api_add_resource._impl",
        "exception-after-network",
        'logger.exception("Error adding resource")',
    ): "the 'get' is dict.get on the request body; no fetch in the try",
    (
        "web/routers/api.py",
        "check_ollama_model",
        "exception-after-network",
        'logger.exception("Error checking Ollama model")',
    ): "_probe_ollama_tags handles request errors itself; SafeSession "
    "refusal texts are redacted at source (security/safe_requests.py)",
    (
        "web/routers/library.py",
        "library_page",
        "exception-after-network",
        'logger.exception("Error loading library data")',
    ): "the 'get' calls are request.query_params.get; the try reads the "
    "library from the database, no fetch",
    (
        "web/routers/api.py",
        "check_ollama_status",
        "exception-after-network",
        'logger.exception("Error checking Ollama status")',
    ): "twin of check_ollama_model: _probe_ollama_tags handles request "
    "errors itself; SafeSession refusal texts are redacted at source "
    "(security/safe_requests.py)",
    (
        "web/routers/metrics.py",
        "get_link_analytics",
        "exception-after-network",
        'logger.exception("Error getting link analytics")',
    ): _METRICS_DB_ONLY,
    (
        "web/routers/metrics.py",
        "api_metrics",
        "exception-after-network",
        'logger.exception("Error getting metrics")',
    ): _METRICS_DB_ONLY,
    (
        "web/routers/metrics.py",
        "get_rate_limiting_metrics",
        "exception-after-network",
        'logger.exception("Error getting rate limiting metrics")',
    ): _METRICS_DB_ONLY,
    (
        "web/routers/metrics.py",
        "api_research_link_metrics",
        "exception-after-network",
        'logger.exception("Error getting research link metrics")',
    ): _METRICS_DB_ONLY,
    (
        "web/routers/metrics.py",
        "api_enhanced_metrics",
        "exception-after-network",
        'logger.exception("Error getting enhanced metrics")',
    ): _METRICS_DB_ONLY,
    (
        "web/routers/metrics.py",
        "api_save_research_rating",
        "exception-after-network",
        'logger.exception("Error saving research rating")',
    ): _METRICS_DB_ONLY,
    (
        "web/routers/metrics.py",
        "api_star_reviews",
        "exception-after-network",
        'logger.exception("Error getting star reviews data")',
    ): _METRICS_DB_ONLY,
    (
        "web/routers/metrics.py",
        "api_cost_calculation",
        "exception-after-network",
        'logger.exception("Error calculating cost")',
    ): _METRICS_PRICING_ONLY,
    (
        "web/routers/metrics.py",
        "api_cost_analytics",
        "exception-after-network",
        'logger.exception("Error getting cost analytics")',
    ): _METRICS_DB_ONLY,
    (
        "web/routers/metrics.py",
        "api_link_analytics",
        "exception-after-network",
        'logger.exception("Error getting link analytics")',
    ): _METRICS_DB_ONLY,
    (
        "web/routers/metrics.py",
        "api_journal_data_download",
        "exception-after-network",
        'logger.exception("Error downloading journal data")',
    ): _METRICS_JOURNAL_FETCH,
    (
        "web/routers/metrics.py",
        "api_journal_quality",
        "exception-after-network",
        'logger.exception("Error getting journal quality data")',
    ): _METRICS_DB_ONLY,
    (
        "web/routers/metrics.py",
        "api_user_research_journals",
        "exception-after-network",
        'logger.exception("Error getting user research journals")',
    ): _METRICS_DB_ONLY,
    (
        "web/routers/metrics.py",
        "api_research_journals",
        "exception-after-network",
        'logger.exception("Error getting per-research journals")',
    ): _METRICS_DB_ONLY,
}


def _call_name(node: ast.AST) -> str | None:
    if isinstance(node, ast.Name):
        return node.id
    if isinstance(node, ast.Attribute):
        return node.attr
    return None


def _logger_level(call: ast.Call) -> str | None:
    """``logger.X(...)``, ``logger.opt(...).X(...)``, ``logger.bind().X``."""
    func = call.func
    if not isinstance(func, ast.Attribute):
        return None
    base = func.value
    while isinstance(base, (ast.Call, ast.Attribute)):
        base = base.func if isinstance(base, ast.Call) else base.value
    if isinstance(base, ast.Name) and base.id == "logger":
        return func.attr
    return None


def _attaches_traceback(call: ast.Call, level: str) -> bool:
    """``logger.exception`` or a receiver chain with
    ``opt(exception=X)`` where X is anything but a literal False/None:
    loguru then renders ``ExcType: str(e)`` whatever the final level."""
    if level == "exception":
        return True
    node = call.func.value
    while isinstance(node, (ast.Call, ast.Attribute)):
        if isinstance(node, ast.Call):
            if _call_name(node.func) == "opt":
                for kw in node.keywords:
                    if kw.arg == "exception" and not (
                        isinstance(kw.value, ast.Constant)
                        and kw.value.value in (False, None)
                    ):
                        return True
            node = node.func
        else:
            node = node.value
    return False


def _url_tainted(expr: ast.AST) -> bool:
    if isinstance(expr, ast.Call) and _call_name(expr.func) in SAFE_WRAPPERS:
        return False
    name = _call_name(expr)
    if name and URL_NAME.search(name) and not NOT_URL_NAME.search(name):
        return True
    return any(_url_tainted(child) for child in ast.iter_child_nodes(expr))


def _exc_tainted(expr: ast.AST, exc_names: frozenset) -> bool:
    if (
        isinstance(expr, ast.Call)
        and _call_name(expr.func) == "type"
        and len(expr.args) == 1
    ):
        return False  # type(e) / type(e).__name__
    if isinstance(expr, ast.Attribute) and expr.attr == "__class__":
        return False  # e.__class__.__name__
    if isinstance(expr, ast.Name) and expr.id in exc_names:
        return True
    return any(
        _exc_tainted(child, exc_names) for child in ast.iter_child_nodes(expr)
    )


def _try_makes_network_call(try_node: ast.Try) -> bool:
    for stmt in try_node.body:
        for node in ast.walk(stmt):
            if isinstance(node, ast.Call):
                name = _call_name(node.func)
                if name and NETWORK_CALL.match(name):
                    return True
    return False


Hit = tuple[str, str, str, int, str]


def scan_source(source: str, rel: str) -> list[Hit]:
    """Return ``(rel, qualname, rule, lineno, call_text)`` for each
    offending call; ``call_text`` is the call's whitespace-normalised
    source."""
    hits: list[Hit] = []

    def visit(node, qual, exc_names, networked_except):
        if isinstance(
            node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)
        ):
            qual = f"{qual}.{node.name}" if qual else node.name
            if not isinstance(node, ast.ClassDef):
                exc_names = frozenset()
                networked_except = False
        if isinstance(node, ast.Try):
            networked = _try_makes_network_call(node)
            for child in node.body + node.orelse + node.finalbody:
                visit(child, qual, exc_names, networked_except)
            for handler in node.handlers:
                names = exc_names | (
                    {handler.name} if handler.name else frozenset()
                )
                for child in handler.body:
                    visit(child, qual, frozenset(names), networked)
            return
        if isinstance(node, ast.Call):
            level = _logger_level(node)
            if level in ALL_LEVELS:
                text = " ".join(
                    (ast.get_source_segment(source, node) or "").split()
                )
                parts = list(node.args) + [kw.value for kw in node.keywords]
                if any(_url_tainted(p) for p in parts):
                    hits.append((rel, qual, "raw-url", node.lineno, text))
                if exc_names and any(_exc_tainted(p, exc_names) for p in parts):
                    hits.append((rel, qual, "exc-text", node.lineno, text))
                if networked_except and _attaches_traceback(node, level):
                    hits.append(
                        (
                            rel,
                            qual,
                            "exception-after-network",
                            node.lineno,
                            text,
                        )
                    )
        for child in ast.iter_child_nodes(node):
            visit(child, qual, exc_names, networked_except)

    visit(ast.parse(source), "", frozenset(), False)
    return hits


def unallowed(hits: list[Hit], allowed=None) -> list[str]:
    """Hits not exempted by an exact ``ALLOWED`` key."""
    allowed = ALLOWED if allowed is None else allowed
    return [
        f"{rel}:{lineno} {qual} [{rule}] {text}"
        for rel, qual, rule, lineno, text in hits
        if (rel, qual, rule, text) not in allowed
    ]


def structural_offenders() -> list[str]:
    root = _src_root()
    offenders: list[str] = []
    for rel in SCANNED_RELATIVE:
        target = root / rel
        modules = sorted(target.rglob("*.py")) if target.is_dir() else [target]
        for module_path in modules:
            module_rel = module_path.relative_to(root).as_posix()
            hits = scan_source(
                module_path.read_text(encoding="utf-8"), module_rel
            )
            offenders += unallowed(hits)
    return offenders


@pytest.fixture
def captured_logs():
    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)


#: Query-secret variant for the SSRF-rejection probe.
SECRET_URL = "https://user:[email protected]:9001/p?token=T0PSECRET"

#: Userinfo-secret variant for the extraction/conversion failure
#: probes: bare of path/query tokens so the id extractors fail before
#: any fetch attempt. (Their progress lines are redacted too; see
#: ``TestAttemptLinesAreRedacted``.)
SECRET_PLAIN_URL = "https://user:[email protected]:9001/"

#: A public-looking credentialed URL: passes SSRF validation (so the
#: request reaches the mocked transport), carries both secret kinds.
PUBLIC_SECRET_URL = (
    "https://user:[email protected]:8443/a.pdf?token=T0PSECRET"
)


def _assert_no_secret(captured: list[str]) -> str:
    joined = "\n".join(captured)
    assert "hunter2-secret" not in joined, joined[-800:]
    assert "T0PSECRET" not in joined, joined[-800:]
    return joined


def _quiet_rate_tracker(downloader) -> None:
    downloader.rate_tracker = MagicMock()
    downloader.rate_tracker.apply_rate_limit.return_value = 0.0


def _base_download_pdf(url: str):
    """Drive ``BaseDownloader._download_pdf`` directly, isolating the
    base fetch's failure handling from the subclass override's progress
    line (pinned separately in ``TestAttemptLinesAreRedacted``)."""
    from local_deep_research.research_library.downloaders.base import (
        BaseDownloader,
    )
    from local_deep_research.research_library.downloaders.direct_pdf import (
        DirectPDFDownloader,
    )

    downloader = DirectPDFDownloader()
    _quiet_rate_tracker(downloader)
    return downloader, lambda: BaseDownloader._download_pdf(downloader, url)


class TestStructural:
    def test_no_failure_path_logs_a_raw_url_or_exception_text(self):
        offenders = structural_offenders()
        assert offenders == [], (
            "failure-path log calls interpolate a raw URL or an exception "
            "text, or attach a traceback (logger.exception / "
            "opt(exception=...)) after a network-shaped call, in "
            f"SCANNED_RELATIVE (residuals: module docstring): {offenders}"
        )

    def test_allowlist_entries_are_live(self):
        """A stale ALLOWED entry would silently exempt a future offender."""
        root = _src_root()
        seen = set()
        for rel in {key[0] for key in ALLOWED}:
            source = (root / rel).read_text(encoding="utf-8")
            seen.update(
                (r, q, rule, text)
                for r, q, rule, _, text in scan_source(source, rel)
            )
        assert set(ALLOWED) <= seen, set(ALLOWED) - seen

    @pytest.mark.parametrize(
        "key", sorted(ALLOWED), ids=lambda key: f"{key[1]}:{key[2]}"
    )
    def test_allowlist_does_not_exempt_a_new_call_in_the_same_function(
        self, key
    ):
        """Plant a second offending call of the same rule next to each
        reviewed call, in the real module source: it must be reported
        although its (module, function, rule) matches the entry."""
        rel, qual, rule, _text = key
        source = (_src_root() / rel).read_text(encoding="utf-8")
        tree = ast.parse(source)
        lineno = next(
            h[3] for h in scan_source(source, rel) if h[:3] + h[4:] == key
        )
        handler = next(
            node
            for node in ast.walk(tree)
            if isinstance(node, ast.ExceptHandler)
            and node.lineno <= lineno <= node.end_lineno
        )
        planted = {
            "exc-text": f'logger.warning(f"planted {{{handler.name}}}")',
            "exception-after-network": 'logger.exception("planted")',
            "raw-url": 'logger.warning(f"planted {url}")',
        }[rule]
        lines = source.splitlines(keepends=True)
        target = lines[lineno - 1]
        indent = target[: len(target) - len(target.lstrip())]
        lines.insert(lineno - 1, f"{indent}{planted}\n")
        mutated = "".join(lines)

        assert unallowed(scan_source(source, rel)) == []
        reported = unallowed(scan_source(mutated, rel))
        assert len(reported) == 1 and "planted" in reported[0], reported

    @pytest.mark.parametrize(
        "snippet",
        [
            'logger.warning(f"x {url}")',
            "logger.warning(f'x {url}')",
            'logger.error(rf"x {url}")',
            'logger.warning("x {}", url)',
            'logger.warning("x {u}", u=url)',
            'logger.warning(f"x {url!r}")',
            'logger.warning(f"x {url=}")',
            'logger.warning(f"x {pdf_url}")',
            'logger.warning(f"x {self.base_url}")',
            'logger.warning(f"x {url[:50]}")',
            'logger.warning(f"x {str(url)}")',
            'logger.warning(f"x {resource.url}")',
            'logger.info(f"x {url}")',
            'logger.debug(f"x {url}")',
            'logger.trace("x {}", pdf_url)',
            'logger.success(f"x {landing_url}")',
            'logger.critical(f"x {url}")',
            'logger.log("WARNING", f"x {url}")',
            'logger.opt(depth=1).warning(f"x {url}")',
            'logger.bind(a=1).error("x {}", redirect_url)',
            'try:\n    f()\nexcept Exception as e:\n    logger.debug(f"x {e}")',
            "try:\n    f()\nexcept Exception as exc:\n"
            '    logger.warning("x {}", exc)',
            "try:\n    f()\nexcept Exception as e:\n"
            '    logger.warning(f"x {str(e)}")',
            "try:\n    session.get(u)\nexcept Exception:\n"
            '    logger.exception("failed")',
            "try:\n    session.get(u)\nexcept Exception:\n"
            '    logger.opt(exception=True).error("failed")',
            "try:\n    session.get(u)\nexcept Exception as e:\n"
            '    logger.opt(exception=e).warning("failed")',
            "try:\n    session.get(u)\nexcept Exception as e:\n"
            '    logger.opt(exception=e).log("ERROR", "failed")',
            "try:\n    session.get(u)\nexcept Exception as e:\n"
            '    logger.bind(a=1).opt(exception=e).debug("failed")',
            "try:\n    _probe_tags(u)\nexcept Exception:\n"
            '    logger.exception("failed")',
        ],
    )
    def test_scanner_flags_each_leak_shape(self, snippet):
        """Teeth: every shape the docstring claims is actually detected."""
        source = "def f():\n" + "".join(
            f"    {line}\n" for line in snippet.splitlines()
        )
        assert scan_source(source, "probe.py"), snippet

    @pytest.mark.parametrize(
        "snippet",
        [
            'logger.warning(f"x {redact_url_for_log(url)}")',
            'logger.warning("x {}", ssrf_validator.redact_url_for_log(url))',
            'logger.info(f"x {redact_url_for_log(url)}")',
            'logger.debug(f"x {url_type.value}")',
            "try:\n    f()\nexcept Exception as e:\n"
            '    logger.warning(f"x {type(e).__name__}")',
            "try:\n    parse()\nexcept Exception:\n"
            '    logger.exception("failed")',
            "try:\n    session.get(u)\nexcept Exception as e:\n"
            '    logger.opt(exception=False).warning(f"x {type(e).__name__}")',
            "try:\n    session.get(u)\nexcept Exception:\n"
            '    logger.opt(exception=None).error("failed")',
            "try:\n    session.get(u)\nexcept Exception:\n"
            '    logger.opt(depth=1).error("failed")',
        ],
    )
    def test_scanner_passes_safe_shapes(self, snippet):
        source = "def f():\n" + "".join(
            f"    {line}\n" for line in snippet.splitlines()
        )
        assert scan_source(source, "probe.py") == [], snippet


class TestRedactHelperIsTotal:
    """The helper runs inside failure handlers — it must never raise."""

    @pytest.mark.parametrize(
        ("value", "expected"),
        [
            (123, "<unparseable:int>"),
            (b"x", "<unparseable:bytes>"),
            ([], "<unparseable:list>"),
            (None, "<unparseable:NoneType>"),
        ],
    )
    def test_non_string_input_returns_placeholder(self, value, expected):
        from local_deep_research.security import redact_url_for_log

        assert redact_url_for_log(value) == expected

    def test_schemeless_userinfo_does_not_leak_username(self):
        from local_deep_research.security import redact_url_for_log

        # urllib3 reads "user" as the scheme when there is no "//".
        result = redact_url_for_log("user:[email protected]/p")
        assert "user" not in result and "hunter2" not in result
        assert result == "?://<no-host>"

    def test_is_downloadable_domain_non_string_is_false(self):
        from local_deep_research.research_library.utils import (
            is_downloadable_domain,
        )

        assert is_downloadable_domain(123) is False

    def test_direct_pdf_can_handle_non_string_is_false(self):
        from local_deep_research.research_library.downloaders.direct_pdf import (
            DirectPDFDownloader,
        )

        assert DirectPDFDownloader().can_handle(123) is False


class TestBehavioralSeams:
    def test_arxiv_id_extraction_failure_redacts(self, captured_logs):
        from local_deep_research.research_library.downloaders.arxiv import (
            ArxivDownloader,
        )

        downloader = ArxivDownloader()
        with patch.object(downloader, "_extract_arxiv_id", return_value=None):
            result = downloader._download_pdf(SECRET_PLAIN_URL)

        assert result is None  # arrival: the failure branch ran
        joined = _assert_no_secret(captured_logs)
        assert "https://127.0.0.1:9001" in joined, joined[-400:]

    def test_biorxiv_conversion_failure_redacts(self, captured_logs):
        from local_deep_research.research_library.downloaders.biorxiv import (
            BioRxivDownloader,
        )

        downloader = BioRxivDownloader()
        with patch.object(downloader, "_convert_to_pdf_url", return_value=None):
            result = downloader._download_pdf(SECRET_PLAIN_URL)

        assert result is None  # arrival: the failure branch ran
        joined = _assert_no_secret(captured_logs)
        assert "https://127.0.0.1:9001" in joined, joined[-400:]

    def test_api_ssrf_rejection_redacts(self, captured_logs, monkeypatch):
        import asyncio

        from local_deep_research.web.routers.api import api_add_resource

        class _FakeRequest:
            headers = {"content-type": "application/json"}

            async def json(self):
                return {"url": SECRET_URL, "title": "probe"}

        # Force the SSRF rejection branch: the validator is imported
        # inside the route, so patch it at its home module.
        monkeypatch.setattr(
            "local_deep_research.security.ssrf_validator.validate_url",
            lambda _url: False,
        )
        response = asyncio.run(
            api_add_resource(_FakeRequest(), research_id="r1", username="u")
        )

        assert response.status_code == 400
        joined = _assert_no_secret(captured_logs)
        assert "https://127.0.0.1:9001" in joined, joined[-400:]


class TestExceptionTextNeverReachesTheLog:
    """The formatted record — message *and* exception section — must not
    carry the URL that the raised exception's text embeds."""

    def test_safesession_refusal_valueerror(self, captured_logs):
        """SafeSession raises ``ValueError("URL failed security validation
        (possible SSRF): <url>")`` for a private target."""
        _downloader, run = _base_download_pdf(SECRET_URL)
        result = run()

        assert result is None
        joined = _assert_no_secret(captured_logs)
        assert "ValueError" in joined  # arrival: the handler logged
        assert "https://127.0.0.1:9001" in joined, joined[-400:]

    @pytest.mark.parametrize(
        "exc",
        [
            requests.exceptions.ConnectionError(
                "HTTPSConnectionPool(host='papers.example.com', port=8443): "
                "Max retries exceeded with url: /a.pdf?token=T0PSECRET "
                f"({PUBLIC_SECRET_URL})"
            ),
            requests.exceptions.HTTPError(
                f"403 Client Error: Forbidden for url: {PUBLIC_SECRET_URL}"
            ),
            RuntimeError(f"unexpected failure fetching {PUBLIC_SECRET_URL}"),
        ],
        ids=["connection-error", "http-error", "unexpected"],
    )
    def test_base_download_pdf_request_failures(self, captured_logs, exc):
        downloader, run = _base_download_pdf(PUBLIC_SECRET_URL)
        with patch.object(downloader.session, "get", side_effect=exc) as get:
            result = run()

        assert result is None
        assert get.called  # arrival: the transport was reached
        joined = _assert_no_secret(captured_logs)
        assert type(exc).__name__ in joined, joined[-400:]
        assert "https://papers.example.com:8443" in joined, joined[-400:]

    def test_html_fetch_failure(self, captured_logs):
        from local_deep_research.research_library.downloaders.html import (
            HTMLDownloader,
        )

        downloader = HTMLDownloader()
        _quiet_rate_tracker(downloader)
        exc = requests.exceptions.ConnectionError(
            f"Max retries exceeded with url: {PUBLIC_SECRET_URL}"
        )
        with patch.object(downloader.session, "get", side_effect=exc):
            result = downloader.download(PUBLIC_SECRET_URL)

        assert result is None
        joined = _assert_no_secret(captured_logs)
        assert "ConnectionError" in joined, joined[-400:]

    def test_download_service_generic_http_error(self, captured_logs):
        from local_deep_research.research_library.services import (
            download_service as ds,
        )

        response = MagicMock()
        response.raise_for_status.side_effect = requests.exceptions.HTTPError(
            f"404 Client Error: Not Found for url: {PUBLIC_SECRET_URL}"
        )
        service = ds.DownloadService.__new__(ds.DownloadService)
        with patch.object(ds, "safe_get", return_value=response):
            result = service._download_generic(PUBLIC_SECRET_URL)

        assert result is None
        joined = _assert_no_secret(captured_logs)
        assert "HTTPError" in joined, joined[-400:]
        assert "https://papers.example.com:8443" in joined, joined[-400:]

    def test_rate_limit_key_drops_userinfo(self):
        from local_deep_research.research_library.downloaders.base import (
            rate_limit_authority,
        )

        assert (
            rate_limit_authority(PUBLIC_SECRET_URL) == "papers.example.com:8443"
        )
        assert rate_limit_authority("https://example.com/x") == "example.com"


class TestAttemptLinesAreRedacted:
    """The INFO/DEBUG attempt lines run before every fetch — including
    the ones that fail — so they must carry the redacted URL only."""

    @staticmethod
    def _failing_transport(downloader):
        _quiet_rate_tracker(downloader)
        exc = requests.exceptions.ConnectionError("refused")
        return (
            patch.object(downloader.session, "get", side_effect=exc),
            patch.object(downloader.session, "head", side_effect=exc),
        )

    @pytest.mark.parametrize(
        ("module", "cls", "method"),
        [
            ("direct_pdf", "DirectPDFDownloader", "download_with_result"),
            ("direct_pdf", "DirectPDFDownloader", "_download_pdf"),
            ("generic", "GenericDownloader", "download_with_result"),
            ("generic", "GenericDownloader", "_download_pdf"),
        ],
    )
    def test_downloader_attempt_lines(self, captured_logs, module, cls, method):
        import importlib

        mod = importlib.import_module(
            f"local_deep_research.research_library.downloaders.{module}"
        )
        downloader = getattr(mod, cls)()
        # No ".pdf" suffix: GenericDownloader also logs its ".pdf" retry.
        url = "https://user:[email protected]:8443/a?token=T0PSECRET"
        get_patch, head_patch = self._failing_transport(downloader)
        with get_patch as get, head_patch:
            getattr(downloader, method)(url)

        assert get.called  # arrival: a fetch was attempted
        joined = _assert_no_secret(captured_logs)
        assert "https://papers.example.com:8443" in joined, joined[-400:]

    def test_biorxiv_attempt_line(self, captured_logs):
        from local_deep_research.research_library.downloaders.biorxiv import (
            BioRxivDownloader,
        )

        downloader = BioRxivDownloader()
        get_patch, head_patch = self._failing_transport(downloader)
        with (
            patch.object(
                downloader,
                "_convert_to_pdf_url",
                return_value=PUBLIC_SECRET_URL,
            ),
            get_patch as get,
            head_patch,
        ):
            downloader._download_pdf(PUBLIC_SECRET_URL)

        assert get.called
        joined = _assert_no_secret(captured_logs)
        assert "bioRxiv/medRxiv PDF from https://papers.example.com:8443" in (
            joined
        ), joined[-400:]


class TestRateLimitKeyCallSite:
    """The rate-limit key is persisted and logged: the call sites must
    feed it through ``rate_limit_authority``, not ``urlparse().netloc``."""

    def test_html_fetch_engine_type_has_no_userinfo(self):
        from local_deep_research.research_library.downloaders.html import (
            HTMLDownloader,
        )

        downloader = HTMLDownloader()
        _quiet_rate_tracker(downloader)
        exc = requests.exceptions.ConnectionError("refused")
        with patch.object(downloader.session, "get", side_effect=exc):
            downloader.download(PUBLIC_SECRET_URL)

        tracker = downloader.rate_tracker
        engine_types = [
            c.kwargs.get("engine_type", c.args[0] if c.args else None)
            for c in tracker.apply_rate_limit.call_args_list
            + tracker.record_outcome.call_args_list
        ]
        assert engine_types, "rate tracker was never consulted"
        assert all(
            et == "html_download_papers.example.com:8443" for et in engine_types
        ), engine_types

    def test_base_pdf_engine_type_has_no_userinfo(self):
        downloader, run = _base_download_pdf(PUBLIC_SECRET_URL)
        exc = requests.exceptions.ConnectionError("refused")
        with patch.object(downloader.session, "get", side_effect=exc):
            run()

        calls = downloader.rate_tracker.apply_rate_limit.call_args_list
        assert calls, "rate tracker was never consulted"
        for c in calls:
            engine_type = c.kwargs.get(
                "engine_type", c.args[0] if c.args else ""
            )
            assert "hunter2-secret" not in engine_type
            assert "user" not in engine_type
            assert "papers.example.com:8443" in engine_type


#: Presigned + credentialed URL for the safe_requests raise sites.
PRESIGNED_URL = (
    "https://usr-SECRET:[email protected]/p/path-SECRET"
    "?X-Amz-Signature=SIG-SECRET&token=TOK-SECRET"
)
PRESIGNED_SECRETS = (
    "usr-SECRET",
    "pw-SECRET",
    "path-SECRET",
    "SIG-SECRET",
    "TOK-SECRET",
)


class _FakeResponse:
    def __init__(self, status_code=200, location=None, url=None):
        self.status_code = status_code
        self.headers = {"Location": location} if location else {}
        self.url = url
        self.closed = False

    def close(self):
        self.closed = True


class TestSafeRequestsExceptionTexts:
    """``safe_requests`` exception messages flow into caller logs
    (``ExcType: str(e)``) and DB error columns: each raise site must name
    only ``scheme://host[:port]`` of the refused URL."""

    @staticmethod
    def _assert_redacted(message: str, host: str = "https://evil.example"):
        for secret in PRESIGNED_SECRETS:
            assert secret not in message, message
        assert host in message, message

    @pytest.fixture
    def sr(self, monkeypatch):
        from contextlib import nullcontext

        from local_deep_research.security import safe_requests

        monkeypatch.setattr(
            safe_requests.dns_pinning,
            "pinned_request",
            lambda *a, **k: nullcontext(),
        )
        return safe_requests

    @staticmethod
    def _validator(monkeypatch, sr, refuse=lambda url: True):
        monkeypatch.setattr(
            sr.ssrf_validator,
            "validate_url",
            lambda url, *a, **k: not refuse(url),
        )

    @pytest.mark.parametrize("func", ["safe_get", "safe_post"])
    def test_initial_validation_refusal(self, sr, monkeypatch, func):
        self._validator(monkeypatch, sr)
        with pytest.raises(ValueError, match="possible SSRF") as excinfo:
            getattr(sr, func)(PRESIGNED_URL)
        self._assert_redacted(str(excinfo.value))

    @staticmethod
    def _transport(monkeypatch, sr, response):
        """Stub both verbs: a 302 hop out of safe_post continues as GET."""
        for verb in ("get", "post"):
            monkeypatch.setattr(sr.requests, verb, lambda *a, **k: response)

    @pytest.mark.parametrize("func", ["safe_get", "safe_post"])
    def test_redirect_target_refusal(self, sr, monkeypatch, func):
        self._validator(monkeypatch, sr, refuse=lambda url: "evil" in url)
        self._transport(
            monkeypatch, sr, _FakeResponse(302, location=PRESIGNED_URL)
        )
        with pytest.raises(ValueError, match="Redirect target") as excinfo:
            getattr(sr, func)("https://origin.example/start")
        self._assert_redacted(str(excinfo.value))

    def test_require_https_initial_refusal(self, sr):
        with pytest.raises(ValueError, match="must use https") as excinfo:
            sr.safe_get(
                PRESIGNED_URL.replace("https://", "http://"),
                require_https=True,
            )
        self._assert_redacted(str(excinfo.value), host="http://evil.example")

    def test_require_https_redirect_downgrade_refusal(self, sr, monkeypatch):
        self._validator(monkeypatch, sr, refuse=lambda url: False)
        self._transport(
            monkeypatch,
            sr,
            _FakeResponse(
                302, location=PRESIGNED_URL.replace("https://", "http://")
            ),
        )
        with pytest.raises(ValueError, match="downgrade") as excinfo:
            sr.safe_get("https://origin.example/start", require_https=True)
        self._assert_redacted(str(excinfo.value), host="http://evil.example")

    @pytest.mark.parametrize("func", ["safe_get", "safe_post"])
    def test_too_many_redirects(self, sr, monkeypatch, func):
        self._validator(monkeypatch, sr, refuse=lambda url: False)
        self._transport(
            monkeypatch, sr, _FakeResponse(302, location="/p/path-SECRET/n")
        )
        with pytest.raises(ValueError, match="Too many redirects") as excinfo:
            getattr(sr, func)(PRESIGNED_URL)
        self._assert_redacted(str(excinfo.value))

    def test_safe_post_body_scope_refusal(self, sr, monkeypatch):
        self._validator(monkeypatch, sr, refuse=lambda url: False)
        self._transport(
            monkeypatch, sr, _FakeResponse(307, location=PRESIGNED_URL)
        )
        with pytest.raises(ValueError, match="outside") as excinfo:
            sr.safe_post("https://origin.example/submit", data="body")
        self._assert_redacted(str(excinfo.value))

    def test_session_request_refusal(self, sr, monkeypatch):
        self._validator(monkeypatch, sr)
        with pytest.raises(ValueError, match="possible SSRF") as excinfo:
            sr.SafeSession().request("GET", PRESIGNED_URL)
        self._assert_redacted(str(excinfo.value))

    def test_session_send_refusal(self, sr, monkeypatch):
        self._validator(monkeypatch, sr)
        prepared = requests.Request("GET", PRESIGNED_URL).prepare()
        with pytest.raises(ValueError, match="Redirect target") as excinfo:
            sr.SafeSession().send(prepared)
        self._assert_redacted(str(excinfo.value))

    def test_session_rebuild_auth_body_refusal(self, sr):
        prepared = requests.Request(
            "POST", PRESIGNED_URL, data="body"
        ).prepare()
        response = MagicMock()
        response.request.url = "https://origin.example/submit"
        with pytest.raises(ValueError, match="outside") as excinfo:
            sr.SafeSession().rebuild_auth(prepared, response)
        self._assert_redacted(str(excinfo.value))


#: What ``DownloadService``'s exception path returns for a request
#: failure: ``sanitize_error_for_client(str(e))`` strips credential
#: shapes but keeps the scheme-less path and query of urllib3's text.
PRESIGNED_REASON = (
    "HTTPSConnectionPool(host='bucket.example', port=443): Max retries "
    "exceeded with url: /a.pdf?X-Amz-Signature=T0PSECRET&sig=T0PSECRET"
)


def _reason_log_names_unwrapped(source: str, names: set[str]) -> list[int]:
    """Line numbers of logger calls that reference one of *names* outside
    a ``failure_reason_for_log(...)`` call. A dotted name
    (``filter_result.reason``) matches that attribute access."""
    lines: list[int] = []

    def walk(node):
        if (
            isinstance(node, ast.Call)
            and _call_name(node.func) == "failure_reason_for_log"
        ):
            return
        if isinstance(node, ast.Name) and node.id in names:
            lines.append(node.lineno)
        if isinstance(node, ast.Attribute) and ast.unparse(node) in names:
            lines.append(node.lineno)
        for child in ast.iter_child_nodes(node):
            walk(child)

    for node in ast.walk(ast.parse(source)):
        if isinstance(node, ast.Call) and _logger_level(node) in ALL_LEVELS:
            for part in list(node.args) + [kw.value for kw in node.keywords]:
                walk(part)
    return lines


class TestFailureReasonLogging:
    """``download_resource`` / ``download_as_text`` failure reasons can be
    exception text that keeps a presigned query; every log line that
    repeats them goes through ``failure_reason_for_log``."""

    @pytest.mark.parametrize(
        "reason",
        [
            PRESIGNED_REASON,
            "Invalid URL 'user:[email protected]': No scheme",
            "Failed to extract arXiv text: x?token=T0PSECRET",
            "bad fragment #T0PSECRET",
            "path;param=T0PSECRET",
            "C:\\T0PSECRET",
        ],
    )
    def test_helper_withholds_url_bearing_reasons(self, reason):
        from local_deep_research.security import failure_reason_for_log
        from local_deep_research.security.log_sanitizer import (
            FAILURE_REASON_WITHHELD,
        )

        assert failure_reason_for_log(reason) == FAILURE_REASON_WITHHELD

    @pytest.mark.parametrize(
        ("reason", "expected"),
        [
            (
                "egress_policy_denied:private_ip",
                "egress_policy_denied:private_ip",
            ),
            (
                "No compatible downloader available",
                "No compatible downloader available",
            ),
            (None, "None"),
            (42, "<int>"),
        ],
    )
    def test_helper_keeps_categories(self, reason, expected):
        from local_deep_research.security import failure_reason_for_log

        assert failure_reason_for_log(reason) == expected

    @pytest.mark.parametrize(
        "reason",
        [
            # generic.py, with a server-sent content type
            "Unexpected content type: text/html - expected PDF",
            # pubmed.py / biorxiv.py fixed literals
            "Full text not available from the PubMed/PMC APIs",
            "Article not found on bioRxiv/medRxiv",
        ],
    )
    def test_helper_withholds_fixed_reasons_with_a_delimiter(self, reason):
        """Fail-closed on shape, not origin: a fixed, URL-free reason that
        contains one of ``/ \\ ? # @ ;`` is withheld as well. Pins that
        trade-off so it cannot be relaxed into a URL-detection heuristic
        without a deliberate test change."""
        from local_deep_research.security import failure_reason_for_log
        from local_deep_research.security.log_sanitizer import (
            FAILURE_REASON_WITHHELD,
        )

        assert failure_reason_for_log(reason) == FAILURE_REASON_WITHHELD

    @pytest.mark.parametrize(
        ("rel", "names"),
        [
            (
                "web/routers/library.py",
                {"error", "skip_reason", "error_msg", "filter_result.reason"},
            ),
            (
                "library/download_management/failure_classifier.py",
                {"details"},
            ),
            (
                "research_library/services/download_service.py",
                {"skip_reason", "error_msg", "error", "retry_decision.reason"},
            ),
            (
                "scheduler/background.py",
                {"error"},
            ),
            (
                "research_library/downloaders/extraction/pipeline.py",
                {
                    "skip_reason",
                    "specialized.skip_reason",
                    "failure.skip_reason",
                },
            ),
        ],
    )
    def test_reason_log_sites_are_wrapped(self, rel, names):
        source = (_src_root() / rel).read_text(encoding="utf-8")
        assert _reason_log_names_unwrapped(source, names) == []

    def test_reason_scan_flags_an_unwrapped_site(self):
        source = 'logger.warning(f"failed: {error}")\n'
        assert _reason_log_names_unwrapped(source, {"error"}) == [1]

    def test_reason_scan_flags_an_unwrapped_attribute(self):
        source = (
            'logger.info("skip {}: {}", rid, decision.reason)\n'
            'logger.info(f"ok: {failure_reason_for_log(decision.reason)}")\n'
        )
        assert _reason_log_names_unwrapped(source, {"decision.reason"}) == [1]

    @staticmethod
    def _download_context(method: str, result):
        service = MagicMock()
        getattr(service, method).return_value = result
        context = MagicMock()
        context.__enter__.return_value = service
        context.__exit__.return_value = False
        return context

    @staticmethod
    def _request(path: str):
        from starlette.requests import Request

        return Request(
            {
                "type": "http",
                "method": "POST",
                "path": path,
                "headers": [],
                "query_string": b"",
            }
        )

    @pytest.mark.parametrize(
        ("route", "method"),
        [
            ("download_single_resource", "download_resource"),
            ("download_text_single", "download_as_text"),
        ],
    )
    def test_single_download_routes_withhold_reason(
        self, captured_logs, route, method
    ):
        from local_deep_research.web.routers import library as library_module

        context = self._download_context(method, (False, PRESIGNED_REASON))
        with (
            patch.object(
                library_module,
                "get_authenticated_user_password",
                return_value="pw",
            ),
            patch.object(
                library_module, "DownloadService", return_value=context
            ),
        ):
            getattr(library_module, route)(
                self._request("/library/api/download/7"),
                resource_id=7,
                username="alice",
            )

        joined = _assert_no_secret(captured_logs)
        # arrival: the failure line ran and named the resource
        assert "failed for resource 7" in joined, joined[-400:]

    @pytest.mark.parametrize("path", ["fallback", "single", "batch"])
    def test_pipeline_reasons_are_withheld(
        self, captured_logs, monkeypatch, path
    ):
        from local_deep_research.research_library.downloaders.base import (
            DownloadResult,
        )
        from local_deep_research.research_library.downloaders.extraction import (
            pipeline,
        )
        from tests.test_utils import restored_loguru_state

        url = "https://pubmed.ncbi.nlm.nih.gov/1/"
        with restored_loguru_state():
            # Assert the call sites themselves protect the reason, even
            # without the application-wide logging redaction patcher.
            logger.configure(patcher=lambda record: None)
            logger.add(
                lambda message: captured_logs.append(str(message)),
                level="DEBUG",
            )
            logger.info("redaction canary {}", PRESIGNED_REASON)
            assert "T0PSECRET" in "\n".join(captured_logs)
            captured_logs.clear()
            if path == "fallback":
                downloader = MagicMock()
                downloader.download_with_result.return_value = DownloadResult(
                    skip_reason=PRESIGNED_REASON
                )
                monkeypatch.setattr(
                    "local_deep_research.research_library.downloaders.pubmed.PubMedDownloader",
                    lambda **kwargs: downloader,
                )
                result = pipeline._try_specialized_downloader(url)
                assert result.fallback_allowed is True
                assert result.skip_reason == PRESIGNED_REASON
                expected_log = "falling back to HTML pipeline"
            else:
                monkeypatch.setattr(
                    pipeline,
                    "_try_specialized_downloader",
                    lambda *args, **kwargs: pipeline._SpecializedResult(
                        fallback_allowed=False, skip_reason=PRESIGNED_REASON
                    ),
                )
                if path == "single":
                    assert pipeline.fetch_and_extract(url) is None
                else:
                    assert pipeline.batch_fetch_and_extract([url]) == {
                        url: None
                    }
                expected_log = "terminal specialized download failed"

            joined = _assert_no_secret(captured_logs)
            assert expected_log in joined
            assert "<withheld: may embed a URL>" in joined

    def test_classifier_unclassified_warning_withholds_reason(
        self, captured_logs
    ):
        from local_deep_research.library.download_management.failure_classifier import (
            FailureClassifier,
        )

        failure = FailureClassifier().classify_failure(
            error_type="str", details=PRESIGNED_REASON
        )

        assert failure.error_type == "unknown_error"
        joined = _assert_no_secret(captured_logs)
        assert "Unclassified error" in joined, joined[-400:]

    def test_classifier_rate_limit_domain_drops_userinfo(self, captured_logs):
        from local_deep_research.library.download_management.failure_classifier import (
            FailureClassifier,
        )

        failure = FailureClassifier().classify_failure(
            error_type="http_error", status_code=429, url=PUBLIC_SECRET_URL
        )

        assert failure.domain == "papers.example.com:8443"
        assert "hunter2-secret" not in failure.message
        _assert_no_secret(captured_logs)


class TestDomainKeyDropsUserinfo:
    """Domains derived from stored resource URLs carry no userinfo.

    ``urlparse(...).netloc`` keeps ``user:pass@``, so a credentialed
    resource URL used to put the credentials into the classifier's
    progress logs, its results payload and the persisted
    ``DomainClassification.domain`` key. The classifier's failure
    handlers log only the exception type, since their frames hold the
    raw resource URLs.
    """

    CLEAN_DOMAIN = "papers.example.com:8443"

    @staticmethod
    def _session(resource_rows):
        session = MagicMock()
        session.query.return_value.distinct.return_value.all.return_value = (
            resource_rows
        )
        session.query.return_value.filter_by.return_value.first.return_value = (
            None
        )
        session.query.return_value.filter.return_value.limit.return_value.all.return_value = []
        return session

    @staticmethod
    def _run(session, llm, force_update=True):
        from local_deep_research.domain_classifier import classifier as mod

        clf = mod.DomainClassifier(username="alice", settings_snapshot={})
        progress = []
        with (
            patch.object(mod, "get_user_db_session") as mock_gs,
            patch.object(clf, "_get_llm", return_value=llm),
        ):
            mock_gs.return_value.__enter__ = MagicMock(return_value=session)
            mock_gs.return_value.__exit__ = MagicMock(return_value=False)
            results = clf.classify_all_domains(
                force_update=force_update, progress_callback=progress.append
            )
        return results, progress

    def test_persisted_domain_and_logs_drop_userinfo(self, captured_logs):
        session = self._session([(PUBLIC_SECRET_URL,)])
        llm = MagicMock()
        llm.invoke.return_value = MagicMock(
            content='{"category": "Other", "subcategory": "Unknown", '
            '"confidence": 0.5, "reasoning": "r"}'
        )

        results, progress = self._run(session, llm)

        added = [c.args[0] for c in session.add.call_args_list]
        assert [a.domain for a in added] == [self.CLEAN_DOMAIN]
        assert results["classified"] == 1
        assert results["domains"][0]["domain"] == self.CLEAN_DOMAIN
        assert progress[0]["domain"] == self.CLEAN_DOMAIN
        assert "hunter2-secret" not in repr(results)
        # the LLM prompt names the domain, not the credentials
        assert "hunter2-secret" not in llm.invoke.call_args.args[0]
        joined = _assert_no_secret(captured_logs)
        assert f"Processing domain 1/1: {self.CLEAN_DOMAIN}" in joined

    def test_classify_failure_logs_type_only(self, captured_logs):
        session = self._session([(PUBLIC_SECRET_URL,)])
        llm = MagicMock()
        llm.invoke.side_effect = RuntimeError(f"boom {PUBLIC_SECRET_URL}")

        results, _ = self._run(session, llm)

        assert results["failed"] == 1
        joined = _assert_no_secret(captured_logs)
        assert "RuntimeError" in joined, joined[-400:]

    def test_per_domain_handler_logs_type_only(self, captured_logs):
        session = self._session([(PUBLIC_SECRET_URL,)])
        session.query.return_value.filter_by.return_value.first.side_effect = (
            RuntimeError(f"db echo {PUBLIC_SECRET_URL}")
        )

        results, _ = self._run(session, MagicMock(), force_update=False)

        assert results["failed"] == 1
        joined = _assert_no_secret(captured_logs)
        assert "RuntimeError" in joined, joined[-400:]

    def test_outer_handler_logs_type_only(self, captured_logs):
        session = MagicMock()
        session.query.return_value.distinct.return_value.all.side_effect = (
            RuntimeError(f"db echo {PUBLIC_SECRET_URL}")
        )

        results, _ = self._run(session, MagicMock())

        assert results["error"] == "Classification failed"
        joined = _assert_no_secret(captured_logs)
        assert "RuntimeError" in joined, joined[-400:]

    def test_metrics_extract_domain_matches_classifier_key(self):
        from local_deep_research.web.routers.metrics import _extract_domain

        assert _extract_domain(PUBLIC_SECRET_URL) == self.CLEAN_DOMAIN
        assert (
            _extract_domain("https://[email protected]/x") == "example.com"
        )

    def test_llm_rate_limit_key_drops_userinfo(self):
        from types import SimpleNamespace

        from local_deep_research.web_search_engines.rate_limiting.llm.wrapper import (
            create_rate_limited_llm_wrapper,
        )

        llm = SimpleNamespace(
            base_url="https://user:[email protected]:8443/v1",
            model_name="m",
        )
        key = create_rate_limited_llm_wrapper(
            llm, provider="openai"
        )._get_rate_limit_key()

        assert key == "openai-llm.example.com:8443-m"


class TestLibraryDomainsDropUserinfo:
    """The library page's domain filter, its research PDF previews and
    the collection search results show a domain derived from a stored
    URL; ``urlparse(...).netloc`` keeps ``user:pass@``, so each strips
    the userinfo before the domain reaches the UI or a JSON response."""

    CLEAN_DOMAIN = "papers.example.com:8443"

    @staticmethod
    def _library_service():
        from local_deep_research.research_library.services.library_service import (
            LibraryService,
        )

        with patch.object(
            LibraryService, "__init__", lambda self, username: None
        ):
            service = LibraryService.__new__(LibraryService)
        service.username = "alice"
        return service

    def test_unique_domains_drop_userinfo(self):
        from local_deep_research.research_library.services import (
            library_service as mod,
        )

        service = self._library_service()
        session = MagicMock()
        session.query.return_value.filter.return_value.yield_per.return_value = [
            (PUBLIC_SECRET_URL,),
            ("https://papers.example.com:8443/b.pdf",),
        ]
        with patch.object(mod, "get_user_db_session") as mock_gs:
            mock_gs.return_value.__enter__.return_value = session
            mock_gs.return_value.__exit__.return_value = False
            domains = service.get_unique_domains()

        assert domains == [self.CLEAN_DOMAIN]
        assert service._extract_domain(PUBLIC_SECRET_URL) == self.CLEAN_DOMAIN

    def test_domain_filter_still_matches_a_credentialed_url(self):
        """Filtering by the stripped domain finds the stored credentialed
        URL: the filter is a substring match on ``original_url``."""
        from sqlalchemy import Column, Integer, String, create_engine
        from sqlalchemy.orm import Session, declarative_base

        base = declarative_base()

        class Doc(base):
            __tablename__ = "doc"
            id = Column(Integer, primary_key=True)
            original_url = Column(String)

        engine = create_engine("sqlite://")
        base.metadata.create_all(engine)
        service = self._library_service()
        with Session(engine) as session:
            session.add_all(
                [
                    Doc(id=1, original_url=PUBLIC_SECRET_URL),
                    Doc(id=2, original_url="https://other.example/c.pdf"),
                ]
            )
            session.commit()
            domain = service._extract_domain(PUBLIC_SECRET_URL)
            query = service._apply_domain_filter(
                session.query(Doc.id), Doc, domain
            )
            assert [row.id for row in query.all()] == [1]

    def test_pdf_previews_batch_domains_drop_userinfo(self):
        from local_deep_research.research_library.services import (
            library_service as mod,
        )

        def doc(doc_id, url):
            return MagicMock(
                id=doc_id,
                research_id="r1",
                file_type="pdf",
                status="completed",
                original_url=url,
                filename="a.pdf",
            )

        resource = MagicMock(url=PUBLIC_SECRET_URL, title="A")
        rows = [
            (doc("d1", "https://x.example/unused"), resource),
            (doc("d2", PUBLIC_SECRET_URL), None),
        ]
        session = MagicMock()
        chain = session.query.return_value.outerjoin.return_value
        chain.filter.return_value.order_by.return_value.limit.return_value.all.return_value = rows
        service = self._library_service()
        with patch.object(mod, "get_user_db_session") as mock_gs:
            mock_gs.return_value.__enter__.return_value = session
            mock_gs.return_value.__exit__.return_value = False
            previews = service.get_pdf_previews_batch(["r1"])

        entry = previews["r1"]
        assert list(entry["domains"]) == [self.CLEAN_DOMAIN]
        assert entry["domains"][self.CLEAN_DOMAIN]["total"] == 2
        assert [s["domain"] for s in entry["pdf_sources"]] == [
            self.CLEAN_DOMAIN
        ] * 2
        assert "hunter2-secret" not in repr(previews)

    def test_collection_search_domain_drops_userinfo(self):
        from local_deep_research.web.routers.library_search import (
            _enrich_with_document_metadata,
        )

        row = MagicMock(
            document_id="d1",
            file_type="pdf",
            original_url=PUBLIC_SECRET_URL,
            created_at=None,
        )
        session = MagicMock()
        session.query.return_value.filter.return_value.all.return_value = [row]
        results = [{"document_id": "d1"}]
        with patch(
            "local_deep_research.database.session_context.get_user_db_session"
        ) as mock_gs:
            mock_gs.return_value.__enter__.return_value = session
            mock_gs.return_value.__exit__.return_value = False
            _enrich_with_document_metadata(results, "alice", "pw")

        assert results[0]["domain"] == self.CLEAN_DOMAIN
        assert "hunter2-secret" not in repr(results)


#: URLs whose userinfo holds an unencoded ``/``, ``?`` or ``#``: the
#: parser ends the netloc inside the userinfo, so ``netloc.rpartition("@")``
#: would keep ``user:<start of password>`` (or a delimiter-split username).
AMBIGUOUS_AUTHORITY_URLS = [
    "https://alice:hunter2-secret#[email protected]/a.pdf",
    "https://AKIAX:hunter2-secret/[email protected]/a.pdf",
    "https://alice:[email protected]/a.pdf",
    "https://hunter2-secret/[email protected]/a.pdf",
    "https://hunter2-secret:12#[email protected]/a.pdf",
]


class TestUrlAuthorityWithoutUserinfo:
    """``url_authority_without_userinfo`` is the one helper every domain
    and key derivation uses: ordinary URLs keep their exact
    ``urlparse(...).netloc`` (so persisted keys stay stable), userinfo is
    dropped, and an ambiguous or unparseable authority yields ``""``."""

    @pytest.mark.parametrize(
        "url",
        [
            "https://arxiv.org/abs/2301.00001",
            "https://EXAMPLE.com:8080/p",
            "https://www.example.com",
            "https://medium.com/@user/post",
            "http://localhost:8080/[email protected]",
            "http://10.0.0.1/a@b",
            "https://a.example?x=@y",
            "http://[::1]:8080/x",
            "http://intranet/",
            "https://host.example:/p",
        ],
    )
    def test_ordinary_urls_keep_the_parsed_netloc(self, url):
        from urllib.parse import urlparse

        from local_deep_research.security import (
            url_authority_without_userinfo,
        )

        assert url_authority_without_userinfo(url) == urlparse(url).netloc

    @pytest.mark.parametrize(
        ("url", "expected"),
        [
            (PUBLIC_SECRET_URL, "papers.example.com:8443"),
            ("https://u:p@[email protected]/", "papers.example.com"),
            ("https://u:hunter2-secret@[::1]:80/", "[::1]:80"),
            ("https://[email protected]/x", "www.Example.com"),
        ],
    )
    def test_userinfo_is_dropped(self, url, expected):
        from local_deep_research.security import (
            url_authority_without_userinfo,
        )

        assert url_authority_without_userinfo(url) == expected

    @pytest.mark.parametrize("url", AMBIGUOUS_AUTHORITY_URLS)
    def test_ambiguous_authority_fails_closed(self, url):
        from local_deep_research.security import (
            url_authority_without_userinfo,
        )

        assert url_authority_without_userinfo(url) == ""

    @pytest.mark.parametrize(
        "value",
        [
            None,
            b"https://x.example/",
            42,
            "",
            "not a url",
            "http://[::1/",
            "https://host.example:99999/",
            "http://intranet/@x",
        ],
    )
    def test_non_str_and_unparseable_yield_empty(self, value):
        from local_deep_research.security import (
            url_authority_without_userinfo,
        )

        assert url_authority_without_userinfo(value) == ""


#: Ambiguous authorities whose userinfo fragment parses as a host, paired
#: with the fragment that must not reach a log line or a key. The last
#: four carry a digits-only password prefix that parses as a *valid*
#: port, so only the "port followed by ``@``" rule catches them.
USERINFO_FRAGMENT_AS_HOST_URLS = [
    ("https://hunter2-secret/[email protected]/a.pdf", "hunter2"),
    ("https://hunter2-secret:12#[email protected]/a.pdf", "hunter2"),
    ("https://ghp_abcdefghij/[email protected]/x.pdf", "ghp_abcdefghij"),
    ("https://[email protected]/", "ghp_abcdefghij"),
    ("https://tok\\[email protected]/", "tok"),
    ("https://deploy:2024/[email protected]/x", "deploy"),
    ("https://x-access-token:1234#[email protected]/o/r", "x-access-token"),
    ("https://john.doe:1234/[email protected]/", "john.doe"),
    ("https://alice.smith:1234#[email protected]/", "alice.smith"),
    ("https://[email protected]:12#[email protected]/", "corp.example"),
]

#: A password holding an unencoded ``@`` followed by a dotted segment and a
#: ``/``, ``?`` or ``#``: the parser splits at the password's own ``@``, so
#: the "host" is the dotted tail of the password (no port, not a single
#: label). The parsed authority carries a userinfo *and* an ``@`` follows
#: it, which is what flags these -- for IP literals and ``localhost`` too.
PASSWORD_AT_DOTTED_TAIL_URLS = [
    ("https://admin:[email protected]#[email protected]/a.pdf", "ss.example"),
    (
        "https://admin:[email protected]/[email protected]/a.pdf",
        "corp.example",
    ),
    ("https://admin:[email protected][email protected]/a.pdf", "pass.example"),
    ("https://admin:[email protected]#[email protected]/a.pdf", "3.14"),
    ("https://admin:[email protected]#[email protected]/a.pdf", "127.0.0.1"),
    ("https://admin:pw@localhost/[email protected]/a.pdf", "localhost"),
    ("https://[email protected]/[email protected]/", "corp.example"),
]
USERINFO_FRAGMENT_AS_HOST_URLS += PASSWORD_AT_DOTTED_TAIL_URLS


class TestLogAndKeyHelpersShareAmbiguityRule:
    """``redact_url_for_log`` (every failure-path log line),
    ``url_authority_without_userinfo`` (domains and DB keys) and
    ``rate_limit_authority`` (adaptive rate-limit keys, which the tracker
    logs) apply the same ``authority_may_be_userinfo`` rule, so a
    userinfo holding an unencoded delimiter never surfaces as a "host"."""

    @pytest.mark.parametrize("url", AMBIGUOUS_AUTHORITY_URLS)
    def test_redact_url_for_log_never_leaks_the_fixture_secret(self, url):
        from local_deep_research.security import redact_url_for_log

        result = redact_url_for_log(url)
        assert "hunter2" not in result
        assert result in ("<unparseable>", "https://<redacted>")

    @pytest.mark.parametrize(
        ("url", "fragment"), USERINFO_FRAGMENT_AS_HOST_URLS
    )
    def test_redact_url_for_log_redacts_the_authority(self, url, fragment):
        from local_deep_research.security import redact_url_for_log

        assert redact_url_for_log(url) == "https://<redacted>"

    @pytest.mark.parametrize(
        ("url", "fragment"), USERINFO_FRAGMENT_AS_HOST_URLS
    )
    def test_domain_helper_drops_the_fragment(self, url, fragment):
        from local_deep_research.security import (
            url_authority_without_userinfo,
        )

        assert fragment not in url_authority_without_userinfo(url)

    @pytest.mark.parametrize(
        ("url", "fragment"), USERINFO_FRAGMENT_AS_HOST_URLS
    )
    def test_rate_limit_key_drops_the_fragment(self, url, fragment):
        from local_deep_research.research_library.downloaders.base import (
            rate_limit_authority,
        )

        key = rate_limit_authority(url)
        assert fragment not in key
        assert key in ("invalid_authority", "example.com")

    @pytest.mark.parametrize(
        ("url", "expected"),
        [
            ("https://arxiv.org/abs/2301.00001", "https://arxiv.org"),
            ("https://EXAMPLE.com:8080/p", "https://example.com:8080"),
            ("https://medium.com/@user/post", "https://medium.com"),
            (
                "http://localhost:8080/[email protected]",
                "http://localhost:8080",
            ),
            ("http://localhost:5173/@vite/client", "http://localhost:5173"),
            ("http://10.0.0.1:81/a@b", "http://10.0.0.1:81"),
            ("http://[::1]:8080/x@y", "http://[::1]:8080"),
            ("https://a.example?x=@y", "https://a.example"),
            ("http://intranet/", "http://intranet"),
            ("https://a.example/users/@name", "https://a.example"),
            ("https://a.example/[email protected]", "https://a.example"),
            ("https://a.example/p#x@y", "https://a.example"),
            (PUBLIC_SECRET_URL, "https://papers.example.com:8443"),
            (
                "https://u:p@[email protected]/a.pdf",
                "https://papers.example.com",
            ),
        ],
    )
    def test_ordinary_urls_render_unchanged(self, url, expected):
        from urllib.parse import urlparse

        from local_deep_research.research_library.downloaders.base import (
            rate_limit_authority,
        )
        from local_deep_research.security import (
            redact_url_for_log,
            url_authority_without_userinfo,
        )

        assert redact_url_for_log(url) == expected
        netloc = urlparse(url).netloc.rpartition("@")[2]
        assert url_authority_without_userinfo(url) == netloc
        assert rate_limit_authority(url) == netloc.lower()

    @pytest.mark.parametrize(
        "url",
        [
            "https://user:[email protected]/@group",
            "https://[email protected]/[email protected]",
            "http://user:pw@localhost:11434/v1#a@b",
        ],
    )
    def test_credentialed_url_with_a_later_at_fails_closed(self, url):
        """Documented cost of the userinfo rule: a real credentialed URL
        with an ``@`` after its authority cannot be told apart from a
        password holding ``@`` plus a delimiter, so it fails closed."""
        from local_deep_research.research_library.downloaders.base import (
            rate_limit_authority,
        )
        from local_deep_research.security import (
            redact_url_for_log,
            url_authority_without_userinfo,
        )

        assert redact_url_for_log(url).endswith("://<redacted>")
        assert url_authority_without_userinfo(url) == ""
        assert rate_limit_authority(url) == "invalid_authority"

    def test_rate_limit_key_drops_a_password_digit_parsed_as_port(self):
        """``https://u:@:1#x@host/`` parses to an empty host and the port
        ``1``, the start of the password."""
        from local_deep_research.research_library.downloaders.base import (
            rate_limit_authority,
        )

        url = "https://admin:@:1#[email protected]/a.pdf"
        assert rate_limit_authority(url) == "invalid_authority"

    @pytest.mark.parametrize(
        "url",
        [
            "https://hunter2-secret/[email protected]/a.pdf",
            "https://admin:[email protected]#[email protected]/a",
        ],
    )
    @pytest.mark.parametrize("resolution", ["fails", "private"])
    def test_validate_url_never_logs_the_parsed_host_raw(
        self, url, resolution, captured_logs
    ):
        """``validate_url``'s resolution-failure and resolves-to-private
        lines log the redacted URL, not the parsed host, which for these
        shapes is (part of) the password."""
        import socket

        from local_deep_research.security import ssrf_validator

        def fake_getaddrinfo(*args, **kwargs):
            if resolution == "fails":
                raise socket.gaierror("no such host")
            return [
                (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("10.0.0.5", 0))
            ]

        with patch.object(
            ssrf_validator.socket, "getaddrinfo", side_effect=fake_getaddrinfo
        ):
            assert ssrf_validator.validate_url(url) is False

        joined = "\n".join(captured_logs)
        assert "https://<redacted>" in joined
        assert "hunter2" not in joined
        assert "ss.example" not in joined

    def test_llm_wrapper_local_url_line_is_redacted(self, captured_logs):
        from types import SimpleNamespace

        from local_deep_research.web_search_engines.rate_limiting.llm.wrapper import (
            create_rate_limited_llm_wrapper,
        )

        llm = SimpleNamespace(
            base_url="http://user:hunter2-secret@localhost:11434/v1?token=T0PSECRET",
            model_name="m",
        )
        wrapper = create_rate_limited_llm_wrapper(llm, provider="openai")

        assert wrapper._check_if_local_model() is True
        joined = _assert_no_secret(captured_logs)
        assert (
            "Skipping rate limiting for local URL: http://localhost:11434"
            in joined
        )


#: Ambiguous authorities followed by dot segments. For http(s)
#: ``urllib3.parse_url`` removes dot segments, so the parsed path of
#: ``https://tok/en@host/../a`` is ``/a``: the ``@`` that marks the
#: authority as a userinfo fragment is only in the URL as written.
DOT_SEGMENT_AMBIGUOUS_URLS = [
    ("https://tok/[email protected]/../a", "tok"),
    ("https://admin:[email protected]/[email protected]/../y", "ss.example"),
    ("https://hunter2:12/[email protected]/../a.pdf", "hunter2"),
    ("https://hunter2-secret/[email protected]/..", "hunter2"),
    ("https://hunter2-secret/[email protected]/./..", "hunter2"),
    ("https://deploy:2024/[email protected]/..", "deploy"),
    ("https://admin:pw@localhost/[email protected]/../a", "localhost"),
    ("https://admin:[email protected]/[email protected]/../a", "127.0.0.1"),
    ("  https://hunter2-secret/[email protected]/../a", "hunter2"),
    # urllib3 keeps percent-encoded dots, so these were never hidden;
    # pinned so a decoding parser upgrade cannot reopen the gap.
    ("https://hunter2-secret/[email protected]/%2e%2e/a", "hunter2"),
    ("https://hunter2-secret/[email protected]/.%2E/a", "hunter2"),
]


#: Ambiguous authorities with an *empty* userinfo (the authority starts
#: with ``@``). ``urllib3.parse_url`` sets ``auth = auth or None``, so its
#: parse reports no userinfo; the ``@`` is only in the URL as written.
EMPTY_USERINFO_AMBIGUOUS_URLS = [
    ("https://@john.doe/[email protected]/", "john.doe"),
    ("https://@ss.example#[email protected]/", "ss.example"),
    ("https://@tok.en#[email protected]/", "tok.en"),
    ("https://@1.9#[email protected]:8443/", "1.9"),
]


class TestEmptyUserinfoIsStillUserinfo:
    """An authority that starts with ``@`` carries a userinfo in all
    three helpers, even though ``parse_url`` drops the empty one."""

    def test_premise_parse_url_drops_an_empty_userinfo(self):
        from urllib3.util import parse_url

        assert (
            parse_url("https://@ss.example#[email protected]/").auth is None
        )

    @pytest.mark.parametrize(("url", "fragment"), EMPTY_USERINFO_AMBIGUOUS_URLS)
    def test_redact_url_for_log_redacts_the_authority(self, url, fragment):
        from local_deep_research.security import redact_url_for_log

        result = redact_url_for_log(url)
        assert result == "https://<redacted>"
        assert fragment not in result

    @pytest.mark.parametrize(("url", "fragment"), EMPTY_USERINFO_AMBIGUOUS_URLS)
    def test_domain_helper_fails_closed(self, url, fragment):
        from local_deep_research.security import (
            url_authority_without_userinfo,
        )

        assert url_authority_without_userinfo(url) == ""

    @pytest.mark.parametrize(("url", "fragment"), EMPTY_USERINFO_AMBIGUOUS_URLS)
    def test_rate_limit_key_fails_closed(self, url, fragment):
        from local_deep_research.research_library.downloaders.base import (
            rate_limit_authority,
        )

        assert rate_limit_authority(url) == "invalid_authority"


class TestDotSegmentsCannotHideTheLaterAt:
    """The "``@`` after the authority" test reads the URL as written, not
    the parser's normalised path, in all three helpers."""

    def test_premise_parse_url_drops_the_at_with_the_dot_segment(self):
        from urllib3.util import parse_url

        assert parse_url("https://tok/[email protected]/../a").path == "/a"

    @pytest.mark.parametrize(("url", "fragment"), DOT_SEGMENT_AMBIGUOUS_URLS)
    def test_redact_url_for_log_redacts_the_authority(self, url, fragment):
        from local_deep_research.security import redact_url_for_log

        result = redact_url_for_log(url)
        assert result == "https://<redacted>"
        assert fragment not in result

    @pytest.mark.parametrize(("url", "fragment"), DOT_SEGMENT_AMBIGUOUS_URLS)
    def test_domain_helper_fails_closed(self, url, fragment):
        from local_deep_research.security import (
            url_authority_without_userinfo,
        )

        assert url_authority_without_userinfo(url) == ""

    @pytest.mark.parametrize(("url", "fragment"), DOT_SEGMENT_AMBIGUOUS_URLS)
    def test_rate_limit_key_fails_closed(self, url, fragment):
        from local_deep_research.research_library.downloaders.base import (
            rate_limit_authority,
        )

        assert rate_limit_authority(url) == "invalid_authority"

    def test_backslash_userinfo_with_dot_segment_is_redacted(self):
        """urllib3 also ends the authority at ``\\``; ``urlsplit`` does
        not, and reads ``papers.example.com`` as the host."""
        from local_deep_research.security import redact_url_for_log

        url = "https://hunter2-secret\\[email protected]/../a"
        assert redact_url_for_log(url) == "https://<redacted>"

    @pytest.mark.parametrize(
        ("url", "expected"),
        [
            ("https://a.example/x/../y", "https://a.example"),
            ("https://a.example/../@user", "https://a.example"),
            ("http://intranet/../a", "http://intranet"),
            ("https://a.example:8443/a/./b?c=d#e", "https://a.example:8443"),
        ],
    )
    def test_ordinary_dot_segment_urls_render_unchanged(self, url, expected):
        from urllib.parse import urlparse

        from local_deep_research.research_library.downloaders.base import (
            rate_limit_authority,
        )
        from local_deep_research.security import (
            redact_url_for_log,
            url_authority_without_userinfo,
        )

        assert redact_url_for_log(url) == expected
        netloc = urlparse(url).netloc
        assert url_authority_without_userinfo(url) == netloc
        assert rate_limit_authority(url) == netloc.lower()


class TestDomainCallSitesFailClosed:
    """Every call site that derives a domain or key from a stored URL
    goes through ``url_authority_without_userinfo``, so an ambiguous
    authority never leaks the userinfo prefix and a ``None`` URL never
    raises."""

    @staticmethod
    def _library_service():
        return TestLibraryDomainsDropUserinfo._library_service()

    @pytest.mark.parametrize("url", AMBIGUOUS_AUTHORITY_URLS)
    def test_metrics_extract_domain(self, url):
        from local_deep_research.web.routers.metrics import _extract_domain

        assert _extract_domain(url) is None

    @pytest.mark.parametrize("url", AMBIGUOUS_AUTHORITY_URLS)
    def test_library_service_extract_domain(self, url):
        assert self._library_service()._extract_domain(url) == ""

    def test_library_service_extract_domain_none(self):
        """``ResearchResource.url`` is nullable; a ``None`` URL must give
        an empty domain, not a TypeError from ``bytes.rpartition``."""
        assert self._library_service()._extract_domain(None) == ""

    def test_document_details_with_a_null_resource_url(self):
        from local_deep_research.research_library.services import (
            library_service as mod,
        )

        doc = MagicMock(
            text_content="a b",
            file_path="/lib/a.pdf",
            original_url=None,
            processed_at=None,
        )
        resource = MagicMock(url=None, title="T")
        session = MagicMock()
        query = session.query.return_value
        query.outerjoin.return_value.outerjoin.return_value.filter.return_value.first.return_value = (
            doc,
            resource,
            None,
        )
        query.join.return_value.filter.return_value.all.return_value = []
        service = self._library_service()
        with patch.object(mod, "get_user_db_session") as mock_gs:
            mock_gs.return_value.__enter__.return_value = session
            mock_gs.return_value.__exit__.return_value = False
            details = service.get_document_by_id("d1")

        assert details["domain"] == ""

    @pytest.mark.parametrize("url", AMBIGUOUS_AUTHORITY_URLS)
    def test_pdf_previews_domain(self, url):
        from local_deep_research.research_library.services import (
            library_service as mod,
        )

        doc = MagicMock(
            id="d1",
            research_id="r1",
            file_type="pdf",
            status="completed",
            original_url=url,
            filename="a.pdf",
        )
        session = MagicMock()
        chain = session.query.return_value.outerjoin.return_value
        chain.filter.return_value.order_by.return_value.limit.return_value.all.return_value = [
            (doc, None)
        ]
        service = self._library_service()
        with patch.object(mod, "get_user_db_session") as mock_gs:
            mock_gs.return_value.__enter__.return_value = session
            mock_gs.return_value.__exit__.return_value = False
            previews = service.get_pdf_previews_batch(["r1"])

        assert list(previews["r1"]["domains"]) == ["unknown"]
        assert "hunter2-secret" not in repr(previews)

    @pytest.mark.parametrize("url", AMBIGUOUS_AUTHORITY_URLS)
    def test_collection_search_domain(self, url):
        from local_deep_research.web.routers.library_search import (
            _enrich_with_document_metadata,
        )

        row = MagicMock(
            document_id="d1", file_type="pdf", original_url=url, created_at=None
        )
        session = MagicMock()
        session.query.return_value.filter.return_value.all.return_value = [row]
        results = [{"document_id": "d1"}]
        with patch(
            "local_deep_research.database.session_context.get_user_db_session"
        ) as mock_gs:
            mock_gs.return_value.__enter__.return_value = session
            mock_gs.return_value.__exit__.return_value = False
            _enrich_with_document_metadata(results, "alice", "pw")

        assert results[0]["domain"] == ""

    @pytest.mark.parametrize("url", AMBIGUOUS_AUTHORITY_URLS)
    def test_classifier_key(self, url, captured_logs):
        session = TestDomainKeyDropsUserinfo._session([(url,)])
        llm = MagicMock()

        results, progress = TestDomainKeyDropsUserinfo._run(session, llm)

        assert results["total"] == 0
        assert not session.add.called
        assert not llm.invoke.called
        assert "hunter2-secret" not in repr((results, progress))
        _assert_no_secret(captured_logs)

    @pytest.mark.parametrize("url", AMBIGUOUS_AUTHORITY_URLS)
    def test_failure_classifier_rate_limit_domain(self, url, captured_logs):
        from local_deep_research.library.download_management.failure_classifier import (
            FailureClassifier,
        )

        failure = FailureClassifier().classify_failure(
            error_type="http_error", status_code=429, url=url
        )

        assert failure.domain == ""
        assert "hunter2-secret" not in failure.message
        _assert_no_secret(captured_logs)

    @pytest.mark.parametrize("url", AMBIGUOUS_AUTHORITY_URLS)
    def test_llm_rate_limit_key(self, url):
        from types import SimpleNamespace

        from local_deep_research.web_search_engines.rate_limiting.llm.wrapper import (
            create_rate_limited_llm_wrapper,
        )

        llm = SimpleNamespace(base_url=url, model_name="m")
        key = create_rate_limited_llm_wrapper(
            llm, provider="openai"
        )._get_rate_limit_key()

        assert key == "openai-unknown-m"

    def test_llm_rate_limit_key_schemeless_userinfo(self):
        from types import SimpleNamespace

        from local_deep_research.web_search_engines.rate_limiting.llm.wrapper import (
            create_rate_limited_llm_wrapper,
        )

        llm = SimpleNamespace(
            base_url="user:[email protected]", model_name="m"
        )
        key = create_rate_limited_llm_wrapper(
            llm, provider="openai"
        )._get_rate_limit_key()

        assert "hunter2-secret" not in key


#: The retry reason for a row permanently failed before the domain fix:
#: ``RateLimitFailure`` used ``urlparse(url).netloc``, userinfo included,
#: and ``status_tracker.can_retry`` embeds the stored message.
LEGACY_RETRY_REASON = (
    "Permanently failed: Rate limited by user:hunter2-secret@"
    "papers.example.com - Unable to access article - server returned "
    "error code 429"
)


class TestLegacyRetryReasonsAreWithheld:
    """Rows written before this change still hold credentialed
    ``failure_message`` text; the retry-refusal log lines withhold it,
    while the value returned to the caller is unchanged (the user's own
    data)."""

    @staticmethod
    def _service_with_retry_refusal():
        from types import SimpleNamespace

        from local_deep_research.research_library.services import (
            download_service as ds,
        )

        service = ds.DownloadService.__new__(ds.DownloadService)
        service.username = "alice"
        service.password = "pw"
        service.retry_manager = MagicMock()
        service.retry_manager.should_retry_resource.return_value = (
            SimpleNamespace(can_retry=False, reason=LEGACY_RETRY_REASON)
        )
        return ds, service

    def test_download_as_text_retry_refusal(self, captured_logs):
        ds, service = self._service_with_retry_refusal()
        resource = MagicMock(
            source_type="web", url="https://papers.example.com/a.pdf"
        )
        session = MagicMock()
        session.query.return_value.filter_by.return_value.first.return_value = (
            resource
        )
        with (
            patch.object(ds, "get_user_db_session") as mock_gs,
            patch.object(service, "_try_existing_text", return_value=None),
            patch.object(service, "_try_legacy_text_file", return_value=None),
            patch.object(
                service, "_try_arxiv_text_extraction", return_value=None
            ),
            patch.object(
                service, "_try_existing_pdf_extraction", return_value=None
            ),
        ):
            mock_gs.return_value.__enter__.return_value = session
            mock_gs.return_value.__exit__.return_value = False
            result = service.download_as_text(7)

        assert result == (False, LEGACY_RETRY_REASON)
        joined = _assert_no_secret(captured_logs)
        assert "Skipping resource 7" in joined, joined[-400:]

    def test_arxiv_text_retry_refusal(self, captured_logs):
        _, service = self._service_with_retry_refusal()
        resource = MagicMock(id=7, url="https://arxiv.org/abs/2301.00001")

        assert service._try_arxiv_text_extraction(MagicMock(), resource) is None
        joined = _assert_no_secret(captured_logs)
        assert "Skipping canonical arXiv text for resource 7" in joined

    def test_queue_all_retry_refusal(self, captured_logs):
        from contextlib import contextmanager
        from types import SimpleNamespace

        from starlette.requests import Request

        from local_deep_research.web.routers import library as library_module

        row = SimpleNamespace(
            id=7, url="https://papers.example.com/a.pdf", research_id="r1"
        )
        session = MagicMock()
        session.query.return_value.outerjoin.return_value.filter.return_value.all.return_value = [
            row
        ]
        resource_filter = MagicMock()
        resource_filter.filter_downloadable_resources.return_value = [
            SimpleNamespace(
                resource_id=7, can_retry=False, reason=LEGACY_RETRY_REASON
            )
        ]
        summary = MagicMock(
            permanently_failed_count=1, temporarily_failed_count=0
        )
        summary.to_dict.return_value = {}
        resource_filter.get_filter_summary.return_value = summary
        resource_filter.get_skipped_resources_info.return_value = []

        @contextmanager
        def fake_db_session(*args, **kwargs):
            yield session

        request = Request(
            {
                "type": "http",
                "method": "POST",
                "path": "/library/api/queue-all-undownloaded",
                "headers": [],
                "query_string": b"",
            }
        )
        with (
            patch.object(
                library_module,
                "get_user_db_session",
                side_effect=fake_db_session,
            ),
            patch.object(
                library_module,
                "get_authenticated_user_password",
                return_value="pw",
            ),
            patch.object(
                library_module, "ResourceFilter", return_value=resource_filter
            ),
        ):
            result = library_module.queue_all_undownloaded(
                request, username="alice"
            )

        assert result["skipped"] == 1
        joined = _assert_no_secret(captured_logs)
        assert "Skipping resource 7 due to retry policy" in joined


class TestFailedTextExtractionRecordLogs:
    """``_record_failed_text_extraction`` stores the reason unchanged but
    logs it through ``failure_reason_for_log``."""

    @pytest.mark.parametrize("existing_doc", [True, False])
    def test_reason_is_withheld(self, captured_logs, existing_doc):
        from local_deep_research.research_library.services import (
            download_service as ds,
        )

        service = ds.DownloadService.__new__(ds.DownloadService)
        service.username = "alice"
        service.password = "pw"
        doc = MagicMock(id="d1") if existing_doc else None
        session = MagicMock()
        resource = MagicMock(
            id=7, url="https://papers.example.com/a.pdf", research_id="r1"
        )
        with (
            patch.object(ds, "get_document_for_resource", return_value=doc),
            patch.object(ds, "get_source_type_id", return_value="st"),
            patch.object(ds, "Document") as document_cls,
        ):
            service._record_failed_text_extraction(
                session, resource, PRESIGNED_REASON
            )

        joined = _assert_no_secret(captured_logs)
        if existing_doc:
            assert doc.error_message == PRESIGNED_REASON
            assert "Recorded failed text extraction for document d1" in joined
        else:
            kwargs = document_cls.call_args.kwargs
            assert kwargs["error_message"] == PRESIGNED_REASON
            assert "resource 7" in joined, joined[-400:]


class TestDocSchedulerTextExtractionLogs:
    """The document scheduler's per-resource text extraction logs the
    returned failure reason through ``failure_reason_for_log`` and an
    escaping exception by type only (its text can carry the resource URL
    via SQLAlchemy ``[parameters: ...]``; ``password`` is in scope)."""

    def test_reason_and_exception_text_are_withheld(self, captured_logs):
        from contextlib import contextmanager
        from datetime import UTC, datetime

        from local_deep_research.scheduler import background as bg

        saved_instance = bg.BackgroundJobScheduler._instance
        bg.BackgroundJobScheduler._instance = None
        try:
            with patch.object(bg, "BackgroundScheduler"):
                scheduler = bg.BackgroundJobScheduler()
            scheduler.user_sessions["testuser"] = {
                "scheduled_jobs": set(),
                "last_activity": datetime.now(UTC),
            }
            scheduler._credential_store.store("testuser", "testpass")
            settings = bg.DocumentSchedulerSettings(
                download_pdfs=False,
                extract_text=True,
                generate_rag=False,
                last_run="",
            )
            research = MagicMock(id="research-1", title="T", completed_at=None)
            resources = [
                MagicMock(id=1, url="https://papers.example.com/a.pdf"),
                MagicMock(id=2, url="https://papers.example.com/b.pdf"),
            ]
            db = MagicMock()
            research_query = MagicMock()
            research_query.filter.return_value = research_query
            research_query.order_by.return_value = research_query
            research_query.limit.return_value = research_query
            research_query.all.return_value = [research]
            resource_query = MagicMock()
            resource_query.filter_by.return_value = resource_query
            resource_query.all.return_value = resources
            db.query.side_effect = [research_query, resource_query]

            @contextmanager
            def fake_get_user_db_session(*a, **kw):
                yield db

            download_service = MagicMock()
            download_service.__enter__ = MagicMock(
                return_value=download_service
            )
            download_service.__exit__ = MagicMock(return_value=False)
            download_service.download_as_text.side_effect = [
                (False, PRESIGNED_REASON),
                RuntimeError(f"[parameters: ('{PUBLIC_SECRET_URL}',)]"),
            ]
            settings_manager = MagicMock()
            settings_manager.get_settings_snapshot.return_value = {
                "search.tool": "searxng"
            }
            with (
                patch.object(
                    scheduler,
                    "_get_document_scheduler_settings",
                    return_value=settings,
                ),
                patch(
                    "local_deep_research.database.session_context.get_user_db_session",
                    side_effect=fake_get_user_db_session,
                ),
                patch(
                    "local_deep_research.settings.manager.SettingsManager",
                    return_value=settings_manager,
                ),
                patch(
                    "local_deep_research.research_library.services.download_service.DownloadService",
                    return_value=download_service,
                ),
                patch(
                    "local_deep_research.research_library.utils.is_downloadable_url",
                    return_value=True,
                ),
            ):
                scheduler._process_user_documents("testuser")
        finally:
            bg.BackgroundJobScheduler._instance = saved_instance

        assert download_service.download_as_text.call_count == 2
        joined = _assert_no_secret(captured_logs)
        assert "Failed to extract text for resource 1" in joined, joined[-600:]
        assert "Error processing resource 2: RuntimeError" in joined, joined[
            -600:
        ]