Reject blank websocket ids methods and error fields

Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
Cursor AgentandTheo Browne committed 2026-02-10 13:43:54 +00:00
1 parent 6e3d048d82
commit 2071e98177
5 files changed
+105 -9

No files matched your search

+9 -1
View File
@@ -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),
},
}),
+3 -3
View File
@@ -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;
+2
View File
@@ -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",
+57
View File
@@ -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",
+34 -5
View File
@@ -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(),