Decode chunked request bodies; reject invalid framing per RFC 9112#210
Merged
swhitty merged 2 commits intoApr 26, 2026
Merged
Conversation
HTTPDecoder.readBody now honors Transfer-Encoding: chunked (RFC 9112 §7.1) by routing the body through a new HTTPChunkedTransferDecoder, the read-side mirror of HTTPChunkedTransferEncoder. Trailer fields are consumed and discarded. readBody also throws HTTPDecoder.Error on framing violations: - both Content-Length and Transfer-Encoding present (§6.1) - non-numeric or negative Content-Length (§6.3 swhitty#5) - Transfer-Encoding whose final coding is not `chunked` (§6.1) Throwing surfaces as a connection-close (RFC-permitted for unrecoverable framing errors); HTTPServer.handleConnection has a TODO to upgrade this to an explicit 400 Bad Request response in a future change. readBody's signature changes from (from:length:) to (from:contentLength:transferEncoding:). Both decodeRequest and decodeResponse pass headers[.contentLength] and headers[.transferEncoding]. Closes TVT-287 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #210 +/- ##
==========================================
+ Coverage 92.67% 92.74% +0.06%
==========================================
Files 69 70 +1
Lines 3551 3638 +87
==========================================
+ Hits 3291 3374 +83
- Misses 260 264 +4 ☔ View full report in Codecov by Sentry. |
Six additional HTTPDecoderTests targeting the uncovered branches in HTTPChunkedTransferDecoder and HTTPDecoder.readBody: - chunkExt_IsIgnored (RFC 9112 §7.1: chunk-ext after `;` is parsed but ignored) - invalidChunkSize_ThrowsError (non-hex chunk-size) - missingCRLFAfterChunkData_ThrowsError (chunk-data not followed by CRLF) - truncatedChunkSize_ThrowsError / truncatedChunkData_ThrowsError (stream ends mid-line / mid-chunk -> SocketError.disconnected) - unsupportedTransferEncoding_ThrowsError (e.g. `gzip`) Lifts HTTPChunkedDecodedSequence.swift to 86.79%/88.73% region/line coverage and HTTPDecoder.swift's TVT-287 additions to 100%. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
swhitty
approved these changes
Apr 26, 2026
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.
Summary
HTTPDecoder.readBodynow honorsTransfer-Encoding: chunked(RFC 9112 §7.1) by routing the body through a newHTTPChunkedTransferDecoder— the read-side mirror of the existingHTTPChunkedTransferEncoder. Trailer fields (RFC 9112 §7.1.2) are consumed and discarded.readBodyalso throwsHTTPDecoder.Erroron framing violations:Content-LengthandTransfer-Encodingpresent (§6.1, smuggling prevention)Content-Length(§6.3 Added most of the HTTP status codes and phrases #5, "unrecoverable error")Transfer-Encodingwhose final coding is notchunked(§6.1)HTTPServer.handleConnectionhas a// TODOto upgrade this to an explicit400 Bad Requestresponse in a future change.readBody's signature changes from(from:length:)to(from:contentLength:transferEncoding:). BothdecodeRequestanddecodeResponsenow pass the relevant headers.Closes TVT-287.
Test plan
HTTPDecoderTestscases covering chunked multi-chunk decode, terminator-only, trailer ignore, conflicting CL+TE, non-numeric CL, negative CL.readBody/test-helper call sites updated to the new signature.