~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
security: validate redirect targets in SSRF-protected HTTP requests (#1949)
LearningCircuitfd06a9359f3d
diff --git a/src/local_deep_research/security/safe_requests.py b/src/local_deep_research/security/safe_requests.pyindex 1cca258602..f9c12d7e4b 100644--- a/src/local_deep_research/security/safe_requests.py+++ b/src/local_deep_research/security/safe_requests.py@@ -6,6 +6,8 @@ Wraps requests library to add SSRF protection and security best practices.import requestsfrom typing import Any, Optional+from urllib.parse import urljoin+from loguru import loggerfrom .ssrf_validator import validate_url@@ -17,6 +19,50 @@ DEFAULT_TIMEOUT = 30 # seconds# Maximum response size to prevent memory exhaustion (10MB)MAX_RESPONSE_SIZE = 10 * 1024 * 1024+# HTTP status codes that indicate a redirect+_REDIRECT_STATUS_CODES = frozenset({301, 302, 303, 307, 308})++# Maximum number of redirects to follow+_MAX_REDIRECTS = 10+++def _check_response_size(response: requests.Response) -> None:+ """Reject responses whose Content-Length exceeds MAX_RESPONSE_SIZE.++ Only checks the Content-Length header; responses using chunked transfer+ encoding (or otherwise missing Content-Length) are not bounded by this+ check.++ Must be called before returning a response to the caller. On rejection+ the response is closed so the connection is not leaked.+ """+ content_length = response.headers.get("Content-Length")+ if content_length:+ try:+ size = int(content_length)+ except (ValueError, TypeError):+ return # Content-Length not a valid number+ if size < 0:+ return # Malformed Content-Length, treat as absent+ if size > MAX_RESPONSE_SIZE:+ response.close()+ raise ValueError(+ f"Response too large: {content_length} bytes "+ f"(max {MAX_RESPONSE_SIZE})"+ )+++def _resolve_redirect_method(method: str, status_code: int) -> str:+ """Determine HTTP method after redirect, per RFC 7231."""+ if status_code == 303 and method != "HEAD":+ method = "GET"+ elif status_code == 302 and method == "POST":+ method = "GET"+ elif status_code == 301 and method == "POST":+ method = "GET"+ # 307, 308: preserve original method (no change needed)+ return method+def safe_get(url: str,@@ -65,35 +111,69 @@ def safe_get(if "timeout" not in kwargs:kwargs["timeout"] = timeout- # Disable redirects by default to prevent SSRF bypass via redirect chains- # Redirects could point to internal services, bypassing initial URL validation- # Callers can explicitly enable redirects if needed and trust the redirect target- if "allow_redirects" not in kwargs:- kwargs["allow_redirects"] = False+ # Intercept allow_redirects — we handle redirects manually to validate+ # each redirect target against SSRF rules+ caller_wants_redirects = kwargs.pop("allow_redirects", True)+ kwargs["allow_redirects"] = False+ current_url = urltry:response = requests.get(url, params=params, **kwargs)- # Check response size- content_length = response.headers.get("Content-Length")- if content_length:- try:- if int(content_length) > MAX_RESPONSE_SIZE:+ # Follow redirects manually with SSRF validation on each hop+ if caller_wants_redirects:+ redirects_followed = 0+ while (+ response.status_code in _REDIRECT_STATUS_CODES+ and redirects_followed < _MAX_REDIRECTS+ ):+ redirect_url = (response.headers.get("Location") or "").strip()+ if not redirect_url:+ break++ # Resolve relative redirects+ redirect_url = urljoin(+ response.url or current_url, redirect_url+ )++ # Validate redirect target against SSRF rules+ if not validate_url(+ redirect_url,+ allow_localhost=allow_localhost,+ allow_private_ips=allow_private_ips,+ ):+ logger.warning(+ f"Redirect to {redirect_url} blocked by SSRF validation "+ f"(from {url}, hop {redirects_followed + 1})"+ )+ response.close()raise ValueError(- f"Response too large: {content_length} bytes "- f"(max {MAX_RESPONSE_SIZE})"+ f"Redirect target failed SSRF validation: {redirect_url}")- except (ValueError, TypeError):- # Ignore if Content-Length is not a valid number (e.g., in mocks)- pass++ current_url = redirect_url+ response.close()+ response = requests.get(redirect_url, **kwargs)+ redirects_followed += 1++ if (+ response.status_code in _REDIRECT_STATUS_CODES+ and redirects_followed >= _MAX_REDIRECTS+ ):+ response.close()+ raise ValueError(+ f"Too many redirects ({_MAX_REDIRECTS}) from {url}"+ )++ _check_response_size(response)return responseexcept requests.Timeout:- logger.warning(f"Request timeout after {timeout}s: {url}")+ logger.warning(f"Request timeout after {timeout}s: {current_url}")raiseexcept requests.RequestException as e:- logger.warning(f"Request failed for {url}: {e}")+ logger.warning(f"Request failed for {current_url}: {e}")raise@@ -146,35 +226,83 @@ def safe_post(if "timeout" not in kwargs:kwargs["timeout"] = timeout- # Disable redirects by default to prevent SSRF bypass via redirect chains- # Redirects could point to internal services, bypassing initial URL validation- # Callers can explicitly enable redirects if needed and trust the redirect target- if "allow_redirects" not in kwargs:- kwargs["allow_redirects"] = False+ # Intercept allow_redirects — we handle redirects manually to validate+ # each redirect target against SSRF rules+ caller_wants_redirects = kwargs.pop("allow_redirects", True)+ kwargs["allow_redirects"] = False+ current_url = urltry:response = requests.post(url, data=data, json=json, **kwargs)- # Check response size- content_length = response.headers.get("Content-Length")- if content_length:- try:- if int(content_length) > MAX_RESPONSE_SIZE:+ # Follow redirects manually with SSRF validation on each hop+ if caller_wants_redirects:+ redirect_method = "POST"+ redirects_followed = 0+ while (+ response.status_code in _REDIRECT_STATUS_CODES+ and redirects_followed < _MAX_REDIRECTS+ ):+ redirect_url = (response.headers.get("Location") or "").strip()+ if not redirect_url:+ break++ # Resolve relative redirects+ redirect_url = urljoin(+ response.url or current_url, redirect_url+ )++ # Validate redirect target against SSRF rules+ if not validate_url(+ redirect_url,+ allow_localhost=allow_localhost,+ allow_private_ips=allow_private_ips,+ ):+ logger.warning(+ f"Redirect to {redirect_url} blocked by SSRF validation "+ f"(from {url}, hop {redirects_followed + 1})"+ )+ response.close()raise ValueError(- f"Response too large: {content_length} bytes "- f"(max {MAX_RESPONSE_SIZE})"+ f"Redirect target failed SSRF validation: {redirect_url}")- except (ValueError, TypeError):- # Ignore if Content-Length is not a valid number (e.g., in mocks)- pass++ redirect_method = _resolve_redirect_method(+ redirect_method, response.status_code+ )+ current_url = redirect_url+ response.close()++ if redirect_method == "GET":+ # 301/302/303: convert to GET, drop body+ data = None+ json = None+ response = requests.get(redirect_url, **kwargs)+ else:+ # 307/308: preserve current method and body+ response = requests.post(+ redirect_url, data=data, json=json, **kwargs+ )+ redirects_followed += 1++ if (+ response.status_code in _REDIRECT_STATUS_CODES+ and redirects_followed >= _MAX_REDIRECTS+ ):+ response.close()+ raise ValueError(+ f"Too many redirects ({_MAX_REDIRECTS}) from {url}"+ )++ _check_response_size(response)return responseexcept requests.Timeout:- logger.warning(f"Request timeout after {timeout}s: {url}")+ logger.warning(f"Request timeout after {timeout}s: {current_url}")raiseexcept requests.RequestException as e:- logger.warning(f"Request failed for {url}: {e}")+ logger.warning(f"Request failed for {current_url}: {e}")raise@@ -183,6 +311,14 @@ class SafeSession(requests.Session):"""Session with built-in SSRF protection.+ Redirect validation relies on ``requests.Session.resolve_redirects()``+ calling ``self.send()`` for each hop — an internal implementation detail+ of the ``requests`` library. This is simpler than re-implementing the+ redirect loop (as ``safe_get``/``safe_post`` do) and keeps session-level+ features (cookies, auth) working. The trade-off is coupling to the+ ``requests`` internals; if a future version stops routing hops through+ ``send()``, redirect targets would no longer be validated.+Usage:with SafeSession() as session:response = session.get(url)@@ -194,6 +330,15 @@ class SafeSession(requests.Session):# For trusted internal services on any private network IP:with SafeSession(allow_private_ips=True) as session:response = session.get(url)++ Raises:+ ValueError: If a URL (initial or redirect target) fails SSRF+ validation, or if the response Content-Length exceeds+ MAX_RESPONSE_SIZE. Note: ``safe_get``/``safe_post`` also raise+ ``ValueError`` for too-many-redirects, but ``SafeSession`` raises+ ``requests.TooManyRedirects`` for that case since it delegates+ redirect counting to the ``requests`` library.+ requests.RequestException: On transport-level failures."""def __init__(@@ -211,6 +356,7 @@ class SafeSession(requests.Session):Note: AWS metadata endpoint (169.254.169.254) is ALWAYS blocked."""super().__init__()+ self.max_redirects = _MAX_REDIRECTSself.allow_localhost = allow_localhostself.allow_private_ips = allow_private_ips@@ -235,20 +381,36 @@ class SafeSession(requests.Session):def send(self, request: requests.PreparedRequest, **kwargs) -> requests.Response:- """Override send to validate redirect targets against SSRF.+ """Override send to validate every outgoing request against SSRF.- The ``requests`` library follows redirects internally via- ``resolve_redirects`` which calls ``send()`` for each hop- **without** going through ``request()``. By validating the- URL in ``send()`` we ensure every redirect target is checked.+ This runs on **all** calls — both the initial request (routed+ here by ``requests.Session.request()``) and each redirect hop+ (routed here by ``resolve_redirects()``). The initial URL is+ therefore validated twice (once in ``request()``, once here);+ this is intentional defense-in-depth."""if request.url and not validate_url(request.url,allow_localhost=self.allow_localhost,allow_private_ips=self.allow_private_ips,):+ logger.warning(+ f"Request to {request.url} blocked by SSRF validation"+ )raise ValueError(f"Redirect target failed security validation (possible SSRF): {request.url}")- return super().send(request, **kwargs)+ response = super().send(request, **kwargs)+ # Only skip size check for 3xx responses that will actually be+ # followed as redirects (have a Location header and allow_redirects+ # is True). A 3xx with no Location, or when allow_redirects=False,+ # is the final response and must be size-checked.+ is_intermediate_redirect = (+ response.status_code in _REDIRECT_STATUS_CODES+ and response.headers.get("Location")+ and kwargs.get("allow_redirects", True)+ )+ if not is_intermediate_redirect:+ _check_response_size(response)+ return responsediff --git a/tests/security/test_safe_requests.py b/tests/security/test_safe_requests.pyindex 61f9fbb7be..132205d661 100644--- a/tests/security/test_safe_requests.py+++ b/tests/security/test_safe_requests.py@@ -79,8 +79,9 @@ class TestSafeGetFunction:_, kwargs = mock_get.call_argsassert kwargs["timeout"] == 60- def test_disables_redirects_by_default(self):- """Should disable redirects by default to prevent SSRF bypass."""+ def test_underlying_requests_always_gets_redirects_disabled(self):+ """The underlying requests.get() always receives allow_redirects=False;+ safe_get handles redirects manually with SSRF validation."""with patch("local_deep_research.security.safe_requests.validate_url",return_value=True,@@ -98,7 +99,8 @@ class TestSafeGetFunction:assert kwargs["allow_redirects"] is Falsedef test_can_enable_redirects(self):- """Should allow enabling redirects explicitly."""+ """With allow_redirects=True, redirects are still disabled at+ requests level (handled manually for SSRF validation)."""with patch("local_deep_research.security.safe_requests.validate_url",return_value=True,@@ -113,7 +115,9 @@ class TestSafeGetFunction:safe_get("https://example.com", allow_redirects=True)_, kwargs = mock_get.call_args- assert kwargs["allow_redirects"] is True+ # Redirects are always disabled at the requests level;+ # safe_get handles them manually with SSRF validation+ assert kwargs["allow_redirects"] is Falsedef test_passes_params(self):"""Should pass URL parameters."""@@ -136,13 +140,8 @@ class TestSafeGetFunction:_, kwargs = mock_get.call_argsassert "params" not in kwargs or kwargs.get("params") == params- def test_oversized_response_silently_passes(self):- """Documents current behavior: oversized responses are NOT rejected.-- NOTE: This is a potential security bug - the ValueError raised for- "Response too large" is caught by the same except block that handles- int() parsing errors, making the size check ineffective.- """+ def test_oversized_response_raises_error(self):+ """Oversized responses are rejected with ValueError."""with patch("local_deep_research.security.safe_requests.validate_url",return_value=True,@@ -151,16 +150,13 @@ class TestSafeGetFunction:"local_deep_research.security.safe_requests.requests.get") as mock_get:mock_response = MagicMock()- # Configure headers as a dict-like objectmock_response.headers = {"Content-Length": str(MAX_RESPONSE_SIZE + 1)}mock_get.return_value = mock_response- # BUG: This should raise ValueError but doesn't because- # the except block catches both int() errors and the raised error- result = safe_get("https://example.com")- assert result == mock_response+ with pytest.raises(ValueError, match="Response too large"):+ safe_get("https://example.com")def test_accepts_response_within_limit(self):"""Should accept response within size limit."""@@ -351,8 +347,9 @@ class TestSafePostFunction:_, kwargs = mock_post.call_argsassert kwargs["timeout"] == DEFAULT_TIMEOUT- def test_disables_redirects_by_default(self):- """Should disable redirects by default."""+ def test_underlying_requests_always_gets_redirects_disabled(self):+ """The underlying requests.post() always receives allow_redirects=False;+ safe_post handles redirects manually with SSRF validation."""with patch("local_deep_research.security.safe_requests.validate_url",return_value=True,@@ -369,11 +366,29 @@ class TestSafePostFunction:_, kwargs = mock_post.call_argsassert kwargs["allow_redirects"] is False- def test_oversized_response_silently_passes(self):- """Documents current behavior: oversized responses are NOT rejected.+ def test_can_enable_redirects(self):+ """With allow_redirects=True, redirects are still disabled at+ requests level (handled manually for SSRF validation)."""+ with patch(+ "local_deep_research.security.safe_requests.validate_url",+ return_value=True,+ ):+ with patch(+ "local_deep_research.security.safe_requests.requests.post"+ ) as mock_post:+ mock_response = MagicMock()+ mock_response.headers = {}+ mock_post.return_value = mock_response++ safe_post("https://example.com", allow_redirects=True)- NOTE: This is a potential security bug - same as in safe_get().- """+ _, kwargs = mock_post.call_args+ # Redirects are always disabled at the requests level;+ # safe_post handles them manually with SSRF validation+ assert kwargs["allow_redirects"] is False++ def test_oversized_response_raises_error(self):+ """Oversized responses are rejected with ValueError."""with patch("local_deep_research.security.safe_requests.validate_url",return_value=True,@@ -387,9 +402,8 @@ class TestSafePostFunction:}mock_post.return_value = mock_response- # BUG: This should raise ValueError but doesn't- result = safe_post("https://example.com")- assert result == mock_response+ with pytest.raises(ValueError, match="Response too large"):+ safe_post("https://example.com")def test_reraises_timeout(self):"""Should re-raise timeout exceptions."""diff --git a/tests/security/test_ssrf_redirect_bypass.py b/tests/security/test_ssrf_redirect_bypass.pynew file mode 100644index 0000000000..a1cae7a202--- /dev/null+++ b/tests/security/test_ssrf_redirect_bypass.py@@ -0,0 +1,899 @@+"""Tests for SSRF-validated redirect following (PR #1949).++Verifies:+- Manual redirect following with SSRF validation on each hop+- Redirect to internal IPs blocked+- Too many redirects raises ValueError+- Missing Location header stops redirect chain+- Both safe_get and safe_post+"""++import pytest+from unittest.mock import patch, MagicMock++import requests++from local_deep_research.security.safe_requests import (+ safe_get,+ safe_post,+ SafeSession,+ _REDIRECT_STATUS_CODES,+ _MAX_REDIRECTS,+ _resolve_redirect_method,+ MAX_RESPONSE_SIZE,+)+++def mock_validate_url():+ with patch(+ "local_deep_research.security.safe_requests.validate_url",+ return_value=True,+ ) as m:+ yield m+++def _make_response(status_code=200, headers=None, url="https://example.com"):+ resp = MagicMock()+ resp.status_code = status_code+ resp.headers = headers or {}+ resp.url = url+ return resp+++class TestSafeGetRedirectFollowing:+ """Test redirect following in safe_get."""++ def test_no_redirects_when_disabled(self, mock_validate_url):+ """With allow_redirects=False, redirect responses are returned as-is."""+ redirect_resp = _make_response(302, {"Location": "https://other.com"})++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ return_value=redirect_resp,+ ):+ result = safe_get("https://example.com", allow_redirects=False)+ assert result.status_code == 302++ def test_follows_valid_redirect(self, mock_validate_url):+ """With allow_redirects=True, follows redirect to validated target."""+ redirect_resp = _make_response(+ 302, {"Location": "https://other.com"}, "https://example.com"+ )+ final_resp = _make_response(200)++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ side_effect=[redirect_resp, final_resp],+ ):+ result = safe_get("https://example.com", allow_redirects=True)+ assert result.status_code == 200++ def test_blocks_redirect_to_internal_ip(self, mock_validate_url):+ """Redirect to internal IP raises ValueError."""+ redirect_resp = _make_response(+ 302,+ {"Location": "http://169.254.169.254/metadata"},+ "https://example.com",+ )++ # First call validates initial URL (True), second validates redirect target (False)+ mock_validate_url.side_effect = [True, False]++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ return_value=redirect_resp,+ ):+ with pytest.raises(ValueError, match="Redirect target failed SSRF"):+ safe_get("https://example.com", allow_redirects=True)++ def test_too_many_redirects_raises(self, mock_validate_url):+ """More than _MAX_REDIRECTS raises ValueError."""+ redirect_resp = _make_response(+ 301, {"Location": "https://example.com/loop"}, "https://example.com"+ )++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ return_value=redirect_resp,+ ):+ with pytest.raises(ValueError, match="Too many redirects"):+ safe_get("https://example.com", allow_redirects=True)++ def test_missing_location_stops_following(self, mock_validate_url):+ """Redirect without Location header stops the chain."""+ redirect_resp = _make_response(302, {}) # No Location header++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ return_value=redirect_resp,+ ):+ result = safe_get("https://example.com", allow_redirects=True)+ assert result.status_code == 302++ def test_all_redirect_status_codes_followed(self, mock_validate_url):+ """All status codes in _REDIRECT_STATUS_CODES trigger redirect following."""+ for code in _REDIRECT_STATUS_CODES:+ redirect_resp = _make_response(+ code, {"Location": "https://final.com"}, "https://example.com"+ )+ final_resp = _make_response(200)++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ side_effect=[redirect_resp, final_resp],+ ):+ result = safe_get("https://example.com", allow_redirects=True)+ assert result.status_code == 200, f"Failed for status {code}"++ def test_each_hop_validated(self, mock_validate_url):+ """Each redirect hop is validated against SSRF rules."""+ resp1 = _make_response(+ 302, {"Location": "https://hop2.com"}, "https://example.com"+ )+ resp2 = _make_response(+ 302, {"Location": "https://hop3.com"}, "https://hop2.com"+ )+ final = _make_response(200)++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ side_effect=[resp1, resp2, final],+ ):+ safe_get("https://example.com", allow_redirects=True)++ # Initial URL + 2 redirect targets = 3 validate_url calls+ assert mock_validate_url.call_count == 3+ validated_urls = [+ call.args[0] for call in mock_validate_url.call_args_list+ ]+ assert validated_urls == [+ "https://example.com",+ "https://hop2.com",+ "https://hop3.com",+ ]++ def test_default_allow_redirects_follows(self, mock_validate_url):+ """Without explicit allow_redirects, safe_get follows redirects (default True)."""+ redirect_resp = _make_response(+ 302, {"Location": "https://other.com"}, "https://example.com"+ )+ final_resp = _make_response(200)++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ side_effect=[redirect_resp, final_resp],+ ) as mock_get:+ # No allow_redirects kwarg — should still follow+ result = safe_get("https://example.com")+ assert result.status_code == 200+ assert mock_get.call_count == 2++ def test_whitespace_stripped_from_location(self, mock_validate_url):+ """Whitespace in Location header is stripped before following."""+ redirect_resp = _make_response(+ 302,+ {"Location": " https://other.com "},+ "https://example.com",+ )+ final_resp = _make_response(200)++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ side_effect=[redirect_resp, final_resp],+ ) as mock_get:+ safe_get("https://example.com", allow_redirects=True)+ second_url = mock_get.call_args_list[1][0][0]+ assert second_url == "https://other.com"+++class TestSafePostRedirectFollowing:+ """Test redirect following in safe_post."""++ def test_follows_valid_redirect(self, mock_validate_url):+ """safe_post follows redirects when allow_redirects=True."""+ redirect_resp = _make_response(+ 307, {"Location": "https://other.com"}, "https://example.com"+ )+ final_resp = _make_response(200)++ with patch(+ "local_deep_research.security.safe_requests.requests.post",+ side_effect=[redirect_resp, final_resp],+ ):+ result = safe_post("https://example.com", allow_redirects=True)+ assert result.status_code == 200++ def test_blocks_redirect_to_internal_ip(self, mock_validate_url):+ """safe_post blocks redirect to internal IP."""+ redirect_resp = _make_response(+ 307, {"Location": "http://10.0.0.1/internal"}, "https://example.com"+ )++ mock_validate_url.side_effect = [True, False]++ with patch(+ "local_deep_research.security.safe_requests.requests.post",+ return_value=redirect_resp,+ ):+ with pytest.raises(ValueError, match="Redirect target failed SSRF"):+ safe_post("https://example.com", allow_redirects=True)++ def test_no_redirects_when_disabled(self, mock_validate_url):+ """With allow_redirects=False, POST redirect responses returned as-is."""+ redirect_resp = _make_response(307, {"Location": "https://other.com"})++ with patch(+ "local_deep_research.security.safe_requests.requests.post",+ return_value=redirect_resp,+ ):+ result = safe_post("https://example.com", allow_redirects=False)+ assert result.status_code == 307++ def test_too_many_redirects_raises(self, mock_validate_url):+ """More than _MAX_REDIRECTS raises ValueError (307 loop)."""+ redirect_resp = _make_response(+ 307, {"Location": "https://example.com/loop"}, "https://example.com"+ )++ with patch(+ "local_deep_research.security.safe_requests.requests.post",+ return_value=redirect_resp,+ ):+ with pytest.raises(ValueError, match="Too many redirects"):+ safe_post("https://example.com", allow_redirects=True)++ def test_missing_location_stops_following(self, mock_validate_url):+ """Redirect without Location header stops the chain."""+ redirect_resp = _make_response(307, {}) # No Location header++ with patch(+ "local_deep_research.security.safe_requests.requests.post",+ return_value=redirect_resp,+ ):+ result = safe_post("https://example.com", allow_redirects=True)+ assert result.status_code == 307+++class TestRedirectConstants:+ """Verify redirect-related constants."""++ def test_redirect_status_codes(self):+ assert 301 in _REDIRECT_STATUS_CODES+ assert 302 in _REDIRECT_STATUS_CODES+ assert 303 in _REDIRECT_STATUS_CODES+ assert 307 in _REDIRECT_STATUS_CODES+ assert 308 in _REDIRECT_STATUS_CODES+ assert 200 not in _REDIRECT_STATUS_CODES++ def test_max_redirects_is_reasonable(self):+ assert _MAX_REDIRECTS == 10+++class TestResolveRedirectMethod:+ """Unit tests for _resolve_redirect_method helper."""++ def test_303_converts_post_to_get(self):+ assert _resolve_redirect_method("POST", 303) == "GET"++ def test_302_converts_post_to_get(self):+ assert _resolve_redirect_method("POST", 302) == "GET"++ def test_301_converts_post_to_get(self):+ assert _resolve_redirect_method("POST", 301) == "GET"++ def test_307_preserves_post(self):+ assert _resolve_redirect_method("POST", 307) == "POST"++ def test_308_preserves_post(self):+ assert _resolve_redirect_method("POST", 308) == "POST"++ def test_301_preserves_get(self):+ assert _resolve_redirect_method("GET", 301) == "GET"++ def test_302_preserves_put(self):+ assert _resolve_redirect_method("PUT", 302) == "PUT"++ def test_303_preserves_head(self):+ assert _resolve_redirect_method("HEAD", 303) == "HEAD"+++class TestSafePostMethodConversion:+ """Test HTTP method conversion in safe_post redirect loop."""++ def test_post_302_converts_to_get(self, mock_validate_url):+ """POST with 302 redirect should convert to GET."""+ redirect_resp = _make_response(+ 302, {"Location": "https://other.com"}, "https://example.com"+ )+ final_resp = _make_response(200)++ with (+ patch(+ "local_deep_research.security.safe_requests.requests.post",+ return_value=redirect_resp,+ ) as mock_post,+ patch(+ "local_deep_research.security.safe_requests.requests.get",+ return_value=final_resp,+ ) as mock_get,+ ):+ result = safe_post("https://example.com", allow_redirects=True)+ assert result.status_code == 200+ assert mock_post.call_count == 1+ assert mock_get.call_count == 1++ def test_post_303_converts_to_get(self, mock_validate_url):+ """POST with 303 redirect should convert to GET."""+ redirect_resp = _make_response(+ 303, {"Location": "https://other.com"}, "https://example.com"+ )+ final_resp = _make_response(200)++ with (+ patch(+ "local_deep_research.security.safe_requests.requests.post",+ return_value=redirect_resp,+ ) as mock_post,+ patch(+ "local_deep_research.security.safe_requests.requests.get",+ return_value=final_resp,+ ) as mock_get,+ ):+ result = safe_post("https://example.com", allow_redirects=True)+ assert result.status_code == 200+ assert mock_post.call_count == 1+ assert mock_get.call_count == 1++ def test_post_307_preserves_method_and_body(self, mock_validate_url):+ """POST with 307 redirect should preserve method and body."""+ test_data = b"form-data"+ redirect_resp = _make_response(+ 307, {"Location": "https://other.com"}, "https://example.com"+ )+ final_resp = _make_response(200)++ with patch(+ "local_deep_research.security.safe_requests.requests.post",+ side_effect=[redirect_resp, final_resp],+ ) as mock_post:+ result = safe_post(+ "https://example.com", data=test_data, allow_redirects=True+ )+ assert result.status_code == 200+ assert mock_post.call_count == 2+ # Verify body was forwarded on the redirect hop+ assert mock_post.call_args_list[1].kwargs.get("data") == test_data++ def test_post_308_preserves_method_and_body(self, mock_validate_url):+ """POST with 308 redirect should preserve method and body."""+ test_json = {"key": "value"}+ redirect_resp = _make_response(+ 308, {"Location": "https://other.com"}, "https://example.com"+ )+ final_resp = _make_response(200)++ with patch(+ "local_deep_research.security.safe_requests.requests.post",+ side_effect=[redirect_resp, final_resp],+ ) as mock_post:+ result = safe_post(+ "https://example.com", json=test_json, allow_redirects=True+ )+ assert result.status_code == 200+ assert mock_post.call_count == 2+ assert mock_post.call_args_list[1].kwargs.get("json") == test_json++ def test_post_multi_hop_302_then_301(self, mock_validate_url):+ """POST→302→GET→301→GET: both hops use GET."""+ resp_302 = _make_response(+ 302, {"Location": "https://hop2.com"}, "https://example.com"+ )+ resp_301 = _make_response(+ 301, {"Location": "https://hop3.com"}, "https://hop2.com"+ )+ final_resp = _make_response(200)++ with (+ patch(+ "local_deep_research.security.safe_requests.requests.post",+ return_value=resp_302,+ ) as mock_post,+ patch(+ "local_deep_research.security.safe_requests.requests.get",+ side_effect=[resp_301, final_resp],+ ) as mock_get,+ ):+ result = safe_post("https://example.com", allow_redirects=True)+ assert result.status_code == 200+ assert mock_post.call_count == 1+ assert mock_get.call_count == 2+++class TestSafeSessionRedirectFollowing:+ """Test redirect validation via SafeSession.send() override.++ SafeSession validates redirect targets in send(), which the requests+ library calls for each redirect hop via resolve_redirects().+ """++ def test_send_validates_redirect_target(self):+ """SafeSession.send() validates each URL against SSRF rules."""+ session = SafeSession()++ # Build a PreparedRequest pointing to an internal IP+ prep = requests.PreparedRequest()+ prep.prepare_url("http://169.254.169.254/metadata", {})+ prep.prepare_method("GET")++ with patch(+ "local_deep_research.security.safe_requests.validate_url",+ return_value=False,+ ):+ with pytest.raises(ValueError, match="Redirect target failed"):+ session.send(prep)++ def test_send_allows_valid_url(self):+ """SafeSession.send() allows valid external URLs."""+ session = SafeSession()++ prep = requests.PreparedRequest()+ prep.prepare_url("https://example.com", {})+ prep.prepare_method("GET")++ with patch(+ "local_deep_research.security.safe_requests.validate_url",+ return_value=True,+ ):+ with patch.object(+ requests.Session, "send", return_value=_make_response(200)+ ):+ result = session.send(prep)+ assert result.status_code == 200++ def test_send_blocks_localhost_redirect(self):+ """SafeSession.send() blocks redirect to localhost."""+ session = SafeSession()++ prep = requests.PreparedRequest()+ prep.prepare_url("http://127.0.0.1/admin", {})+ prep.prepare_method("GET")++ with patch(+ "local_deep_research.security.safe_requests.validate_url",+ return_value=False,+ ):+ with pytest.raises(ValueError, match="SSRF"):+ session.send(prep)++ def test_send_respects_allow_localhost(self):+ """SafeSession.send() passes allow_localhost to validate_url."""+ session = SafeSession(allow_localhost=True)++ prep = requests.PreparedRequest()+ prep.prepare_url("http://localhost:8080/api", {})+ prep.prepare_method("GET")++ with patch(+ "local_deep_research.security.safe_requests.validate_url",+ return_value=True,+ ) as mock_validate:+ with patch.object(+ requests.Session, "send", return_value=_make_response(200)+ ):+ session.send(prep)+ mock_validate.assert_called_once_with(+ "http://localhost:8080/api",+ allow_localhost=True,+ allow_private_ips=False,+ )++ def test_send_respects_allow_private_ips(self):+ """SafeSession.send() passes allow_private_ips to validate_url."""+ session = SafeSession(allow_private_ips=True)++ prep = requests.PreparedRequest()+ prep.prepare_url("http://192.168.1.1/api", {})+ prep.prepare_method("GET")++ with patch(+ "local_deep_research.security.safe_requests.validate_url",+ return_value=True,+ ) as mock_validate:+ with patch.object(+ requests.Session, "send", return_value=_make_response(200)+ ):+ session.send(prep)+ mock_validate.assert_called_once_with(+ "http://192.168.1.1/api",+ allow_localhost=False,+ allow_private_ips=True,+ )++ def test_request_validates_initial_url(self):+ """SafeSession.request() validates the initial URL."""+ with patch(+ "local_deep_research.security.safe_requests.validate_url",+ return_value=False,+ ):+ session = SafeSession()+ with pytest.raises(ValueError, match="SSRF"):+ session.request("GET", "http://169.254.169.254/metadata")++ def test_send_handles_none_url(self):+ """SafeSession.send() skips SSRF validation when URL is None.++ A PreparedRequest with url=None is unreachable in normal ``requests``+ flow — both ``request()`` and ``resolve_redirects()`` always populate+ the URL before calling ``send()``. The ``if request.url`` guard is a+ defensive measure; this test documents that it does not raise.+ """+ session = SafeSession()++ prep = requests.PreparedRequest()+ # URL is None by default — unreachable in normal flow++ with patch.object(+ requests.Session, "send", return_value=_make_response(200)+ ):+ # Should not raise — None URL skips validation (defensive guard)+ result = session.send(prep)+ assert result.status_code == 200+++class TestResponseCloseOnRedirect:+ """Verify response.close() is called on intermediate and error paths."""++ def test_intermediate_responses_closed_in_chain(self, mock_validate_url):+ """Each intermediate response is closed during a multi-hop redirect."""+ resp1 = _make_response(+ 302, {"Location": "https://hop2.com"}, "https://example.com"+ )+ resp2 = _make_response(+ 302, {"Location": "https://hop3.com"}, "https://hop2.com"+ )+ final = _make_response(200)++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ side_effect=[resp1, resp2, final],+ ):+ safe_get("https://example.com", allow_redirects=True)++ resp1.close.assert_called_once()+ resp2.close.assert_called_once()+ final.close.assert_not_called()++ def test_response_closed_on_ssrf_failure(self, mock_validate_url):+ """Response is closed when a redirect target fails SSRF validation."""+ redirect_resp = _make_response(+ 302,+ {"Location": "http://169.254.169.254/metadata"},+ "https://example.com",+ )++ mock_validate_url.side_effect = [True, False]++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ return_value=redirect_resp,+ ):+ with pytest.raises(ValueError, match="Redirect target failed SSRF"):+ safe_get("https://example.com", allow_redirects=True)++ redirect_resp.close.assert_called_once()++ def test_response_closed_on_too_many_redirects(self, mock_validate_url):+ """Response is closed when too-many-redirects limit is exceeded."""+ redirect_resp = _make_response(+ 301,+ {"Location": "https://example.com/loop"},+ "https://example.com",+ )++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ return_value=redirect_resp,+ ):+ with pytest.raises(ValueError, match="Too many redirects"):+ safe_get("https://example.com", allow_redirects=True)++ # The last response in the chain should be closed+ assert redirect_resp.close.called++ def test_response_closed_on_oversized_content_length(+ self, mock_validate_url+ ):+ """Response is closed when Content-Length exceeds MAX_RESPONSE_SIZE."""+ resp = _make_response(200)+ resp.headers = {"Content-Length": str(MAX_RESPONSE_SIZE + 1)}++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ return_value=resp,+ ):+ with pytest.raises(ValueError, match="Response too large"):+ safe_get("https://example.com")++ resp.close.assert_called_once()++ def test_post_response_closed_on_ssrf_failure(self, mock_validate_url):+ """safe_post closes response when redirect target fails SSRF validation."""+ redirect_resp = _make_response(+ 307,+ {"Location": "http://169.254.169.254/metadata"},+ "https://example.com",+ )++ mock_validate_url.side_effect = [True, False]++ with patch(+ "local_deep_research.security.safe_requests.requests.post",+ return_value=redirect_resp,+ ):+ with pytest.raises(ValueError, match="Redirect target failed SSRF"):+ safe_post("https://example.com", allow_redirects=True)++ redirect_resp.close.assert_called_once()++ def test_post_response_closed_on_too_many_redirects(+ self, mock_validate_url+ ):+ """safe_post closes response when too-many-redirects limit is exceeded."""+ redirect_resp = _make_response(+ 307,+ {"Location": "https://example.com/loop"},+ "https://example.com",+ )++ with patch(+ "local_deep_research.security.safe_requests.requests.post",+ return_value=redirect_resp,+ ):+ with pytest.raises(ValueError, match="Too many redirects"):+ safe_post("https://example.com", allow_redirects=True)++ assert redirect_resp.close.called++ def test_post_response_closed_on_oversized_content_length(+ self, mock_validate_url+ ):+ """safe_post closes response when Content-Length exceeds MAX_RESPONSE_SIZE."""+ resp = _make_response(200)+ resp.headers = {"Content-Length": str(MAX_RESPONSE_SIZE + 1)}++ with patch(+ "local_deep_research.security.safe_requests.requests.post",+ return_value=resp,+ ):+ with pytest.raises(ValueError, match="Response too large"):+ safe_post("https://example.com")++ resp.close.assert_called_once()+++class TestPostBodyNotForwardedOnConversion:+ """POST body must not leak when method converts to GET on 302/303."""++ def test_post_302_then_307_keeps_get_no_body(self, mock_validate_url):+ """POST→302→GET→307→GET: body is dropped and method stays GET.++ The 302 converts POST to GET and drops the body. A subsequent 307+ preserves the (now-GET) method. The body must not reappear on any+ GET hop — this is the real invariant that ``data = None; json = None``+ in safe_post protects.+ """+ resp_302 = _make_response(+ 302, {"Location": "https://hop2.com"}, "https://example.com"+ )+ resp_307 = _make_response(+ 307, {"Location": "https://hop3.com"}, "https://hop2.com"+ )+ final_resp = _make_response(200)++ with (+ patch(+ "local_deep_research.security.safe_requests.requests.post",+ return_value=resp_302,+ ) as mock_post,+ patch(+ "local_deep_research.security.safe_requests.requests.get",+ side_effect=[resp_307, final_resp],+ ) as mock_get,+ ):+ safe_post(+ "https://example.com",+ data=b"secret",+ json={"password": "x"},+ allow_redirects=True,+ )++ # Only one POST (the initial request)+ assert mock_post.call_count == 1+ # Two GETs: the 302→GET hop and the 307→GET hop+ assert mock_get.call_count == 2++ # Neither GET hop should carry body kwargs+ for i, call in enumerate(mock_get.call_args_list):+ _, kwargs = call+ assert "data" not in kwargs, f"GET hop {i} leaked 'data'"+ assert "json" not in kwargs, f"GET hop {i} leaked 'json'"++ def test_post_303_drops_body(self, mock_validate_url):+ """On 303, the converted GET request must not carry data or json."""+ resp_303 = _make_response(+ 303, {"Location": "https://other.com"}, "https://example.com"+ )+ final_resp = _make_response(200)++ with (+ patch(+ "local_deep_research.security.safe_requests.requests.post",+ return_value=resp_303,+ ),+ patch(+ "local_deep_research.security.safe_requests.requests.get",+ return_value=final_resp,+ ) as mock_get,+ ):+ safe_post(+ "https://example.com",+ data=b"secret",+ json={"password": "x"},+ allow_redirects=True,+ )++ # requests.get is called without data/json positional or keyword args+ assert mock_get.call_count == 1+ call_args, call_kwargs = mock_get.call_args+ # Positional: only the URL+ assert call_args == ("https://other.com",)+ assert "data" not in call_kwargs+ assert "json" not in call_kwargs+++class TestSafePostPerHopValidation:+ """Verify validate_url is called for each hop in safe_post."""++ def test_each_hop_validated(self, mock_validate_url):+ """Each redirect hop in safe_post is validated against SSRF rules."""+ resp1 = _make_response(+ 307, {"Location": "https://hop2.com"}, "https://example.com"+ )+ resp2 = _make_response(+ 307, {"Location": "https://hop3.com"}, "https://hop2.com"+ )+ final = _make_response(200)++ with patch(+ "local_deep_research.security.safe_requests.requests.post",+ side_effect=[resp1, resp2, final],+ ):+ safe_post("https://example.com", allow_redirects=True)++ # Initial URL + 2 redirect targets = 3 validate_url calls+ assert mock_validate_url.call_count == 3+ validated_urls = [+ call.args[0] for call in mock_validate_url.call_args_list+ ]+ assert validated_urls == [+ "https://example.com",+ "https://hop2.com",+ "https://hop3.com",+ ]+++class TestRelativeRedirectURL:+ """Test redirect with relative Location header."""++ def test_relative_location_resolved(self, mock_validate_url):+ """Location: /path should be resolved relative to the current URL."""+ redirect_resp = _make_response(+ 302, {"Location": "/new-path"}, "https://example.com/old"+ )+ final_resp = _make_response(200)++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ side_effect=[redirect_resp, final_resp],+ ) as mock_get:+ safe_get("https://example.com/old", allow_redirects=True)++ # The second call should use the resolved absolute URL+ second_call_url = mock_get.call_args_list[1][0][0]+ assert second_call_url == "https://example.com/new-path"++ def test_response_url_none_fallback(self, mock_validate_url):+ """When response.url is None, current_url is used as the base."""+ redirect_resp = _make_response(302, {"Location": "/path"}, url=None)+ final_resp = _make_response(200)++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ side_effect=[redirect_resp, final_resp],+ ) as mock_get:+ safe_get("https://example.com/start", allow_redirects=True)++ second_call_url = mock_get.call_args_list[1][0][0]+ assert second_call_url == "https://example.com/path"+++class TestSafeSessionContentLength:+ """Verify SafeSession.send() rejects oversized responses."""++ def test_send_rejects_oversized_response(self):+ """SafeSession.send() raises ValueError for oversized Content-Length."""+ session = SafeSession()++ prep = requests.PreparedRequest()+ prep.prepare_url("https://example.com", {})+ prep.prepare_method("GET")++ oversized_resp = _make_response(200)+ oversized_resp.headers = {"Content-Length": str(MAX_RESPONSE_SIZE + 1)}++ with patch(+ "local_deep_research.security.safe_requests.validate_url",+ return_value=True,+ ):+ with patch.object(+ requests.Session, "send", return_value=oversized_resp+ ):+ with pytest.raises(ValueError, match="Response too large"):+ session.send(prep)++ oversized_resp.close.assert_called_once()++ def test_send_allows_normal_response(self):+ """SafeSession.send() allows responses within size limit."""+ session = SafeSession()++ prep = requests.PreparedRequest()+ prep.prepare_url("https://example.com", {})+ prep.prepare_method("GET")++ normal_resp = _make_response(200)+ normal_resp.headers = {"Content-Length": "1024"}++ with patch(+ "local_deep_research.security.safe_requests.validate_url",+ return_value=True,+ ):+ with patch.object(+ requests.Session, "send", return_value=normal_resp+ ):+ result = session.send(prep)+ assert result.status_code == 200+++class TestNonWhitelistedStatusCodes:+ """Verify that non-whitelisted 3xx codes are NOT followed as redirects."""++ def test_304_not_followed(self, mock_validate_url):+ """304 Not Modified with Location header is returned as-is, not followed."""+ resp_304 = _make_response(+ 304, {"Location": "https://other.com"}, "https://example.com"+ )++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ return_value=resp_304,+ ) as mock_get:+ result = safe_get("https://example.com", allow_redirects=True)+ assert result.status_code == 304+ # Only one request — the redirect was NOT followed+ assert mock_get.call_count == 1++ def test_300_not_followed(self, mock_validate_url):+ """300 Multiple Choices with Location header is returned as-is, not followed."""+ resp_300 = _make_response(+ 300, {"Location": "https://other.com"}, "https://example.com"+ )++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ return_value=resp_300,+ ) as mock_get:+ result = safe_get("https://example.com", allow_redirects=True)+ assert result.status_code == 300+ assert mock_get.call_count == 1diff --git a/tests/security/test_ssrf_response_size.py b/tests/security/test_ssrf_response_size.pynew file mode 100644index 0000000000..f5a2f87ca7--- /dev/null+++ b/tests/security/test_ssrf_response_size.py@@ -0,0 +1,127 @@+"""Tests for SSRF response size check restructuring (PR #1942).++Verifies try/except/else pattern ensures ValueError from int() parsing+does not mask ValueError from legitimate size-exceeded checks.+"""++import pytest+from unittest.mock import patch, MagicMock++from local_deep_research.security.safe_requests import (+ safe_get,+ safe_post,+ MAX_RESPONSE_SIZE,+)+++def mock_validate_url():+ with patch(+ "local_deep_research.security.safe_requests.validate_url",+ return_value=True,+ ) as m:+ yield m+++class TestSafeGetResponseSize:+ """Verify safe_get Content-Length handling with try/except/else."""++ def test_large_content_length_raises(self, mock_validate_url):+ """A Content-Length exceeding MAX_RESPONSE_SIZE should raise ValueError."""+ mock_resp = MagicMock()+ mock_resp.headers = {"Content-Length": str(MAX_RESPONSE_SIZE + 1)}++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ return_value=mock_resp,+ ):+ with pytest.raises(ValueError, match="Response too large"):+ safe_get("https://example.com")++ def test_valid_content_length_returns_response(self, mock_validate_url):+ """A Content-Length within limit should return the response."""+ mock_resp = MagicMock()+ mock_resp.headers = {"Content-Length": "1024"}++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ return_value=mock_resp,+ ):+ result = safe_get("https://example.com")+ assert result is mock_resp++ def test_invalid_content_length_ignored(self, mock_validate_url):+ """Non-numeric Content-Length should be silently ignored."""+ mock_resp = MagicMock()+ mock_resp.headers = {"Content-Length": "not-a-number"}++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ return_value=mock_resp,+ ):+ result = safe_get("https://example.com")+ assert result is mock_resp++ def test_missing_content_length_returns_response(self, mock_validate_url):+ """Missing Content-Length header should not affect response."""+ mock_resp = MagicMock()+ mock_resp.headers = {}++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ return_value=mock_resp,+ ):+ result = safe_get("https://example.com")+ assert result is mock_resp++ def test_exact_max_size_allowed(self, mock_validate_url):+ """Content-Length exactly at MAX_RESPONSE_SIZE should be allowed."""+ mock_resp = MagicMock()+ mock_resp.headers = {"Content-Length": str(MAX_RESPONSE_SIZE)}++ with patch(+ "local_deep_research.security.safe_requests.requests.get",+ return_value=mock_resp,+ ):+ result = safe_get("https://example.com")+ assert result is mock_resp+++class TestSafePostResponseSize:+ """Verify safe_post Content-Length handling with try/except/else."""++ def test_large_content_length_raises(self, mock_validate_url):+ """A Content-Length exceeding MAX_RESPONSE_SIZE should raise ValueError."""+ mock_resp = MagicMock()+ mock_resp.headers = {"Content-Length": str(MAX_RESPONSE_SIZE + 1)}++ with patch(+ "local_deep_research.security.safe_requests.requests.post",+ return_value=mock_resp,+ ):+ with pytest.raises(ValueError, match="Response too large"):+ safe_post("https://example.com")++ def test_valid_content_length_returns_response(self, mock_validate_url):+ """A Content-Length within limit should return the response."""+ mock_resp = MagicMock()+ mock_resp.headers = {"Content-Length": "512"}++ with patch(+ "local_deep_research.security.safe_requests.requests.post",+ return_value=mock_resp,+ ):+ result = safe_post("https://example.com")+ assert result is mock_resp++ def test_invalid_content_length_ignored(self, mock_validate_url):+ """Non-numeric Content-Length should be silently ignored."""+ mock_resp = MagicMock()+ mock_resp.headers = {"Content-Length": "abc"}++ with patch(+ "local_deep_research.security.safe_requests.requests.post",+ return_value=mock_resp,+ ):+ result = safe_post("https://example.com")+ assert result is mock_resp