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.

3.7 KB
Raw Blame
"""Sink-leak test for the local-library ``[FETCH] source=library`` event.

The library branch of ``_fetch_raw_content`` resolves a document from the
user DB and logs a ``[FETCH] mode=... source=library url=...`` event. The
URL there is the fetch URL a user typed (or a document URL returned by the
library), so it may carry credentials — the event must render only the
redacted scheme://host:port origin across every real sink, and no HTTP
fetch may run (it is a local read).
"""

from __future__ import annotations

import re
from unittest.mock import patch

import pytest

from local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy import (
    SearchResultsCollector,
)
from local_deep_research.advanced_search_system.tools.fetch import (
    build_fetch_tool,
)

from ._fetch_sink_support import (
    BODY,
    CREDENTIALED_URL,
    create_sink_harness,
    TITLE,
    REDACTED_ORIGIN,
    assert_all_sinks_clean,
    assert_record_exception_none,
    fetch_db_messages,
    fetch_events_from,
    fetch_file_lines,
    fetch_socket_messages,
    fetch_stderr_lines,
)

FETCH_LIBRARY_EVENT_RE = re.compile(
    r"^\[FETCH\] mode=(?P<mode>\S+) source=library url=(?P<url>\S+) "
    r"— resolved local library document directly\s*$"
)


@pytest.fixture
def sink_harness(tmp_path):
    with create_sink_harness(tmp_path) as harness:
        yield harness


def test_library_resolved_fetch_emits_redacted_event_to_all_sinks(sink_harness):
    """A library-resolved fetch must emit one ``source=library`` event whose
    URL is the redacted origin only — in every sink — with no ContentFetcher
    run (local DB read, not HTTP)."""
    collector = SearchResultsCollector([])
    library_resolver = lambda url: (  # noqa: E731 - one-shot test stub
        {
            "status": "success",
            "title": TITLE,
            "content": BODY * 3,
            "url": CREDENTIALED_URL,
        }
        if url == CREDENTIALED_URL
        else None
    )
    tool = build_fetch_tool(
        "full", collector, library_resolver=library_resolver
    )
    assert tool is not None

    # If pre-resolution regresses and falls through to the HTTP path, the
    # patched ContentFetcher raises and this test fails.
    def _no_http(*_args, **_kwargs):
        raise AssertionError(
            "ContentFetcher must not run for a library-resolved URL"
        )

    with patch(
        "local_deep_research.content_fetcher.ContentFetcher",
        side_effect=_no_http,
    ):
        out = tool.invoke({"url": CREDENTIALED_URL})

    sink_harness.flush()

    # The full tool returns the document (registered as citation [1]).
    assert out.startswith("[1] ")
    assert BODY in out
    assert len(collector.results) == 1

    db_msgs = fetch_db_messages(sink_harness.db_entries)
    socket_msgs = fetch_socket_messages(sink_harness.socket_payloads)
    stderr_lines = fetch_stderr_lines(sink_harness.stderr)
    file_lines = fetch_file_lines(sink_harness.file_path)
    fetch_records = fetch_events_from(sink_harness.records)

    assert len(db_msgs) == 1, f"Expected 1 DB event, got {db_msgs}"
    assert len(socket_msgs) == 1, f"Expected 1 socket event, got {socket_msgs}"
    assert len(file_lines) == 1, f"Expected 1 file line, got {file_lines}"
    assert len(fetch_records) == 1, f"Expected 1 record, got {fetch_records}"
    assert len(stderr_lines) >= 1

    event_msg = fetch_records[0]["message"].strip()
    match = FETCH_LIBRARY_EVENT_RE.match(event_msg)
    assert match is not None, f"Event does not match schema: {event_msg!r}"
    assert match.group("mode") == "full"
    assert match.group("url") == REDACTED_ORIGIN

    assert_record_exception_none(fetch_records[0], "library-resolved record")
    assert_all_sinks_clean(sink_harness)