Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions apps/server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
21 changes: 21 additions & 0 deletions tests/conversation-body-validation.test.ts
Original file line number Diff line number Diff line change
@@ -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}`);
}
});