| 1 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | const mocks = vi.hoisted(() => ({ |
| 3 | getAgentEnv: vi.fn(), safeEqual: vi.fn(), createSession: vi.fn(), limit: vi.fn(), |
| 4 | })); |
| 5 | vi.mock("@/lib/community-agent", () => mocks); |
| 6 | import { POST } from "@/app/api/admin/login/route"; |
| 7 | |
| 8 | const STRONG_TOKEN = "local-fixture-token-0123456789-abcdefghij"; |
| 9 | const LONE_SURROGATES = Array.from({ length: 16 }, (_, index) => String.fromCharCode(0xd800 + index)).join("").repeat(2); |
| 10 | |
| 11 | function request(token = "wrong", ip = "127.0.0.1") { |
| 12 | return new Request("https://codewhale.net/api/admin/login?locale=en", { |
| 13 | method: "POST", |
| 14 | headers: { "Content-Type": "application/x-www-form-urlencoded", "CF-Connecting-IP": ip }, |
| 15 | body: new URLSearchParams({ token }), |
| 16 | }); |
| 17 | } |
| 18 | beforeEach(() => { |
| 19 | vi.resetAllMocks(); |
| 20 | mocks.getAgentEnv.mockResolvedValue({ MAINTAINER_TOKEN: STRONG_TOKEN, ADMIN_LOGIN_LIMITER: { limit: mocks.limit }, CURATED_KV: {} }); |
| 21 | mocks.limit.mockResolvedValue({ success: true }); |
| 22 | mocks.safeEqual.mockResolvedValue(false); |
| 23 | mocks.createSession.mockResolvedValue("session-id"); |
| 24 | }); |
| 25 | describe("admin login attempt boundary", () => { |
| 26 | it("bounds repeated guesses independently of token and forged IP", async () => { |
| 27 | let attempts = 0; |
| 28 | mocks.limit.mockImplementation(async () => ({ success: ++attempts <= 5 })); |
| 29 | for (let index = 0; index < 7; index++) { |
| 30 | const response = await POST(request(`guess-${index}`, `192.0.2.${index}`)); |
| 31 | expect(response.status).toBe(index < 5 ? 303 : 429); |
| 32 | if (index >= 5) expect(response.headers.get("Retry-After")).toBe("60"); |
| 33 | expect(response.headers.get("Cache-Control")).toBe("no-store"); |
| 34 | } |
| 35 | expect(mocks.safeEqual).toHaveBeenCalledTimes(5); |
| 36 | expect(mocks.createSession).not.toHaveBeenCalled(); |
| 37 | expect(mocks.limit.mock.calls.map(([options]) => options.key)).toEqual(Array(7).fill("codewhale-web:admin-login")); |
| 38 | }); |
| 39 | it("does not parse or compare credentials after refusal", async () => { |
| 40 | mocks.limit.mockResolvedValue({ success: false }); |
| 41 | const req = request(STRONG_TOKEN); |
| 42 | const read = vi.spyOn(req, "text"); |
| 43 | expect((await POST(req)).status).toBe(429); |
| 44 | expect(read).not.toHaveBeenCalled(); |
| 45 | expect(mocks.safeEqual).not.toHaveBeenCalled(); |
| 46 | expect(mocks.createSession).not.toHaveBeenCalled(); |
| 47 | }); |
| 48 | it("fails closed if the binding is missing or unavailable", async () => { |
| 49 | mocks.getAgentEnv.mockResolvedValueOnce({ MAINTAINER_TOKEN: STRONG_TOKEN }); |
| 50 | expect((await POST(request())).status).toBe(503); |
| 51 | mocks.limit.mockRejectedValue(new Error("binding unavailable")); |
| 52 | expect((await POST(request())).status).toBe(503); |
| 53 | expect(mocks.safeEqual).not.toHaveBeenCalled(); |
| 54 | expect(mocks.createSession).not.toHaveBeenCalled(); |
| 55 | }); |
| 56 | it("refuses to run with a configured token below the minimum strength", async () => { |
| 57 | const weakTokens = [ |
| 58 | "x", "configured", "a".repeat(31), "a".repeat(32), " ".repeat(40), "password".repeat(4), |
| 59 | "Password1".repeat(4), "0123456".repeat(5), "\u{1F433}".repeat(16), "L".repeat(600), LONE_SURROGATES, |
| 60 | ]; |
| 61 | for (const weak of weakTokens) { |
| 62 | mocks.getAgentEnv.mockResolvedValueOnce({ MAINTAINER_TOKEN: weak, ADMIN_LOGIN_LIMITER: { limit: mocks.limit }, CURATED_KV: {} }); |
| 63 | mocks.safeEqual.mockResolvedValue(true); |
| 64 | const response = await POST(request(weak)); |
| 65 | expect(response.status).toBe(503); |
| 66 | expect(response.headers.get("Cache-Control")).toBe("no-store"); |
| 67 | expect(await response.text()).toContain("at least 32 characters"); |
| 68 | } |
| 69 | expect(mocks.limit).not.toHaveBeenCalled(); |
| 70 | expect(mocks.safeEqual).not.toHaveBeenCalled(); |
| 71 | expect(mocks.createSession).not.toHaveBeenCalled(); |
| 72 | }); |
| 73 | it("accepts a token of exactly the minimum length", async () => { |
| 74 | const exact = "abcdefghijklmnopqrstuvwxyz012345"; |
| 75 | expect(exact).toHaveLength(32); |
| 76 | mocks.getAgentEnv.mockResolvedValueOnce({ MAINTAINER_TOKEN: exact, ADMIN_LOGIN_LIMITER: { limit: mocks.limit }, CURATED_KV: {} }); |
| 77 | mocks.safeEqual.mockResolvedValue(true); |
| 78 | expect((await POST(request(exact))).status).toBe(303); |
| 79 | }); |
| 80 | it("accepts a 32-character hex secret with fewer than 16 distinct characters", async () => { |
| 81 | const hex = "3f9a3f9a1c7e1c7e3f9a1c7e5b2d5b2d"; |
| 82 | expect(new Set(hex).size).toBeLessThan(16); |
| 83 | mocks.getAgentEnv.mockResolvedValueOnce({ MAINTAINER_TOKEN: hex, ADMIN_LOGIN_LIMITER: { limit: mocks.limit }, CURATED_KV: {} }); |
| 84 | mocks.safeEqual.mockResolvedValue(true); |
| 85 | expect((await POST(request(hex))).status).toBe(303); |
| 86 | }); |
| 87 | it("retains the authenticated session and redirect after an allowed attempt", async () => { |
| 88 | mocks.safeEqual.mockResolvedValue(true); |
| 89 | const response = await POST(request(STRONG_TOKEN)); |
| 90 | expect(response.status).toBe(303); |
| 91 | expect(response.headers.get("Location")).toBe("https://codewhale.net/en/admin"); |
| 92 | const cookie = response.headers.get("Set-Cookie"); |
| 93 | expect(cookie).toContain("mt_sid=session-id"); |
| 94 | expect(cookie).toContain("HttpOnly"); |
| 95 | expect(cookie).toContain("Secure"); |
| 96 | expect(cookie?.toLowerCase()).toContain("samesite=strict"); |
| 97 | expect(mocks.createSession).toHaveBeenCalledOnce(); |
| 98 | }); |
| 99 | }); |
| 100 |