Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

harden: fix security issue in server.py (#3479)

Anupam Mediratta690fb251dc48
diff --git a/headroom/proxy/helpers.py b/headroom/proxy/helpers.py
index 0f9a80f11..9ccadbb8d 100644
--- a/headroom/proxy/helpers.py
+++ b/headroom/proxy/helpers.py
@@ -2954,12 +2954,41 @@ async def _read_request_body_bytes(request: Request) -> bytes:
Mirrors ``_read_request_json`` but returns the bytes pre-parse so
forwarders can implement byte-faithful passthrough (PR-A3, fixes P0-2).
- Raises ``ValueError`` on any decompression failure, and the
- :class:`RequestBodyTooLarge` subclass when the *decompressed* body would
- exceed :data:`MAX_DECOMPRESSED_BODY_SIZE`.
+ Raises the :class:`RequestBodyTooLarge` ``ValueError`` subclass if the
+ wire-size body itself exceeds :data:`MAX_REQUEST_BODY_SIZE` (checked while
+ streaming, before the full body is buffered) or if the *decompressed*
+ body would exceed :data:`MAX_DECOMPRESSED_BODY_SIZE`. Raises plain
+ ``ValueError`` on any other decompression failure.
"""
encoding = (request.headers.get("content-encoding") or "").lower().strip()
- raw = await request.body()
+
+ # Content-Length is an optimization only, not the enforcement boundary: it
+ # can be absent, understated, or belong to a chunked transfer. The
+ # streaming loop below is what actually bounds every case (#3479).
+ content_length = request.headers.get("content-length")
+ if content_length is not None:
+ try:
+ declared = int(content_length)
+ except ValueError:
+ declared = None
+ if declared is not None and declared > MAX_REQUEST_BODY_SIZE:
+ raise RequestBodyTooLarge(
+ f"Request body exceeds {MAX_REQUEST_BODY_SIZE // (1024 * 1024)}MB "
+ f"(Content-Length: {declared})"
+ )
+
+ chunks = bytearray()
+ async for chunk in request.stream():
+ chunks.extend(chunk)
+ if len(chunks) > MAX_REQUEST_BODY_SIZE:
+ raise RequestBodyTooLarge(
+ f"Request body exceeds {MAX_REQUEST_BODY_SIZE // (1024 * 1024)}MB"
+ )
+ raw: bytes = bytes(chunks)
+ # Cache like Starlette's own body() would, so any other .body() caller on
+ # this request (there is none today, but future callers get the same
+ # semantics) sees the bytes already read rather than a consumed stream.
+ request._body = raw
# Every branch below decompresses incrementally against
# MAX_DECOMPRESSED_BODY_SIZE. RequestBodyTooLarge is re-raised ahead of the
diff --git a/tests/test_output_only_request_blocks.py b/tests/test_output_only_request_blocks.py
index 2f624f893..1df9924e8 100644
--- a/tests/test_output_only_request_blocks.py
+++ b/tests/test_output_only_request_blocks.py
@@ -38,6 +38,9 @@ class _FakeRequest:
async def body(self):
return self._raw
+ async def stream(self):
+ yield self._raw
+
def _has_fallback(messages):
for msg in messages:
diff --git a/tests/test_proxy_handler_helpers.py b/tests/test_proxy_handler_helpers.py
index e151c79c4..910ce58fd 100644
--- a/tests/test_proxy_handler_helpers.py
+++ b/tests/test_proxy_handler_helpers.py
@@ -141,6 +141,9 @@ class _VertexGeminiImageRequest:
query="",
)
+ async def stream(self):
+ yield await self.body()
+
async def body(self) -> bytes:
return json.dumps(
{
diff --git a/tests/test_proxy_handlers_batch.py b/tests/test_proxy_handlers_batch.py
index 504e0c6f1..4d1383a09 100644
--- a/tests/test_proxy_handlers_batch.py
+++ b/tests/test_proxy_handlers_batch.py
@@ -163,6 +163,10 @@ class FakeRequest:
async def body(self) -> bytes:
return self._body
+ async def stream(self):
+ # The body reader streams (so it can cap chunked bodies) like Starlette.
+ yield self._body
+
class NativeGeminiHandler(DummyBatchHandler):
def __init__(self, responses: list[FakeResponse]) -> None:
diff --git a/tests/test_request_body_decompression_limits.py b/tests/test_request_body_decompression_limits.py
index 3b4e1be7d..7851ba4ca 100644
--- a/tests/test_request_body_decompression_limits.py
+++ b/tests/test_request_body_decompression_limits.py
@@ -73,13 +73,30 @@ def _deflate_bomb(total: int = BOMB_PLAIN_SIZE) -> bytes:
class _Request:
"""Minimal stand-in for the Starlette Request the reader actually takes."""
- def __init__(self, body: bytes, content_encoding: str = "") -> None:
+ def __init__(
+ self, body: bytes, content_encoding: str = "", headers: dict | None = None
+ ) -> None:
self._body = body
- self.headers = {"content-encoding": content_encoding}
+ self.headers = {"content-encoding": content_encoding, **(headers or {})}
async def body(self) -> bytes:
return self._body
+ async def stream(self):
+ yield self._body
+
+
+class _NeverStreamedRequest(_Request):
+ """A request whose ``.stream()`` fails if ever iterated.
+
+ Used to prove a fast-path (e.g. a Content-Length pre-check) rejects
+ before touching the body, rather than merely rejecting eventually.
+ """
+
+ async def stream(self):
+ raise AssertionError("stream() should not have been read")
+ yield b"" # pragma: no cover - unreachable, keeps this an async generator
+
@pytest.fixture
def small_cap(monkeypatch: pytest.MonkeyPatch) -> int:
@@ -87,6 +104,12 @@ def small_cap(monkeypatch: pytest.MonkeyPatch) -> int:
return SMALL_CAP
+def small_raw_cap(monkeypatch: pytest.MonkeyPatch) -> int:
+ monkeypatch.setattr(_helpers(), "MAX_REQUEST_BODY_SIZE", SMALL_CAP)
+ return SMALL_CAP
+
+
# ───────────────────────────── the ceiling itself ──────────────────────────
@@ -298,3 +321,53 @@ async def test_reader_leaves_uncompressed_bodies_alone(small_cap: int) -> None:
async def test_reader_still_rejects_an_unknown_encoding() -> None:
with pytest.raises(ValueError, match="Unsupported Content-Encoding"):
await _helpers()._read_request_body_bytes(_Request(PAYLOAD, "snappy"))
+
+
+# ────────────────── the raw (pre-decompression) wire size is bounded too ────
+#
+# A plain (identity-encoded) body was never bounded at all: the old reader did
+# ``raw = await request.body()`` and only checked size *after* decompression,
+# which is a no-op for identity bodies. #3479.
+
+
+async def test_reader_refuses_an_oversized_plain_body(small_raw_cap: int) -> None:
+ oversized = b"x" * (small_raw_cap + 1)
+ with pytest.raises(_helpers().RequestBodyTooLarge):
+ await _helpers()._read_request_body_bytes(_Request(oversized, ""))
+
+
+async def test_reader_accepts_a_plain_body_at_the_cap(small_raw_cap: int) -> None:
+ at_cap = b"x" * small_raw_cap
+ assert await _helpers()._read_request_body_bytes(_Request(at_cap, "")) == at_cap
+
+
+async def test_reader_honors_a_truthful_content_length_before_reading(
+ small_raw_cap: int,
+) -> None:
+ """A Content-Length that already overstates the cap must short-circuit.
+
+ ``_NeverStreamedRequest.stream()`` raises if ever iterated, so this only
+ passes if the Content-Length check runs (and rejects) before the body is
+ ever read.
+ """
+ request = _NeverStreamedRequest(b"", "", headers={"content-length": str(small_raw_cap + 1)})
+ with pytest.raises(_helpers().RequestBodyTooLarge):
+ await _helpers()._read_request_body_bytes(request)
+
+
+async def test_reader_ignores_an_untrustworthy_content_length(small_raw_cap: int) -> None:
+ """Content-Length is an optimization, not the enforcement boundary.
+
+ A header that understates (or lies about) the body's real size must not
+ let an oversized body through -- the streaming check still catches it.
+ """
+ oversized = b"x" * (small_raw_cap + 1)
+ request = _Request(oversized, "", headers={"content-length": "1"})
+ with pytest.raises(_helpers().RequestBodyTooLarge):
+ await _helpers()._read_request_body_bytes(request)
+
+ # A non-numeric header is likewise not trusted -- it neither
+ # short-circuits acceptance nor raises on its own; the stream still runs.
+ request = _Request(oversized, "", headers={"content-length": "not-a-number"})
+ with pytest.raises(_helpers().RequestBodyTooLarge):
+ await _helpers()._read_request_body_bytes(request)