diff --git a/apps/renderer/src/wsNativeApi.test.ts b/apps/renderer/src/wsNativeApi.test.ts index d78db60f1f..21f8129e0b 100644 --- a/apps/renderer/src/wsNativeApi.test.ts +++ b/apps/renderer/src/wsNativeApi.test.ts @@ -3150,6 +3150,14 @@ describe("wsNativeApi", () => { }, }), ); + socket?.emitMessage( + JSON.stringify({ + type: "response", + id: " ", + ok: true, + result: [], + }), + ); socket?.emitMessage( JSON.stringify({ type: "response", @@ -3165,7 +3173,7 @@ describe("wsNativeApi", () => { id: requestEnvelope.id, ok: false, error: { - code: "request_failed", + code: " ", message: "m".repeat(WS_ERROR_MESSAGE_MAX_CHARS + 1), }, }), diff --git a/apps/t3/scripts/smoke-test.mjs b/apps/t3/scripts/smoke-test.mjs index 98f1852d67..c4b4069024 100644 --- a/apps/t3/scripts/smoke-test.mjs +++ b/apps/t3/scripts/smoke-test.mjs @@ -82,7 +82,7 @@ function parseWsMessage(raw) { if ( typeof parsed.id !== "string" || - parsed.id.length === 0 || + parsed.id.trim().length === 0 || parsed.id.length > WS_REQUEST_ID_MAX_CHARS || typeof parsed.ok !== "boolean" ) { @@ -111,10 +111,10 @@ function parseWsMessage(raw) { !isRecord(parsed.error) || !hasOnlyKeys(parsed.error, ["code", "message"]) || typeof parsed.error.code !== "string" || - parsed.error.code.length === 0 || + parsed.error.code.trim().length === 0 || parsed.error.code.length > WS_ERROR_CODE_MAX_CHARS || typeof parsed.error.message !== "string" || - parsed.error.message.length === 0 || + parsed.error.message.trim().length === 0 || parsed.error.message.length > WS_ERROR_MESSAGE_MAX_CHARS ) { return null; diff --git a/apps/t3/src/runtimeApiServer.test.ts b/apps/t3/src/runtimeApiServer.test.ts index e7abcd1bca..74f86427a7 100644 --- a/apps/t3/src/runtimeApiServer.test.ts +++ b/apps/t3/src/runtimeApiServer.test.ts @@ -776,6 +776,8 @@ describe("runtimeApiServer", () => { client.socket.send("not-json"); client.socket.send(JSON.stringify({ type: "request", id: "", method: "" })); + client.socket.send(JSON.stringify({ type: "request", id: " ", method: "todos.list" })); + client.socket.send(JSON.stringify({ type: "request", id: "malformed-space-method", method: " " })); client.socket.send( JSON.stringify({ type: "request", diff --git a/packages/contracts/src/ws.test.ts b/packages/contracts/src/ws.test.ts index 3c8072845e..b0d616e879 100644 --- a/packages/contracts/src/ws.test.ts +++ b/packages/contracts/src/ws.test.ts @@ -34,6 +34,16 @@ describe("wsClientMessageSchema", () => { ).toThrow(); }); + it("rejects whitespace-only request ids", () => { + expect(() => + wsClientMessageSchema.parse({ + type: "request", + id: " ", + method: "providers.startSession", + }), + ).toThrow(); + }); + it("rejects empty request methods", () => { expect(() => wsClientMessageSchema.parse({ @@ -44,6 +54,16 @@ describe("wsClientMessageSchema", () => { ).toThrow(); }); + it("rejects whitespace-only request methods", () => { + expect(() => + wsClientMessageSchema.parse({ + type: "request", + id: "req-1", + method: " ", + }), + ).toThrow(); + }); + it("rejects overly long request ids", () => { expect(() => wsClientMessageSchema.parse({ @@ -232,6 +252,43 @@ describe("wsServerMessageSchema", () => { expect(parsed.type).toBe("response"); }); + it("rejects whitespace-only response ids", () => { + expect(() => + wsServerMessageSchema.parse({ + type: "response", + id: " ", + ok: true, + result: {}, + }), + ).toThrow(); + }); + + it("rejects whitespace-only response error fields", () => { + expect(() => + wsServerMessageSchema.parse({ + type: "response", + id: "req-1", + ok: false, + error: { + code: " ", + message: "valid", + }, + }), + ).toThrow(); + + expect(() => + wsServerMessageSchema.parse({ + type: "response", + id: "req-1", + ok: false, + error: { + code: "request_failed", + message: " ", + }, + }), + ).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 66656c521c..22e51969b2 100644 --- a/packages/contracts/src/ws.ts +++ b/packages/contracts/src/ws.ts @@ -23,22 +23,51 @@ export const WS_METHOD_MAX_CHARS = 256; export const WS_ERROR_CODE_MAX_CHARS = 128; export const WS_ERROR_MESSAGE_MAX_CHARS = 8_192; +const wsRequestIdSchema = z + .string() + .min(1) + .max(WS_REQUEST_ID_MAX_CHARS) + .refine((value) => value.trim().length > 0, { + message: "request.id must not be blank", + }); +const wsMethodSchema = z + .string() + .min(1) + .max(WS_METHOD_MAX_CHARS) + .refine((value) => value.trim().length > 0, { + message: "request.method must not be blank", + }); +const wsErrorCodeSchema = z + .string() + .min(1) + .max(WS_ERROR_CODE_MAX_CHARS) + .refine((value) => value.trim().length > 0, { + message: "response.error.code must not be blank", + }); +const wsErrorMessageSchema = z + .string() + .min(1) + .max(WS_ERROR_MESSAGE_MAX_CHARS) + .refine((value) => value.trim().length > 0, { + message: "response.error.message must not be blank", + }); + const wsRequestSchema = z.object({ type: z.literal("request"), - id: z.string().min(1).max(WS_REQUEST_ID_MAX_CHARS), - method: z.string().min(1).max(WS_METHOD_MAX_CHARS), + id: wsRequestIdSchema, + method: wsMethodSchema, params: z.unknown().optional(), }).strict(); const wsResponseErrorSchema = z.object({ - code: z.string().min(1).max(WS_ERROR_CODE_MAX_CHARS), - message: z.string().min(1).max(WS_ERROR_MESSAGE_MAX_CHARS), + code: wsErrorCodeSchema, + message: wsErrorMessageSchema, }).strict(); const wsResponseSchema = z .object({ type: z.literal("response"), - id: z.string().min(1).max(WS_REQUEST_ID_MAX_CHARS), + id: wsRequestIdSchema, ok: z.boolean(), result: z.unknown().optional(), error: wsResponseErrorSchema.optional(),