From eb44d30ebd172bfc6393d91363fd8d1202703c29 Mon Sep 17 00:00:00 2001 From: Ayman Mohamed Elsaid Ali Date: Wed, 18 Nov 2026 22:34:44 +0100 Subject: [PATCH] fix(server): validate PUT /api/conversation request body as an object --- apps/server/src/app.ts | 5 +++-- tests/conversation-body-validation.test.ts | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 tests/conversation-body-validation.test.ts diff --git a/apps/server/src/app.ts b/apps/server/src/app.ts index 811a5f520..b9db2e640 100644 --- a/apps/server/src/app.ts +++ b/apps/server/src/app.ts @@ -241,8 +241,9 @@ export async function createApp( c.json((await db.get(c.get("owner"), "conversations", "default")) ?? { messages: [] }), ); app.put("/api/conversation", async (c) => { - const body = await c.req.json(); - const messages = z.array(z.unknown()).max(1000).parse(body.messages); + const { messages } = z + .object({ messages: z.array(z.unknown()).max(1000) }) + .parse(await c.req.json()); for (const message of messages) MessageSchema.parse(message); await db.put(c.get("owner"), "conversations", { id: "default", messages }); return c.json({ ok: true }); diff --git a/tests/conversation-body-validation.test.ts b/tests/conversation-body-validation.test.ts new file mode 100644 index 000000000..949d6b5d9 --- /dev/null +++ b/tests/conversation-body-validation.test.ts @@ -0,0 +1,21 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { createApp } from "../apps/server/src/app.ts"; +import { browserFixture } from "./helpers/browser.ts"; + +test("PUT /api/conversation rejects null and primitive JSON bodies with 422 instead of 502", async (t) => { + const { db, config } = await browserFixture(t, () => ({ data: {} })); + const { app, auth, agent } = await createApp(db, config); + t.after(() => agent.stop()); + const { token } = await auth.session(); + const headers = { Authorization: `Bearer ${token}`, "Content-Type": "application/json" }; + + for (const body of ["null", "123", '"hello"', "true", "[]"]) { + const response = await app.request("/api/conversation", { + method: "PUT", + headers, + body, + }); + assert.equal(response.status, 422, `Expected 422 for body ${body}, got ${response.status}`); + } +});