|
| 1 | +// Cloud-specific: the SSR auth gate's session handling BEYOND the basic |
| 2 | +// signed-out redirect (unauthenticated-skeleton.test.ts owns that): an |
| 3 | +// invalid cookie is actively cleared, /cloud (the marketing CTA path) routes |
| 4 | +// into the app, and — the nastiest path — a session whose access token no |
| 5 | +// longer verifies is refreshed in-flight, with the rotated sealed session |
| 6 | +// reaching the browser. WorkOS refresh tokens are single-use, so losing that |
| 7 | +// Set-Cookie silently logs the user out on next expiry; these scenarios pin |
| 8 | +// it against the real WorkOS emulator (real rotation, real revocation). |
| 9 | +import { expect } from "@effect/vitest"; |
| 10 | +import { Effect } from "effect"; |
| 11 | +import * as Iron from "iron-webcrypto"; |
| 12 | + |
| 13 | +import { scenario } from "../src/scenario"; |
| 14 | +import { Api, Target } from "../src/services"; |
| 15 | +import { E2E_COOKIE_PASSWORD } from "../targets/cloud"; |
| 16 | + |
| 17 | +/** A signed-out-style document request (what the gate keys on). */ |
| 18 | +const documentRequest = (url: URL, cookie?: string) => |
| 19 | + Effect.promise(() => |
| 20 | + fetch(url, { |
| 21 | + redirect: "manual", |
| 22 | + headers: { accept: "text/html", ...(cookie ? { cookie } : {}) }, |
| 23 | + }), |
| 24 | + ); |
| 25 | + |
| 26 | +const setCookieFor = (response: Response, name: string): string => { |
| 27 | + for (const header of response.headers.getSetCookie()) { |
| 28 | + if (header.startsWith(`${name}=`)) return header; |
| 29 | + } |
| 30 | + return ""; |
| 31 | +}; |
| 32 | + |
| 33 | +scenario( |
| 34 | + "Session gate · an invalid session cookie is cleared on the way to /login", |
| 35 | + {}, |
| 36 | + Effect.gen(function* () { |
| 37 | + // Gate: the REST API plane is mounted on this target. |
| 38 | + yield* Api; |
| 39 | + const target = yield* Target; |
| 40 | + |
| 41 | + // A cookie that was never a sealed session. On executor.sh its mere |
| 42 | + // presence routes / past the marketing page, so the gate must not just |
| 43 | + // redirect — it must take the cookie (and the auth hint beside it) away. |
| 44 | + const response = yield* documentRequest( |
| 45 | + new URL("/", target.baseUrl), |
| 46 | + "wos-session=not-a-real-session; executor-auth-hint=stale", |
| 47 | + ); |
| 48 | + expect(response.status, "an unverifiable session is signed out").toBe(302); |
| 49 | + expect(response.headers.get("location"), "…to the login page").toBe("/login"); |
| 50 | + |
| 51 | + const clearedSession = setCookieFor(response, "wos-session"); |
| 52 | + expect(clearedSession, "the dead session cookie is dropped").toContain("Max-Age=0"); |
| 53 | + const clearedHint = setCookieFor(response, "executor-auth-hint"); |
| 54 | + expect(clearedHint, "the auth hint goes with it").toContain("Max-Age=0"); |
| 55 | + }), |
| 56 | +); |
| 57 | + |
| 58 | +scenario( |
| 59 | + "Session gate · /cloud (the marketing CTA path) routes into the app", |
| 60 | + {}, |
| 61 | + Effect.gen(function* () { |
| 62 | + // Gate: the REST API plane is mounted on this target. |
| 63 | + yield* Api; |
| 64 | + const target = yield* Target; |
| 65 | + |
| 66 | + // Marketing CTAs link to /cloud, which is not a route — it means "open |
| 67 | + // the app". Signed out, that lands on login (no returnTo: the deep link |
| 68 | + // IS the root)… |
| 69 | + const signedOut = yield* documentRequest(new URL("/cloud", target.baseUrl)); |
| 70 | + expect(signedOut.status, "signed-out /cloud is gated like any page").toBe(302); |
| 71 | + expect(signedOut.headers.get("location"), "…straight to login").toBe("/login"); |
| 72 | + |
| 73 | + // …and signed in, it opens the app at the root instead of 404ing. |
| 74 | + const identity = yield* target.newIdentity(); |
| 75 | + const signedIn = yield* documentRequest( |
| 76 | + new URL("/cloud", target.baseUrl), |
| 77 | + identity.headers!.cookie!, |
| 78 | + ); |
| 79 | + expect(signedIn.status, "signed-in /cloud is a redirect, not a 404").toBe(302); |
| 80 | + expect(signedIn.headers.get("location"), "…into the app").toBe("/"); |
| 81 | + }), |
| 82 | +); |
| 83 | + |
| 84 | +// The sealed wos-session is iron-sealed JSON { accessToken, refreshToken, … }. |
| 85 | +// Corrupting the access token's signature makes the gate's verify fail the |
| 86 | +// same way an EXPIRED token does (invalid JWT → refresh path) — without |
| 87 | +// waiting out a real expiry. Same sealing library + password map the WorkOS |
| 88 | +// SDK uses, so the gate can't tell this seal from one the SDK minted. |
| 89 | +const withTamperedAccessToken = async (sessionCookie: string): Promise<string> => { |
| 90 | + const sealed = sessionCookie.slice("wos-session=".length).replace(/~\d$/, ""); |
| 91 | + const session = (await Iron.unseal(sealed, { "1": E2E_COOKIE_PASSWORD }, Iron.defaults)) as { |
| 92 | + accessToken: string; |
| 93 | + }; |
| 94 | + const [header, payload, signature] = session.accessToken.split("."); |
| 95 | + const tampered = { |
| 96 | + ...session, |
| 97 | + accessToken: `${header}.${payload}.${[...(signature ?? "")].reverse().join("")}`, |
| 98 | + }; |
| 99 | + const resealed = await Iron.seal( |
| 100 | + tampered, |
| 101 | + { id: "1", secret: E2E_COOKIE_PASSWORD }, |
| 102 | + Iron.defaults, |
| 103 | + ); |
| 104 | + return `wos-session=${resealed}~2`; |
| 105 | +}; |
| 106 | + |
| 107 | +scenario( |
| 108 | + "Session gate · a stale access token is refreshed in-flight and the rotated session reaches the browser", |
| 109 | + {}, |
| 110 | + Effect.gen(function* () { |
| 111 | + // Gate: the REST API plane is mounted on this target. |
| 112 | + yield* Api; |
| 113 | + const target = yield* Target; |
| 114 | + |
| 115 | + const identity = yield* target.newIdentity(); |
| 116 | + const staleCookie = yield* Effect.promise(() => |
| 117 | + withTamperedAccessToken(identity.headers!.cookie!), |
| 118 | + ); |
| 119 | + |
| 120 | + // 1. The page is served (no login detour) — the gate refreshed the |
| 121 | + // session against WorkOS mid-request — and the ROTATED sealed session |
| 122 | + // is set on the response. Refresh tokens are single-use: dropping |
| 123 | + // this Set-Cookie would log the user out at the next expiry. |
| 124 | + const refreshed = yield* documentRequest(new URL("/", target.baseUrl), staleCookie); |
| 125 | + expect(refreshed.status, "a refreshable session still gets the page").toBe(200); |
| 126 | + const rotated = setCookieFor(refreshed, "wos-session"); |
| 127 | + expect(rotated, "the rotated sealed session is persisted").toContain("Max-Age="); |
| 128 | + expect(rotated, "…as a fresh value, not the stale one").not.toContain( |
| 129 | + staleCookie.slice("wos-session=".length, 80), |
| 130 | + ); |
| 131 | + |
| 132 | + // 2. The rotation was real: the OLD session's refresh token is revoked |
| 133 | + // server-side, so replaying the stale cookie now signs out. |
| 134 | + const replayed = yield* documentRequest(new URL("/", target.baseUrl), staleCookie); |
| 135 | + expect(replayed.status, "the spent session is refused").toBe(302); |
| 136 | + expect(replayed.headers.get("location"), "…and signed out to login").toBe("/login"); |
| 137 | + expect( |
| 138 | + setCookieFor(replayed, "wos-session"), |
| 139 | + "the spent cookie is cleared, not left to retry forever", |
| 140 | + ).toContain("Max-Age=0"); |
| 141 | + |
| 142 | + // 3. And the rotated session the browser was handed actually works. |
| 143 | + const rotatedCookie = rotated.split(";")[0]!; |
| 144 | + const next = yield* documentRequest(new URL("/tools", target.baseUrl), rotatedCookie); |
| 145 | + expect(next.status, "the rotated session opens the app").toBe(200); |
| 146 | + }), |
| 147 | +); |
0 commit comments