Skip to content

fix: cap upstream SSE line length and event size in the proxy (v1) - #2994

Merged
cliffhall merged 3 commits into
v1/mainfrom
v1/fix/2992-sse-line-cap
Nov 18, 2026
Merged

cliffhall merged 3 commits into
v1/mainfrom
v1/fix/2992-sse-line-cap

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #2992

The v1 fix for GHSA-3xgv-mvj6-g78r, developed and reviewed in the advisory's private fork (inspector-GHSA-3xgv-mvj6-g78r#1) and landed here as an ordinary PR. The commits are the fork branch's, unchanged. The v2 fix is tracked in #2991.

Problem

A malicious upstream on the sse (or streamable-http) transport can open an event stream and then send data: followed by an endless run of bytes with no line terminator. The proxy hands that body to the v1 SDK, which parses it with eventsource-parser. The parser buffers the partial line until it sees CR or LF and has no limit unless maxBufferSize is passed, and neither eventsource 3.0.7 (sse transport) nor EventSourceParserStream (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, terminated data: 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):

  • 8 KiB/ms upstream: proxy RSS rose steadily from 122 MB to 502 MB over 40 s (274 MiB streamed).
  • 256 KiB/tick upstream with --max-old-space-size=1024: FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory after about 887 MiB.

Fix

createCustomFetch already 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 is text/event-stream or it is a GET whose request asked for text/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:

  • bytes since the last CR or LF (line length). Past the cap it errors with Upstream SSE line exceeds the 16777216-byte limit (MAX_SSE_LINE_BYTES) without a line terminator.
  • non-terminator bytes since the last blank line (event size). Past the cap it errors with 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 logged SSE error: Upstream SSE line exceeds the 16777216-byte limit (MAX_SSE_LINE_BYTES) without a line terminator 30 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):

  • Line cap only (previous revision): RSS went from 441 MB to a 4,601 MB peak. Each connection's pending event grew until V8 refused it (SSE error: Invalid string length, 12 times).
  • With the event cap: the proxy stayed up. RSS started at 147 MB, peaked at 275 MB and ended at 214 MB, while the upstream streamed 496.8 MiB. The proxy logged SSE error: Upstream SSE event exceeds the 16777216-byte limit (MAX_SSE_LINE_BYTES) without a blank line ending it 30 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:

  • sse transport GET stream (eventsource)
  • streamable-http SSE responses (EventSourceParserStream): the POST response, and the standalone GET stream even when it is mislabelled

Tests

  • 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, over sse, a streamable-http POST, a streamable-http standalone GET served as application/octet-stream, and an sse stream of terminated 1 KiB data: 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

  • Non-SSE (application/json) streamable-http responses are still read unbounded by the SDK's response.json().
  • eventsource reconnects 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

cliffhall and others added 3 commits November 17, 2026 22:46
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]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified for the upstream SSE buffering-limit changes.

No open issues 🎉

What changed in this PR

This PR addresses #2992 by bounding upstream server-sent event (SSE) buffering in the v1 proxy, preventing oversized lines or unfinished events from exhausting the process heap.

Changes:

  • Adds a 16 MiB line and event cap with chunk-aware CR, LF, and CRLF handling.
  • Applies the cap to successful SSE responses, including mislabelled standalone GET streams, while leaving JSON POST replies and error responses unchanged.
  • Adds unit and proxy integration tests covering limits, stream detection, and upstream connection cleanup.
File Description
server/​src/​sse-line-cap.ts Implements the size-limiting transform and response-selection helpers.
server/​src/​index.ts Applies the transform to upstream SSE bodies before SDK parsing.
server/​src/​__tests__/​sse-line-cap.test.ts Tests size boundaries, chunk handling, terminators, and response selection.
server/​src/​__tests__/​sse-line-cap-route.test.ts Tests transport coverage, oversize failures, and upstream connection release.

Review effort: Balanced

@cliffhall

Copy link
Copy Markdown
Member Author

Copilot review loop closed after round 1: the round was clean (no inline comments, nothing in the headline or suppressed block). CI is green.

@cliffhall
cliffhall merged commit d1f6422 into v1/main Nov 18, 2026
9 checks passed
@cliffhall
cliffhall deleted the v1/fix/2992-sse-line-cap branch November 18, 2026 07:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[GHSA-3xgv-mvj6-g78r] - [v1] - Unbounded long-lived SSE read can crash the proxy

2 participants