Bound websocket request id and method lengths in contracts
Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
1 parent
0ba9932438
commit
8d84fdc3f5
2 files changed
+39
-3
No files matched your search
@@ -4,6 +4,8 @@ import {
|
||||
WS_CLOSE_CODES,
|
||||
WS_CLOSE_REASONS,
|
||||
WS_EVENT_CHANNELS,
|
||||
WS_METHOD_MAX_CHARS,
|
||||
WS_REQUEST_ID_MAX_CHARS,
|
||||
wsClientMessageSchema,
|
||||
wsServerMessageSchema,
|
||||
} from "./ws";
|
||||
@@ -40,6 +42,26 @@ describe("wsClientMessageSchema", () => {
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it("rejects overly long request ids", () => {
|
||||
expect(() =>
|
||||
wsClientMessageSchema.parse({
|
||||
type: "request",
|
||||
id: "r".repeat(WS_REQUEST_ID_MAX_CHARS + 1),
|
||||
method: "providers.startSession",
|
||||
}),
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it("rejects overly long request methods", () => {
|
||||
expect(() =>
|
||||
wsClientMessageSchema.parse({
|
||||
type: "request",
|
||||
id: "req-1",
|
||||
method: "m".repeat(WS_METHOD_MAX_CHARS + 1),
|
||||
}),
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it("rejects unexpected request properties", () => {
|
||||
expect(() =>
|
||||
wsClientMessageSchema.parse({
|
||||
@@ -126,6 +148,17 @@ describe("wsServerMessageSchema", () => {
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it("rejects overly long response ids", () => {
|
||||
expect(() =>
|
||||
wsServerMessageSchema.parse({
|
||||
type: "response",
|
||||
id: "r".repeat(WS_REQUEST_ID_MAX_CHARS + 1),
|
||||
ok: true,
|
||||
result: {},
|
||||
}),
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it("accepts typed event channels", () => {
|
||||
const parsed = wsServerMessageSchema.parse({
|
||||
type: "event",
|
||||
|
||||
@@ -18,10 +18,13 @@ export const WS_CLOSE_REASONS = {
|
||||
unauthorized: "unauthorized",
|
||||
} as const;
|
||||
|
||||
export const WS_REQUEST_ID_MAX_CHARS = 256;
|
||||
export const WS_METHOD_MAX_CHARS = 256;
|
||||
|
||||
const wsRequestSchema = z.object({
|
||||
type: z.literal("request"),
|
||||
id: z.string().min(1),
|
||||
method: z.string().min(1),
|
||||
id: z.string().min(1).max(WS_REQUEST_ID_MAX_CHARS),
|
||||
method: z.string().min(1).max(WS_METHOD_MAX_CHARS),
|
||||
params: z.unknown().optional(),
|
||||
}).strict();
|
||||
|
||||
@@ -33,7 +36,7 @@ const wsResponseErrorSchema = z.object({
|
||||
const wsResponseSchema = z
|
||||
.object({
|
||||
type: z.literal("response"),
|
||||
id: z.string().min(1),
|
||||
id: z.string().min(1).max(WS_REQUEST_ID_MAX_CHARS),
|
||||
ok: z.boolean(),
|
||||
result: z.unknown().optional(),
|
||||
error: wsResponseErrorSchema.optional(),
|
||||
|
||||
Reference in new issue
Block a user