[GH-ISSUE #893] serve/server.py reads POST bodies as empty when sent with Transfer-Encoding: chunked (no Content-Length fallback) — breaks requests through some reverse-proxy/relay agents #392

Closed
opened 2026-11-04 01:42:35 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @oscar-investmatic on GitHub (Oct 5, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/893

Environment: Linux, engine 0.1.39, IQ2_XS, behind a reverse-proxy relay agent (Model Uplink) that tunnels requests over a WebSocket and re-issues them to the local server without a known Content-Length, using Transfer-Encoding: chunked instead.

Bug: Every POST handler in serve/server.py reads the request body the same way:

self.rfile.read(int(self.headers.get("Content-Length", 0)))

When a client/proxy sends Transfer-Encoding: chunked instead of Content-Length (valid per RFC 7230, and common for proxies streaming a body of unknown length), Content-Length is absent, so this reads 0 bytes. The JSON body then parses as empty/{}, and the API returns a misleading 400 invalid_request_error: "No messages provided" instead of actually seeing the request.

Repro (no proxy needed):

curl -s http://127.0.0.1:8080/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Transfer-Encoding: chunked" \
  --data-binary '{"model":"X","messages":[{"role":"user","content":"hi"}],"max_tokens":10}'
# -> 400 "No messages provided", even though a normal (Content-Length) request with the
#    identical payload returns 200

Impact: Any reverse proxy, relay, or load balancer that forwards requests without a precomputed Content-Length (common — many proxies stream the body through before they know its final size) will see every request fail with a confusing "no messages" error, with no indication it's a transport-framing issue.

Fix: added a _read_body() helper to the request Handler that falls back to manually decoding a chunked body (reading <size-hex>\r\n<data>\r\n segments until the terminating 0-size chunk) when Content-Length is missing and Transfer-Encoding: chunked is present, used in do_POST's main dispatch (covers /v1/chat/completions, /v1/messages, etc.). PR incoming. Three other spots (_control_body, _config_post, _settings) read bodies the same unsafe way and would benefit from the same helper for consistency - left them alone here to keep the fix minimal and scoped to the reported bug.

Originally created by @oscar-investmatic on GitHub (Oct 5, 2026). Original GitHub issue: https://github.com/Niko1221/Strata/issues/893 **Environment:** Linux, engine 0.1.39, IQ2_XS, behind a reverse-proxy relay agent (Model Uplink) that tunnels requests over a WebSocket and re-issues them to the local server without a known `Content-Length`, using `Transfer-Encoding: chunked` instead. **Bug:** Every POST handler in `serve/server.py` reads the request body the same way: ```python self.rfile.read(int(self.headers.get("Content-Length", 0))) ``` When a client/proxy sends `Transfer-Encoding: chunked` instead of `Content-Length` (valid per RFC 7230, and common for proxies streaming a body of unknown length), `Content-Length` is absent, so this reads `0` bytes. The JSON body then parses as empty/`{}`, and the API returns a misleading `400 invalid_request_error: "No messages provided"` instead of actually seeing the request. **Repro (no proxy needed):** ```bash curl -s http://127.0.0.1:8080/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Transfer-Encoding: chunked" \ --data-binary '{"model":"X","messages":[{"role":"user","content":"hi"}],"max_tokens":10}' # -> 400 "No messages provided", even though a normal (Content-Length) request with the # identical payload returns 200 ``` **Impact:** Any reverse proxy, relay, or load balancer that forwards requests without a precomputed `Content-Length` (common — many proxies stream the body through before they know its final size) will see every request fail with a confusing "no messages" error, with no indication it's a transport-framing issue. **Fix:** added a `_read_body()` helper to the request `Handler` that falls back to manually decoding a chunked body (reading `<size-hex>\r\n<data>\r\n` segments until the terminating `0`-size chunk) when `Content-Length` is missing and `Transfer-Encoding: chunked` is present, used in `do_POST`'s main dispatch (covers `/v1/chat/completions`, `/v1/messages`, etc.). PR incoming. Three other spots (`_control_body`, `_config_post`, `_settings`) read bodies the same unsafe way and would benefit from the same helper for consistency - left them alone here to keep the fix minimal and scoped to the reported bug.
Author
Owner

@Niko1221 commented on GitHub (Oct 7, 2026):

Fixed in 0.1.40.2 (with #894): a POST body sent as Transfer-Encoding: chunked is decoded on every route that reads a body, with a 256 MiB cap (413) and a 400 for a malformed body. Content-Length requests are unchanged. Thank you for the report.

<!-- gh-comment-id:6037855590 --> @Niko1221 commented on GitHub (Oct 7, 2026): Fixed in 0.1.40.2 (with #894): a POST body sent as Transfer-Encoding: chunked is decoded on every route that reads a body, with a 256 MiB cap (413) and a 400 for a malformed body. Content-Length requests are unchanged. Thank you for the report.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#392
No description provided.