Skip to content

fix: limit JSON response bodies to 16 MiB in the HTTP client - #406

Open
canarik wants to merge 1 commit into
modelcontextprotocol:rel/1.0.0from
canarik:oss/http-client-json-body-limit
Open

canarik wants to merge 1 commit into
modelcontextprotocol:rel/1.0.0from
canarik:oss/http-client-json-body-limit

Conversation

@canarik

@canarik canarik commented Nov 29, 2026

Copy link
Copy Markdown

Motivation and Context

On a 2xx response with Content-Type: application/json, HTTPClientTransport 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 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/json instead of text/event-stream bypassed the
transport's limit.

  • An application/json body longer than 16 MiB, the limit of one SSE event, now fails the
    request with internalError("JSON response body longer than 16 MiB"), and its download
    is cancelled.
  • A Content-Length over the limit is refused before the body is read. Without one, the
    read stops one byte past the limit.
  • A body of at most 16 MiB is delivered unchanged, as before.
  • The HTTPClientTransport documentation, the migration guide and the threat model note
    the limit.

How Has This Been Tested?

Adds four tests to HTTPClientTransportTests ("HTTP client transport sessions"), using a
URLProtocol that streams the body in 1 MiB chunks and records how much of it the client
read: a 40 MiB body fails and the client stops after 16 MiB plus one chunk, a
Content-Length over the limit fails before any byte is sent, a body one byte over the
limit fails, and a body of exactly 16 MiB with a matching Content-Length is delivered
byte for byte. Without the fix the first three fail (the whole 40 MiB is read and delivered
as one message). swift test passes on macOS. I did not run the tests on Linux.

Breaking Changes

None in the API. A server that answers with an application/json body larger than 16 MiB
now gets the request failed with internalError; such a body was read whole before, as an
SSE event of that size already fails.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

AI Assistance

Drafted with an AI coding agent, then reviewed by me.

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.
@canarik
canarik requested a review from a team as a code owner November 29, 2026 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant