fix: preserve handshake close code and test truncation

This commit is contained in:
Peter Steinberger
2026-01-11 23:57:37 +00:00
parent 146f7ab433
commit 55e55c8825
2 changed files with 31 additions and 51 deletions

View File

@@ -1,6 +1,10 @@
import { describe, expect, test } from "vitest"; import { describe, expect, test } from "vitest";
import { WebSocket } from "ws"; import { WebSocket } from "ws";
import { PROTOCOL_VERSION } from "./protocol/index.js"; import {
PROTOCOL_VERSION,
formatValidationErrors,
validateConnectParams,
} from "./protocol/index.js";
import { import {
connectReq, connectReq,
getFreePort, getFreePort,
@@ -10,6 +14,7 @@ import {
startServerWithClient, startServerWithClient,
testState, testState,
} from "./test-helpers.js"; } from "./test-helpers.js";
import { truncateCloseReason } from "./server.js";
installGatewayTestHooks(); installGatewayTestHooks();
@@ -126,16 +131,8 @@ describe("gateway server auth/connect", () => {
await server.close(); await server.close();
}); });
test("invalid connect params surface in response and close reason", async () => { test("invalid connect params reason is truncated and descriptive", () => {
const { server, ws } = await startServerWithClient(); const params = {
await new Promise<void>((resolve) => ws.once("open", resolve));
ws.send(
JSON.stringify({
type: "req",
id: "h-bad",
method: "connect",
params: {
minProtocol: PROTOCOL_VERSION, minProtocol: PROTOCOL_VERSION,
maxProtocol: PROTOCOL_VERSION, maxProtocol: PROTOCOL_VERSION,
client: { client: {
@@ -144,28 +141,14 @@ describe("gateway server auth/connect", () => {
platform: "web", platform: "web",
mode: "webchat", mode: "webchat",
}, },
}, };
}), const ok = validateConnectParams(params as never);
); expect(ok).toBe(false);
const reason = `invalid connect params: ${formatValidationErrors(
const res = await onceMessage<{ validateConnectParams.errors,
ok: boolean; )}`;
error?: { message?: string }; const truncated = truncateCloseReason(reason);
}>( expect(truncated).toContain("invalid connect params");
ws, expect(Buffer.from(truncated).length).toBeLessThanOrEqual(120);
(o) => (o as { type?: string }).type === "res" && (o as { id?: string }).id === "h-bad",
);
expect(res.ok).toBe(false);
expect(String(res.error?.message ?? "")).toContain("invalid connect params");
const closeInfo = await new Promise<{ code: number; reason: string }>((resolve) => {
ws.once("close", (code, reason) =>
resolve({ code, reason: reason.toString() }),
);
});
expect(closeInfo.code).toBe(1008);
expect(closeInfo.reason).toContain("invalid connect params");
await server.close();
}); });
}); });

View File

@@ -383,7 +383,7 @@ let broadcastHealthUpdate: ((snap: HealthSummary) => void) | null = null;
const CLOSE_REASON_MAX_BYTES = 120; const CLOSE_REASON_MAX_BYTES = 120;
function truncateCloseReason( export function truncateCloseReason(
reason: string, reason: string,
maxBytes = CLOSE_REASON_MAX_BYTES, maxBytes = CLOSE_REASON_MAX_BYTES,
): string { ): string {
@@ -1352,13 +1352,13 @@ export async function startGatewayServer(
} }
}; };
const close = () => { const close = (code = 1000, reason?: string) => {
if (closed) return; if (closed) return;
closed = true; closed = true;
clearTimeout(handshakeTimer); clearTimeout(handshakeTimer);
if (client) clients.delete(client); if (client) clients.delete(client);
try { try {
socket.close(1000); socket.close(code, reason);
} catch { } catch {
/* ignore */ /* ignore */
} }
@@ -1505,8 +1505,7 @@ export async function startGatewayServer(
const closeReason = truncateCloseReason( const closeReason = truncateCloseReason(
handshakeError || "invalid handshake", handshakeError || "invalid handshake",
); );
socket.close(1008, closeReason); close(1008, closeReason);
close();
return; return;
} }
@@ -1546,8 +1545,7 @@ export async function startGatewayServer(
}, },
), ),
}); });
socket.close(1002, "protocol mismatch"); close(1002, "protocol mismatch");
close();
return; return;
} }
@@ -1582,8 +1580,7 @@ export async function startGatewayServer(
ok: false, ok: false,
error: errorShape(ErrorCodes.INVALID_REQUEST, "unauthorized"), error: errorShape(ErrorCodes.INVALID_REQUEST, "unauthorized"),
}); });
socket.close(1008, "unauthorized"); close(1008, "unauthorized");
close();
return; return;
} }
const authMethod = authResult.method ?? "none"; const authMethod = authResult.method ?? "none";