| 1 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | |
| 3 | const securityMocks = vi.hoisted(() => ({ |
| 4 | agentChat: vi.fn(), |
| 5 | getAgentEnv: vi.fn(), |
| 6 | validateSession: vi.fn(), |
| 7 | })); |
| 8 | |
| 9 | vi.mock("@/lib/community-agent", async (importOriginal) => { |
| 10 | const actual = await importOriginal<typeof import("./community-agent")>(); |
| 11 | return { |
| 12 | ...actual, |
| 13 | agentChat: securityMocks.agentChat, |
| 14 | getAgentEnv: securityMocks.getAgentEnv, |
| 15 | validateSession: securityMocks.validateSession, |
| 16 | }; |
| 17 | }); |
| 18 | |
| 19 | import { POST as adminPost } from "../app/api/admin/post/route"; |
| 20 | import { reviewedBodyHash } from "./community-agent"; |
| 21 | |
| 22 | class FakeKv { |
| 23 | readonly values = new Map<string, string>(); |
| 24 | readonly reads: string[] = []; |
| 25 | readonly deleted: string[] = []; |
| 26 | |
| 27 | async get(key: string): Promise<string | null> { |
| 28 | this.reads.push(key); |
| 29 | return this.values.get(key) ?? null; |
| 30 | } |
| 31 | |
| 32 | async put(key: string, value: string): Promise<void> { |
| 33 | this.values.set(key, value); |
| 34 | } |
| 35 | |
| 36 | async list(options?: { prefix?: string; limit?: number }): Promise<{ keys: { name: string }[] }> { |
| 37 | const prefix = options?.prefix ?? ""; |
| 38 | const limit = options?.limit ?? Number.POSITIVE_INFINITY; |
| 39 | return { |
| 40 | keys: [...this.values.keys()] |
| 41 | .filter((key) => key.startsWith(prefix)) |
| 42 | .slice(0, limit) |
| 43 | .map((name) => ({ name })), |
| 44 | }; |
| 45 | } |
| 46 | |
| 47 | async delete(key: string): Promise<void> { |
| 48 | this.deleted.push(key); |
| 49 | this.values.delete(key); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | function inputUrl(input: string | URL | Request): string { |
| 54 | if (typeof input === "string") return input; |
| 55 | return input instanceof URL ? input.toString() : input.url; |
| 56 | } |
| 57 | |
| 58 | beforeEach(() => { |
| 59 | securityMocks.agentChat.mockReset(); |
| 60 | securityMocks.getAgentEnv.mockReset(); |
| 61 | securityMocks.validateSession.mockReset(); |
| 62 | securityMocks.validateSession.mockResolvedValue(true); |
| 63 | }); |
| 64 | |
| 65 | afterEach(() => { |
| 66 | vi.unstubAllEnvs(); |
| 67 | vi.unstubAllGlobals(); |
| 68 | }); |
| 69 | |
| 70 | describe("public security boundaries", () => { |
| 71 | it("rejects a non-draft admin key before any draft KV read or delete", async () => { |
| 72 | const kv = new FakeKv(); |
| 73 | kv.values.set("agent:state:latest", JSON.stringify({ state: "running" })); |
| 74 | securityMocks.getAgentEnv.mockResolvedValue({ |
| 75 | CURATED_KV: kv, |
| 76 | MAINTAINER_TOKEN: "local-fixture-token-0123456789-abcdefghij", |
| 77 | }); |
| 78 | |
| 79 | const response = await adminPost(new Request("https://codewhale.net/api/admin/post", { |
| 80 | method: "POST", |
| 81 | headers: { |
| 82 | "content-type": "application/json", |
| 83 | cookie: "mt_sid=test-session", |
| 84 | origin: "https://codewhale.net", |
| 85 | }, |
| 86 | body: JSON.stringify({ action: "discard", draftKey: "agent:state:latest" }), |
| 87 | })); |
| 88 | |
| 89 | await expect(response.json()).resolves.toEqual({ error: "invalid draftKey namespace" }); |
| 90 | expect(response.status).toBe(400); |
| 91 | expect(securityMocks.validateSession).toHaveBeenCalledOnce(); |
| 92 | expect(kv.reads).toEqual([]); |
| 93 | expect(kv.deleted).toEqual([]); |
| 94 | expect(kv.values.has("agent:state:latest")).toBe(true); |
| 95 | }); |
| 96 | |
| 97 | it("posts bodyZh when lang=zh and no editedBody is supplied", async () => { |
| 98 | const draft = { |
| 99 | id: "42", |
| 100 | type: "linkcheck", |
| 101 | targetNumber: 42, |
| 102 | bodyEn: "English body", |
| 103 | bodyZh: "中文正文", |
| 104 | generatedAt: "2026-01-01T00:00:00.000Z", |
| 105 | posted: false, |
| 106 | }; |
| 107 | const kv = new FakeKv(); |
| 108 | kv.values.set("draft:linkcheck:42", JSON.stringify(draft)); |
| 109 | |
| 110 | const capturedBodies: string[] = []; |
| 111 | const mockFetch = vi.fn(async (input: string | URL | Request, init?: RequestInit) => { |
| 112 | const url = inputUrl(input); |
| 113 | if (url.includes("/issues/42/comments")) { |
| 114 | const reqBody = JSON.parse((init?.body as string) ?? "{}"); |
| 115 | capturedBodies.push(reqBody.body); |
| 116 | return new Response(JSON.stringify({ id: 1 }), { status: 201, headers: { "content-type": "application/json" } }); |
| 117 | } |
| 118 | throw new Error(`unexpected URL: ${url}`); |
| 119 | }); |
| 120 | vi.stubGlobal("fetch", mockFetch); |
| 121 | |
| 122 | securityMocks.getAgentEnv.mockResolvedValue({ |
| 123 | CURATED_KV: kv, |
| 124 | MAINTAINER_TOKEN: "local-fixture-token-0123456789-abcdefghij", |
| 125 | MAINTAINER_GITHUB_PAT: "ghp_test", |
| 126 | GITHUB_REPO: "codewhale-hq/CodeWhale", |
| 127 | }); |
| 128 | |
| 129 | const response = await adminPost(new Request("https://codewhale.net/api/admin/post", { |
| 130 | method: "POST", |
| 131 | headers: { |
| 132 | "content-type": "application/json", |
| 133 | cookie: "mt_sid=test-session", |
| 134 | origin: "https://codewhale.net", |
| 135 | }, |
| 136 | body: JSON.stringify({ |
| 137 | action: "post", |
| 138 | draftKey: "draft:linkcheck:42", |
| 139 | lang: "zh", |
| 140 | reviewedSha256: await reviewedBodyHash("中文正文"), |
| 141 | }), |
| 142 | })); |
| 143 | |
| 144 | await expect(response.json()).resolves.toMatchObject({ ok: true }); |
| 145 | expect(capturedBodies).toHaveLength(1); |
| 146 | expect(capturedBodies[0]).toBe("中文正文"); |
| 147 | }); |
| 148 | }); |
| 149 |