Unified · Split
| @@ -0,0 +1,5 @@ | |||
| 1 | --- | ||
| 2 | "@executor-js/cloud": patch | ||
| 3 | --- | ||
| 4 | |||
| 5 | Restart hosted invitation logins that return without state, while keeping authorization codes bound to the browser that started the login. | ||
| @@ -188,14 +188,19 @@ export const CloudAuthPublicHandlers = HttpApiBuilder.group( · 187 unmodified lines | |||
| 188 | Effect.gen(function* () { | 188 | Effect.gen(function* () { |
| 189 | const workos = yield* WorkOSClient; | 189 | const workos = yield* WorkOSClient; |
| 190 | const users = yield* UserStoreService; | 190 | const users = yield* UserStoreService; |
| 191 | // Hosted invitations can start at WorkOS without app-issued state. | ||
| 192 | // Discard that unbound code and start a fresh browser-bound login. | ||
| 193 | // Exchanging it here would allow login CSRF. | ||
| 194 | if (query.state === undefined) { | ||
| 195 | return deleteResponseCookie( | ||
| 196 | HttpServerResponse.redirect(AUTH_PATHS.login, { status: 302 }), | ||
| 197 | STATE_COOKIE, | ||
| 198 | ); | ||
| 199 | } | ||
| 200 | |||
| 191 | const cookieState = request.cookies[STATE_COOKIE] ?? null; | 201 | const cookieState = request.cookies[STATE_COOKIE] ?? null; |
| 192 | // CSRF is unconditional: every callback must carry a state that | 202 | // Only exchange codes bound to the state cookie set on /login. |
| 193 | // matches the cookie set on /login. There is no legitimate | 203 | if (!cookieState || !timingSafeEqual(cookieState, query.state)) { |
| 194 | // no-state entry path — omitting state previously allowed an | ||
| 195 | // attacker to complete their own OAuth round-trip and redirect a | ||
| 196 | // victim's browser through this callback, signing the victim into | ||
| 197 | // the attacker's account (login CSRF). | ||
| 198 | if (!cookieState || !timingSafeEqual(cookieState, query.state ?? "")) { | ||
| 199 | return deleteResponseCookie( | 204 | return deleteResponseCookie( |
| 200 | HttpServerResponse.text("Invalid login state", { status: 400 }), | 205 | HttpServerResponse.text("Invalid login state", { status: 400 }), |
| 201 | STATE_COOKIE, | 206 | STATE_COOKIE, |
| @@ -1,9 +1,8 @@ | |||
| 1 | // --------------------------------------------------------------------------- | 1 | // --------------------------------------------------------------------------- |
| 2 | // Focused tests — the WorkOS login callback's CSRF gate. | 2 | // Focused tests — the WorkOS login callback's CSRF gate. |
| 3 | // | 3 | // |
| 4 | // The callback's CSRF check must be unconditional: no state ⇒ 400 before any | 4 | // Codes without state restart login without a WorkOS exchange; a replayed |
| 5 | // WorkOS call; a replayed (already consumed) state ⇒ 400; a fresh state | 5 | // state still fails with 400; a fresh state matching the cookie issues a session. |
| 6 | // matching the cookie ⇒ 302 + session. | ||
| 7 | // | 6 | // |
| 8 | // Test seams follow repo conventions: @effect/vitest, Layer.succeed stubs | 7 | // Test seams follow repo conventions: @effect/vitest, Layer.succeed stubs |
| 9 | // (see org-selector-auth.node.test.ts), and HttpRouter.toWebHandler for the | 8 | // (see org-selector-auth.node.test.ts), and HttpRouter.toWebHandler for the |
| @@ -38,12 +37,14 @@ const stubWorkOS = Layer.succeed( · 28 unmodified lines | |||
| 38 | new Proxy({} as WorkOSClientService, { | 37 | new Proxy({} as WorkOSClientService, { |
| 39 | get: (_t, prop) => { | 38 | get: (_t, prop) => { |
| 40 | if (prop === "authenticateWithCode") { | 39 | if (prop === "authenticateWithCode") { |
| 41 | return () => | 40 | return (code: string) => |
| 42 | Effect.succeed({ | 41 | code === "unbound-code" |
| 43 | user: { id: STUB_USER_ID, email: "u@test" }, | 42 | ? Effect.die("An unbound authorization code must never be exchanged") |
| 44 | organizationId: STUB_ORG_ID, | 43 | : Effect.succeed({ |
| 45 | sealedSession: STUB_SESSION, | 44 | user: { id: STUB_USER_ID, email: "u@test" }, |
| 46 | }); | 45 | organizationId: STUB_ORG_ID, |
| 46 | sealedSession: STUB_SESSION, | ||
| 47 | }); | ||
| 47 | } | 48 | } |
| 48 | if (prop === "listUserMemberships") { | 49 | if (prop === "listUserMemberships") { |
| 49 | return () => Effect.succeed({ data: [] }); | 50 | return () => Effect.succeed({ data: [] }); |
| @@ -128,20 +129,30 @@ describe("workos callback · CSRF state hardening", () => { · 78 unmodified lines | |||
| 128 | }); | 129 | }); |
| 129 | } | 130 | } |
| 130 | 131 | ||
| 131 | it("rejects a callback with NO state (the former bypass) before any WorkOS call", async () => { | 132 | it("restarts login without exchanging a code that has no state", async () => { |
| 132 | const res = await run(new Request(callbackUrl(undefined), { redirect: "manual" })); | 133 | const res = await run( |
| 133 | expect(res.status).toBe(400); | 134 | new Request(callbackUrl(undefined, "unbound-code"), { redirect: "manual" }), |
| 134 | expect(await res.text()).toContain("Invalid login state"); | 135 | ); |
| 136 | expect(res.status).toBe(302); | ||
| 137 | expect(res.headers.get("location")).toBe("/api/auth/login"); | ||
| 135 | expect(res.headers.get("set-cookie") ?? "").not.toContain(SESSION_COOKIE); | 138 | expect(res.headers.get("set-cookie") ?? "").not.toContain(SESSION_COOKIE); |
| 136 | }); | 139 | }); |
| 137 | 140 | ||
| 138 | it("rejects missing state even when the browser has a login cookie", async () => { | 141 | it("discards an existing login cookie when restarting a callback without state", async () => { |
| 139 | const res = await run( | 142 | const res = await run( |
| 140 | new Request(callbackUrl(undefined), { | 143 | new Request(callbackUrl(undefined, "unbound-code"), { |
| 141 | headers: { cookie: `${STATE_COOKIE}=victim-login-state` }, | 144 | headers: { cookie: `${STATE_COOKIE}=victim-login-state` }, |
| 142 | redirect: "manual", | 145 | redirect: "manual", |
| 143 | }), | 146 | }), |
| 144 | ); | 147 | ); |
| 148 | expect(res.status).toBe(302); | ||
| 149 | expect(res.headers.get("location")).toBe("/api/auth/login"); | ||
| 150 | expect(res.headers.get("set-cookie")).toContain(`${STATE_COOKIE}=; Max-Age=0`); | ||
| 151 | expect(res.headers.get("set-cookie") ?? "").not.toContain(SESSION_COOKIE); | ||
| 152 | }); | ||
| 153 | |||
| 154 | it("rejects empty state instead of treating it as a provider-initiated login", async () => { | ||
| 155 | const res = await run(new Request(`${callbackUrl(undefined, "unbound-code")}&state=`)); | ||
| 145 | expect(res.status).toBe(400); | 156 | expect(res.status).toBe(400); |
| 146 | expect(await res.text()).toBe("Invalid login state"); | 157 | expect(await res.text()).toBe("Invalid login state"); |
| 147 | expect(res.headers.get("set-cookie") ?? "").not.toContain(SESSION_COOKIE); | 158 | expect(res.headers.get("set-cookie") ?? "").not.toContain(SESSION_COOKIE); |
| @@ -36,12 +36,12 @@ scenario( · 35 unmodified lines | |||
| 36 | if (!callback) throw new Error("AuthKit did not return a callback"); | 36 | if (!callback) throw new Error("AuthKit did not return a callback"); |
| 37 | return callback; | 37 | return callback; |
| 38 | }; | 38 | }; |
| 39 | await step("Refuse a valid authorization code with no state", async () => { | 39 | await step("Discard a valid code without state and restart login", async () => { |
| 40 | const callback = new URL(await interceptCallback()); | 40 | const callback = new URL(await interceptCallback()); |
| 41 | callback.searchParams.delete("state"); | 41 | callback.searchParams.delete("state"); |
| 42 | const response = await page.request.get(callback.toString(), { maxRedirects: 0 }); | 42 | const response = await page.request.get(callback.toString(), { maxRedirects: 0 }); |
| 43 | expect(response.status()).toBe(400); | 43 | expect(response.status()).toBe(302); |
| 44 | expect(await response.text()).toBe("Invalid login state"); | 44 | expect(response.headers().location).toBe("/api/auth/login"); |
| 45 | expect( | 45 | expect( |
| 46 | (await page.context().cookies()).some((cookie) => cookie.name === "wos-session"), | 46 | (await page.context().cookies()).some((cookie) => cookie.name === "wos-session"), |
| 47 | ).toBe(false); | 47 | ).toBe(false); |
| @@ -73,3 +73,54 @@ scenario( · 25 unmodified lines | |||
| 73 | }); | 73 | }); |
| 74 | }), | 74 | }), |
| 75 | ); | 75 | ); |
| 76 | |||
| 77 | scenario( | ||
| 78 | "Auth · a provider-initiated login restarts with browser-bound state", | ||
| 79 | { timeout: 180_000 }, | ||
| 80 | Effect.gen(function* () { | ||
| 81 | const target = yield* Target; | ||
| 82 | const browser = yield* Browser; | ||
| 83 | const email = `provider-login-${randomUUID()}@e2e.test`; | ||
| 84 | // Discover this deployment's provider URL without setting a browser cookie. | ||
| 85 | const login = yield* Effect.promise(() => | ||
| 86 | fetch(new URL("/api/auth/login", target.baseUrl), { redirect: "manual" }), | ||
| 87 | ); | ||
| 88 | expect(login.status).toBe(302); | ||
| 89 | const location = login.headers.get("location"); | ||
| 90 | if (!location) throw new Error("Login did not redirect to AuthKit"); | ||
| 91 | const providerUrl = new URL(location); | ||
| 92 | providerUrl.searchParams.delete("state"); | ||
| 93 | |||
| 94 | yield* browser.session({ label: "anonymous" }, async ({ page, step }) => { | ||
| 95 | await step("Sign in directly at the provider, as a hosted invitation does", async () => { | ||
| 96 | await page.goto(providerUrl.toString()); | ||
| 97 | await page.getByPlaceholder("new-user@example.com").fill(email); | ||
| 98 | const callbackResponse = page.waitForResponse( | ||
| 99 | (response) => new URL(response.url()).pathname === "/api/auth/callback", | ||
| 100 | ); | ||
| 101 | await page.getByRole("button", { name: /Continue/ }).click(); | ||
| 102 | const callback = await callbackResponse; | ||
| 103 | expect(new URL(callback.url()).searchParams.has("state")).toBe(false); | ||
| 104 | expect(callback.status()).toBe(302); | ||
| 105 | expect(callback.headers().location).toBe("/api/auth/login"); | ||
| 106 | await page.waitForURL((url) => url.searchParams.has("state")); | ||
| 107 | expect((await page.context().cookies()).map((cookie) => cookie.name)).not.toContain( | ||
| 108 | "wos-session", | ||
| 109 | ); | ||
| 110 | }); | ||
| 111 | |||
| 112 | await step("Complete the fresh login and reach the signed-in app", async () => { | ||
| 113 | // The emulator asks again; hosted AuthKit can reuse its browser session. | ||
| 114 | await page.getByPlaceholder("new-user@example.com").fill(email); | ||
| 115 | await page.getByRole("button", { name: /Continue/ }).click(); | ||
| 116 | await page.waitForURL((url) => url.pathname === "/create-org", { timeout: 30_000 }); | ||
| 117 | const me = await page.request.get(new URL("/api/auth/me", target.baseUrl).toString()); | ||
| 118 | expect(me.status()).toBe(200); | ||
| 119 | expect(await me.json()).toMatchObject({ user: { email } }); | ||
| 120 | const cookieNames = (await page.context().cookies()).map((cookie) => cookie.name); | ||
| 121 | expect(cookieNames).toContain("wos-session"); | ||
| 122 | expect(cookieNames).not.toContain("wos-login-state"); | ||
| 123 | }); | ||
| 124 | }); | ||
| 125 | }), | ||
| 126 | ); | ||