Repository navigation
v1: Cap upstream SSE line and event size in the proxy (GHSA-3xgv-mvj6-g78r) - #2998
Merged
Merged
Conversation
The proxy hands every upstream event stream to the MCP SDK, which parses it with eventsource-parser. That parser buffers the current line until it sees a CR or LF, and neither eventsource (sse transport) nor EventSourceParserStream (streamable-http) sets its opt-in maxBufferSize. An upstream that sends "data: " followed by an endless unterminated run of bytes therefore grew the proxy's heap until the process crashed, dropping every session. createCustomFetch now pipes text/event-stream bodies through a transform that counts bytes since the last line terminator and errors the stream once that passes MAX_SSE_LINE_BYTES (32 MiB). The one connection fails with an error naming the cap, and the upstream socket is released. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
Match the v2 fix. The cap drops to 16 MiB: four times the 4 MB inbound message limit of the SDK's reference servers, roughly 12 MiB of binary after base64, and a bound on what a hostile upstream can make the parser hold per stream per reconnect. The SDK's Streamable HTTP standalone GET (_startOrAuthSse) hands any 2xx body to the SSE parser without checking Content-Type, so gating the cap on text/event-stream alone let a mislabelled stream bypass it. The proxy now also caps any 2xx GET whose request asked for text/event-stream, whatever the response Content-Type. Error statuses, the 401 path and JSON POST replies are untouched. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
eventsource-parser appends every data: line to the pending event and releases it only at a blank line, so an upstream sending short, terminated data: lines with no blank line passed the per-line cap while the parser's pending event grew without limit. The transform now also counts non-terminator bytes since the last blank line and errors the stream past the same 16 MiB cap, with a message that names the event limit. CRLF is one terminator even when split across chunks, so a split CRLF is never read as a blank line. The doc comment for createSseLineCapTransform is moved back onto it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
This was
linked to
issues
Nov 18, 2026
This was referenced Nov 18, 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.
Closes #2992
Advisory: GHSA-3xgv-mvj6-g78r (v1; v2 is fixed separately for #2991).
Problem
A malicious upstream on the
sse(orstreamable-http) transport can open an event stream and then senddata:followed by an endless run of bytes with no line terminator. The proxy hands that body to the v1 SDK, which parses it witheventsource-parser. The parser buffers the partial line until it sees CR or LF and has no limit unlessmaxBufferSizeis passed, and neithereventsource3.0.7 (sse transport) norEventSourceParserStream(streamable-http) passes it. The proxy heap grows until the process OOMs, dropping every session.The same parser also appends every
data:line to the pending event and frees it only at a blank line. So an upstream sending short, terminateddata:lines with no blank line grows the pending event without limit, even though every individual line is short.Reproduced on unpatched
v1/main(1.0.5, SDK 1.29.0):--max-old-space-size=1024:FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memoryafter about 887 MiB.Fix
createCustomFetchalready turns SSE bodies into web streams before the SDK sees them. That stream now goes through a pass-through transform when the response is 2xx and either its content-type istext/event-streamor it is a GET whose request asked fortext/event-stream. The second case covers mislabelled streams: the SDK's Streamable HTTP standalone GET (_startOrAuthSse,client/streamableHttp.js:94-107) passes any 2xx body to the SSE parser without checking content-type. JSON POST replies and error statuses are left alone. The body goes through a pass-through transform (server/src/sse-line-cap.ts). The transform keeps two counts, both carried across chunks, against one cap,MAX_SSE_LINE_BYTES= 16 MiB:Upstream SSE line exceeds the 16777216-byte limit (MAX_SSE_LINE_BYTES) without a line terminator.Upstream SSE event exceeds the 16777216-byte limit (MAX_SSE_LINE_BYTES) without a blank line ending it.CRLF counts as one terminator even when the CR and LF arrive in different chunks, so a split CRLF is never read as a blank line. Erroring the pipe cancels the source, which destroys the upstream socket. 401 handling is unchanged.
The cap has to sit well above any legitimate line or event, because one
data:line carries a whole JSON-RPC message, such as a large tool result. 16 MiB is four times the 4 MB inbound message limit of the SDK's reference servers and holds about 12 MiB of binary after base64. It also bounds what a hostile upstream can make the parser hold, per stream and per reconnect. This matches the v2 fix.Patched (16 MiB cap), same fast upstream with
--max-old-space-size=1024, for 90 s: the proxy stayed up. RSS started at 129 MB, peaked at 265 MB and ended at 261 MB while the upstream streamed 495.8 MiB. The proxy loggedSSE error: Upstream SSE line exceeds the 16777216-byte limit (MAX_SSE_LINE_BYTES) without a line terminator30 times, once per connection attempt, and the upstream saw its connection closed all 30 times.Many short lines, no blank line (same setup, 90 s, upstream writing terminated 1 KiB
data:lines in 256 KiB batches):SSE error: Invalid string length, 12 times).SSE error: Upstream SSE event exceeds the 16777216-byte limit (MAX_SSE_LINE_BYTES) without a blank line ending it30 times, once per connection attempt, and the upstream saw its connection closed all 30 times.Coverage
Both SDK paths that receive an SSE body from this fetch are covered:
ssetransport GET stream (eventsource)streamable-httpSSE responses (EventSourceParserStream): the POST response, and the standalone GET stream even when it is mislabelledTests
server/src/__tests__/sse-line-cap.test.ts: an unterminated line over a small injected cap errors the stream, and the message names the cap. The count carries across chunks. A long line between terminators inside one chunk is caught. A long legitimate line under the cap passes intact, and so does a line of exactly the cap. The line count resets on CR, LF and CRLF, including at chunk boundaries and with CRLF split across chunks. Many terminated lines with no blank line error with the event-size message. A blank line (LF LF, CR CR, CRLF CRLF, including split across chunks) resets the event count, while a CRLF split across chunks is not taken for a blank line. A legitimate stream of 200 events, delivered in 7-byte pieces, passes intact.server/src/__tests__/sse-line-cap-route.test.ts: drives the real proxy against an upstream that sends cap + 1 MiB unterminated, oversse, astreamable-httpPOST, astreamable-httpstandalone GET served asapplication/octet-stream, and anssestream of terminated 1 KiBdata:lines with no blank line. It asserts the error is logged and the upstream connection is released. With the wiring removed, the tests fail by timing out. With the cap gated on content-type only, the mislabelled-GET test fails and the other two pass. With the event count disabled, the terminated-lines test fails and the other three pass.v1 CI steps all pass locally:
prettier-check,check-version, client lint, client tests (36 suites), server tests (12 files, 265 tests),build.Out of scope
application/json) streamable-http responses are still read unbounded by the SDK'sresponse.json().eventsourcereconnects after the stream errors, so a hostile upstream can keep making it re-fetch. Each attempt is bounded by the cap, and that reconnect behavior predates this change.🤖 Generated with Claude Code