diff --git a/packages/contracts/src/ws.test.ts b/packages/contracts/src/ws.test.ts index a8942f9742..a39da23fe5 100644 --- a/packages/contracts/src/ws.test.ts +++ b/packages/contracts/src/ws.test.ts @@ -39,6 +39,17 @@ describe("wsClientMessageSchema", () => { }), ).toThrow(); }); + + it("rejects unexpected request properties", () => { + expect(() => + wsClientMessageSchema.parse({ + type: "request", + id: "req-1", + method: "providers.listSessions", + unexpected: true, + }), + ).toThrow(); + }); }); describe("wsServerMessageSchema", () => { @@ -103,6 +114,18 @@ describe("wsServerMessageSchema", () => { ).toThrow(); }); + it("rejects unexpected response properties", () => { + expect(() => + wsServerMessageSchema.parse({ + type: "response", + id: "req-1", + ok: true, + result: { status: "ok" }, + unexpected: true, + }), + ).toThrow(); + }); + it("accepts typed event channels", () => { const parsed = wsServerMessageSchema.parse({ type: "event", @@ -216,6 +239,17 @@ describe("wsServerMessageSchema", () => { }), ).toThrow(); }); + + it("rejects unexpected hello properties", () => { + expect(() => + wsServerMessageSchema.parse({ + type: "hello", + version: 1, + launchCwd: "/workspace", + unexpected: true, + }), + ).toThrow(); + }); }); describe("ws close metadata", () => { diff --git a/packages/contracts/src/ws.ts b/packages/contracts/src/ws.ts index 65d8d40302..25b0cf7115 100644 --- a/packages/contracts/src/ws.ts +++ b/packages/contracts/src/ws.ts @@ -23,12 +23,12 @@ const wsRequestSchema = z.object({ id: z.string().min(1), method: z.string().min(1), params: z.unknown().optional(), -}); +}).strict(); const wsResponseErrorSchema = z.object({ code: z.string().min(1), message: z.string().min(1), -}); +}).strict(); const wsResponseSchema = z .object({ @@ -38,6 +38,7 @@ const wsResponseSchema = z result: z.unknown().optional(), error: wsResponseErrorSchema.optional(), }) + .strict() .superRefine((value, ctx) => { if (value.ok && value.error) { ctx.addIssue({ @@ -73,24 +74,24 @@ const wsEventSchema = z.union([ type: z.literal("event"), channel: z.literal(WS_EVENT_CHANNELS.providerEvent), payload: providerEventSchema, - }), + }).strict(), z.object({ type: z.literal("event"), channel: z.literal(WS_EVENT_CHANNELS.agentOutput), payload: outputChunkSchema, - }), + }).strict(), z.object({ type: z.literal("event"), channel: z.literal(WS_EVENT_CHANNELS.agentExit), payload: agentExitSchema, - }), + }).strict(), ]); const wsHelloSchema = z.object({ type: z.literal("hello"), version: z.literal(1), launchCwd: z.string().min(1), -}); +}).strict(); export const wsClientMessageSchema = wsRequestSchema; export const wsServerMessageSchema = z.union([wsResponseSchema, wsEventSchema, wsHelloSchema]);