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.

fix: handle multiple Content-Length values in SSRF size check (#2621)

LearningCircuit486b97929c11
diff --git a/src/local_deep_research/security/safe_requests.py b/src/local_deep_research/security/safe_requests.py
index b5ad17937d..f68f3f9d8d 100644
--- a/src/local_deep_research/security/safe_requests.py
+++ b/src/local_deep_research/security/safe_requests.py
@@ -29,27 +29,47 @@ _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. A complete solution would require streaming with iter_content()
- and counting bytes, which is out of scope here.
+ Handles comma-separated values per RFC 7230 §3.3.2: identical
+ duplicates (from proxies) are normalized; differing values are
+ rejected as invalid framing. Empty parts from malformed headers
+ (trailing/doubled commas) are filtered before parsing. Non-integer
+ or negative values cause the header to be treated as absent.
- Must be called before returning a response to the caller. On rejection
- the response is closed so the connection is not leaked.
+ Only checks the Content-Length header; chunked transfer encoding
+ responses are not bounded by this check.
+
+ Must be called before returning a response to the caller. On
+ rejection the response is closed to avoid leaking the connection.
+
+ Raises:
+ ValueError: If Content-Length values conflict or exceed
+ MAX_RESPONSE_SIZE.
"""
content_length = response.headers.get("Content-Length")
if content_length:
try:
- size = int(content_length)
+ # Handle comma-separated Content-Length values (RFC 7230 §3.3.2).
+ # Multiple identical values may be sent by proxies; differing
+ # values indicate invalid framing and must be rejected.
+ raw_parts = [v.strip() for v in content_length.split(",")]
+ parts = [p for p in raw_parts if p]
+ if not parts:
+ return # Only commas/whitespace — treat as absent
+ sizes = [int(p) for p in parts]
except (ValueError, TypeError):
return # Content-Length not a valid number
+ if len(set(sizes)) > 1:
+ response.close()
+ raise ValueError(
+ f"Conflicting Content-Length values: {content_length}"
+ )
+ size = sizes[0]
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})"
+ f"Response too large: {size} bytes (max {MAX_RESPONSE_SIZE})"
)
diff --git a/tests/security/test_ssrf_response_size.py b/tests/security/test_ssrf_response_size.py
index f5a2f87ca7..e19201da19 100644
--- a/tests/security/test_ssrf_response_size.py
+++ b/tests/security/test_ssrf_response_size.py
@@ -29,6 +29,7 @@ class TestSafeGetResponseSize:
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.status_code = 200
mock_resp.headers = {"Content-Length": str(MAX_RESPONSE_SIZE + 1)}
with patch(
@@ -37,10 +38,12 @@ class TestSafeGetResponseSize:
):
with pytest.raises(ValueError, match="Response too large"):
safe_get("https://example.com")
+ mock_resp.close.assert_called_once()
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.status_code = 200
mock_resp.headers = {"Content-Length": "1024"}
with patch(
@@ -53,6 +56,7 @@ class TestSafeGetResponseSize:
def test_invalid_content_length_ignored(self, mock_validate_url):
"""Non-numeric Content-Length should be silently ignored."""
mock_resp = MagicMock()
+ mock_resp.status_code = 200
mock_resp.headers = {"Content-Length": "not-a-number"}
with patch(
@@ -65,6 +69,7 @@ class TestSafeGetResponseSize:
def test_missing_content_length_returns_response(self, mock_validate_url):
"""Missing Content-Length header should not affect response."""
mock_resp = MagicMock()
+ mock_resp.status_code = 200
mock_resp.headers = {}
with patch(
@@ -77,6 +82,7 @@ class TestSafeGetResponseSize:
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.status_code = 200
mock_resp.headers = {"Content-Length": str(MAX_RESPONSE_SIZE)}
with patch(
@@ -86,6 +92,144 @@ class TestSafeGetResponseSize:
result = safe_get("https://example.com")
assert result is mock_resp
+ def test_comma_separated_identical_content_length(self, mock_validate_url):
+ """Identical comma-separated Content-Length values should be accepted."""
+ mock_resp = MagicMock()
+ mock_resp.status_code = 200
+ mock_resp.headers = {"Content-Length": "1024, 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_comma_separated_different_content_length_rejected(
+ self, mock_validate_url
+ ):
+ """Differing comma-separated Content-Length values should be rejected."""
+ mock_resp = MagicMock()
+ mock_resp.status_code = 200
+ mock_resp.headers = {"Content-Length": "100, 200"}
+
+ with patch(
+ "local_deep_research.security.safe_requests.requests.get",
+ return_value=mock_resp,
+ ):
+ with pytest.raises(ValueError, match="Conflicting"):
+ safe_get("https://example.com")
+ mock_resp.close.assert_called_once()
+
+ def test_comma_separated_oversized_content_length(self, mock_validate_url):
+ """Identical but oversized comma-separated values should be rejected."""
+ mock_resp = MagicMock()
+ mock_resp.status_code = 200
+ mock_resp.headers = {
+ "Content-Length": f"{MAX_RESPONSE_SIZE + 1}, {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")
+ mock_resp.close.assert_called_once()
+
+ def test_zero_content_length_accepted(self, mock_validate_url):
+ """Zero Content-Length should be accepted."""
+ mock_resp = MagicMock()
+ mock_resp.status_code = 200
+ mock_resp.headers = {"Content-Length": "0"}
+
+ 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_negative_content_length_ignored(self, mock_validate_url):
+ """Negative Content-Length should be treated as absent."""
+ mock_resp = MagicMock()
+ mock_resp.status_code = 200
+ mock_resp.headers = {"Content-Length": "-1"}
+
+ 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_trailing_comma_within_limit_accepted(self, mock_validate_url):
+ """Trailing comma with valid size should be accepted."""
+ mock_resp = MagicMock()
+ mock_resp.status_code = 200
+ mock_resp.headers = {"Content-Length": "100,"}
+
+ 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_trailing_comma_oversized_raises(self, mock_validate_url):
+ """Trailing comma must not bypass size check for oversized values."""
+ mock_resp = MagicMock()
+ mock_resp.status_code = 200
+ mock_resp.headers = {"Content-Length": f"{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")
+ mock_resp.close.assert_called_once()
+
+ def test_double_comma_conflicting_values_rejected(self, mock_validate_url):
+ """Double comma between values must not mask a conflict."""
+ mock_resp = MagicMock()
+ mock_resp.status_code = 200
+ mock_resp.headers = {"Content-Length": "100,,200"}
+
+ with patch(
+ "local_deep_research.security.safe_requests.requests.get",
+ return_value=mock_resp,
+ ):
+ with pytest.raises(ValueError, match="Conflicting"):
+ safe_get("https://example.com")
+ mock_resp.close.assert_called_once()
+
+ def test_only_commas_treated_as_absent(self, mock_validate_url):
+ """Header containing only commas should be treated as absent."""
+ mock_resp = MagicMock()
+ mock_resp.status_code = 200
+ mock_resp.headers = {"Content-Length": ","}
+
+ 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_trailing_comma_identical_values_accepted(self, mock_validate_url):
+ """Identical values with trailing comma should be accepted."""
+ mock_resp = MagicMock()
+ mock_resp.status_code = 200
+ mock_resp.headers = {"Content-Length": "100, 100,"}
+
+ 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."""
@@ -93,6 +237,7 @@ class TestSafePostResponseSize:
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.status_code = 200
mock_resp.headers = {"Content-Length": str(MAX_RESPONSE_SIZE + 1)}
with patch(
@@ -101,10 +246,12 @@ class TestSafePostResponseSize:
):
with pytest.raises(ValueError, match="Response too large"):
safe_post("https://example.com")
+ mock_resp.close.assert_called_once()
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.status_code = 200
mock_resp.headers = {"Content-Length": "512"}
with patch(
@@ -117,6 +264,7 @@ class TestSafePostResponseSize:
def test_invalid_content_length_ignored(self, mock_validate_url):
"""Non-numeric Content-Length should be silently ignored."""
mock_resp = MagicMock()
+ mock_resp.status_code = 200
mock_resp.headers = {"Content-Length": "abc"}
with patch(
@@ -125,3 +273,17 @@ class TestSafePostResponseSize:
):
result = safe_post("https://example.com")
assert result is mock_resp
+
+ def test_trailing_comma_oversized_raises(self, mock_validate_url):
+ """Trailing comma must not bypass size check for oversized values."""
+ mock_resp = MagicMock()
+ mock_resp.status_code = 200
+ mock_resp.headers = {"Content-Length": f"{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")
+ mock_resp.close.assert_called_once()