fix(gateway): report why /v1/compress/response rejected a body

handle_compress_response caught every body-read failure and answered 'Invalid JSON in request body.'. That covered an oversize body, a failed decompression and invalid UTF-8 too, so an integrator could not tell what was wrong. It now uses helpers.invalid_request_body_message, as the other handlers do. That picks a fixed, CodeQL-safe string by exception type and logs the type. The status (400) and error type (invalid_request) are unchanged.

The isinstance(body, dict) branch after it is removed. _read_request_json raises RequestBodyNotObject for a non-object, so the branch could not be reached.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Assisted-by: claude-code:claude-opus-5-5
Headline-Session: 6e77a807-8dd4-4a77-a2fa-ddce6dc2515e
Headline-AI-Share: 100% strict / 100% weighted
This commit is contained in:
Tejas Chopra
2026-10-09 17:11:45 -07:00
co-authored by Claude Opus 5.5
parent a9815bbb5f
commit ae6a419a26
2 changed files with 23 additions and 5 deletions
+3 -5
View File
@@ -1498,14 +1498,12 @@ async def handle_compress_response(proxy: Any, request: Any) -> Any:
"""``POST /v1/compress/response`` — finish or re-drive a pending turn."""
from fastapi.responses import JSONResponse
from headroom.proxy.helpers import _read_request_json
from headroom.proxy.helpers import _read_request_json, invalid_request_body_message
try:
body = await _read_request_json(request)
except Exception:
return _error(400, "invalid_request", "Invalid JSON in request body.")
if not isinstance(body, dict):
return _error(400, "invalid_request", "Request body must be a JSON object.")
except Exception as exc:
return _error(400, "invalid_request", invalid_request_body_message(exc))
turn_id = body.get("turn_id")
if not isinstance(turn_id, str) or not turn_id.strip() or len(turn_id) > 128:
@@ -104,6 +104,26 @@ def test_invalid_json_is_400(headroom_client) -> None:
assert resp.status_code == 400
@pytest.mark.parametrize(
("content", "reason"),
[
(b"nope", "malformed JSON"),
(b"[1, 2]", "must be a JSON object"),
(b"\xff\xfe{}", "not valid UTF-8"),
],
ids=["malformed", "not-object", "not-utf8"],
)
def test_invalid_body_400_says_why(headroom_client, content: bytes, reason: str) -> None:
"""The 400 names the reason, like every other handler's body errors."""
resp = headroom_client.post(
RESPONSE_HALF, content=content, headers={"content-type": "application/json"}
)
assert resp.status_code == 400
error = resp.json()["error"]
assert error["type"] == "invalid_request"
assert reason in error["message"]
@pytest.mark.parametrize(
"extra",
[