Enforce strict websocket envelope object schemas
Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
1 parent
32e0f58e6d
commit
dc4e46ed89
2 files changed
+41
-6
No files matched your search
@@ -39,6 +39,17 @@ describe("wsClientMessageSchema", () => {
|
||||
}),
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it("rejects unexpected request properties", () => {
|
||||
expect(() =>
|
||||
wsClientMessageSchema.parse({
|
||||
type: "request",
|
||||
id: "req-1",
|
||||
method: "providers.listSessions",
|
||||
unexpected: true,
|
||||
}),
|
||||
).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("wsServerMessageSchema", () => {
|
||||
@@ -103,6 +114,18 @@ describe("wsServerMessageSchema", () => {
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it("rejects unexpected response properties", () => {
|
||||
expect(() =>
|
||||
wsServerMessageSchema.parse({
|
||||
type: "response",
|
||||
id: "req-1",
|
||||
ok: true,
|
||||
result: { status: "ok" },
|
||||
unexpected: true,
|
||||
}),
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it("accepts typed event channels", () => {
|
||||
const parsed = wsServerMessageSchema.parse({
|
||||
type: "event",
|
||||
@@ -216,6 +239,17 @@ describe("wsServerMessageSchema", () => {
|
||||
}),
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it("rejects unexpected hello properties", () => {
|
||||
expect(() =>
|
||||
wsServerMessageSchema.parse({
|
||||
type: "hello",
|
||||
version: 1,
|
||||
launchCwd: "/workspace",
|
||||
unexpected: true,
|
||||
}),
|
||||
).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("ws close metadata", () => {
|
||||
|
||||
@@ -23,12 +23,12 @@ const wsRequestSchema = z.object({
|
||||
id: z.string().min(1),
|
||||
method: z.string().min(1),
|
||||
params: z.unknown().optional(),
|
||||
});
|
||||
}).strict();
|
||||
|
||||
const wsResponseErrorSchema = z.object({
|
||||
code: z.string().min(1),
|
||||
message: z.string().min(1),
|
||||
});
|
||||
}).strict();
|
||||
|
||||
const wsResponseSchema = z
|
||||
.object({
|
||||
@@ -38,6 +38,7 @@ const wsResponseSchema = z
|
||||
result: z.unknown().optional(),
|
||||
error: wsResponseErrorSchema.optional(),
|
||||
})
|
||||
.strict()
|
||||
.superRefine((value, ctx) => {
|
||||
if (value.ok && value.error) {
|
||||
ctx.addIssue({
|
||||
@@ -73,24 +74,24 @@ const wsEventSchema = z.union([
|
||||
type: z.literal("event"),
|
||||
channel: z.literal(WS_EVENT_CHANNELS.providerEvent),
|
||||
payload: providerEventSchema,
|
||||
}),
|
||||
}).strict(),
|
||||
z.object({
|
||||
type: z.literal("event"),
|
||||
channel: z.literal(WS_EVENT_CHANNELS.agentOutput),
|
||||
payload: outputChunkSchema,
|
||||
}),
|
||||
}).strict(),
|
||||
z.object({
|
||||
type: z.literal("event"),
|
||||
channel: z.literal(WS_EVENT_CHANNELS.agentExit),
|
||||
payload: agentExitSchema,
|
||||
}),
|
||||
}).strict(),
|
||||
]);
|
||||
|
||||
const wsHelloSchema = z.object({
|
||||
type: z.literal("hello"),
|
||||
version: z.literal(1),
|
||||
launchCwd: z.string().min(1),
|
||||
});
|
||||
}).strict();
|
||||
|
||||
export const wsClientMessageSchema = wsRequestSchema;
|
||||
export const wsServerMessageSchema = z.union([wsResponseSchema, wsEventSchema, wsHelloSchema]);
|
||||
|
||||
Reference in new issue
Block a user