Skip to content

fix(core): cap SSE line length and event size on every SDK transport stream (v2) - #2993

Merged
cliffhall merged 2 commits into
v2/mainfrom
v2/fix/2991-sse-line-cap
Nov 18, 2026
Merged

cliffhall merged 2 commits into
v2/mainfrom
v2/fix/2991-sse-line-cap

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #2991

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

Problem

A malicious upstream server can open an SSE stream, send a valid endpoint event, and then stream data: followed by megabytes with no line terminator. It can do the same with many short, terminated data: lines and no blank line, because the parser appends each data: 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:

  1. SDK parser. eventsource-parser (3.1.1) concatenates the bytes since the last terminator without limit. It has a maxBufferSize option, but nothing passes it. It is reached through the legacy SSEClientTransport (via eventsource) and through EventSourceParserStream in the Streamable HTTP transport, which covers both POST replies and the standalone GET stream.
  2. Our stream watcher. In watchLongLivedStream (core/mcp/fetchTracking.ts), pending grew the same way.

Fix

  • core/mcp/node/sseLineCapFetch.ts (new): createSseLineCapFetch pipes the body through a transform that enforces two bounds, both at SSE_MAX_LINE_BYTES (16 MiB):

    • Line length: bytes since the last CR or LF. Any of LF, CR or CRLF resets it.
    • Event size: bytes since the last blank line, meaning a terminator that ends an empty line (\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. Its kind is "line" or "event", and its message names the line-length limit or the event-size limit to match. The error reaches the transport's onerror, or rejects start() if it happens during connect. The pipe cancels the upstream body, which frees the connection. Which responses it wraps:

    • every 2xx text/event-stream response
    • every 2xx GET that asked for text/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.
    • JSON replies, error statuses and OAuth traffic pass through untouched.

    The rebuilt Response keeps url, redirected and type, the same way toGlobalResponse in proxyFetch.ts does.

  • 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-supplied eventSourceInit.fetch. This covers the web backend (createBackendBaseFetch) and the CLI, TUI and mcpdo, which all build transports through createTransportNode.

  • core/mcp/fetchTracking.ts: the watcher now splits lines with createLineSplitter. It keeps only the first WATCHED_LINE_HEAD_CHARS (64) characters of a line, which is all it needs to tell whether the line is blank or a data field. 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 MiB TRACKED_BODY_MAX_BYTES is 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:
    • transform, line cap: overflow errors with the named limit and cancels upstream; a line under the cap passes intact; LF, CR and CRLF reset the count, including across chunks
    • transform, event cap: many terminated 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 intact
    • real transports, event cap: terminated 1 MiB lines with no blank line fail with the event-size limit on the legacy SSE GET and on the Streamable HTTP POST reply stream
    • wrapper: which responses get wrapped, and that metadata is preserved
    • real SDK transports through createTransportNode with the shipped cap: legacy SSE GET, Streamable HTTP POST reply stream, Streamable HTTP standalone GET with a mislabelled Content-Type, and a caller-supplied eventSourceInit.fetch
    • mutation checks:
      • with the wiring removed from transport.ts, all four line-cap transport cases fail
      • with the event-size check disabled, 4 tests fail: the transform case, the split-CRLF case, and both transport event cases
      • with split-CRLF detection disabled, the split-CRLF case fails
  • fetchTracking.test.ts:
    • an endless partial line stays bounded in createLineSplitter
    • overlong SSE and NDJSON lines are still classified correctly by the watcher

The stream watcher keeps nothing per event beyond the blockHasData flag 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 the response.text() reads on error statuses, are still unbounded at the fetch layer.

npm run local:gate passes.

🤖 Generated with Claude Code

cliffhall and others added 2 commits November 17, 2026 22:46
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]>
@cliffhall cliffhall added the v2 Issues and PRs for v2 label Nov 18, 2026
@cliffhall
cliffhall requested a balanced review from Copilot November 18, 2026 06:46

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 are blocking the SSE buffering-limit changes.

No open issues 🎉

What changed in this PR

Addresses #2991 by bounding SSE buffering in the shared Node transport layer, protecting the web backend and Node clients from malicious streams that exhaust memory. This is the v2 fix; v1 is tracked separately in #2992.

Changes:

  • Enforces 16 MiB limits on SSE lines and events, cancelling upstream streams when exceeded.
  • Applies the cap across SDK transport fetch paths and bounds the stream watcher’s retained line data.
  • Adds regression coverage for limits, line endings, response selection, cancellation, and transport wiring.
File Description
core/​mcp/​node/​transport.ts Wraps base and caller-supplied SSE fetches with the cap.
core/​mcp/​node/​sseLineCapFetch.ts Implements line and event limits while preserving response metadata.
core/​mcp/​fetchTracking.ts Bounds retained line data while preserving stream event counting.
clients/​web/​src/​test/​core/​mcp/​node/​sseLineCapFetch.test.ts Tests cap behavior, response handling, and real SDK transport wiring.
clients/​web/​src/​test/​core/​mcp/​fetchTracking.test.ts Tests bounded line splitting and overlong-line classification.

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).

@cliffhall
cliffhall merged commit 2143c08 into v2/main Nov 18, 2026
24 checks passed
@cliffhall
cliffhall deleted the v2/fix/2991-sse-line-cap branch November 18, 2026 07:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2 Issues and PRs for v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants