| 1 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | |
| 3 | const mocks = vi.hoisted(() => ({ getEnv: vi.fn(), safeEqual: vi.fn(), limit: vi.fn() })); |
| 4 | vi.mock("@/lib/kv", () => ({ getEnv: mocks.getEnv })); |
| 5 | vi.mock("@/lib/community-agent", () => ({ safeEqual: mocks.safeEqual })); |
| 6 | vi.mock("@/lib/facts-drift", () => ({ runFactsDrift: vi.fn() })); |
| 7 | vi.mock("@/lib/content-watch", () => ({ runLinkCheck: vi.fn(), runSemanticDrift: vi.fn() })); |
| 8 | import { GET } from "@/app/api/cron/route"; |
| 9 | |
| 10 | function request(secret = "wrong", ip = "127.0.0.1") { |
| 11 | return new Request("https://codewhale.net/api/cron?task=nope", { |
| 12 | headers: { "x-cron-secret": secret, "CF-Connecting-IP": ip }, |
| 13 | }); |
| 14 | } |
| 15 | |
| 16 | beforeEach(() => { |
| 17 | vi.resetAllMocks(); |
| 18 | mocks.getEnv.mockResolvedValue({ CRON_SECRET: "configured-cron-secret-0123456789-abcdef", ADMIN_LOGIN_LIMITER: { limit: mocks.limit } }); |
| 19 | mocks.limit.mockResolvedValue({ success: true }); |
| 20 | mocks.safeEqual.mockResolvedValue(false); |
| 21 | }); |
| 22 | |
| 23 | describe("manual cron trigger attempt boundary", () => { |
| 24 | it("bounds repeated guesses independently of the secret and forged IP", async () => { |
| 25 | let attempts = 0; |
| 26 | mocks.limit.mockImplementation(async () => ({ success: ++attempts <= 5 })); |
| 27 | for (let index = 0; index < 7; index++) { |
| 28 | const response = await GET(request(`guess-${index}`, `192.0.2.${index}`)); |
| 29 | expect(response.status).toBe(index < 5 ? 401 : 429); |
| 30 | if (index >= 5) expect(response.headers.get("Retry-After")).toBe("60"); |
| 31 | expect(response.headers.get("Cache-Control")).toBe("no-store"); |
| 32 | } |
| 33 | expect(mocks.safeEqual).toHaveBeenCalledTimes(5); |
| 34 | expect(mocks.limit.mock.calls.map(([options]) => options.key)).toEqual(Array(7).fill("codewhale-web:cron-secret")); |
| 35 | }); |
| 36 | it("does not compare the secret after refusal", async () => { |
| 37 | mocks.limit.mockResolvedValue({ success: false }); |
| 38 | expect((await GET(request("configured-cron-secret-0123456789-abcdef"))).status).toBe(429); |
| 39 | expect(mocks.safeEqual).not.toHaveBeenCalled(); |
| 40 | }); |
| 41 | it("fails closed if the binding is missing or unavailable", async () => { |
| 42 | mocks.getEnv.mockResolvedValueOnce({ CRON_SECRET: "configured-cron-secret-0123456789-abcdef" }); |
| 43 | expect((await GET(request())).status).toBe(503); |
| 44 | mocks.limit.mockRejectedValue(new Error("binding unavailable")); |
| 45 | expect((await GET(request())).status).toBe(503); |
| 46 | expect(mocks.safeEqual).not.toHaveBeenCalled(); |
| 47 | }); |
| 48 | it("stays disabled without a configured secret and never consults the limiter", async () => { |
| 49 | mocks.getEnv.mockResolvedValue({ ADMIN_LOGIN_LIMITER: { limit: mocks.limit } }); |
| 50 | expect((await GET(request())).status).toBe(503); |
| 51 | expect(mocks.limit).not.toHaveBeenCalled(); |
| 52 | }); |
| 53 | it("stays disabled for a secret below the minimum strength and never consults the limiter or compares", async () => { |
| 54 | const loneSurrogates = Array.from({ length: 16 }, (_, index) => String.fromCharCode(0xd800 + index)).join("").repeat(2); |
| 55 | const weak = ["x", "a".repeat(32), " ".repeat(40), "password".repeat(4), "Password1".repeat(4), "\u{1F433}".repeat(16), "0123456".repeat(5), "L".repeat(600), loneSurrogates]; |
| 56 | for (const secret of weak) { |
| 57 | mocks.getEnv.mockResolvedValueOnce({ CRON_SECRET: secret, ADMIN_LOGIN_LIMITER: { limit: mocks.limit } }); |
| 58 | const response = await GET(request()); |
| 59 | expect(response.status).toBe(503); |
| 60 | expect(response.headers.get("Cache-Control")).toBe("no-store"); |
| 61 | expect(JSON.stringify(await response.json())).toContain("at least 32 characters"); |
| 62 | } |
| 63 | expect(mocks.limit).not.toHaveBeenCalled(); |
| 64 | expect(mocks.safeEqual).not.toHaveBeenCalled(); |
| 65 | }); |
| 66 | it("accepts a 32-character hex secret with fewer than 16 distinct characters", async () => { |
| 67 | const hex = "3f9a3f9a1c7e1c7e3f9a1c7e5b2d5b2d"; |
| 68 | mocks.getEnv.mockResolvedValueOnce({ CRON_SECRET: hex, ADMIN_LOGIN_LIMITER: { limit: mocks.limit } }); |
| 69 | mocks.safeEqual.mockResolvedValue(true); |
| 70 | expect((await GET(request(hex))).status).toBe(400); |
| 71 | }); |
| 72 | it("still reaches task validation after an allowed, correct attempt", async () => { |
| 73 | mocks.safeEqual.mockResolvedValue(true); |
| 74 | expect((await GET(request("configured-cron-secret-0123456789-abcdef"))).status).toBe(400); |
| 75 | }); |
| 76 | }); |
| 77 |