|
| 1 | +import { randomUUID } from "node:crypto"; |
| 2 | + |
| 3 | +import { expect } from "@effect/vitest"; |
| 4 | +import { Effect } from "effect"; |
| 5 | + |
| 6 | +import { scenario } from "../src/scenario"; |
| 7 | +import { Browser, Target } from "../src/services"; |
| 8 | + |
| 9 | +scenario( |
| 10 | + "Login CSRF · state is required, bound to the browser, and consumed after login", |
| 11 | + { timeout: 180_000 }, |
| 12 | + Effect.gen(function* () { |
| 13 | + const target = yield* Target; |
| 14 | + const browser = yield* Browser; |
| 15 | + const email = `csrf-${randomUUID()}@e2e.test`; |
| 16 | + yield* browser.session({ label: "anonymous" }, async ({ page, step }) => { |
| 17 | + const interceptCallback = async (): Promise<string> => { |
| 18 | + let callback: string | undefined; |
| 19 | + await page.route("**/api/auth/callback?**", async (route) => { |
| 20 | + callback = route.request().url(); |
| 21 | + await route.abort(); |
| 22 | + }); |
| 23 | + await page.goto(new URL("/api/auth/login", target.baseUrl).toString()); |
| 24 | + await page.getByPlaceholder("new-user@example.com").fill(email); |
| 25 | + await page.getByRole("button", { name: /Continue/ }).click(); |
| 26 | + await expect.poll(() => callback).toBeDefined(); |
| 27 | + await page.unroute("**/api/auth/callback?**"); |
| 28 | + if (!callback) throw new Error("AuthKit did not return a callback"); |
| 29 | + return callback; |
| 30 | + }; |
| 31 | + await step("Refuse a valid authorization code with no state", async () => { |
| 32 | + const callback = new URL(await interceptCallback()); |
| 33 | + callback.searchParams.delete("state"); |
| 34 | + const response = await page.request.get(callback.toString(), { maxRedirects: 0 }); |
| 35 | + expect(response.status()).toBe(400); |
| 36 | + expect(await response.text()).toBe("Invalid login state"); |
| 37 | + expect( |
| 38 | + (await page.context().cookies()).some((cookie) => cookie.name === "wos-session"), |
| 39 | + ).toBe(false); |
| 40 | + }); |
| 41 | + await step("Refuse a state from another login", async () => { |
| 42 | + const callback = new URL(await interceptCallback()); |
| 43 | + callback.searchParams.set("state", "another-browser-state"); |
| 44 | + const response = await page.request.get(callback.toString(), { maxRedirects: 0 }); |
| 45 | + expect(response.status()).toBe(400); |
| 46 | + expect(await response.text()).toBe("Invalid login state"); |
| 47 | + expect( |
| 48 | + (await page.context().cookies()).some((cookie) => cookie.name === "wos-session"), |
| 49 | + ).toBe(false); |
| 50 | + }); |
| 51 | + await step("Complete a fresh login, then reject the same callback again", async () => { |
| 52 | + const callback = await interceptCallback(); |
| 53 | + await page.goto(callback); |
| 54 | + await page.waitForURL((url) => url.pathname === "/create-org", { timeout: 30_000 }); |
| 55 | + const cookies = await page.context().cookies(); |
| 56 | + expect(cookies.some((cookie) => cookie.name === "wos-session")).toBe(true); |
| 57 | + expect(cookies.some((cookie) => cookie.name === "wos-login-state")).toBe(false); |
| 58 | + const me = await page.request.get(new URL("/api/auth/me", target.baseUrl).toString()); |
| 59 | + expect(me.status()).toBe(200); |
| 60 | + expect(await me.json()).toMatchObject({ user: { email } }); |
| 61 | + const replay = await page.request.get(callback, { maxRedirects: 0 }); |
| 62 | + expect(replay.status()).toBe(400); |
| 63 | + expect(await replay.text()).toBe("Invalid login state"); |
| 64 | + }); |
| 65 | + }); |
| 66 | + }), |
| 67 | +); |
0 commit comments