Repository navigation
Conversation
On a 2xx response with Content-Type application/json, `HTTPClientTransport.processResponse` read the body with `ResponseBody.collect()`, whose limit defaults to `Int.max`. A server could send a body of any size, or one that never ends, and the client kept all of it in memory. The SSE path already fails a line or the data of an event longer than 16 MiB, and the error body of a 4xx reply is read up to 1 MiB, so answering with application/json instead of text/event-stream bypassed the transport's limit. Fix: an application/json body longer than 16 MiB, the limit of one SSE event, now fails the request with `internalError` and its download is cancelled. A `Content-Length` over the limit is refused before the body is read; otherwise the read stops one byte past the limit. A body of at most 16 MiB is delivered unchanged. The class documentation, the migration guide and the threat model say so. Adds four tests to `HTTPClientTransportTests`: a 40 MiB body, a Content-Length over the limit, a body one byte over the limit, and a body of exactly 16 MiB. The first three fail without the fix.
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.
Motivation and Context
On a 2xx response with
Content-Type: application/json,HTTPClientTransportread thebody with
ResponseBody.collect(), whose limit defaults toInt.max. A server could send abody of any size, or one that never ends, and the client kept all of it in memory until the
request finished or the process ran out of memory. The SSE path already fails a line or the
data of an event longer than 16 MiB, and the error body of a 4xx reply is read up to 1 MiB,
so answering with
application/jsoninstead oftext/event-streambypassed thetransport's limit.
application/jsonbody longer than 16 MiB, the limit of one SSE event, now fails therequest with
internalError("JSON response body longer than 16 MiB"), and its downloadis cancelled.
Content-Lengthover the limit is refused before the body is read. Without one, theread stops one byte past the limit.
HTTPClientTransportdocumentation, the migration guide and the threat model notethe limit.
How Has This Been Tested?
Adds four tests to
HTTPClientTransportTests("HTTP client transport sessions"), using aURLProtocolthat streams the body in 1 MiB chunks and records how much of it the clientread: a 40 MiB body fails and the client stops after 16 MiB plus one chunk, a
Content-Lengthover the limit fails before any byte is sent, a body one byte over thelimit fails, and a body of exactly 16 MiB with a matching
Content-Lengthis deliveredbyte for byte. Without the fix the first three fail (the whole 40 MiB is read and delivered
as one message).
swift testpasses on macOS. I did not run the tests on Linux.Breaking Changes
None in the API. A server that answers with an
application/jsonbody larger than 16 MiBnow gets the request failed with
internalError; such a body was read whole before, as anSSE event of that size already fails.
Types of changes
AI Assistance
Drafted with an AI coding agent, then reviewed by me.