Skip to content

Commit 5dfb202

Browse files
yetvalTakhoffman
authored andcommitted
fix(mcp): guard stdio stdout frame parsing against oversized-buffer crash
An MCP server whose stdout frame exceeds the SDK ReadBuffer 10 MiB cap makes ReadBuffer.append throw synchronously inside the child stdout data callback. The handler had no try/catch, so the error escaped as an uncaughtException and took down the host process, killing every concurrent session. The frame size is driven by tool-result data a lower-privileged sender can influence, not by the operator who chose the server. Wrap append and processReadBuffer in the same try/catch the upstream SDK transport uses: route the error to onerror and close the transport so the MCP runtime handles it as a server disconnect. This matches the existing stdin (#75438) and stderr (#99803) guards in this file; stdout was the missed path.
1 parent 17ef777 commit 5dfb202

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

‎src/agents/mcp-stdio-transport.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,23 @@ describe("OpenClawStdioClientTransport", () => {
246246
child.stderr.write("server diagnostic");
247247
expect(transport.stderr?.read()?.toString()).toBe("server diagnostic");
248248
});
249+
250+
it("reports an oversized stdout frame without an unhandled error crash", async () => {
251+
const child = new MockChildProcess();
252+
spawnMock.mockReturnValue(child);
253+
254+
const transport = new OpenClawStdioClientTransport({ command: "npx" });
255+
const onerror = vi.fn();
256+
Object.assign(transport, { onerror });
257+
const started = transport.start();
258+
child.emit("spawn");
259+
await started;
260+
261+
const oversized = Buffer.alloc(10 * 1024 * 1024 + 1, 0x20);
262+
expect(() => child.stdout.emit("data", oversized)).not.toThrow();
263+
expect(onerror).toHaveBeenCalledTimes(1);
264+
const reported = onerror.mock.calls.at(0)?.at(0) as Error;
265+
expect(reported).toBeInstanceOf(Error);
266+
expect(reported.message).toMatch(/exceeded maximum size/);
267+
});
249268
});

‎src/agents/mcp-stdio-transport.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,13 @@ export class OpenClawStdioClientTransport implements Transport {
8282
});
8383
child.stdin?.on("error", (error: Error) => this.onerror?.(error));
8484
child.stdout?.on("data", (chunk: Buffer) => {
85-
this.readBuffer.append(chunk);
86-
this.processReadBuffer();
85+
try {
86+
this.readBuffer.append(chunk);
87+
this.processReadBuffer();
88+
} catch (error) {
89+
this.onerror?.(error instanceof Error ? error : new Error(String(error)));
90+
void this.close();
91+
}
8792
});
8893
child.stdout?.on("error", (error: Error) => this.onerror?.(error));
8994
if (this.stderrStream && child.stderr) {

0 commit comments

Comments
 (0)