~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
test(arxiv): cover full-text downloads with real SafeSession (#6886)
LearningCircuit2daca75ad83a
diff --git a/tests/web_search_engines/engines/test_arxiv_pdf_egress_gating.py b/tests/web_search_engines/engines/test_arxiv_pdf_egress_gating.pyindex 3e27fe5776..18ced2022f 100644--- a/tests/web_search_engines/engines/test_arxiv_pdf_egress_gating.py+++ b/tests/web_search_engines/engines/test_arxiv_pdf_egress_gating.py@@ -14,6 +14,9 @@ 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 downloaderand 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, patchdiff --git a/tests/web_search_engines/engines/test_arxiv_pdf_safe_session.py b/tests/web_search_engines/engines/test_arxiv_pdf_safe_session.pynew file mode 100644index 0000000000..0ca9ebc396--- /dev/null+++ b/tests/web_search_engines/engines/test_arxiv_pdf_safe_session.py@@ -0,0 +1,403 @@+"""Exercise both arXiv full-text entry points through the real SafeSession.++Both entry points fetch full text through the shared HTML-first+``ArxivDownloader.download_full_text`` (official HTML rendition, then the+PDF). Only DNS answers, the HTTP adapter, metadata lookup, request pacing,+and PDF text parsing are replaced. Redirect validation, HTTP+framing/decompression, and the size checks remain active. No external+requests are made. The synthetic DNS answers keep these tests offline, so+they do not exercise connect-time DNS pinning; the shared security tests+cover that layer.+"""++import gzip+import re+import socket+import urllib.request+from http.client import HTTPResponse as WireResponse+from io import BytesIO+from types import SimpleNamespace+from unittest.mock import Mock++import pytest+import requests+from urllib3.response import HTTPResponse++from local_deep_research.research_library.downloaders.arxiv import (+ ArxivDownloader,+ ArxivFullTextStatus,+)+from local_deep_research.security import dns_pinning, safe_requests+from local_deep_research.web_search_engines.engines import (+ search_engine_arxiv as arxiv_engine,+)+from local_deep_research.web_search_engines.rate_limiting import (+ AdaptiveRateLimitTracker,+)+++PAPER_ID = "2101.12345v2"+HTML_URL = f"https://export.arxiv.org/html/{PAPER_ID}"+PDF_URL = f"https://export.arxiv.org/pdf/{PAPER_ID}"+PUBLIC_REDIRECT = "https://cdn.example.org/paper.pdf"+PDF_BODY = b"%PDF-1.4 test paper"+EXTRACTED_TEXT = "Extracted paper text"+SIZE_CAP = 128+++def _response(request, status, headers, body):+ """Use real HTTP readers, including the separate chunked reader."""+ if headers.get("Transfer-Encoding") == "chunked":+ body = (+ b"".join(+ b"%x\r\n" % len(chunk) + chunk + b"\r\n"+ for offset in range(0, len(body), 64)+ if (chunk := body[offset : offset + 64])+ )+ + b"0\r\n\r\n"+ )+ wire = (+ f"HTTP/1.1 {status} Test\r\n".encode()+ + b"".join(+ f"{key}: {value}\r\n".encode() for key, value in headers.items()+ )+ + b"\r\n"+ + body+ )+ incoming = WireResponse(+ SimpleNamespace(makefile=lambda *args, **kwargs: BytesIO(wire)),+ method=request.method,+ )+ incoming.begin()+ response = requests.Response()+ response.status_code = status+ response.headers = requests.structures.CaseInsensitiveDict(headers)+ response.url = request.url+ response.request = request+ response.raw = HTTPResponse(+ body=incoming,+ headers=dict(incoming.getheaders()),+ original_response=incoming,+ preload_content=False,+ )+ response.close = Mock(wraps=response.close)+ return response+++def transport(monkeypatch, tmp_path):+ # The HTML leg answers "no rendition" unless a test routes it, so the+ # PDF leg is what these cases exercise by default.+ state = SimpleNamespace(+ routes={HTML_URL: (404, {}, b"")},+ requests=[],+ responses=[],+ session_errors=[],+ outcome_errors=[],+ )++ def resolve(host, port, *args, **kwargs):+ addresses = {+ "export.arxiv.org": "93.184.216.34",+ "cdn.example.org": "93.184.216.35",+ "internal.example.org": "10.0.0.7",+ }+ return [+ (socket.AF_INET, socket.SOCK_STREAM, 6, "", (addresses[host], port))+ ]++ def send(_adapter, request, **kwargs):+ state.requests.append(request)+ assert kwargs["stream"] is True+ assert kwargs["timeout"] is not None+ status, headers, body = state.routes[request.url]+ response = _response(request, status, headers, body)+ state.responses.append(response)+ return response++ real_request = safe_requests.SafeSession.request++ def recording_request(session, method, url, **kwargs):+ # Record what SafeSession raises (SSRF refusals, declared+ # oversize) before the downloader turns it into a status.+ try:+ return real_request(session, method, url, **kwargs)+ except Exception as error:+ state.session_errors.append(error)+ raise++ def record_outcome(_tracker, *args, **kwargs):+ if kwargs.get("error_type"):+ state.outcome_errors.append(kwargs["error_type"])++ monkeypatch.setattr(dns_pinning, "_real_getaddrinfo", resolve)+ monkeypatch.setattr(requests.adapters.HTTPAdapter, "send", send)+ monkeypatch.setattr(safe_requests.SafeSession, "request", recording_request)+ monkeypatch.setattr(+ AdaptiveRateLimitTracker, "apply_rate_limit", lambda *a, **k: 0.0+ )+ monkeypatch.setattr(+ AdaptiveRateLimitTracker, "record_outcome", record_outcome+ )+ monkeypatch.setenv("NETRC", str(tmp_path / "unused-netrc"))+ # Fail closed if a regression restores urllib or bypasses the adapter.+ blocked_calls = []+ for owner, name in [+ (urllib.request, "urlretrieve"),+ (urllib.request, "urlopen"),+ (socket.socket, "connect"),+ ]:+ blocked = Mock(side_effect=AssertionError("Unexpected network access"))+ monkeypatch.setattr(owner, name, blocked)+ blocked_calls.append(blocked)+ yield state+ for blocked in blocked_calls:+ blocked.assert_not_called()++[email protected](params=["search_results", "paper_details"])+def full_text_request(request, monkeypatch, tmp_path, transport):+ monkeypatch.setattr(+ arxiv_engine.ArXivSearchEngine,+ "_create_journal_filter",+ lambda *args, **kwargs: None,+ )+ download_dir = tmp_path / "arxiv_downloads"+ engine = arxiv_engine.ArXivSearchEngine(+ include_full_text=True, download_dir=str(download_dir)+ )+ scrub_error = Mock(wraps=engine._scrub_error)+ monkeypatch.setattr(engine, "_scrub_error", scrub_error)+ paper = SimpleNamespace(+ entry_id=f"https://arxiv.org/abs/{PAPER_ID}",+ title="Test paper",+ summary="The abstract remains usable if a download is rejected.",+ pdf_url=f"https://arxiv.org/pdf/{PAPER_ID}",+ authors=[],+ published=None,+ updated=None,+ categories=["cs.AI"],+ comment=None,+ doi=None,+ journal_ref=None,+ download_pdf=Mock(side_effect=AssertionError("Ungated PDF download")),+ )+ engine._papers = {paper.entry_id: paper}+ monkeypatch.setattr(arxiv_engine, "fetch_arxiv_results", lambda _: [paper])+ # Parsing is outside this transport regression; record the bytes that+ # reached extraction instead.+ extract = Mock(return_value=EXTRACTED_TEXT)+ monkeypatch.setattr(+ ArxivDownloader, "extract_text_from_pdf", staticmethod(extract)+ )+ outcomes = []+ real_download_full_text = ArxivDownloader.download_full_text++ def recording_download_full_text(downloader, url):+ outcome = real_download_full_text(downloader, url)+ outcomes.append(outcome)+ return outcome++ monkeypatch.setattr(+ ArxivDownloader, "download_full_text", recording_download_full_text+ )++ def fetch():+ if request.param == "search_results":+ return engine._get_full_content([{"id": paper.entry_id}])[0]+ return engine.get_paper_details(PAPER_ID)++ yield SimpleNamespace(+ fetch=fetch,+ paper=paper,+ extract=extract,+ outcomes=outcomes,+ scrub_error=scrub_error,+ )+ engine.close()+ # The engine catches download errors, so check this separately even+ # when the test expects it to fall back to the abstract.+ paper.download_pdf.assert_not_called()+ # download_dir only gates full text; nothing is written to it.+ assert not download_dir.exists()+++def _assert_downloaded(result, full_text_request, body):+ assert [o.status for o in full_text_request.outcomes] == [+ ArxivFullTextStatus.TEXT+ ]+ full_text_request.extract.assert_called_once_with(body)+ assert result["full_content"].startswith(EXTRACTED_TEXT)+ assert result["content"] == result["full_content"]+ full_text_request.scrub_error.assert_not_called()+++def _assert_rejected(result, full_text_request):+ assert result["content"] == full_text_request.paper.summary+ assert result["full_content"] == full_text_request.paper.summary+ full_text_request.extract.assert_not_called()+ assert [o.status for o in full_text_request.outcomes] == [+ ArxivFullTextStatus.FETCH_FAILED+ ]+ # A failed request comes back as a status, not as an exception that+ # reaches the engine's generic error path.+ full_text_request.scrub_error.assert_not_called()+++def _assert_session_refused(transport, error_match):+ assert len(transport.session_errors) == 1+ error = transport.session_errors[0]+ assert isinstance(error, ValueError)+ assert re.search(error_match, str(error))+++ "untrusted_pdf_url",+ [+ "http://127.0.0.1/private.pdf",+ "http://169.254.169.254/latest/meta-data/",+ "file:///etc/passwd",+ "https://attacker.example.org/paper.pdf",+ ],+)+def test_feed_pdf_url_cannot_choose_download_target(+ full_text_request, transport, untrusted_pdf_url+):+ full_text_request.paper.pdf_url = untrusted_pdf_url+ transport.routes[PDF_URL] = (200, {}, PDF_BODY)++ result = full_text_request.fetch()++ assert [req.url for req in transport.requests] == [HTML_URL, PDF_URL]+ assert result["pdf_url"] == f"https://arxiv.org/pdf/{PAPER_ID}"+ assert all(r.close.called for r in transport.responses)+ _assert_downloaded(result, full_text_request, PDF_BODY)++[email protected]("leg", ["pdf", "html"])[email protected]("via_public_hop", [False, True])+ "blocked_target",+ [+ "http://127.0.0.1/admin",+ "http://10.0.0.7/private",+ "http://169.254.169.254/latest/meta-data/",+ "http://[::1]/admin",+ "http://[fd00:ec2::254]/latest/meta-data/",+ # https:// targets: every redirect starts from https://, so these are+ # never a scheme downgrade and can only be refused by the per-hop+ # SSRF check (a downgrade guard cannot mask its removal).+ "https://127.0.0.1/admin",+ "https://10.0.0.7/private",+ "https://169.254.169.254/latest/meta-data/",+ "https://[::1]/admin",+ "https://[fd00:ec2::254]/latest/meta-data/",+ "https://internal.example.org/private",+ ],+)+def test_private_redirect_is_blocked_before_transport(+ full_text_request, transport, blocked_target, via_public_hop, leg+):+ first_url = PDF_URL if leg == "pdf" else HTML_URL+ first_target = PUBLIC_REDIRECT if via_public_hop else blocked_target+ transport.routes[first_url] = (302, {"Location": first_target}, b"")+ transport.routes[PUBLIC_REDIRECT] = (+ 302,+ {"Location": blocked_target},+ b"",+ )+ if leg == "html":+ # No PDF either, so the blocked HTML hop decides the outcome.+ transport.routes[PDF_URL] = (404, {}, b"")+ # Supply a success response for the forbidden destination so removing+ # validation cannot pass merely because the fixture raises there.+ transport.routes[blocked_target] = (200, {}, PDF_BODY)++ result = full_text_request.fetch()++ hops = [first_url, PUBLIC_REDIRECT] if via_public_hop else [first_url]+ expected = [HTML_URL, *hops] if leg == "pdf" else [*hops, PDF_URL]+ assert [req.url for req in transport.requests] == expected+ assert len(transport.responses) == len(expected)+ assert all(response.close.called for response in transport.responses)+ _assert_rejected(result, full_text_request)+ # A combined egress policy may reject HTTPS-to-HTTP downgrades before+ # reaching its private-address check. Either refusal is safe.+ # https:// targets are never a downgrade, so only the per-hop SSRF check+ # may refuse them; the downgrade alternative must not mask its removal.+ if blocked_target.startswith("https://"):+ _assert_session_refused(transport, "SSRF")+ else:+ _assert_session_refused(transport, r"SSRF|downgrade to cleartext")+++def test_public_redirect_still_downloads(full_text_request, transport):+ transport.routes[PDF_URL] = (302, {"Location": PUBLIC_REDIRECT}, b"")+ transport.routes[PUBLIC_REDIRECT] = (200, {}, PDF_BODY)++ result = full_text_request.fetch()++ assert [req.url for req in transport.requests] == [+ HTML_URL,+ PDF_URL,+ PUBLIC_REDIRECT,+ ]+ transport.responses[-1].close.assert_called()+ _assert_downloaded(result, full_text_request, PDF_BODY)++[email protected]("framing", ["length", "close", "chunked", "gzip"])+ "size", [SIZE_CAP, SIZE_CAP + 1], ids=["at_cap", "over_cap"]+)+def test_response_limit_covers_decoded_body(+ full_text_request, transport, monkeypatch, framing, size+):+ monkeypatch.setattr(safe_requests, "MAX_RESPONSE_SIZE", SIZE_CAP)+ body = PDF_BODY.ljust(size, b"x")+ wire_body = gzip.compress(body) if framing == "gzip" else body+ headers = {}+ if framing in {"length", "gzip"}:+ headers["Content-Length"] = str(len(wire_body))+ if framing == "gzip":+ assert len(wire_body) < SIZE_CAP+ headers["Content-Encoding"] = "gzip"+ elif framing == "chunked":+ headers["Transfer-Encoding"] = "chunked"+ transport.routes[PDF_URL] = (200, headers, wire_body)++ result = full_text_request.fetch()++ assert [req.url for req in transport.requests] == [HTML_URL, PDF_URL]+ pdf_response = transport.responses[-1]+ assert pdf_response.close.called+ assert pdf_response.raw.closed+ if size > SIZE_CAP:+ _assert_rejected(result, full_text_request)+ if framing == "length":+ # Reject a declared oversize before reading any body bytes.+ assert pdf_response.raw.tell() == 0+ _assert_session_refused(transport, r"Response too large")+ else:+ # Bounded while the decoded body is read.+ assert transport.session_errors == []+ assert "ResponseBodyTooLarge" in transport.outcome_errors+ else:+ _assert_downloaded(result, full_text_request, body)+++def test_interrupted_body_falls_back_to_abstract(full_text_request, transport):+ body = PDF_BODY.ljust(1024, b"x")+ transport.routes[PDF_URL] = (+ 200,+ {"Content-Length": str(len(body) + 1)},+ body,+ )++ result = full_text_request.fetch()++ assert [req.url for req in transport.requests] == [HTML_URL, PDF_URL]+ _assert_rejected(result, full_text_request)+ assert "ChunkedEncodingError" in transport.outcome_errors+ transport.responses[-1].close.assert_called()+ assert transport.responses[-1].raw.closed