Skip to content
Merged
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
84 changes: 83 additions & 1 deletion app/api/posts/edit/[token]/route.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextRequest } from "next/server";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

const from = vi.fn();

Expand All @@ -16,9 +16,52 @@ function createPatchRequest(body: unknown) {
}

describe("PATCH /api/posts/edit/[token]", () => {
const originalEnv = { ...process.env };

beforeEach(() => {
vi.resetModules();
vi.clearAllMocks();
process.env.R2_PUBLIC_URL = "https://cdn.example.com";
});

afterEach(() => {
process.env = { ...originalEnv };
});

it("rejects a screenshot URL that isn't on our own R2 bucket", async () => {
from.mockImplementation((table: string) => {
if (table === "posts") {
return {
select: () => ({
eq: () => ({
maybeSingle: async () => ({
data: { id: "post-1" },
error: null,
}),
}),
}),
};
}

throw new Error(`Unexpected table ${table}`);
});

const { PATCH } = await import("./route");
const response = await PATCH(
createPatchRequest({
agentSlug: "claude",
title: "Agent deleted a customer record during a routine sync",
outcome:
"The assistant misunderstood the task, deleted a live customer record, and forced the team into a manual restore that took several hours to unwind safely.",
damageLevel: 3,
tags: ["hallucination"],
isAnonymous: true,
screenshotUrls: ["https://evil.example.com/pwned.png"],
}),
{ params: { token: "token-123" } },
);

expect(response.status).toBe(400);
});

it("returns 404 for an invalid edit token", async () => {
Expand Down Expand Up @@ -120,6 +163,9 @@ describe("PATCH /api/posts/edit/[token]", () => {
tags: ["hallucination"],
isAnonymous: false,
authorHandle: "ops-team",
screenshotUrls: [
"https://cdn.example.com/screenshots/8f14e45f-ceea-467e-b7d1-3cfa78f5c15e.png",
],
}),
{ params: { token: "token-123" } },
);
Expand All @@ -131,4 +177,40 @@ describe("PATCH /api/posts/edit/[token]", () => {
{ post_id: "post-1", tag_id: "tag-1" },
]);
});

it("rejects a well-formed R2 URL with a malformed object key", async () => {
from.mockImplementation((table: string) => {
if (table === "posts") {
return {
select: () => ({
eq: () => ({
maybeSingle: async () => ({
data: { id: "post-1" },
error: null,
}),
}),
}),
};
}

throw new Error(`Unexpected table ${table}`);
});

const { PATCH } = await import("./route");
const response = await PATCH(
createPatchRequest({
agentSlug: "claude",
title: "Agent deleted a customer record during a routine sync",
outcome:
"The assistant misunderstood the task, deleted a live customer record, and forced the team into a manual restore that took several hours to unwind safely.",
damageLevel: 3,
tags: ["hallucination"],
isAnonymous: true,
screenshotUrls: ["https://cdn.example.com/screenshots/not-a-uuid.png"],
}),
{ params: { token: "token-123" } },
);

expect(response.status).toBe(400);
});
});
5 changes: 2 additions & 3 deletions app/api/posts/edit/[token]/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { createHash } from "crypto";
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import { createSupabaseAdminClient } from "@/lib/supabase/admin";
import { submitSchema } from "@/lib/schemas/submit";
import { submitSchema, screenshotUrlsSchema } from "@/lib/schemas/submit";
import { redactPii } from "@/lib/utils/pii";

const editSchema = submitSchema.extend({
screenshotUrls: z.array(z.string().url()).max(5).optional(),
screenshotUrls: screenshotUrlsSchema,
});

interface EditablePostRow {
Expand Down
217 changes: 216 additions & 1 deletion app/api/posts/route.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextRequest } from "next/server";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

const sendEditTokenEmail = vi.fn();
const hashIp = vi.fn(() => "hashed-ip");
Expand Down Expand Up @@ -42,9 +42,224 @@ function createRequest(body: unknown) {
}

describe("POST /api/posts", () => {
const originalEnv = { ...process.env };

beforeEach(() => {
vi.resetModules();
vi.clearAllMocks();
process.env.R2_PUBLIC_URL = "https://cdn.example.com";
});

afterEach(() => {
process.env = { ...originalEnv };
});

it("rejects a screenshot URL that isn't on our own R2 bucket", async () => {
from.mockImplementation((table: string) => {
if (table === "agents") {
return {
select: () => ({
eq: () => ({
single: async () => ({ data: { id: "agent-1" }, error: null }),
}),
}),
};
}

if (table === "tags") {
return {
select: () => ({
in: async () => ({
data: [{ id: "tag-1", slug: "hallucination" }],
error: null,
}),
}),
};
}

throw new Error(`Unexpected table ${table}`);
});

const { POST } = await import("./route");
const response = await POST(
createRequest({
agentSlug: "claude",
title: "Agent deleted a customer record during a routine sync",
outcome:
"The assistant misunderstood the task, deleted a live customer record, and forced the team into a manual restore that took several hours to unwind safely.",
damageLevel: 3,
tags: ["hallucination"],
isAnonymous: true,
screenshotUrls: ["https://evil.example.com/pwned.png"],
}),
);

expect(response.status).toBe(400);
});

it("rejects a well-formed R2 URL with a malformed object key", async () => {
from.mockImplementation((table: string) => {
if (table === "agents") {
return {
select: () => ({
eq: () => ({
single: async () => ({ data: { id: "agent-1" }, error: null }),
}),
}),
};
}

if (table === "tags") {
return {
select: () => ({
in: async () => ({
data: [{ id: "tag-1", slug: "hallucination" }],
error: null,
}),
}),
};
}

throw new Error(`Unexpected table ${table}`);
});

const { POST } = await import("./route");
const response = await POST(
createRequest({
agentSlug: "claude",
title: "Agent deleted a customer record during a routine sync",
outcome:
"The assistant misunderstood the task, deleted a live customer record, and forced the team into a manual restore that took several hours to unwind safely.",
damageLevel: 3,
tags: ["hallucination"],
isAnonymous: true,
screenshotUrls: ["https://cdn.example.com/screenshots/not-a-uuid.png"],
}),
);

expect(response.status).toBe(400);
});

it("accepts a submission with a valid R2 screenshot URL", async () => {
const insertSpy = vi.fn().mockReturnValue({
select: () => ({
single: async () => ({ data: { id: "post-1" }, error: null }),
}),
});

from.mockImplementation((table: string) => {
if (table === "agents") {
return {
select: () => ({
eq: () => ({
single: async () => ({ data: { id: "agent-1" }, error: null }),
}),
}),
};
}

if (table === "tags") {
return {
select: () => ({
in: async () => ({
data: [{ id: "tag-1", slug: "hallucination" }],
error: null,
}),
}),
};
}

if (table === "posts") {
return { insert: insertSpy };
}

if (table === "post_tags") {
return { insert: vi.fn().mockResolvedValue({ error: null }) };
}

throw new Error(`Unexpected table ${table}`);
});

const { POST } = await import("./route");
const response = await POST(
createRequest({
agentSlug: "claude",
title: "Agent deleted a customer record during a routine sync",
outcome:
"The assistant misunderstood the task, deleted a live customer record, and forced the team into a manual restore that took several hours to unwind safely.",
damageLevel: 3,
tags: ["hallucination"],
isAnonymous: true,
screenshotUrls: [
"https://cdn.example.com/screenshots/8f14e45f-ceea-467e-b7d1-3cfa78f5c15e.png",
],
}),
);

expect(response.status).toBe(201);
expect(insertSpy).toHaveBeenCalledWith(
expect.objectContaining({
screenshot_urls: [
"https://cdn.example.com/screenshots/8f14e45f-ceea-467e-b7d1-3cfa78f5c15e.png",
],
}),
);
});

it("accepts a submission with no screenshots at all", async () => {
const insertSpy = vi.fn().mockReturnValue({
select: () => ({
single: async () => ({ data: { id: "post-1" }, error: null }),
}),
});

from.mockImplementation((table: string) => {
if (table === "agents") {
return {
select: () => ({
eq: () => ({
single: async () => ({ data: { id: "agent-1" }, error: null }),
}),
}),
};
}

if (table === "tags") {
return {
select: () => ({
in: async () => ({
data: [{ id: "tag-1", slug: "hallucination" }],
error: null,
}),
}),
};
}

if (table === "posts") {
return { insert: insertSpy };
}

if (table === "post_tags") {
return { insert: vi.fn().mockResolvedValue({ error: null }) };
}

throw new Error(`Unexpected table ${table}`);
});

const { POST } = await import("./route");
const response = await POST(
createRequest({
agentSlug: "claude",
title: "Agent deleted a customer record during a routine sync",
outcome:
"The assistant misunderstood the task, deleted a live customer record, and forced the team into a manual restore that took several hours to unwind safely.",
damageLevel: 3,
tags: ["hallucination"],
isAnonymous: true,
}),
);

expect(response.status).toBe(201);
});

it("rejects unknown tags instead of silently dropping them", async () => {
Expand Down
5 changes: 2 additions & 3 deletions app/api/posts/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import { submitSchema } from "@/lib/schemas/submit";
import { submitSchema, screenshotUrlsSchema } from "@/lib/schemas/submit";
import { createSupabaseAdminClient } from "@/lib/supabase/admin";
import { sendEditTokenEmail } from "@/lib/resend/send";
import { hashIp, getClientIp } from "@/lib/utils/hash";
Expand All @@ -13,7 +12,7 @@ const WINDOW_SECONDS = 60 * 60;
const MAX_SUBMISSIONS = 3;

const bodySchema = submitSchema.extend({
screenshotUrls: z.array(z.string().url()).max(5).optional(),
screenshotUrls: screenshotUrlsSchema,
});

export async function POST(req: NextRequest) {
Expand Down
Loading
Loading