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.

9.4 KB
Raw Blame
"""
Regression tests for the arXiv engine's gated full-text path.

Invariant under test: the engine's full-text step never fetches through
``arxiv.Result.download_pdf`` (which uses ``urllib.request.urlretrieve``
and bypasses the project's SSRF controls). It goes through
``ArxivDownloader.download_full_text`` instead, whose HTML and PDF
legs use ``SafeSession`` (SSRF pre-validation and DNS pinning), request
with ``stream=True`` and install the running decoded-byte guard before
reading the body. The id used to build any egress URL is validated first.
Metadata queries separately use the typed ``utilities.arxiv_api``
boundary, with its own ``SafeSession``, fixed timeout, and request pacing.

The engine used to download PDFs to disk itself (``_download_pdf_safely``);
that helper had no callers once full text moved to the shared downloader
and was removed together with its tests.

``test_arxiv_pdf_safe_session.py`` complements these tests by driving
both entry points through the real ``SafeSession`` and synthetic HTTP bodies.
"""

from unittest.mock import MagicMock, Mock, patch

import pytest


def _make_engine(**kwargs):
    from local_deep_research.web_search_engines.engines.search_engine_arxiv import (
        ArXivSearchEngine,
    )

    with patch(
        "local_deep_research.advanced_search_system.filters."
        "journal_reputation_filter.JournalReputationFilter.create_default",
        return_value=None,
    ):
        return ArXivSearchEngine(**kwargs)


@pytest.fixture
def download_dir(tmp_path):
    """A download directory: only gates full text, nothing is written."""
    return tmp_path / "arxiv_downloads"


class TestValidatedArxivId:
    """The id used to build the egress URL must be validated first."""

    @pytest.mark.parametrize(
        "entry_id,expected",
        [
            ("https://arxiv.org/abs/2101.12345", "2101.12345"),
            ("http://arxiv.org/abs/2301.12345v2", "2301.12345v2"),
            ("https://arxiv.org/abs/2401.01234", "2401.01234"),
            ("http://arxiv.org/abs/cond-mat/0501234", "cond-mat/0501234"),
            ("http://arxiv.org/abs/math.GT/0309136v1", "math.GT/0309136v1"),
        ],
    )
    def test_accepts_canonical_ids(self, entry_id, expected):
        from local_deep_research.web_search_engines.engines.search_engine_arxiv import (
            ArXivSearchEngine,
        )

        paper = Mock()
        paper.entry_id = entry_id
        assert ArXivSearchEngine._validated_arxiv_id(paper) == expected

    @pytest.mark.parametrize(
        "entry_id",
        [
            "https://arxiv.org/abs/../../etc/passwd",
            "http://arxiv.org/abs/2101.12345/../../../secret",
            "https://evil.example.com/abs/[email protected]",
            "http://arxiv.org/abs/not an id",
            "",
            "https://arxiv.org/abs/12345",
        ],
    )
    def test_rejects_malformed_ids(self, entry_id):
        from local_deep_research.web_search_engines.engines.search_engine_arxiv import (
            ArXivSearchEngine,
        )

        paper = Mock()
        paper.entry_id = entry_id
        assert ArXivSearchEngine._validated_arxiv_id(paper) is None


class TestFullContentUsesGatedPath:
    """The higher-level full-content path must ride the capped downloader
    transport (SafeSession + running decoded-byte guard) and never the
    deprecated ``paper.download_pdf``."""

    def test_get_full_content_routes_full_text_through_shared_downloader(
        self, download_dir
    ):
        engine = _make_engine(
            include_full_text=True,
            download_dir=str(download_dir),
            max_full_text=1,
        )

        paper = Mock()
        paper.entry_id = "https://arxiv.org/abs/2101.12345"
        paper.pdf_url = "https://arxiv.org/pdf/2101.12345"
        paper.authors = []
        paper.published = None
        paper.updated = None
        paper.categories = ["cs.AI"]
        paper.summary = "Summary text"
        paper.comment = None
        paper.doi = None
        paper.journal_ref = None
        engine._papers = {"https://arxiv.org/abs/2101.12345": paper}

        item = {"id": "https://arxiv.org/abs/2101.12345"}

        from local_deep_research.research_library.downloaders.arxiv import (
            ArxivFullTextOutcome,
            ArxivFullTextStatus,
            ArxivTextResult,
            ArxivTextSource,
        )

        downloader = MagicMock()
        downloader.download_full_text.return_value = ArxivFullTextOutcome(
            ArxivFullTextStatus.TEXT,
            ArxivTextResult(
                text="HTML rendition text", source=ArxivTextSource.ARXIV_HTML
            ),
        )
        # The engine imports ArxivDownloader lazily inside _fetch_full_text,
        # so patching the class at its source module intercepts the seam.
        with patch(
            "local_deep_research.research_library.downloaders.arxiv."
            "ArxivDownloader",
            return_value=downloader,
        ):
            results = engine._get_full_content([item])

        # The HTML-first gated downloader performed the fetch through the
        # SafeSession-based download stack; the deprecated paper.download_pdf
        # was never called.
        downloader.download_full_text.assert_called_once_with(
            "https://arxiv.org/abs/2101.12345"
        )
        assert not paper.download_pdf.called
        assert results[0]["content"] == "HTML rendition text"

    def test_get_full_content_pdf_transport_is_byte_capped(self, download_dir):
        """The engine's full-text PDF fallback leg bounds the decoded body.

        Drives the real downloader chain from ``_get_full_content``: the
        HTML leg answers 404, so ``download_full_text`` falls back
        to ``BaseDownloader._download_pdf``, whose response is a crafted
        body decoding past ``MAX_RESPONSE_SIZE`` while claiming a valid,
        under-cap ``Content-Length``. ``session.get`` is stubbed, so
        ``SafeSession.send`` (which guards that framing too) never runs:
        the abort can only come from
        ``BaseDownloader._ensure_decoded_body_cap``'s guard install;
        reverting it lets the finite bomb through as a "successful"
        download and the ResponseBodyTooLarge outcome is never recorded.
        """
        import contextlib

        import requests as requests_lib

        from local_deep_research.research_library.downloaders.arxiv import (
            ArxivDownloader,
        )
        from local_deep_research.security import safe_requests

        chunk = b"x" * (1024 * 1024)

        class BombRaw:
            def __init__(self, chunks):
                self.remaining = chunks
                self.read_calls = 0

            def read(self, amt=None, decode_content=False):
                self.read_calls += 1
                if self.remaining <= 0:
                    return b""
                self.remaining -= 1
                return chunk

            def stream(self, amt=None, decode_content=None):
                while True:
                    data = self.read(amt, decode_content)
                    if not data:
                        break
                    yield data

            def close(self):
                pass

            def release_conn(self):
                pass

        def not_found():
            return Mock(status_code=404, headers={})

        bomb = requests_lib.Response()
        bomb.status_code = 200
        bomb.headers["Content-Type"] = "application/pdf"
        bomb.headers["Content-Length"] = "100"
        bomb.raw = BombRaw(chunks=4)

        downloader = ArxivDownloader()
        downloader.rate_tracker = MagicMock()

        engine = _make_engine(
            include_full_text=True,
            download_dir=str(download_dir),
            max_full_text=1,
        )
        paper = Mock()
        paper.entry_id = "https://arxiv.org/abs/2101.12345"
        paper.pdf_url = "https://arxiv.org/pdf/2101.12345"
        paper.authors = []
        paper.published = None
        paper.updated = None
        paper.categories = ["cs.AI"]
        paper.summary = "Summary text"
        paper.comment = None
        paper.doi = None
        paper.journal_ref = None
        engine._papers = {"https://arxiv.org/abs/2101.12345": paper}
        # Pre-seed the engine's downloader cache with our instrumented one
        engine._arxiv_text_downloader = downloader

        item = {"id": "https://arxiv.org/abs/2101.12345"}

        with (
            patch.object(
                downloader.session,
                "get",
                side_effect=[not_found(), bomb],
            ),
            patch(
                "local_deep_research.research_library.downloaders.arxiv."
                "arxiv_api_request_gate",
                contextlib.nullcontext,
            ),
            patch.object(safe_requests, "MAX_RESPONSE_SIZE", 2 * 1024 * 1024),
        ):
            results = engine._get_full_content([item])

        # The PDF fetch aborted at the cap: exactly one outcome carries the
        # size-cap label (the HTML leg carries HTTP_404; the engine skips
        # the API leg)
        cap_calls = [
            call
            for call in downloader.rate_tracker.record_outcome.call_args_list
            if call.kwargs.get("error_type") == "ResponseBodyTooLarge"
        ]
        assert len(cap_calls) == 1
        # The full-text fetch yielded nothing, so the summary stands
        assert results[0]["content"] == "Summary text"
        # The read stopped at the cap (1 + 1 MiB fit; the 3rd crosses)
        assert bomb.raw.read_calls == 3