From 8d84fdc3f55670a8afe4199635b4283ab69a5e40 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 10 Feb 2026 11:33:24 +0000 Subject: [PATCH] Bound websocket request id and method lengths in contracts Co-authored-by: Theo Browne --- packages/contracts/src/ws.test.ts | 33 +++++++++++++++++++++++++++++++ packages/contracts/src/ws.ts | 9 ++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/packages/contracts/src/ws.test.ts b/packages/contracts/src/ws.test.ts index 2e8c36b5c1..765c69394f 100644 --- a/packages/contracts/src/ws.test.ts +++ b/packages/contracts/src/ws.test.ts @@ -4,6 +4,8 @@ import { WS_CLOSE_CODES, WS_CLOSE_REASONS, WS_EVENT_CHANNELS, + WS_METHOD_MAX_CHARS, + WS_REQUEST_ID_MAX_CHARS, wsClientMessageSchema, wsServerMessageSchema, } from "./ws"; @@ -40,6 +42,26 @@ describe("wsClientMessageSchema", () => { ).toThrow(); }); + it("rejects overly long request ids", () => { + expect(() => + wsClientMessageSchema.parse({ + type: "request", + id: "r".repeat(WS_REQUEST_ID_MAX_CHARS + 1), + method: "providers.startSession", + }), + ).toThrow(); + }); + + it("rejects overly long request methods", () => { + expect(() => + wsClientMessageSchema.parse({ + type: "request", + id: "req-1", + method: "m".repeat(WS_METHOD_MAX_CHARS + 1), + }), + ).toThrow(); + }); + it("rejects unexpected request properties", () => { expect(() => wsClientMessageSchema.parse({ @@ -126,6 +148,17 @@ describe("wsServerMessageSchema", () => { ).toThrow(); }); + it("rejects overly long response ids", () => { + expect(() => + wsServerMessageSchema.parse({ + type: "response", + id: "r".repeat(WS_REQUEST_ID_MAX_CHARS + 1), + ok: true, + result: {}, + }), + ).toThrow(); + }); + it("accepts typed event channels", () => { const parsed = wsServerMessageSchema.parse({ type: "event", diff --git a/packages/contracts/src/ws.ts b/packages/contracts/src/ws.ts index 25b0cf7115..85c05325f5 100644 --- a/packages/contracts/src/ws.ts +++ b/packages/contracts/src/ws.ts @@ -18,10 +18,13 @@ export const WS_CLOSE_REASONS = { unauthorized: "unauthorized", } as const; +export const WS_REQUEST_ID_MAX_CHARS = 256; +export const WS_METHOD_MAX_CHARS = 256; + const wsRequestSchema = z.object({ type: z.literal("request"), - id: z.string().min(1), - method: z.string().min(1), + id: z.string().min(1).max(WS_REQUEST_ID_MAX_CHARS), + method: z.string().min(1).max(WS_METHOD_MAX_CHARS), params: z.unknown().optional(), }).strict(); @@ -33,7 +36,7 @@ const wsResponseErrorSchema = z.object({ const wsResponseSchema = z .object({ type: z.literal("response"), - id: z.string().min(1), + id: z.string().min(1).max(WS_REQUEST_ID_MAX_CHARS), ok: z.boolean(), result: z.unknown().optional(), error: wsResponseErrorSchema.optional(),