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: remove early-return guard in _install_body_guard for invalid Content-Length (#2654)

LearningCircuitca37a9e323b0
diff --git a/src/local_deep_research/security/safe_requests.py b/src/local_deep_research/security/safe_requests.py
index e401891775..7879eb3d4c 100644
--- a/src/local_deep_research/security/safe_requests.py
+++ b/src/local_deep_research/security/safe_requests.py
@@ -27,19 +27,16 @@ _MAX_REDIRECTS = 10
def _install_body_guard(response: requests.Response) -> None:
- """Install a bounded reader on responses without Content-Length.
+ """Install a bounded reader that enforces MAX_RESPONSE_SIZE.
Wraps response.raw.read() to track cumulative bytes and raise
ValueError if MAX_RESPONSE_SIZE is exceeded during body consumption.
This transparently protects both streamed (.iter_content) and
non-streamed (.text, .json(), .content) access patterns.
- No-op when Content-Length is present (header check is sufficient).
+ Callers decide when to install the guard (e.g. when Content-Length
+ is absent, invalid, or negative). This function always installs.
"""
- content_length = response.headers.get("Content-Length")
- if content_length:
- return # Header-based check handles this case
-
original_read = response.raw.read
bytes_read = 0
diff --git a/tests/security/test_ssrf_response_size.py b/tests/security/test_ssrf_response_size.py
index f2026b6f76..1d8fe03f2f 100644
--- a/tests/security/test_ssrf_response_size.py
+++ b/tests/security/test_ssrf_response_size.py
@@ -456,16 +456,40 @@ class TestBodySizeEnforcement:
with pytest.raises(ValueError, match="Response body too large"):
resp.raw.read()
+ resp.close.assert_called_once()
- def test_guard_not_installed_when_cl_present(self):
- """With Content-Length header, raw.read is not wrapped."""
+ def test_guard_installed_even_when_cl_present(self):
+ """Guard always installs — callers decide when to call it."""
body = b"x" * 100
resp = self._make_response(body, headers={"Content-Length": "100"})
original_read = resp.raw.read
_install_body_guard(resp)
- # read function should be unchanged
- assert resp.raw.read is original_read
+ # read function should be wrapped
+ assert resp.raw.read is not original_read
+ # but small body still reads fine
+ data = resp.raw.read()
+ assert data == body
+
+ def test_invalid_cl_gets_body_guard(self):
+ """Invalid Content-Length must still get body guard via _check_response_size."""
+ body = b"x" * (MAX_RESPONSE_SIZE + 1)
+ resp = self._make_response(body, headers={"Content-Length": "abc"})
+ _install_body_guard(resp)
+
+ with pytest.raises(ValueError, match="Response body too large"):
+ resp.raw.read()
+ resp.close.assert_called_once()
+
+ def test_negative_cl_gets_body_guard(self):
+ """Negative Content-Length must still get body guard via _check_response_size."""
+ body = b"x" * (MAX_RESPONSE_SIZE + 1)
+ resp = self._make_response(body, headers={"Content-Length": "-1"})
+ _install_body_guard(resp)
+
+ with pytest.raises(ValueError, match="Response body too large"):
+ resp.raw.read()
+ resp.close.assert_called_once()
def test_streamed_access_exceeds_limit(self):
"""Chunked reads that cumulatively exceed limit raise ValueError."""