Repository navigation
Cap SSE line and event size on every SDK transport stream (GHSA-3xgv-mvj6-g78r) - #2997
Merged
Merged
Conversation
A server could stream `data: ` followed by an endless run of bytes with no line terminator. Both the SDK's SSE parser (eventsource-parser, reached through the legacy SSE transport and the Streamable HTTP POST-reply and standalone GET streams) and the network tracker's stream watcher buffered that partial line without limit, so the process grew until V8 aborted it. - Add createSseLineCapFetch: every successful text/event-stream response, and every successful GET that asked for one (the Streamable HTTP transport parses a 2xx standalone GET as SSE whatever its Content-Type), is piped through a transform that errors with SseLineTooLongError once more than SSE_MAX_LINE_BYTES (16 MiB) arrive without a CR or LF. The error surfaces through the transport's onerror naming the limit, and the pipe cancels the upstream body. Applied innermost in createTransportNode, over the base fetch and a caller-supplied eventSourceInit.fetch alike. - The stream watcher now splits lines with a bounded splitter that keeps only a line's head (WATCHED_LINE_HEAD_CHARS), which is all it needs to classify the line, so its memory no longer grows with line length. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
The line cap reset on every CR or LF, so a server could still exhaust
memory with many short, terminated `data:` lines and no blank line: the
SDK's eventsource-parser appends each data line to the pending event and
frees it only when a blank line dispatches the event.
The same transform now also counts bytes since the last blank line (a
terminator ending an empty line; CRLF is one terminator even when split
across chunks, so a split CRLF is never read as a blank line) and errors
past the same 16 MiB limit. SseLineTooLongError gains a `kind`
("line" | "event") and a message naming the line-length or the
event-size limit accordingly.
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 #2991
Advisory: GHSA-3xgv-mvj6-g78r (v2; v1 is fixed separately for #2992).
Problem
A malicious upstream server can open an SSE stream, send a valid
endpointevent, and then streamdata:followed by megabytes with no line terminator. It can do the same with many short, terminateddata:lines and no blank line, because the parser appends eachdata:line to the pending event and frees it only at the blank line that dispatches the event. The backend's memory grows without limit until V8 aborts the process, which drops every session on the backend. Two independent buffers hold that line:eventsource-parser(3.1.1) concatenates the bytes since the last terminator without limit. It has amaxBufferSizeoption, but nothing passes it. It is reached through the legacySSEClientTransport(viaeventsource) and throughEventSourceParserStreamin the Streamable HTTP transport, which covers both POST replies and the standalone GET stream.watchLongLivedStream(core/mcp/fetchTracking.ts),pendinggrew the same way.Fix
core/mcp/node/sseLineCapFetch.ts(new):createSseLineCapFetchpipes the body through a transform that enforces two bounds, both atSSE_MAX_LINE_BYTES(16 MiB):\n\n,\r\r,\r\n\r\n). A blank line resets 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.Crossing either bound fails the stream with
SseLineTooLongError. Itskindis"line"or"event", and its message names the line-length limit or the event-size limit to match. The error reaches the transport'sonerror, or rejectsstart()if it happens during connect. The pipe cancels the upstream body, which frees the connection. Which responses it wraps:text/event-streamresponsetext/event-stream, whatever its Content-Type. The Streamable HTTP transport parses a standalone-GET body as SSE without checking the header, so a mislabelled stream would otherwise get past the cap.The rebuilt
Responsekeepsurl,redirectedandtype, the same waytoGlobalResponseinproxyFetch.tsdoes.core/mcp/node/transport.ts: the cap is applied innermost, directly on the base fetch, so it sits below the tracker, the challenge observer, the cookie jar and the auth interceptor. It also wraps a caller-suppliedeventSourceInit.fetch. This covers the web backend (createBackendBaseFetch) and the CLI, TUI and mcpdo, which all build transports throughcreateTransportNode.core/mcp/fetchTracking.ts: the watcher now splits lines withcreateLineSplitter. It keeps only the firstWATCHED_LINE_HEAD_CHARS(64) characters of a line, which is all it needs to tell whether the line is blank or adatafield. Event counts stay correct for overlong lines, and the watcher holds a bounded amount however long the line runs.Why 16 MiB
One event, usually one
data:line, carries a whole JSON-RPC message, so the cap has to sit well above any legitimate result. 16 MiB is 4× the 4 MB inbound-message limit in the SDK's own reference servers, and holds about 12 MiB of base64-encoded binary. In exchange, a hostile server can make the parser hold up to about one cap per open stream (a line or an event), once per reconnect attempt, before the stream is cut. That cost is bounded. The 1 MiBTRACKED_BODY_MAX_BYTESis deliberately not reused: it only truncates what the Network log records, whereas this cap ends the connection.Tests
clients/web/src/test/core/mcp/node/sseLineCapFetch.test.ts:data:lines with no blank line fail with the event-size limit; a blank line resets the count (LF LF, CR CR, CRLF CRLF, each also split across chunks); a CRLF split across chunks is not read as a blank line; a legitimate 200-event stream passes intactcreateTransportNodewith the shipped cap: legacy SSE GET, Streamable HTTP POST reply stream, Streamable HTTP standalone GET with a mislabelled Content-Type, and a caller-suppliedeventSourceInit.fetchtransport.ts, all four line-cap transport cases failfetchTracking.test.ts:createLineSplitterThe stream watcher keeps nothing per event beyond the
blockHasDataflag and the count, so it needed no event bound.The event bound counts every byte since the last blank line, including comment and
id:/event:lines that the parser does not keep. That is conservative: tripping it takes 16 MiB of such lines with no blank line in between, and no keepalive pattern comes close.Out of scope (noted)
A JSON body the SDK reads, such as a Streamable HTTP POST answered with
application/json(response.json()), and theresponse.text()reads on error statuses, are still unbounded at the fetch layer.npm run local:gatepasses.🤖 Generated with Claude Code