|
| 1 | +import { randomBytes } from "node:crypto"; |
| 2 | +import { readFile, writeFile } from "node:fs/promises"; |
| 3 | +import { join } from "node:path"; |
| 4 | + |
| 5 | +import { expect } from "@effect/vitest"; |
| 6 | +import { Effect, Schema } from "effect"; |
| 7 | + |
| 8 | +import { RUNS_DIR, scenario } from "../src/scenario"; |
| 9 | +import { RunDir, Target, Telemetry } from "../src/services"; |
| 10 | + |
| 11 | +const decodeString = Schema.decodeUnknownSync(Schema.String); |
| 12 | + |
| 13 | +const decodeKey = Schema.decodeUnknownSync( |
| 14 | + Schema.Struct({ id: Schema.String, value: Schema.String }), |
| 15 | +); |
| 16 | + |
| 17 | +scenario( |
| 18 | + "Authentication · valid credentials work in headers but not query parameters", |
| 19 | + {}, |
| 20 | + Effect.gen(function* () { |
| 21 | + const target = yield* Target; |
| 22 | + const runDir = yield* RunDir; |
| 23 | + const identity = yield* target.newIdentity(); |
| 24 | + const keyResponse = yield* Effect.promise(() => |
| 25 | + fetch(new URL("/api/account/api-keys", target.baseUrl), { |
| 26 | + method: "POST", |
| 27 | + headers: { |
| 28 | + ...identity.headers, |
| 29 | + origin: target.baseUrl, |
| 30 | + "content-type": "application/json", |
| 31 | + }, |
| 32 | + body: JSON.stringify({ name: "authentication-evidence" }), |
| 33 | + }), |
| 34 | + ); |
| 35 | + expect(keyResponse.status).toBe(200); |
| 36 | + const key = decodeKey(yield* Effect.promise(() => keyResponse.json())); |
| 37 | + yield* Effect.promise(async () => { |
| 38 | + const cases: Array<{ surface: string; carrier: string; status: number }> = []; |
| 39 | + for (const surface of ["api", "mcp"]) { |
| 40 | + const send = async (query: string | null, header: boolean) => { |
| 41 | + const url = new URL(surface === "api" ? "/api/policies" : "/mcp", target.baseUrl); |
| 42 | + if (query) url.searchParams.set(query, key.value); |
| 43 | + const response = await fetch(url, { |
| 44 | + method: surface === "api" ? "GET" : "POST", |
| 45 | + headers: { |
| 46 | + accept: "application/json, text/event-stream", |
| 47 | + "content-type": "application/json", |
| 48 | + ...(header ? { authorization: `Bearer ${key.value}` } : {}), |
| 49 | + }, |
| 50 | + ...(surface === "mcp" |
| 51 | + ? { |
| 52 | + body: JSON.stringify({ |
| 53 | + jsonrpc: "2.0", |
| 54 | + id: 1, |
| 55 | + method: "initialize", |
| 56 | + params: { |
| 57 | + protocolVersion: "2025-03-26", |
| 58 | + capabilities: {}, |
| 59 | + clientInfo: { name: "authentication-evidence", version: "1" }, |
| 60 | + }, |
| 61 | + }), |
| 62 | + } |
| 63 | + : {}), |
| 64 | + }); |
| 65 | + await response.text(); |
| 66 | + cases.push({ |
| 67 | + surface, |
| 68 | + carrier: query ?? "Authorization header", |
| 69 | + status: response.status, |
| 70 | + }); |
| 71 | + return response.status; |
| 72 | + }; |
| 73 | + expect(await send(null, true), `${surface} accepts the valid header credential`).toBe(200); |
| 74 | + for (const query of [ |
| 75 | + "api_key", |
| 76 | + "apikey", |
| 77 | + "key", |
| 78 | + "token", |
| 79 | + "access_token", |
| 80 | + "authorization", |
| 81 | + ]) { |
| 82 | + expect(await send(query, false), `${surface} rejects query-only ${query}`).toBe( |
| 83 | + surface === "api" ? 403 : 401, |
| 84 | + ); |
| 85 | + } |
| 86 | + } |
| 87 | + await writeFile( |
| 88 | + join(runDir, "authentication-carriers.json"), |
| 89 | + JSON.stringify({ cases }, null, 2), |
| 90 | + ); |
| 91 | + }).pipe( |
| 92 | + Effect.ensuring( |
| 93 | + Effect.promise(async () => { |
| 94 | + const response = await fetch(new URL(`/api/account/api-keys/${key.id}`, target.baseUrl), { |
| 95 | + method: "DELETE", |
| 96 | + headers: { ...identity.headers, origin: target.baseUrl }, |
| 97 | + }); |
| 98 | + expect(response.status, "the disposable key is revoked").toBe(200); |
| 99 | + }), |
| 100 | + ), |
| 101 | + ); |
| 102 | + }), |
| 103 | +); |
| 104 | + |
| 105 | +scenario( |
| 106 | + "Authentication · successful login exports diagnostics without its credentials", |
| 107 | + {}, |
| 108 | + Effect.gen(function* () { |
| 109 | + const target = yield* Target; |
| 110 | + const telemetry = yield* Telemetry; |
| 111 | + const runDir = yield* RunDir; |
| 112 | + const identity = yield* target.newIdentity(); |
| 113 | + const bootLog = join(RUNS_DIR, "cloud", "server-logs", "boot.log"); |
| 114 | + const initialLogLength = (yield* Effect.promise(() => readFile(bootLog, "utf8"))).length; |
| 115 | + const traceId = randomBytes(16).toString("hex"); |
| 116 | + const headers = { traceparent: `00-${traceId}-${randomBytes(8).toString("hex")}-01` }; |
| 117 | + const credentials = yield* Effect.promise(async () => { |
| 118 | + const login = await fetch(new URL("/api/auth/login", target.baseUrl), { redirect: "manual" }); |
| 119 | + expect(login.status).toBe(302); |
| 120 | + expect(login.headers.get("referrer-policy")).toBe("no-referrer"); |
| 121 | + const authorize = new URL(decodeString(login.headers.get("location"))); |
| 122 | + const state = decodeString(authorize.searchParams.get("state")); |
| 123 | + const stateCookie = login.headers |
| 124 | + .getSetCookie() |
| 125 | + .find((cookie) => cookie.startsWith("wos-login-state=")); |
| 126 | + expect(stateCookie !== undefined).toBe(true); |
| 127 | + authorize.searchParams.set("login_hint", identity.label); |
| 128 | + const consent = await fetch(authorize, { redirect: "manual" }); |
| 129 | + expect(consent.status).toBe(302); |
| 130 | + const callback = new URL(decodeString(consent.headers.get("location"))); |
| 131 | + const code = decodeString(callback.searchParams.get("code")); |
| 132 | + const signedIn = await fetch(callback, { |
| 133 | + redirect: "manual", |
| 134 | + headers: { |
| 135 | + ...headers, |
| 136 | + cookie: decodeString(stateCookie).split(";")[0] ?? "", |
| 137 | + }, |
| 138 | + }); |
| 139 | + expect(signedIn.status).toBe(302); |
| 140 | + expect(signedIn.headers.get("referrer-policy")).toBe("no-referrer"); |
| 141 | + const session = signedIn.headers |
| 142 | + .getSetCookie() |
| 143 | + .find((cookie) => cookie.startsWith("wos-session=")); |
| 144 | + const sessionPair = decodeString(session).split(";")[0] ?? ""; |
| 145 | + const verified = await fetch(new URL("/api/auth/me", target.baseUrl), { |
| 146 | + headers: { cookie: sessionPair }, |
| 147 | + }); |
| 148 | + expect(verified.status).toBe(200); |
| 149 | + return [state, code, sessionPair.slice("wos-session=".length)]; |
| 150 | + }); |
| 151 | + yield* telemetry.expectSpan({ traceId }); |
| 152 | + const spans = yield* telemetry.searchSpans({ traceId }); |
| 153 | + const exported = JSON.stringify(spans); |
| 154 | + const logs = (yield* Effect.promise(() => readFile(bootLog, "utf8"))).slice(initialLogLength); |
| 155 | + const matches = credentials.map((credential) => ({ |
| 156 | + trace: exported.includes(credential), |
| 157 | + serverLog: logs.includes(credential), |
| 158 | + })); |
| 159 | + // Assert booleans so even a failure cannot print a credential. |
| 160 | + expect(matches.every((match) => !match.trace && !match.serverLog)).toBe(true); |
| 161 | + yield* Effect.promise(() => |
| 162 | + writeFile( |
| 163 | + join(runDir, "login-diagnostics.json"), |
| 164 | + JSON.stringify( |
| 165 | + { |
| 166 | + environment: "isolated cloud Worker with WorkOS emulator", |
| 167 | + loginStatus: 302, |
| 168 | + authenticatedSessionStatus: 200, |
| 169 | + traceId, |
| 170 | + exportedSpanCount: spans.length, |
| 171 | + checkedCredentials: ["OAuth state", "authorization code", "sealed session cookie"], |
| 172 | + credentialMatches: matches, |
| 173 | + sample: spans.map(({ span }) => ({ |
| 174 | + operation: span.operationName, |
| 175 | + status: span.status, |
| 176 | + attributeNames: Object.keys(span.tags), |
| 177 | + })), |
| 178 | + }, |
| 179 | + null, |
| 180 | + 2, |
| 181 | + ), |
| 182 | + ), |
| 183 | + ); |
| 184 | + }), |
| 185 | +); |
0 commit comments