Fix: ds4-server rejects HTTP requests using Transfer-Encoding: chunked#423
Open
moritzburgard wants to merge 1 commit into
Open
Fix: ds4-server rejects HTTP requests using Transfer-Encoding: chunked#423moritzburgard wants to merge 1 commit into
moritzburgard wants to merge 1 commit into
Conversation
The read_http_request() function determines request body length solely from Content-Length. When a client uses Transfer-Encoding: chunked without Content-Length (RFC 7230 §3.3.3), the body is empty and the JSON parser fails with 'invalid JSON request'. This adds has_chunked_transfer_encoding() to detect the header and a chunked body decoder in read_http_request(). Requests with Content-Length continue on the original fast path.
a4fe1d1 to
4b4aeb5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug: ds4-server rejects HTTP requests using
Transfer-Encoding: chunkedThe
ds4-serverHTTP API (/v1/chat/completions,/v1/messages, etc.) rejects POST requests that use chunked transfer encoding with a400 Bad Requestand{"error":{"message":"invalid JSON request"}}. Requests with an explicitContent-Lengthheader work correctly.This affects any client that sends chunked requests — notably the Roo Code VS Code extension, which uses Chromium's networking stack and may send chunked requests even when the OpenAI SDK would normally set
Content-Length.Root cause
read_http_request()inds4_server.cuses a custom HTTP parser that determines the request body length solely from theContent-Lengthheader. WhenContent-Lengthis absent and the client usesTransfer-Encoding: chunked,content_length()returns0. The parser reads zero bytes of body, leaving the request body empty. The JSON parser then fails, producing the "invalid JSON request" error.Per RFC 7230 section 3.3.3, a client may send
Transfer-Encoding: chunkedwithoutContent-Length. A compliant server must handle this.Fix
Added
has_chunked_transfer_encoding()— scans headers forTransfer-Encodingwith valuechunked(case-insensitive, correctly matching the final encoding token to avoid false positives on values likegzip, chunked). Modifiedread_http_request()to detect chunked encoding and, when present, incrementally read and decode the chunked body in a single linear pass.Key properties of the implementation:
Transfer-Encoding: chunkedandContent-Lengthstrtoloutput; invalid hex or out-of-range sizes produce a400 Bad Requestimmediately\r\nvs\nchunk terminators dynamicallyVerification
Before the fix, a Node.js POST without
Content-Lengthreturned 400:A raw chunked request via netcat also works:
Backward compatibility
Requests with
Content-Lengthcontinue to use the original fast path. Chunked requests incur a small scan-and-copy overhead to reassemble the body. No existing functionality is affected.Note: The initial bug diagnosis was developed with ds4-agent. The implementation was reviewed for RFC compliance, memory safety, and correctness by Antigravity (Google DeepMind), which identified and fixed additional edge cases including TCP packet boundary bugs, trailer handling, and request smuggling.