Bound websocket error field sizes and sanitize runtime errors

Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
Cursor AgentandTheo Browne committed 2026-02-10 13:13:58 +00:00
1 parent 2cf291e504
commit 170460f6dd
5 files changed
+116 -4

No files matched your search

+7 -1
View File
@@ -21,6 +21,8 @@ const WS_EVENT_CHANNELS = {
};
const WS_REQUEST_ID_MAX_CHARS = 256;
const WS_METHOD_MAX_CHARS = 256;
const WS_ERROR_CODE_MAX_CHARS = 128;
const WS_ERROR_MESSAGE_MAX_CHARS = 8192;
function isRecord(value) {
return typeof value === "object" && value !== null;
@@ -109,7 +111,11 @@ function parseWsMessage(raw) {
!isRecord(parsed.error) ||
!hasOnlyKeys(parsed.error, ["code", "message"]) ||
typeof parsed.error.code !== "string" ||
typeof parsed.error.message !== "string"
parsed.error.code.length === 0 ||
parsed.error.code.length > WS_ERROR_CODE_MAX_CHARS ||
typeof parsed.error.message !== "string" ||
parsed.error.message.length === 0 ||
parsed.error.message.length > WS_ERROR_MESSAGE_MAX_CHARS
) {
return null;
}
+33
View File
@@ -8,6 +8,7 @@ import { WebSocket } from "ws";
import {
WS_METHOD_MAX_CHARS,
WS_REQUEST_ID_MAX_CHARS,
WS_ERROR_MESSAGE_MAX_CHARS,
WS_CLOSE_CODES,
WS_CLOSE_REASONS,
WS_EVENT_CHANNELS,
@@ -2008,6 +2009,38 @@ describe("runtimeApiServer", () => {
client.socket.close();
});
it("truncates overlong runtime error messages to websocket protocol limits", async () => {
const server = await startRuntimeApiServer({
port: 0,
launchCwd: process.cwd(),
});
servers.push(server);
const client = await connectClient(server.wsUrl);
await client.nextMessage();
const response = await sendRequest(
client.socket,
client.nextMessage,
"shell-overlong-error-1",
"shell.openInEditor",
{
cwd: `/${"x".repeat(WS_ERROR_MESSAGE_MAX_CHARS + 500)}`,
editor: "cursor",
},
);
expect(response.ok).toBe(false);
if (response.ok) {
throw new Error("Expected overlong shell target request to fail.");
}
expect(response.error?.code).toBe("request_failed");
expect(typeof response.error?.message).toBe("string");
expect((response.error?.message ?? "").length).toBeLessThanOrEqual(WS_ERROR_MESSAGE_MAX_CHARS);
expect(response.error?.message?.endsWith("…")).toBe(true);
client.socket.close();
});
it("returns structured errors for invalid terminal.run payloads", async () => {
const server = await startRuntimeApiServer({
port: 0,
+30 -1
View File
@@ -16,6 +16,8 @@ import {
type WsResponseMessage,
type WsServerMessage,
WS_EVENT_CHANNELS,
WS_ERROR_CODE_MAX_CHARS,
WS_ERROR_MESSAGE_MAX_CHARS,
WS_CLOSE_CODES,
WS_CLOSE_REASONS,
agentConfigSchema,
@@ -111,14 +113,41 @@ function responseSuccess(id: string, result: unknown): WsResponseMessage {
}
function responseError(id: string, error: JsonRpcErrorResult): WsResponseMessage {
const normalizedCode = normalizeErrorField(
error.code,
"request_failed",
WS_ERROR_CODE_MAX_CHARS,
);
const normalizedMessage = normalizeErrorField(
error.message,
"Request failed",
WS_ERROR_MESSAGE_MAX_CHARS,
);
return {
type: "response",
id,
ok: false,
error,
error: {
code: normalizedCode,
message: normalizedMessage,
},
};
}
function normalizeErrorField(value: string, fallback: string, maxChars: number): string {
const trimmed = value.trim();
const safeValue = trimmed.length > 0 ? trimmed : fallback;
if (safeValue.length <= maxChars) {
return safeValue;
}
if (maxChars <= 1) {
return safeValue.slice(0, maxChars);
}
return `${safeValue.slice(0, maxChars - 1)}…`;
}
function sendMessage(socket: WebSocket, message: unknown): void {
if (socket.readyState !== WebSocket.OPEN) {
return;
+42
View File
@@ -3,6 +3,8 @@ import { describe, expect, it } from "vitest";
import {
WS_CLOSE_CODES,
WS_CLOSE_REASONS,
WS_ERROR_CODE_MAX_CHARS,
WS_ERROR_MESSAGE_MAX_CHARS,
WS_EVENT_CHANNELS,
WS_METHOD_MAX_CHARS,
WS_REQUEST_ID_MAX_CHARS,
@@ -116,6 +118,46 @@ describe("wsServerMessageSchema", () => {
).toThrow();
});
it("accepts response errors at max field lengths", () => {
const parsed = wsServerMessageSchema.parse({
type: "response",
id: "req-1",
ok: false,
error: {
code: "c".repeat(WS_ERROR_CODE_MAX_CHARS),
message: "m".repeat(WS_ERROR_MESSAGE_MAX_CHARS),
},
});
expect(parsed.type).toBe("response");
});
it("rejects overlong response error fields", () => {
expect(() =>
wsServerMessageSchema.parse({
type: "response",
id: "req-1",
ok: false,
error: {
code: "c".repeat(WS_ERROR_CODE_MAX_CHARS + 1),
message: "valid",
},
}),
).toThrow();
expect(() =>
wsServerMessageSchema.parse({
type: "response",
id: "req-1",
ok: false,
error: {
code: "request_failed",
message: "m".repeat(WS_ERROR_MESSAGE_MAX_CHARS + 1),
},
}),
).toThrow();
});
it("requires result for successful responses", () => {
expect(() =>
wsServerMessageSchema.parse({
+4 -2
View File
@@ -20,6 +20,8 @@ export const WS_CLOSE_REASONS = {
export const WS_REQUEST_ID_MAX_CHARS = 256;
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 wsRequestSchema = z.object({
type: z.literal("request"),
@@ -29,8 +31,8 @@ const wsRequestSchema = z.object({
}).strict();
const wsResponseErrorSchema = z.object({
code: z.string().min(1),
message: z.string().min(1),
code: z.string().min(1).max(WS_ERROR_CODE_MAX_CHARS),
message: z.string().min(1).max(WS_ERROR_MESSAGE_MAX_CHARS),
}).strict();
const wsResponseSchema = z