From c9b5c015e0657453879d3cd8532377a9e8c2908f Mon Sep 17 00:00:00 2001 From: Brian O'Kelley Date: Sun, 13 Sep 2026 14:10:02 +0000 Subject: [PATCH 1/7] fix(auth): hydrate primary authorization snapshots per request --- server/src/auth/organization-principal.ts | 2 +- .../src/db/user-authorization-snapshot-db.ts | 166 ++++++ server/src/middleware/auth.ts | 389 +++++-------- server/src/types.ts | 2 + .../utils/resolve-user-org-authorization.ts | 78 ++- .../user-authorization-snapshot.test.ts | 354 ++++++++++++ .../unit/addie-admin-global-auth.test.ts | 12 +- .../unit/authorization-epoch-session.test.ts | 533 +++++++++--------- .../resolve-user-org-authorization.test.ts | 312 +++++----- .../working-group-admin-global-auth.test.ts | 12 +- 10 files changed, 1151 insertions(+), 709 deletions(-) create mode 100644 server/src/db/user-authorization-snapshot-db.ts create mode 100644 server/tests/integration/user-authorization-snapshot.test.ts diff --git a/server/src/auth/organization-principal.ts b/server/src/auth/organization-principal.ts index dccf139df37..5f4ef47cb06 100644 --- a/server/src/auth/organization-principal.ts +++ b/server/src/auth/organization-principal.ts @@ -2,7 +2,7 @@ import type { WorkOSUser } from "../types.js"; export type OrgAuthorizationPrincipal = Pick< WorkOSUser, - "id" | "authWorkosUserId" + "id" | "authWorkosUserId" | "authorizationSnapshot" >; /** diff --git a/server/src/db/user-authorization-snapshot-db.ts b/server/src/db/user-authorization-snapshot-db.ts new file mode 100644 index 00000000000..74d32facc08 --- /dev/null +++ b/server/src/db/user-authorization-snapshot-db.ts @@ -0,0 +1,166 @@ +import { getPool } from './client.js'; + +/** The exact credential and its authority, observed in one primary-database statement. */ +export interface AuthorizationSnapshot { + readonly authenticatedUserId: string; + readonly canonicalUserId: string; + readonly identityId: string | null; + readonly selectedOrganizationId: string | null; + /** PostgreSQL bigint text: converting to a number would lose revocations above 2^53. */ + readonly authorizationEpoch: string; + readonly credential: Readonly<{ + email: string | null; + emailVerified: boolean; + firstName: string | null; + lastName: string | null; + }>; + readonly credentialGrant: Readonly<{ + id: string; + organizationId: string; + role: 'owner' | 'admin' | 'member'; + effectiveFrom: string; + effectiveUntil: string | null; + }> | null; +} + +/** Distinct from a missing credential or denied organization access. */ +export class AuthorizationSnapshotUnavailableError extends Error { + constructor(message = 'Authorization snapshot unavailable') { + super(message); + this.name = 'AuthorizationSnapshotUnavailableError'; + } +} + +interface SnapshotRow { + in_recovery: boolean; + authenticated_user_id: string | null; + canonical_user_id: string | null; + identity_id: string | null; + authorization_epoch: string; + email: string | null; + email_verified: boolean; + first_name: string | null; + last_name: string | null; + grant_id: string | null; + grant_organization_id: string | null; + grant_role: 'owner' | 'admin' | 'member' | null; + grant_effective_from: string | null; + grant_effective_until: string | null; +} + +/** + * Never hydrate identity and epoch separately: a binding change can commit + * between the reads and stamp old attribution with the new epoch. One SELECT + * gives every field the same PostgreSQL MVCC snapshot. The pool is the primary + * database; a connection to a recovering replica fails closed in this statement. + * + * Organization selection must be explicit. Neither linked credentials nor the + * organization_memberships cache supplies authority on a cold start/cache miss. + */ +export async function loadAuthorizationSnapshot( + authenticatedUserId: string, + selectedOrganizationId: string | null, +): Promise { + const organizationId = selectedOrganizationId || null; + try { + const result = await getPool().query( + `SELECT pg_catalog.pg_is_in_recovery() AS in_recovery, + credential.workos_user_id AS authenticated_user_id, + primary_binding.workos_user_id AS canonical_user_id, + binding.identity_id, + COALESCE(epoch.epoch, 0)::text AS authorization_epoch, + credential.email, credential.email_verified, credential.first_name, credential.last_name, + credential_grant.id AS grant_id, + credential_grant.workos_organization_id AS grant_organization_id, + credential_grant.role AS grant_role, + to_char(credential_grant.effective_from AT TIME ZONE 'UTC', + 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS grant_effective_from, + to_char(credential_grant.effective_until AT TIME ZONE 'UTC', + 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS grant_effective_until + FROM (VALUES (1)) AS anchor(value) + LEFT JOIN users credential ON credential.workos_user_id = $1 + LEFT JOIN identity_workos_users binding + ON binding.workos_user_id = credential.workos_user_id + LEFT JOIN identity_workos_users primary_binding + ON primary_binding.identity_id = binding.identity_id AND primary_binding.is_primary + LEFT JOIN authorization_epochs epoch + ON epoch.workos_user_id = credential.workos_user_id + LEFT JOIN organization_credential_grants credential_grant + ON credential_grant.workos_user_id = credential.workos_user_id + AND credential_grant.workos_organization_id = $2 + AND credential_grant.revoked_at IS NULL + AND credential_grant.effective_from <= statement_timestamp() + AND (credential_grant.effective_until IS NULL + OR credential_grant.effective_until > statement_timestamp())`, + [authenticatedUserId, organizationId], + ); + const row = result.rows[0]; + if (!row || row.in_recovery) { + throw new AuthorizationSnapshotUnavailableError(); + } + if (!row.authenticated_user_id) return null; + // Migration 460 backfills bindings and creates them on every user insert. + // Missing attribution is an integrity failure, not a singleton fallback. + if (!row.identity_id || !row.canonical_user_id) { + throw new AuthorizationSnapshotUnavailableError(); + } + + const credentialGrant = row.grant_id + ? Object.freeze({ + id: row.grant_id, + organizationId: row.grant_organization_id!, + role: row.grant_role!, + effectiveFrom: row.grant_effective_from!, + effectiveUntil: row.grant_effective_until, + }) + : null; + return Object.freeze({ + authenticatedUserId: row.authenticated_user_id, + canonicalUserId: row.canonical_user_id, + identityId: row.identity_id, + selectedOrganizationId: organizationId, + authorizationEpoch: row.authorization_epoch, + credential: Object.freeze({ + email: row.email, + emailVerified: row.email_verified, + firstName: row.first_name, + lastName: row.last_name, + }), + credentialGrant, + }); + } catch (error) { + if (error instanceof AuthorizationSnapshotUnavailableError) throw error; + // Callers must distinguish unavailable from denied without exposing raw DB + // errors or reusing stale identity, organization, or grant cache entries. + throw new AuthorizationSnapshotUnavailableError(); + } +} + +export function sameAuthorizationIdentity( + previous: AuthorizationSnapshot, + current: AuthorizationSnapshot, +): boolean { + return previous.authenticatedUserId === current.authenticatedUserId + && previous.canonicalUserId === current.canonicalUserId + && previous.identityId === current.identityId + && previous.authorizationEpoch === current.authorizationEpoch + && previous.credential.email === current.credential.email + && previous.credential.emailVerified === current.credential.emailVerified; +} + +/** Compare equality, not epoch ordering: deletions and replay can move backwards. */ +export function sameAuthorizationSnapshot( + previous: AuthorizationSnapshot, + current: AuthorizationSnapshot, +): boolean { + if (!sameAuthorizationIdentity(previous, current) + || previous.selectedOrganizationId !== current.selectedOrganizationId) return false; + const before = previous.credentialGrant; + const after = current.credentialGrant; + if (!before || !after) return before === after; + return before.id === after.id + && before.organizationId === after.organizationId + && before.role === after.role + && before.effectiveFrom === after.effectiveFrom + && before.effectiveUntil === after.effectiveUntil; +} diff --git a/server/src/middleware/auth.ts b/server/src/middleware/auth.ts index 902ffb9ac50..d6df5c6d0bf 100644 --- a/server/src/middleware/auth.ts +++ b/server/src/middleware/auth.ts @@ -13,12 +13,7 @@ import { bansDb } from '../db/bans-db.js'; import { isWorkOSApiKeyFormat } from './api-key-format.js'; import { verifyWorkOSJWT, looksLikeJWT } from '../auth/workos-jwt.js'; import { storeRefreshedSession, getRefreshedSession, cleanExpiredRefreshes } from '../db/session-refresh-db.js'; -import { getPool } from '../db/client.js'; -import { - readCredentialAuthorizationLifecycle, - type ActiveCredentialAuthorizationSnapshot, - type CredentialAuthorizationLifecycle, -} from '../db/authorization-epoch-db.js'; +import { loadAuthorizationSnapshot, AuthorizationSnapshotUnavailableError } from '../db/user-authorization-snapshot-db.js'; import { getOrganizationAuthorizationUserId } from '../auth/organization-principal.js'; import { constantTimeEqual } from '../utils/constant-time-equal.js'; import { resolveEffectiveMembership } from '../db/org-filters.js'; @@ -39,8 +34,8 @@ interface CachedSession { accessToken: string; expiresAt: number; newSealedSession?: string; // Set if session was refreshed - /** Persisted authorization epoch observed when this entry was stored (#6827). */ - authorizationFingerprint?: string; + /** Explicit organization from the authenticated provider session, never inferred. */ + orgId?: string; } const sessionCache = new Map(); @@ -317,19 +312,15 @@ export interface ValidatedBearerJWT { /** * Short-lived positive cache for successfully-validated bearer JWTs. - * Keyed on SHA-256 of the raw token; value includes the synthesized user + * Keyed on SHA-256 of the raw token; value includes verified provider claims * and an expiry that is the minimum of (token exp, now + BEARER_JWT_CACHE_TTL_MS). * - * Without this, every REST request with a Bearer JWT incurs a DB round-trip - * for the local-user lookup — the cookie path has an equivalent cache - * (`sessionCache`) and its absence here creates a DoS amplifier. + * Only signature-verified provider claims live here. Request authority is + * hydrated afresh from the primary database; no canonical user or grant is cached. */ interface CachedBearerJWT { - user: WorkOSUser; - orgId?: string; + verified: Awaited>; expiresAt: number; - /** Persisted authorization epoch observed when this entry was stored (#6827). */ - authorizationFingerprint?: string; } const bearerJwtCache = new Map(); const BEARER_JWT_CACHE_TTL_MS = 60 * 1000; @@ -366,63 +357,37 @@ export async function validateWorkOSBearerJWT(req: Request): Promise>; if (cached && cached.expiresAt > now) { - if (await isAuthorizationFingerprintCurrent(cached.user, cached.authorizationFingerprint)) { - return { user: cached.user, rawToken: token, orgId: cached.orgId }; + verified = cached.verified; + } else { + try { + verified = await verifyWorkOSJWT(token); + } catch (err) { + logger.debug({ err }, 'Bearer JWT verification failed'); + return null; } - // Identity bindings changed since this entry was stored — re-verify. - bearerJwtCache.delete(cacheKey); } - - let verified: Awaited>; - try { - verified = await verifyWorkOSJWT(token); - } catch (err) { - logger.debug({ err }, 'Bearer JWT verification failed'); - return null; - } - if (verified.isM2M || !verified.sub) return null; - // A valid WorkOS signature is not enough: accept and cache only an - // authoritative local credential snapshot that includes the binding and - // proves no durable deletion tombstone exists. - const lifecycle = await readCredentialAuthorizationLifecycle(verified.sub); - if (lifecycle.status === 'unavailable') { - logger.warn({ sub: verified.sub }, 'Bearer JWT local credential lifecycle unavailable — failed closed'); - return null; - } - if (lifecycle.status === 'terminal') { - logger.warn({ sub: verified.sub }, 'Bearer JWT verified but credential is not active locally'); - return null; - } - const activeSnapshot = lifecycle.snapshot; - - const email = verified.email ?? activeSnapshot.email ?? ''; - const user: WorkOSUser = { + // Only provider authentication is cached. Identity, grants and the epoch + // always come from the same fresh primary-database snapshot, even on hits. + const user = await hydrateAuthenticatedUser({ id: verified.sub, - email, - firstName: activeSnapshot.first_name?.trim() || undefined, - lastName: activeSnapshot.last_name?.trim() || undefined, + email: verified.email ?? '', emailVerified: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString(), - }; - applyActiveCredentialSnapshot(user, activeSnapshot); + }, selectedOrganizationForAuthentication(req, verified.orgId)); const tokenExpMs = verified.expiresAt ? verified.expiresAt * 1000 : Infinity; const cacheUntil = Math.min(now + BEARER_JWT_CACHE_TTL_MS, tokenExpMs); - if (cacheUntil > now) { + if ((!cached || cached.expiresAt <= now) && cacheUntil > now) { if (bearerJwtCache.size >= BEARER_JWT_CACHE_MAX_SIZE) { const oldest = bearerJwtCache.keys().next().value; if (oldest) bearerJwtCache.delete(oldest); } - bearerJwtCache.set(cacheKey, { - user, - orgId: verified.orgId, - expiresAt: cacheUntil, - authorizationFingerprint: activeSnapshot.fingerprint, - }); + bearerJwtCache.set(cacheKey, { verified, expiresAt: cacheUntil }); } return { user, rawToken: token, orgId: verified.orgId }; @@ -712,135 +677,90 @@ function hasValidAdminApiKey(req: Request): boolean { return constantTimeEqual(token, ADMIN_API_KEY); } -/** - * True if `id` is a synthetic user (static admin API key or per-org WorkOS - * API key). Synthetic users don't represent a person, so they have no - * identity binding. - */ -function isSyntheticUser(id: string): boolean { - return id === 'admin_api_key' || id.startsWith('api_key_'); -} - /** * Drop any cached sessions whose WorkOS user id matches one of the given - * ids, on either auth credential (post-swap canonical or actual auth). - * Called when an identity binding changes (mergeUsers, admin rebind) so - * subsequent requests re-resolve identity instead of serving a stale swap. + * ids. This saves a provider round trip after local mutations; correctness + * comes from the fresh database snapshot on every request, on every instance. */ export function invalidateSessionsForUsers(workosUserIds: string[]): void { if (workosUserIds.length === 0) return; const ids = new Set(workosUserIds); for (const [key, value] of sessionCache.entries()) { - if (ids.has(value.user.id) || (value.user.authWorkosUserId && ids.has(value.user.authWorkosUserId))) { + if (ids.has(value.user.id)) { sessionCache.delete(key); } } for (const [key, value] of bearerJwtCache.entries()) { - if (ids.has(value.user.id) || (value.user.authWorkosUserId && ids.has(value.user.authWorkosUserId))) { + if (ids.has(value.verified.sub)) { bearerJwtCache.delete(key); } } } -/** - * Resolve the identity for a WorkOS user. Sets `user.identityId` and, when - * the authenticated user is a non-primary binding, swaps `user.id` to the - * identity's primary workos_user_id so app-state reads land on the right - * person. The original authenticated id is preserved on `user.authWorkosUserId`. - * - * Skipped for synthetic users (admin API key, WorkOS API key) — they don't - * represent a person. Failures are swallowed: identity resolution must - * never block an authenticated request, and a degraded request that sees - * only the auth user's slice of data is still better than a 500. - */ -async function attachIdentityId(user: WorkOSUser): Promise { - if (isSyntheticUser(user.id)) return; - try { - const result = await getPool().query<{ - identity_id: string; - primary_workos_user_id: string | null; - }>( - `SELECT iwu.identity_id, primary_iwu.workos_user_id AS primary_workos_user_id - FROM identity_workos_users iwu - LEFT JOIN identity_workos_users primary_iwu - ON primary_iwu.identity_id = iwu.identity_id - AND primary_iwu.is_primary = TRUE - WHERE iwu.workos_user_id = $1`, - [user.id] - ); - const row = result.rows[0]; - if (!row) return; - - user.identityId = row.identity_id; - - if (row.primary_workos_user_id && row.primary_workos_user_id !== user.id) { - // Non-primary binding signed in. Swap id so app-state reads see the - // canonical person; preserve the actual auth user on authWorkosUserId. - logger.debug( - { authWorkosUserId: user.id, canonicalUserId: row.primary_workos_user_id, identityId: row.identity_id }, - 'Identity id-swap: routing non-primary binding to canonical user' - ); - user.authWorkosUserId = user.id; - user.id = row.primary_workos_user_id; - } - } catch (err) { - logger.warn({ err, userId: user.id }, 'Failed to resolve identity_id'); - } +class InvalidAuthorizationCredentialError extends Error {} +class ConflictingOrganizationSelectionError extends Error {} + +/** Request selectors must agree; authenticated provider selection is used only + * when the request supplies none. Neither memberships nor caches pick an org. */ +function selectedOrganizationForAuthentication(req: Request, providerOrg?: string): string | null { + const selectors = [ + req.headers['x-organization-id'], req.query?.org, req.query?.organization_id, + req.body?.organization_id, req.body?.organizationId, + req.params?.orgId, req.params?.organizationId, + ].filter((value) => value !== undefined && value !== null); + if (selectors.some((value) => typeof value !== 'string' || !value.trim())) { + throw new ConflictingOrganizationSelectionError(); + } + const ids = new Set(selectors.map((value) => (value as string).trim())); + if (ids.size > 1) throw new ConflictingOrganizationSelectionError(); + return ids.values().next().value ?? providerOrg ?? null; } -function applyActiveCredentialSnapshot( - user: WorkOSUser, - snapshot: ActiveCredentialAuthorizationSnapshot, -): void { - const authenticatedWorkosUserId = snapshot.workos_user_id; - user.identityId = snapshot.identity_id; - if (snapshot.primary_workos_user_id - && snapshot.primary_workos_user_id !== authenticatedWorkosUserId) { - user.authWorkosUserId = authenticatedWorkosUserId; - user.id = snapshot.primary_workos_user_id; - } +/** Hydrate a new request object; never mutate the provider object in a cache. */ +async function hydrateAuthenticatedUser(user: WorkOSUser, organizationId: string | null): Promise { + const authenticatedUserId = getOrganizationAuthorizationUserId(user); + const snapshot = await loadAuthorizationSnapshot(authenticatedUserId, organizationId); + if (!snapshot) throw new InvalidAuthorizationCredentialError(); + const hydrated: WorkOSUser = { + ...user, + id: snapshot.canonicalUserId, + authWorkosUserId: snapshot.canonicalUserId !== authenticatedUserId ? authenticatedUserId : undefined, + identityId: snapshot.identityId ?? undefined, + email: snapshot.credential.email ?? '', + emailVerified: snapshot.credential.emailVerified, + firstName: snapshot.credential.firstName?.trim() || user.firstName?.trim() || undefined, + lastName: snapshot.credential.lastName?.trim() || user.lastName?.trim() || undefined, + }; + // Server-only authority must not leak through JSON responses or user spreads. + Object.defineProperty(hydrated, 'authorizationSnapshot', { value: snapshot }); + return hydrated; } -/** - * Authorization fingerprint for the credential that actually authenticated - * this session. `attachIdentityId` may swap `user.id` to the identity's - * canonical credential, so the authenticated credential is read through - * `getOrganizationAuthorizationUserId` — that value is stable across the - * swap, and every identity-binding mutation bumps the epoch for each - * credential bound to the identity, so a change always moves this value. - * - * Synthetic principals (admin API key, WorkOS API key) have no credential - * row and no identity binding; they return '' and never mismatch. - */ -async function activeAuthorizationSnapshotFor( - user: WorkOSUser, -): Promise { - if (isSyntheticUser(user.id)) return { status: 'terminal', reason: 'missing_user' }; - return readCredentialAuthorizationLifecycle(getOrganizationAuthorizationUserId(user)); +/** Local dev login is an explicit synthetic-auth bypass. Keep attribution useful + * against seeded databases without making a database mandatory for dev fixtures. */ +async function hydrateDevUser(user: WorkOSUser): Promise { + try { + return await hydrateAuthenticatedUser(user, null); + } catch { + return user; + } } -/** - * True when a cached entry still reflects persisted authorization state. - * - * Entries cached before an identity-binding change carry the pre-change - * fingerprint and must not be served — on any instance, not just the one - * that ran the mutation. A lookup failure returns false so the request - * falls through to full re-validation rather than serving state we could - * not confirm; that degrades the cache, it does not sign anyone out. - */ -async function isAuthorizationFingerprintCurrent( - user: WorkOSUser, - stamped: string | undefined, -): Promise { - if (isSyntheticUser(user.id)) return true; - try { - const lifecycle = await activeAuthorizationSnapshotFor(user); - return lifecycle.status === 'active' - && lifecycle.snapshot.fingerprint === (stamped ?? ''); - } catch (err) { - logger.warn({ err }, 'Authoritative credential check failed — bypassing session cache'); - return false; +function sendAuthorizationStateError(error: unknown, res: Response): boolean { + if (error instanceof AuthorizationSnapshotUnavailableError) { + logger.warn({ err: error }, 'Primary authorization snapshot unavailable'); + res.status(503).json({ error: 'Authorization service temporarily unavailable' }); + return true; + } + if (error instanceof InvalidAuthorizationCredentialError) { + res.status(401).json({ error: 'Invalid session', login_url: '/auth/login' }); + return true; + } + if (error instanceof ConflictingOrganizationSelectionError) { + res.status(403).json({ error: 'An unambiguous organization selection is required' }); + return true; } + return false; } /** @@ -897,7 +817,13 @@ export async function requireAuth(req: Request, res: Response, next: NextFunctio // Check for OAuth-issued user JWT (user SSO'd via AuthKit through the // MCP OAuth flow and is now calling the REST API with that token). - const jwtAuth = await validateWorkOSBearerJWT(req); + let jwtAuth: ValidatedBearerJWT | null; + try { + jwtAuth = await validateWorkOSBearerJWT(req); + } catch (error) { + if (sendAuthorizationStateError(error, res)) return; + throw error; + } if (jwtAuth) { logger.debug({ path: req.path, userId: jwtAuth.user.id }, 'Authenticated via OAuth user JWT'); req.user = jwtAuth.user; @@ -905,8 +831,8 @@ export async function requireAuth(req: Request, res: Response, next: NextFunctio try { const userBan = await checkPlatformBan( - `user:${jwtAuth.user.id}`, - () => bansDb.checkPlatformBan(jwtAuth.user.id), + `user:${getOrganizationAuthorizationUserId(jwtAuth.user)}`, + () => bansDb.checkPlatformBan(getOrganizationAuthorizationUserId(jwtAuth.user)), ); if (userBan) { logger.info({ userId: jwtAuth.user.id, banId: userBan.id }, 'User request blocked by platform ban'); @@ -923,14 +849,13 @@ export async function requireAuth(req: Request, res: Response, next: NextFunctio if (DEV_MODE_ENABLED) { const devUser = createDevUser(req); if (devUser) { - req.user = devUser; + req.user = await hydrateDevUser(devUser); req.accessToken = 'dev-mode-token'; // Carry over dev config flags (isMember, isAdmin) so enrichUserWithMembership skips DB lookup const devConfig = getDevUser(req); if (devConfig) { (req.user as unknown as Record).isMember = devConfig.isMember; } - await attachIdentityId(req.user); return next(); } // No dev session - redirect to dev login page @@ -985,19 +910,12 @@ export async function requireAuth(req: Request, res: Response, next: NextFunctio deadSessionCache.delete(cacheKey); } - if ( - cached && - cached.expiresAt > now && - !(await isAuthorizationFingerprintCurrent(cached.user, cached.authorizationFingerprint)) - ) { - // Identity bindings changed since this entry was stored. Drop it so - // this request re-resolves identity and organization context instead - // of inheriting pre-change authority. - sessionCache.delete(cacheKey); - } else if (cached && cached.expiresAt > now) { + if (cached && cached.expiresAt > now) { // Cache hit - use cached session data logger.debug({ userId: cached.user.id }, 'Using cached session'); - req.user = cached.user; + req.user = await hydrateAuthenticatedUser( + cached.user, selectedOrganizationForAuthentication(req, cached.orgId), + ); req.accessToken = cached.accessToken; // If session was refreshed, update the cookie @@ -1172,51 +1090,11 @@ export async function requireAuth(req: Request, res: Response, next: NextFunctio impersonator?: { email: string; reason: string | null }; }; - let firstName = result.user.firstName ?? undefined; - let lastName = result.user.lastName ?? undefined; - - // WorkOS session validity alone cannot authorize a deleted credential. - // Read the user, binding, canonical route, tombstone absence, and epoch - // atomically before accepting or caching this session. - const lifecycle = await readCredentialAuthorizationLifecycle(result.user.id); - if (lifecycle.status === 'unavailable') { - sessionCache.delete(cacheKey); - logger.warn({ userId: result.user.id, path: req.path }, - 'Local credential lifecycle unavailable — failing authentication closed'); - return res.status(503).json({ - error: 'Service temporarily unavailable', - message: 'Unable to verify your account right now. Please try again shortly.', - }); - } - if (lifecycle.status === 'terminal') { - logger.warn({ userId: result.user.id, email: result.user.email, path: req.path }, - 'Authenticated credential is not active in local DB — forcing re-login'); - sessionCache.delete(cacheKey); - if (deadSessionCache.size >= DEAD_SESSION_MAX_SIZE) { - const oldest = deadSessionCache.keys().next().value; - if (oldest) deadSessionCache.delete(oldest); - } - deadSessionCache.set(cacheKey, Date.now()); - if (isHtmlRequest) { - return res.redirect(`/auth/login?return_to=${encodeURIComponent(req.originalUrl)}`); - } - return res.status(401).json({ - error: 'Invalid session', - message: 'Your account could not be verified. Please log in again.', - login_url: '/auth/login', - }); - } - const activeSnapshot = lifecycle.snapshot; - if (!firstName?.trim()) { - if (activeSnapshot.first_name?.trim()) firstName = activeSnapshot.first_name; - if (activeSnapshot.last_name?.trim()) lastName = activeSnapshot.last_name; - } - const user: WorkOSUser = { id: result.user.id, email: result.user.email, - firstName, - lastName, + firstName: result.user.firstName ?? undefined, + lastName: result.user.lastName ?? undefined, emailVerified: result.user.emailVerified, createdAt: result.user.createdAt, updatedAt: result.user.updatedAt, @@ -1231,18 +1109,20 @@ export async function requireAuth(req: Request, res: Response, next: NextFunctio ); } - applyActiveCredentialSnapshot(user, activeSnapshot); + const hydrated = await hydrateAuthenticatedUser( + user, selectedOrganizationForAuthentication(req, result.organizationId), + ); - // Cache the validated session + // Retain authentication only; derived request authority is never cached. sessionCache.set(cacheKey, { - user, + user: Object.freeze(user), accessToken: result.accessToken, expiresAt: now + SESSION_CACHE_TTL_MS, newSealedSession, - authorizationFingerprint: activeSnapshot.fingerprint, + orgId: result.organizationId, }); - req.user = user; + req.user = hydrated; req.accessToken = result.accessToken; // Check platform ban for cookie-authenticated user (fail-open: DB timeout should not log users out) @@ -1261,6 +1141,7 @@ export async function requireAuth(req: Request, res: Response, next: NextFunctio next(); } catch (error) { + if (sendAuthorizationStateError(error, res)) return; if (isTransientAuthError(error)) { logger.warn({ err: error, path: req.path }, 'Transient auth upstream failure — returning 503 without clearing session'); if (isHtmlRequest) { @@ -1547,9 +1428,8 @@ export async function requireAdmin(req: Request, res: Response, next: NextFuncti if (!req.user) { const mockUser = createDevUser(req); if (mockUser) { - req.user = mockUser; + req.user = await hydrateDevUser(mockUser); req.accessToken = 'dev-mode-token'; - await attachIdentityId(req.user); } } @@ -1947,7 +1827,13 @@ export async function optionalAuth(req: Request, res: Response, next: NextFuncti return next(); } - const jwtAuth = await validateWorkOSBearerJWT(req); + let jwtAuth: ValidatedBearerJWT | null; + try { + jwtAuth = await validateWorkOSBearerJWT(req); + } catch (error) { + if (sendAuthorizationStateError(error, res)) return; + throw error; + } if (jwtAuth) { logger.debug({ path: req.path, userId: jwtAuth.user.id }, 'Authenticated via OAuth user JWT (optional auth)'); req.user = jwtAuth.user; @@ -1955,8 +1841,8 @@ export async function optionalAuth(req: Request, res: Response, next: NextFuncti try { const userBan = await checkPlatformBan( - `user:${jwtAuth.user.id}`, - () => bansDb.checkPlatformBan(jwtAuth.user.id), + `user:${getOrganizationAuthorizationUserId(jwtAuth.user)}`, + () => bansDb.checkPlatformBan(getOrganizationAuthorizationUserId(jwtAuth.user)), ); if (userBan) { logger.info({ userId: jwtAuth.user.id, banId: userBan.id }, 'User optional-auth request blocked by platform ban'); @@ -1973,7 +1859,7 @@ export async function optionalAuth(req: Request, res: Response, next: NextFuncti if (DEV_MODE_ENABLED) { const devUser = createDevUser(req); if (devUser) { - req.user = devUser; + req.user = await hydrateDevUser(devUser); req.accessToken = 'dev-mode-token'; // Carry over dev config flags (isMember, isAdmin) so enrichUserWithMembership skips DB lookup const devConfig = getDevUser(req); @@ -2006,16 +1892,12 @@ export async function optionalAuth(req: Request, res: Response, next: NextFuncti deadSessionCache.delete(cacheKey); } - if ( - cached && - cached.expiresAt > now && - !(await isAuthorizationFingerprintCurrent(cached.user, cached.authorizationFingerprint)) - ) { - sessionCache.delete(cacheKey); - } else if (cached && cached.expiresAt > now) { + if (cached && cached.expiresAt > now) { // Cache hit - use cached session data logger.debug({ userId: cached.user.id }, 'Using cached session (optional auth)'); - req.user = cached.user; + req.user = await hydrateAuthenticatedUser( + cached.user, selectedOrganizationForAuthentication(req, cached.orgId), + ); req.accessToken = cached.accessToken; // If session was refreshed, update the cookie @@ -2146,35 +2028,24 @@ export async function optionalAuth(req: Request, res: Response, next: NextFuncti ); } - // The shared cache may be consumed by requireAuth. Only an authoritative - // active local credential snapshot may create a positive entry. - const lifecycle = await readCredentialAuthorizationLifecycle(user.id); - if (lifecycle.status !== 'active') { - sessionCache.delete(cacheKey); - return next(); - } - const activeSnapshot = lifecycle.snapshot; - if (!user.firstName?.trim() && activeSnapshot.first_name?.trim()) { - user.firstName = activeSnapshot.first_name; - } - if (!user.lastName?.trim() && activeSnapshot.last_name?.trim()) { - user.lastName = activeSnapshot.last_name; - } - applyActiveCredentialSnapshot(user, activeSnapshot); + const hydrated = await hydrateAuthenticatedUser( + user, selectedOrganizationForAuthentication(req, result.organizationId), + ); - // Cache the validated session + // Retain authentication only; derived request authority is never cached. sessionCache.set(cacheKey, { - user, + user: Object.freeze(user), accessToken: result.accessToken, expiresAt: now + SESSION_CACHE_TTL_MS, newSealedSession, - authorizationFingerprint: activeSnapshot.fingerprint, + orgId: result.organizationId, }); - req.user = user; + req.user = hydrated; req.accessToken = result.accessToken; } } catch (error) { + if (sendAuthorizationStateError(error, res)) return; // Silently fail for optional auth logger.debug({ err: error }, 'Optional auth failed'); } diff --git a/server/src/types.ts b/server/src/types.ts index f9fef3edb8a..8ff1eba3646 100644 --- a/server/src/types.ts +++ b/server/src/types.ts @@ -393,6 +393,8 @@ export interface Impersonator { } export interface WorkOSUser { + /** Immutable primary-DB snapshot for this request only; never serialize or cache. */ + readonly authorizationSnapshot?: import('./db/user-authorization-snapshot-db.js').AuthorizationSnapshot; /** * The canonical workos_user_id for app-state queries. For singleton * identities this equals the authenticated WorkOS user. For non-primary diff --git a/server/src/utils/resolve-user-org-authorization.ts b/server/src/utils/resolve-user-org-authorization.ts index 142b94b63ee..a0bc851c1f6 100644 --- a/server/src/utils/resolve-user-org-authorization.ts +++ b/server/src/utils/resolve-user-org-authorization.ts @@ -1,19 +1,24 @@ import type { WorkOS } from "@workos-inc/node"; import type { OrgAuthorizationPrincipal } from "../auth/organization-principal.js"; import { getOrganizationAuthorizationUserId } from "../auth/organization-principal.js"; -import { query } from "../db/client.js"; +import { + loadAuthorizationSnapshot, + sameAuthorizationIdentity, + sameAuthorizationSnapshot, + type AuthorizationSnapshot, +} from "../db/user-authorization-snapshot-db.js"; import { createLogger } from "../logger.js"; import { resolveUserRole } from "./resolve-user-role.js"; const logger = createLogger("resolve-user-org-authorization"); export type MembershipRole = "owner" | "admin" | "member"; -export type OrgAuthorizationSource = "workos" | "credential_grant"; +export type OrgAuthorizationSource = "workos" | "credential_grant" | "authorization_snapshot"; export interface UserOrgAuthorizationMembership { organizationId: string; role: MembershipRole; - source: OrgAuthorizationSource; + source: "workos" | "credential_grant"; } export type UserOrgAuthorizationResolution = @@ -52,7 +57,34 @@ export async function resolveUserOrgAuthorization( principal: OrgAuthorizationPrincipal, organizationId: string ): Promise { + const forbidden: UserOrgAuthorizationResolution = { + status: "forbidden", complete: true, unavailableSources: [], + }; + const unavailable = { + status: "unavailable", complete: false, unavailableSources: ["authorization_snapshot"], + } satisfies UserOrgAuthorizationResolution; + // Signing in alone does not select an organization or confer tenant access. + if (!organizationId?.trim()) return forbidden; const userId = getOrganizationAuthorizationUserId(principal); + let snapshot: AuthorizationSnapshot | null; + try { + snapshot = await loadAuthorizationSnapshot(userId, organizationId); + } catch (err) { + logger.warn({ err }, "Primary authorization snapshot unavailable"); + return unavailable; + } + if (!snapshot) return forbidden; + const previous = principal.authorizationSnapshot; + if (previous) { + // A sign-in with no org can bind an explicit route selection here. Once + // selected, a context belongs to that org and grant: replay cannot switch + // it or silently upgrade a revoked/replaced grant without a fresh request. + const current = previous.selectedOrganizationId === null + ? sameAuthorizationIdentity(previous, snapshot) + : sameAuthorizationSnapshot(previous, snapshot); + if (!current) return forbidden; + } + let directMembership: UserOrgAuthorizationMembership | null = null; let workosAvailable = false; @@ -64,7 +96,7 @@ export async function resolveUserOrgAuthorization( organizationId, }); const matchingMemberships = memberships.data.filter( - (membership) => membership.organizationId === organizationId + (membership) => membership.userId === userId && membership.organizationId === organizationId ); const activeRow = matchingMemberships.find( (membership) => membership.status === "active" @@ -88,32 +120,25 @@ export async function resolveUserOrgAuthorization( logger.warn("WorkOS client unavailable; checking explicit credential grant"); } - let grantAvailable = false; - let grantMembership: UserOrgAuthorizationMembership | null = null; + // WorkOS is an asynchronous authority source. Reject this decision if the + // persisted identity, epoch, selection or grant moved while it was in flight. + // In particular, never combine a pre-change provider decision with a new grant. try { - const grant = await query<{ workos_organization_id: string; role: string }>( - `SELECT workos_organization_id, role - FROM organization_credential_grants - WHERE workos_user_id = $1 - AND workos_organization_id = $2 - AND revoked_at IS NULL - AND effective_from <= NOW() - AND (effective_until IS NULL OR effective_until > NOW()) - LIMIT 1`, - [userId, organizationId] - ); - grantAvailable = true; - const row = grant.rows[0]; - if (row && VALID_ROLES.has(row.role)) { - grantMembership = { - organizationId: row.workos_organization_id, - role: row.role as MembershipRole, - source: "credential_grant", - }; + const current = await loadAuthorizationSnapshot(userId, organizationId); + if (!current || !sameAuthorizationSnapshot(snapshot, current)) { + return forbidden; } } catch (err) { - logger.warn({ err }, "Credential grant lookup failed"); + logger.warn({ err }, "Primary authorization snapshot revalidation unavailable"); + return unavailable; } + const grantMembership: UserOrgAuthorizationMembership | null = snapshot.credentialGrant + ? { + organizationId: snapshot.credentialGrant.organizationId, + role: snapshot.credentialGrant.role, + source: "credential_grant", + } + : null; let membership = directMembership; if ( @@ -126,7 +151,6 @@ export async function resolveUserOrgAuthorization( const unavailableSources: OrgAuthorizationSource[] = []; if (!workosAvailable) unavailableSources.push("workos"); - if (!grantAvailable) unavailableSources.push("credential_grant"); if (membership) { return { diff --git a/server/tests/integration/user-authorization-snapshot.test.ts b/server/tests/integration/user-authorization-snapshot.test.ts new file mode 100644 index 00000000000..52e4d037e17 --- /dev/null +++ b/server/tests/integration/user-authorization-snapshot.test.ts @@ -0,0 +1,354 @@ +import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; +import type { Pool, PoolClient } from 'pg'; +import { closeDatabase, initializeDatabase } from '../../src/db/client.js'; +import { runMigrations } from '../../src/db/migrate.js'; +import { + AuthorizationSnapshotUnavailableError, + loadAuthorizationSnapshot, + sameAuthorizationIdentity, + sameAuthorizationSnapshot, + type AuthorizationSnapshot, +} from '../../src/db/user-authorization-snapshot-db.js'; +import { bumpAuthorizationEpochs } from '../../src/db/authorization-epoch-db.js'; + +const PERSONAL_ID = 'user_authorization_snapshot_personal'; +const CORPORATE_ID = 'user_authorization_snapshot_corporate'; +const PERSONAL_ORG = 'org_authorization_snapshot_personal'; +const CORPORATE_ORG = 'org_authorization_snapshot_corporate'; +const USER_IDS = [PERSONAL_ID, CORPORATE_ID]; +const ORG_IDS = [PERSONAL_ORG, CORPORATE_ORG]; + +describe('primary database authorization snapshots', () => { + let pool: Pool; + let fixtureIdentityIds: string[] = []; + + beforeAll(async () => { + pool = initializeDatabase({ + connectionString: process.env.DATABASE_URL || 'postgresql://adcp:localdev@localhost:5432/adcp_test', + }); + await runMigrations(); + }, 60000); + + async function cleanup() { + await pool.query('DELETE FROM organization_memberships WHERE workos_organization_id = ANY($1)', [ORG_IDS]); + await pool.query('DELETE FROM organizations WHERE workos_organization_id = ANY($1)', [ORG_IDS]); + const identities = await pool.query<{ identity_id: string }>( + 'SELECT identity_id FROM identity_workos_users WHERE workos_user_id = ANY($1)', [USER_IDS], + ); + await pool.query('DELETE FROM users WHERE workos_user_id = ANY($1)', [USER_IDS]); + await pool.query('DELETE FROM identities WHERE id = ANY($1)', [ + [...fixtureIdentityIds, ...identities.rows.map(row => row.identity_id)], + ]); + fixtureIdentityIds = []; + } + + beforeEach(async () => { + vi.restoreAllMocks(); + await cleanup(); + await pool.query( + `INSERT INTO users (workos_user_id, email, first_name, last_name, email_verified, + workos_created_at, workos_updated_at) + VALUES ($1, 'sam.personal@example.test', 'Sam', 'Adeyemi', true, NOW(), NOW()), + ($2, 'sam@pinnacle.example', 'Sam', 'Adeyemi', true, NOW(), NOW())`, + USER_IDS, + ); + await pool.query( + `INSERT INTO organizations (workos_organization_id, name) + VALUES ($1, 'Sam personal organization'), ($2, 'Pinnacle Agency')`, + ORG_IDS, + ); + const identities = await pool.query<{ identity_id: string }>( + 'SELECT identity_id FROM identity_workos_users WHERE workos_user_id = ANY($1)', [USER_IDS], + ); + fixtureIdentityIds = identities.rows.map(row => row.identity_id); + }); + + afterAll(async () => { + vi.restoreAllMocks(); + if (pool) await cleanup(); + await closeDatabase(); + }); + + async function snapshot(userId = PERSONAL_ID, organizationId: string | null = null) { + const result = await loadAuthorizationSnapshot(userId, organizationId); + expect(result).not.toBeNull(); + return result!; + } + + async function link(primaryId: string, secondaryId: string, db: Pool | PoolClient = pool) { + await db.query( + `UPDATE identity_workos_users + SET identity_id = (SELECT identity_id FROM identity_workos_users WHERE workos_user_id = $1), + is_primary = FALSE + WHERE workos_user_id = $2`, + [primaryId, secondaryId], + ); + } + + async function grant( + userId: string, + organizationId: string, + options: { from?: string; until?: string | null; revoked?: boolean; role?: 'member' | 'admin' | 'owner' } = {}, + ) { + const result = await pool.query<{ id: string }>( + `INSERT INTO organization_credential_grants + (workos_user_id, workos_organization_id, role, granted_by_workos_user_id, + effective_from, effective_until, revoked_at, revoked_by_workos_user_id) + VALUES ($1, $2, $3, $1, $4, $5, + CASE WHEN $6 THEN NOW() ELSE NULL END, + CASE WHEN $6 THEN $1::varchar ELSE NULL END) + RETURNING id`, + [userId, organizationId, options.role ?? 'member', options.from ?? '2000-01-01T00:00:00.123456Z', + options.until ?? null, options.revoked ?? false], + ); + return result.rows[0].id; + } + + it('hydrates a cold exact credential with epoch zero and no implicit organization', async () => { + await grant(PERSONAL_ID, PERSONAL_ORG); + await pool.query( + `INSERT INTO organization_memberships (workos_user_id, workos_organization_id, email, role) + VALUES ($1, $2, 'sam.personal@example.test', 'owner')`, + [PERSONAL_ID, CORPORATE_ORG], + ); + + const cold = await snapshot(); + expect(cold).toMatchObject({ + authenticatedUserId: PERSONAL_ID, + canonicalUserId: PERSONAL_ID, + authorizationEpoch: '0', + selectedOrganizationId: null, + credentialGrant: null, + credential: { email: 'sam.personal@example.test', firstName: 'Sam', lastName: 'Adeyemi' }, + }); + expect(cold.identityId).toBeTruthy(); + expect(Object.isFrozen(cold)).toBe(true); + expect(Object.isFrozen(cold.credential)).toBe(true); + expect(await snapshot(PERSONAL_ID, '')).toEqual(cold); + expect((await snapshot(PERSONAL_ID, CORPORATE_ORG)).credentialGrant).toBeNull(); + }); + + it.each([ + { direction: 'personal primary', primaryId: PERSONAL_ID, secondaryId: CORPORATE_ID }, + { direction: 'corporate primary', primaryId: CORPORATE_ID, secondaryId: PERSONAL_ID }, + ])('keeps exact credential grants isolated with $direction', async ({ primaryId, secondaryId }) => { + await link(primaryId, secondaryId); + const personalGrantId = await grant(PERSONAL_ID, PERSONAL_ORG); + const corporateGrantId = await grant(CORPORATE_ID, CORPORATE_ORG, { role: 'admin' }); + // Stale consolidated membership state is deliberately stronger than the + // exact grants. Neither identity direction may inherit it as authority. + await pool.query( + `INSERT INTO organization_memberships (workos_user_id, workos_organization_id, email, role) + VALUES ($1, $2, 'sam@pinnacle.example', 'owner'), + ($1, $3, 'sam@pinnacle.example', 'owner')`, + [primaryId, PERSONAL_ORG, CORPORATE_ORG], + ); + + const personal = await snapshot(PERSONAL_ID, PERSONAL_ORG); + const corporate = await snapshot(CORPORATE_ID, CORPORATE_ORG); + expect(personal.canonicalUserId).toBe(primaryId); + expect(corporate.canonicalUserId).toBe(primaryId); + expect(personal.identityId).toBe(corporate.identityId); + expect(personal.credential.email).toBe('sam.personal@example.test'); + expect(corporate.credential.email).toBe('sam@pinnacle.example'); + expect(personal.credentialGrant).toEqual({ + id: personalGrantId, organizationId: PERSONAL_ORG, role: 'member', + effectiveFrom: '2000-01-01T00:00:00.123456Z', effectiveUntil: null, + }); + expect(corporate.credentialGrant?.id).toBe(corporateGrantId); + expect(corporate.credentialGrant?.role).toBe('admin'); + expect(Object.isFrozen(personal.credentialGrant)).toBe(true); + expect((await snapshot(PERSONAL_ID, CORPORATE_ORG)).credentialGrant).toBeNull(); + expect((await snapshot(CORPORATE_ID, PERSONAL_ORG)).credentialGrant).toBeNull(); + expect((await snapshot(secondaryId, null)).credentialGrant).toBeNull(); + expect(sameAuthorizationIdentity(personal, corporate)).toBe(false); + }); + + it.each([ + { state: 'expired', from: '2000-01-01T00:00:00Z', until: '2001-01-01T00:00:00Z' }, + { state: 'future', from: '2999-01-01T00:00:00Z', until: null }, + { state: 'revoked', revoked: true }, + ])('excludes $state grants on a cold read', async options => { + await grant(PERSONAL_ID, PERSONAL_ORG, options); + const cold = await snapshot(PERSONAL_ID, PERSONAL_ORG); + expect(cold.selectedOrganizationId).toBe(PERSONAL_ORG); + expect(cold.credentialGrant).toBeNull(); + }); + + it('does not reuse a previously loaded grant after revocation', async () => { + const grantId = await grant(PERSONAL_ID, PERSONAL_ORG); + const cached = await snapshot(PERSONAL_ID, PERSONAL_ORG); + await pool.query( + `UPDATE organization_credential_grants + SET revoked_at = NOW(), revoked_by_workos_user_id = $2 WHERE id = $1`, + [grantId, PERSONAL_ID], + ); + const fresh = await snapshot(PERSONAL_ID, PERSONAL_ORG); + expect(fresh.credentialGrant).toBeNull(); + expect(sameAuthorizationIdentity(cached, fresh)).toBe(true); + expect(sameAuthorizationSnapshot(cached, fresh)).toBe(false); + }); + + it('preserves bigint epochs and rejects forwards and backwards replay', async () => { + await pool.query( + 'INSERT INTO authorization_epochs (workos_user_id, epoch) VALUES ($1, $2)', + [PERSONAL_ID, '9007199254740992'], + ); + const old = await snapshot(); + await bumpAuthorizationEpochs(pool, [PERSONAL_ID]); + const bumped = await snapshot(); + expect(old.authorizationEpoch).toBe('9007199254740992'); + expect(bumped.authorizationEpoch).toBe('9007199254740993'); + expect(sameAuthorizationSnapshot(old, bumped)).toBe(false); + expect(sameAuthorizationSnapshot(bumped, old)).toBe(false); + await pool.query('DELETE FROM authorization_epochs WHERE workos_user_id = $1', [PERSONAL_ID]); + const absent = await snapshot(); + expect(absent.authorizationEpoch).toBe('0'); + expect(sameAuthorizationSnapshot(bumped, absent)).toBe(false); + }); + + it('rejects replays with altered credential, identity, selection, or grant details', async () => { + await grant(PERSONAL_ID, PERSONAL_ORG, { until: '2999-01-01T00:00:00.654321Z' }); + const current = await snapshot(PERSONAL_ID, PERSONAL_ORG); + expect(current.credentialGrant?.effectiveUntil).toBe('2999-01-01T00:00:00.654321Z'); + expect(sameAuthorizationSnapshot(current, structuredClone(current))).toBe(true); + const identityChanges: Partial[] = [ + { authenticatedUserId: CORPORATE_ID }, + { canonicalUserId: CORPORATE_ID }, + { identityId: null }, + { authorizationEpoch: '1' }, + { credential: { ...current.credential, email: 'changed@example.test' } }, + ]; + for (const change of identityChanges) { + expect(sameAuthorizationIdentity(current, { ...current, ...change })).toBe(false); + expect(sameAuthorizationSnapshot(current, { ...current, ...change })).toBe(false); + } + expect(sameAuthorizationSnapshot(current, { ...current, selectedOrganizationId: CORPORATE_ORG })).toBe(false); + expect(sameAuthorizationSnapshot(current, { ...current, credentialGrant: null })).toBe(false); + const grantChanges: Partial>[] = [ + { id: 'replacement-grant' }, + { organizationId: CORPORATE_ORG }, + { role: 'owner' }, + { effectiveFrom: '2000-01-01T00:00:00.123457Z' }, + { effectiveUntil: '2999-01-01T00:00:00.654322Z' }, + ]; + for (const change of grantChanges) { + expect(sameAuthorizationSnapshot(current, { + ...current, credentialGrant: { ...current.credentialGrant!, ...change }, + })).toBe(false); + } + }); + + it.each([ + { direction: 'personal into corporate', primaryId: CORPORATE_ID, secondaryId: PERSONAL_ID }, + { direction: 'corporate into personal', primaryId: PERSONAL_ID, secondaryId: CORPORATE_ID }, + ])('cannot stamp old identity with a newly committed epoch: $direction', async ({ primaryId, secondaryId }) => { + const before = await snapshot(secondaryId); + const writer = await pool.connect(); + let releaseRead!: () => void; + let readCompleted!: () => void; + const readBarrier = new Promise(resolve => { readCompleted = resolve; }); + const deliveryBarrier = new Promise(resolve => { releaseRead = resolve; }); + let pendingRead: Promise | undefined; + const actualQuery = pool.query.bind(pool); + try { + await writer.query('BEGIN'); + await link(primaryId, secondaryId, writer); + // An observer while identity changed but before the transactional epoch + // write still sees the complete old state through PostgreSQL MVCC. + expect(await snapshot(secondaryId)).toEqual(before); + await bumpAuthorizationEpochs(writer, [primaryId, secondaryId]); + + // Hold delivery of the first real SQL result while the writer commits. + // A split identity/epoch loader would then issue its epoch query against + // the new commit and return old identity + new epoch. No rows are mocked. + const readSpy = vi.spyOn(pool, 'query').mockImplementationOnce(async (...args: any[]) => { + const result = await (actualQuery as (...queryArgs: any[]) => Promise)(...args); + readCompleted(); + await deliveryBarrier; + return result; + }); + pendingRead = loadAuthorizationSnapshot(secondaryId, null); + await Promise.race([readBarrier, pendingRead.then(() => { + throw new Error('Snapshot returned without waiting for the database read'); + })]); + await writer.query('COMMIT'); + releaseRead(); + const overlapping = await pendingRead; + expect(overlapping).toEqual(before); + expect(readSpy).toHaveBeenCalledTimes(1); + readSpy.mockRestore(); + + const after = await snapshot(secondaryId); + expect(after.canonicalUserId).toBe(primaryId); + expect(after.identityId).not.toBe(before.identityId); + expect(after.authorizationEpoch).toBe('1'); + expect(sameAuthorizationSnapshot(before, after)).toBe(false); + expect(sameAuthorizationSnapshot(after, before)).toBe(false); + } finally { + releaseRead(); + await writer.query('ROLLBACK'); + await pendingRead?.catch(() => undefined); + vi.restoreAllMocks(); + writer.release(); + } + }); + + it('returns null for a missing exact credential, without using its former canonical person', async () => { + await link(CORPORATE_ID, PERSONAL_ID); + const cached = await snapshot(PERSONAL_ID); + await pool.query('DELETE FROM users WHERE workos_user_id = $1', [PERSONAL_ID]); + expect(await loadAuthorizationSnapshot(PERSONAL_ID, CORPORATE_ORG)).toBeNull(); + expect((await snapshot(CORPORATE_ID)).canonicalUserId).toBe(cached.canonicalUserId); + }); + + it.each(['binding', 'primary'])('fails unavailable for a missing identity %s', async missing => { + if (missing === 'binding') { + await pool.query('DELETE FROM identity_workos_users WHERE workos_user_id = $1', [PERSONAL_ID]); + } else { + await pool.query('UPDATE identity_workos_users SET is_primary = FALSE WHERE workos_user_id = $1', [PERSONAL_ID]); + } + await expect(loadAuthorizationSnapshot(PERSONAL_ID, null)) + .rejects.toBeInstanceOf(AuthorizationSnapshotUnavailableError); + }); + + it('fails unavailable on database errors instead of replaying a warm snapshot', async () => { + await snapshot(PERSONAL_ID, PERSONAL_ORG); + vi.spyOn(pool, 'query').mockRejectedValueOnce(new Error('private database connection details')); + await expect(loadAuthorizationSnapshot(PERSONAL_ID, PERSONAL_ORG)) + .rejects.toThrow(new AuthorizationSnapshotUnavailableError()); + }); + + it.each([ + { state: 'valid credential', hasUser: true }, + { state: 'absent credential', hasUser: false }, + ])('rejects a recovering replica with $state', async ({ hasUser }) => { + const primary = await snapshot(PERSONAL_ID, PERSONAL_ORG); + // The integration database is a primary. Simulate only the single query + // result from a recovering replica, including its no-user anchor row. + const readSpy = vi.spyOn(pool, 'query').mockResolvedValueOnce({ + rows: [{ + in_recovery: true, + authenticated_user_id: hasUser ? primary.authenticatedUserId : null, + canonical_user_id: hasUser ? primary.canonicalUserId : null, + identity_id: hasUser ? primary.identityId : null, + authorization_epoch: '0', + email: hasUser ? primary.credential.email : null, + first_name: hasUser ? primary.credential.firstName : null, + last_name: hasUser ? primary.credential.lastName : null, + grant_id: null, + grant_organization_id: null, + grant_role: null, + grant_effective_from: null, + grant_effective_until: null, + }], + rowCount: 1, + command: 'SELECT', + oid: 0, + fields: [], + }); + await expect(loadAuthorizationSnapshot(PERSONAL_ID, PERSONAL_ORG)) + .rejects.toBeInstanceOf(AuthorizationSnapshotUnavailableError); + expect(readSpy).toHaveBeenCalledTimes(1); + }); +}); diff --git a/server/tests/unit/addie-admin-global-auth.test.ts b/server/tests/unit/addie-admin-global-auth.test.ts index b3b0bf57234..8ec2ff778c2 100644 --- a/server/tests/unit/addie-admin-global-auth.test.ts +++ b/server/tests/unit/addie-admin-global-auth.test.ts @@ -147,9 +147,17 @@ describe('Addie real global-admin boundary', () => { mocks.getAdminWorkingGroupIdBySlug.mockResolvedValue('wg_aao_admin'); mocks.isAdminGroupMember.mockResolvedValue(true); mocks.poolQuery.mockImplementation((sql: string) => { - if (sql.includes('FROM users')) { + if (sql.includes('pg_catalog.pg_is_in_recovery()')) { return Promise.resolve({ - rows: [{ first_name: 'SSO', last_name: 'Admin' }], + rows: [{ + in_recovery: false, + authenticated_user_id: 'user_sso_admin', + canonical_user_id: 'user_sso_admin', + identity_id: 'identity_sso_admin', + authorization_epoch: '0', + email: 'sso-admin@example.test', email_verified: true, + first_name: 'SSO', last_name: 'Admin', grant_id: null, + }], rowCount: 1, }); } diff --git a/server/tests/unit/authorization-epoch-session.test.ts b/server/tests/unit/authorization-epoch-session.test.ts index b167fdad474..d648f7ed07f 100644 --- a/server/tests/unit/authorization-epoch-session.test.ts +++ b/server/tests/unit/authorization-epoch-session.test.ts @@ -1,323 +1,294 @@ -/** - * Persisted authorization epoch enforcement (#6827). - * - * The session cache is per-instance, so evicting it cannot revoke authority - * granted before an identity-binding change — another instance still serves - * its own cached entry. These tests pin the replacement: every cache hit - * revalidates the persisted epoch fingerprint stamped when the entry was - * stored, and a moved fingerprint forces full re-validation. - */ - +/** Provider caches must never become identity, organization, or epoch authority. */ import type { NextFunction, Request, Response } from 'express'; -import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'; +import type { AuthorizationSnapshot } from '../../src/db/user-authorization-snapshot-db.js'; const mocks = vi.hoisted(() => ({ - authenticate: vi.fn(), - loadSealedSession: vi.fn(), - checkPlatformBan: vi.fn(), - checkPlatformBanForApiKey: vi.fn(), - readCredentialAuthorizationLifecycle: vi.fn(), - poolQuery: vi.fn(), - verifyWorkOSJWT: vi.fn(), + authenticate: vi.fn(), loadSealedSession: vi.fn(), checkPlatformBan: vi.fn(), + loadAuthorizationSnapshot: vi.fn(), verifyWorkOSJWT: vi.fn(), poolQuery: vi.fn(), })); - vi.hoisted(() => { - delete process.env.DEV_USER_EMAIL; - delete process.env.DEV_USER_ID; - process.env.WORKOS_API_KEY = process.env.WORKOS_API_KEY ?? 'sk_test'; - process.env.WORKOS_CLIENT_ID = process.env.WORKOS_CLIENT_ID ?? 'client_test'; - process.env.WORKOS_COOKIE_PASSWORD = - process.env.WORKOS_COOKIE_PASSWORD ?? 'placeholder-cookie-password-32-bytes-min'; + process.env.DEV_USER_EMAIL = ''; + process.env.DEV_USER_ID = ''; + process.env.WORKOS_API_KEY ??= 'sk_test'; + process.env.WORKOS_CLIENT_ID ??= 'client_test'; + process.env.WORKOS_COOKIE_PASSWORD ??= 'placeholder-cookie-password-32-bytes-min'; }); - vi.mock('@workos-inc/node', () => ({ WorkOS: vi.fn(function WorkOS() { return { - userManagement: { - loadSealedSession: mocks.loadSealedSession, - }, + userManagement: { loadSealedSession: mocks.loadSealedSession }, apiKeys: { createValidation: vi.fn() }, }; }), })); - vi.mock('../../src/db/bans-db.js', () => ({ - bansDb: { - checkPlatformBan: mocks.checkPlatformBan, - checkPlatformBanForApiKey: mocks.checkPlatformBanForApiKey, - }, -})); - -vi.mock('../../src/db/authorization-epoch-db.js', () => ({ - readCredentialAuthorizationLifecycle: mocks.readCredentialAuthorizationLifecycle, - bumpAuthorizationEpochs: vi.fn(), + bansDb: { checkPlatformBan: mocks.checkPlatformBan, checkPlatformBanForApiKey: vi.fn() }, })); - -vi.mock('../../src/db/client.js', () => ({ - getPool: () => ({ query: mocks.poolQuery }), - query: mocks.poolQuery, - isDatabaseInitialized: () => true, +vi.mock('../../src/db/user-authorization-snapshot-db.js', async (importOriginal) => ({ + ...await importOriginal(), + loadAuthorizationSnapshot: mocks.loadAuthorizationSnapshot, })); - vi.mock('../../src/auth/workos-jwt.js', () => ({ - looksLikeJWT: vi.fn(() => true), verifyWorkOSJWT: mocks.verifyWorkOSJWT, + looksLikeJWT: (token: string) => token.split('.').length === 3, })); +vi.mock('../../src/db/client.js', () => ({ + getPool: () => ({ query: mocks.poolQuery }), query: mocks.poolQuery, isDatabaseInitialized: () => true, +})); +import { AuthorizationSnapshotUnavailableError } from '../../src/db/user-authorization-snapshot-db.js'; +import { invalidateBanCache, optionalAuth, requireAuth, stopAuthTimers } from '../../src/middleware/auth.js'; -import { optionalAuth, requireAuth, validateWorkOSBearerJWT } from '../../src/middleware/auth.js'; - -const SESSION_USER = { - id: 'user_epoch_primary', - email: 'primary@epoch.test', - firstName: 'Epoch', - lastName: 'User', - emailVerified: true, - createdAt: '2026-01-01T00:00:00.000Z', - updatedAt: '2026-01-01T00:00:00.000Z', +const AUTHENTICATED_ID = 'user_epoch_authenticated'; +const PROVIDER_USER = { + id: AUTHENTICATED_ID, email: 'sam@example.test', firstName: 'Sam', lastName: 'Adeyemi', + emailVerified: true, createdAt: '2026-01-01T00:00:00.000Z', updatedAt: '2026-01-01T00:00:00.000Z', }; - -function makeRequest(cookie: string): Request { +let sequence = 0; +function snapshot(overrides: Partial = {}): AuthorizationSnapshot { return { - headers: {}, - cookies: { 'wos-session': cookie }, - path: '/api/me', - originalUrl: '/api/me', - accepts: () => false, - } as unknown as Request; -} - -function makeResponse(): Response { - return { - cookie: vi.fn(), - status: vi.fn().mockReturnThis(), - json: vi.fn().mockReturnThis(), - redirect: vi.fn().mockReturnThis(), - } as unknown as Response; + authenticatedUserId: AUTHENTICATED_ID, canonicalUserId: AUTHENTICATED_ID, + identityId: 'identity_epoch', authorizationEpoch: '1', selectedOrganizationId: null, + credential: { email: PROVIDER_USER.email, firstName: 'Sam', lastName: 'Adeyemi', emailVerified: true }, + credentialGrant: null, ...overrides, + }; } - -function makeBearerRequest(token: string): Request { +function request(token: string, bearer = false): Request { return { - headers: { authorization: `Bearer ${token}` }, + headers: bearer ? { authorization: `Bearer ${token}` } : {}, + cookies: bearer ? {} : { 'wos-session': token }, query: {}, body: {}, params: {}, + path: '/api/me', originalUrl: '/api/me', accepts: () => false, } as unknown as Request; } - -function activeLifecycle(fingerprint: string) { +function response(): Response { return { - status: 'active' as const, - snapshot: { - workos_user_id: SESSION_USER.id, - email: SESSION_USER.email, - first_name: SESSION_USER.firstName, - last_name: SESSION_USER.lastName, - identity_id: 'identity_epoch', - primary_workos_user_id: SESSION_USER.id, - fingerprint, - }, - }; + cookie: vi.fn(), status: vi.fn().mockReturnThis(), json: vi.fn().mockReturnThis(), + redirect: vi.fn().mockReturnThis(), + } as unknown as Response; } - -describe('persisted authorization epoch gates the session cache', () => { - beforeEach(() => { - vi.clearAllMocks(); - mocks.checkPlatformBan.mockResolvedValue({ banned: false, ban: null }); - // Local user lookup resolves; attachIdentityId sees a singleton identity - // (primary === authenticated credential), so there is no id-swap. - mocks.poolQuery.mockResolvedValue({ - rows: [ - { - first_name: 'Epoch', - last_name: 'User', - identity_id: 'identity_epoch', - primary_workos_user_id: 'user_epoch_primary', - }, - ], - rowCount: 1, - }); - mocks.authenticate.mockResolvedValue({ - authenticated: true, - user: SESSION_USER, - accessToken: 'access-token', - }); - mocks.loadSealedSession.mockReturnValue({ - authenticate: mocks.authenticate, - refresh: vi.fn(), - }); - mocks.readCredentialAuthorizationLifecycle.mockResolvedValue( - activeLifecycle('user_epoch_primary:1'), - ); - mocks.verifyWorkOSJWT.mockResolvedValue({ - sub: SESSION_USER.id, - email: SESSION_USER.email, - expiresAt: Math.floor(Date.now() / 1000) + 3600, - isM2M: false, - }); - }); - - it('serves the cached session while the fingerprint is unchanged', async () => { - const cookie = `sealed-unchanged-${Date.now()}`; +beforeEach(() => { + vi.clearAllMocks(); + mocks.loadAuthorizationSnapshot.mockReset(); + mocks.loadAuthorizationSnapshot.mockImplementation(async (_id: string, org: string | null) => + snapshot({ selectedOrganizationId: org })); + invalidateBanCache('user', AUTHENTICATED_ID); + mocks.checkPlatformBan.mockResolvedValue({ banned: false }); + mocks.authenticate.mockResolvedValue({ authenticated: true, user: { ...PROVIDER_USER }, accessToken: 'access-token' }); + mocks.loadSealedSession.mockReturnValue({ authenticate: mocks.authenticate, refresh: vi.fn() }); + mocks.verifyWorkOSJWT.mockResolvedValue({ sub: AUTHENTICATED_ID, email: PROVIDER_USER.email, isM2M: false }); +}); +afterAll(stopAuthTimers); + +describe.each([ + ['required cookie', requireAuth, false], ['optional cookie', optionalAuth, false], + ['required bearer', requireAuth, true], ['optional bearer', optionalAuth, true], +] as const)('%s authorization snapshots', (_label, middleware, bearer) => { + function credential(): string { + sequence += 1; + return bearer ? `header.payload${sequence}.signature` : `sealed-snapshot-${sequence}`; + } + it('hydrates an atomic snapshot on cold start and every cached request', async () => { + const token = credential(); + const first = request(token, bearer); + const second = request(token, bearer); const next = vi.fn() as NextFunction; - - await requireAuth(makeRequest(cookie), makeResponse(), next); - await requireAuth(makeRequest(cookie), makeResponse(), next); - + await middleware(first, response(), next); + mocks.loadAuthorizationSnapshot.mockResolvedValue(snapshot({ authorizationEpoch: '2' })); + await middleware(second, response(), next); expect(next).toHaveBeenCalledTimes(2); - // Second request came from cache — the sealed session was unsealed once. - expect(mocks.authenticate).toHaveBeenCalledTimes(1); + expect(mocks.loadAuthorizationSnapshot).toHaveBeenCalledTimes(2); + expect(mocks.loadAuthorizationSnapshot).toHaveBeenNthCalledWith(2, AUTHENTICATED_ID, null); + expect(first.user?.authorizationSnapshot?.authorizationEpoch).toBe('1'); + expect(second.user?.authorizationSnapshot?.authorizationEpoch).toBe('2'); + expect(bearer ? mocks.verifyWorkOSJWT : mocks.authenticate).toHaveBeenCalledTimes(1); + expect(mocks.poolQuery).not.toHaveBeenCalled(); }); - - it('re-validates when an identity-binding change moved the fingerprint', async () => { - const cookie = `sealed-bumped-${Date.now()}`; - const next = vi.fn() as NextFunction; - - await requireAuth(makeRequest(cookie), makeResponse(), next); - expect(mocks.authenticate).toHaveBeenCalledTimes(1); - - // A binding mutation bumped the credential's epoch on another instance. - mocks.readCredentialAuthorizationLifecycle.mockResolvedValue( - activeLifecycle('user_epoch_primary:2'), - ); - - await requireAuth(makeRequest(cookie), makeResponse(), next); - - expect(next).toHaveBeenCalledTimes(2); - expect(mocks.authenticate).toHaveBeenCalledTimes(2); + it.each([ + ['secondary becomes primary', 'user_other_primary', AUTHENTICATED_ID], + ['primary becomes secondary', AUTHENTICATED_ID, 'user_other_primary'], + ])('keeps the exact credential when %s', async (_direction, before, after) => { + const token = credential(); + mocks.loadAuthorizationSnapshot.mockResolvedValue(snapshot({ canonicalUserId: before })); + const first = request(token, bearer); + await middleware(first, response(), vi.fn()); + // A handler mutates its request object, which must never poison provider authentication. + first.user!.id = 'user_attacker_canonical'; + first.user!.authWorkosUserId = 'user_attacker_credential'; + first.user!.identityId = 'identity_attacker'; + mocks.loadAuthorizationSnapshot.mockResolvedValue(snapshot({ + canonicalUserId: after, identityId: 'identity_changed', authorizationEpoch: '2', + })); + const second = request(token, bearer); + const next = vi.fn(); + await middleware(second, response(), next); + expect(next).toHaveBeenCalledOnce(); + expect(mocks.loadAuthorizationSnapshot).toHaveBeenLastCalledWith(AUTHENTICATED_ID, null); + expect(second.user).toMatchObject({ id: after, identityId: 'identity_changed' }); + expect(second.user?.authWorkosUserId ?? second.user?.id).toBe(AUTHENTICATED_ID); + expect(second.user?.authorizationSnapshot?.authorizationEpoch).toBe('2'); }); - - it('bypasses the cache when the epoch lookup fails rather than serving unconfirmed state', async () => { - const cookie = `sealed-lookup-error-${Date.now()}`; - const next = vi.fn() as NextFunction; - - await requireAuth(makeRequest(cookie), makeResponse(), next); - expect(mocks.authenticate).toHaveBeenCalledTimes(1); - - mocks.readCredentialAuthorizationLifecycle.mockResolvedValueOnce({ status: 'unavailable' }); - - await requireAuth(makeRequest(cookie), makeResponse(), next); - - expect(next).toHaveBeenCalledTimes(2); - expect(mocks.authenticate).toHaveBeenCalledTimes(2); + it.each(['cold', 'warm'] as const)('returns unavailable with no fallback on a %s-cache database outage', async (cacheState) => { + const token = credential(); + if (cacheState === 'warm') await middleware(request(token, bearer), response(), vi.fn()); + mocks.loadAuthorizationSnapshot.mockRejectedValue(new AuthorizationSnapshotUnavailableError()); + const req = request(token, bearer); + const res = response(); + const next = vi.fn(); + await middleware(req, res, next); + expect(res.status).toHaveBeenCalledWith(503); + expect(next).not.toHaveBeenCalled(); + expect(req.user).toBeUndefined(); + expect(res.redirect).not.toHaveBeenCalled(); + expect(bearer ? mocks.verifyWorkOSJWT : mocks.authenticate).toHaveBeenCalledTimes(1); }); - - it('denies an epoch-0 credential on replica B immediately after replica A records deletion', async () => { - const cookie = `sealed-epoch-zero-deleted-${Date.now()}`; - const next = vi.fn() as NextFunction; - mocks.readCredentialAuthorizationLifecycle.mockResolvedValue(activeLifecycle('')); - - await requireAuth(makeRequest(cookie), makeResponse(), next); - expect(next).toHaveBeenCalledTimes(1); - expect(mocks.authenticate).toHaveBeenCalledTimes(1); - - // Replica A committed a non-cascading deletion tombstone. Replica B still - // has its own cold epoch-0 session entry, but the next hit sees the marker. - mocks.readCredentialAuthorizationLifecycle.mockResolvedValue({ - status: 'terminal', reason: 'deleted_or_quarantined', - }); - const response = makeResponse(); - await requireAuth(makeRequest(cookie), response, next); - - expect(next).toHaveBeenCalledTimes(1); - expect(mocks.authenticate).toHaveBeenCalledTimes(2); - expect(response.status).toHaveBeenCalledWith(401); + it('rejects a deleted exact credential instead of recovering its canonical user', async () => { + const token = credential(); + await middleware(request(token, bearer), response(), vi.fn()); + mocks.loadAuthorizationSnapshot.mockResolvedValue(null); + const req = request(token, bearer); + const res = response(); + const next = vi.fn(); + await middleware(req, res, next); + expect(res.status).toHaveBeenCalledWith(401); + expect(next).not.toHaveBeenCalled(); + expect(req.user).toBeUndefined(); }); - - it('does not serve replica-B cached authority when deletion-marker lookup is unavailable', async () => { - const cookie = `sealed-deletion-marker-outage-${Date.now()}`; - const next = vi.fn() as NextFunction; - mocks.readCredentialAuthorizationLifecycle.mockResolvedValue(activeLifecycle('')); - - await requireAuth(makeRequest(cookie), makeResponse(), next); - mocks.readCredentialAuthorizationLifecycle.mockResolvedValue({ status: 'unavailable' }); - const response = makeResponse(); - await requireAuth(makeRequest(cookie), response, next); - - expect(next).toHaveBeenCalledTimes(1); - expect(mocks.authenticate).toHaveBeenCalledTimes(2); - expect(response.status).toHaveBeenCalledWith(503); + it('keeps organization selection request-local across cache hits and leaves no selection empty', async () => { + const token = credential(); + for (const org of ['org_pinnacle', 'org_streamhaus', null]) { + const req = request(token, bearer); + if (org) req.headers['x-organization-id'] = org; + await middleware(req, response(), vi.fn()); + expect(mocks.loadAuthorizationSnapshot).toHaveBeenLastCalledWith(AUTHENTICATED_ID, org); + expect(req.user?.authorizationSnapshot?.selectedOrganizationId).toBe(org); + } + expect(bearer ? mocks.verifyWorkOSJWT : mocks.authenticate).toHaveBeenCalledTimes(1); }); - - it('invalidates replica B through persistence when only replica A receives local eviction', async () => { - vi.resetModules(); - const replicaA = await import('../../src/middleware/auth.js'); - vi.resetModules(); - const replicaB = await import('../../src/middleware/auth.js'); - const cookieA = `sealed-replica-a-${Date.now()}`; - const cookieB = `sealed-replica-b-${Date.now()}`; - const nextA = vi.fn() as NextFunction; - const nextB = vi.fn() as NextFunction; - mocks.readCredentialAuthorizationLifecycle.mockResolvedValue(activeLifecycle('')); - - await replicaA.requireAuth(makeRequest(cookieA), makeResponse(), nextA); - await replicaB.requireAuth(makeRequest(cookieB), makeResponse(), nextB); - expect(mocks.authenticate).toHaveBeenCalledTimes(2); - - // The deletion process only has access to replica A's in-memory cache. - // Replica B retains its independent entry and must observe the durable - // deletion marker on its immediate next request, without waiting for TTL. - replicaA.invalidateSessionsForUsers([SESSION_USER.id]); - mocks.readCredentialAuthorizationLifecycle.mockResolvedValue({ - status: 'terminal', reason: 'deleted_or_quarantined', - }); - const responseB = makeResponse(); - await replicaB.requireAuth(makeRequest(cookieB), responseB, nextB); - - expect(nextB).toHaveBeenCalledTimes(1); - expect(mocks.authenticate).toHaveBeenCalledTimes(3); - expect(responseB.status).toHaveBeenCalledWith(401); + it('uses current local credential email instead of a stale provider email', async () => { + const token = credential(); + await middleware(request(token, bearer), response(), vi.fn()); + mocks.loadAuthorizationSnapshot.mockResolvedValue(snapshot({ + credential: { email: 'sam-current@example.test', firstName: 'Sam', lastName: 'Adeyemi', emailVerified: true }, + })); + const req = request(token, bearer); + await middleware(req, response(), vi.fn()); + expect(req.user?.email).toBe('sam-current@example.test'); }); - - it('denies an epoch-0 cached bearer JWT on the immediate hit after deletion', async () => { - const token = `header.${Date.now()}.signature`; - mocks.readCredentialAuthorizationLifecycle.mockResolvedValue(activeLifecycle('')); - - await expect(validateWorkOSBearerJWT(makeBearerRequest(token))).resolves.toMatchObject({ - user: { id: SESSION_USER.id }, - }); - expect(mocks.verifyWorkOSJWT).toHaveBeenCalledTimes(1); - - mocks.readCredentialAuthorizationLifecycle.mockResolvedValue({ - status: 'terminal', reason: 'deleted_or_quarantined', - }); - await expect(validateWorkOSBearerJWT(makeBearerRequest(token))).resolves.toBeNull(); - - expect(mocks.verifyWorkOSJWT).toHaveBeenCalledTimes(2); + it('does not apply an old provider verification to the current unverified email', async () => { + mocks.loadAuthorizationSnapshot.mockResolvedValue(snapshot({ + credential: { email: 'sam-unverified@example.test', firstName: 'Sam', lastName: 'Adeyemi', emailVerified: false }, + })); + const req = request(credential(), bearer); + await middleware(req, response(), vi.fn()); + expect(req.user).toMatchObject({ email: 'sam-unverified@example.test', emailVerified: false }); }); - - it('cannot poison requireAuth from optionalAuth with a deleted sealed credential', async () => { - const cookie = `sealed-optional-poison-${Date.now()}`; - const optionalRequest = makeRequest(cookie); - const optionalNext = vi.fn() as NextFunction; - mocks.readCredentialAuthorizationLifecycle.mockResolvedValue({ - status: 'terminal', reason: 'deleted_or_quarantined', - }); - - await optionalAuth(optionalRequest, makeResponse(), optionalNext); - expect(optionalNext).toHaveBeenCalledTimes(1); - expect(optionalRequest.user).toBeUndefined(); - - const requiredResponse = makeResponse(); - const requiredNext = vi.fn() as NextFunction; - await requireAuth(makeRequest(cookie), requiredResponse, requiredNext); - - expect(mocks.authenticate).toHaveBeenCalledTimes(2); - expect(requiredNext).not.toHaveBeenCalled(); - expect(requiredResponse.status).toHaveBeenCalledWith(401); + it('rejects conflicting explicit organization selectors before hydrating authority', async () => { + const req = request(credential(), bearer); + req.headers['x-organization-id'] = 'org_pinnacle'; + req.params.orgId = 'org_streamhaus'; + const res = response(); + const next = vi.fn(); + await middleware(req, res, next); + expect(res.status).toHaveBeenCalledWith(403); + expect(next).not.toHaveBeenCalled(); + expect(mocks.loadAuthorizationSnapshot).not.toHaveBeenCalled(); }); + it('keeps authorization provenance out of serialized and spread user objects', async () => { + const req = request(credential(), bearer); + await middleware(req, response(), vi.fn()); + expect(req.user?.authorizationSnapshot?.authenticatedUserId).toBe(AUTHENTICATED_ID); + expect(JSON.stringify(req.user)).not.toContain('authorizationSnapshot'); + expect({ ...req.user }).not.toHaveProperty('authorizationSnapshot'); + }); +}); +it('a cookie cached by optional auth is rehydrated before required auth', async () => { + const token = `shared-optional-${++sequence}`; + await optionalAuth(request(token), response(), vi.fn()); + mocks.loadAuthorizationSnapshot.mockResolvedValue(snapshot({ canonicalUserId: 'user_changed', authorizationEpoch: '7' })); + const second = request(token); + await requireAuth(second, response(), vi.fn()); + expect(mocks.authenticate).toHaveBeenCalledTimes(1); + expect(second.user).toMatchObject({ id: 'user_changed', authWorkosUserId: AUTHENTICATED_ID }); + expect(second.user?.authorizationSnapshot?.authorizationEpoch).toBe('7'); +}); - it('cannot serve optionalAuth from a requireAuth cache after deletion', async () => { - const cookie = `sealed-required-poison-${Date.now()}`; - const requiredNext = vi.fn() as NextFunction; - await requireAuth(makeRequest(cookie), makeResponse(), requiredNext); - expect(requiredNext).toHaveBeenCalledTimes(1); - - mocks.readCredentialAuthorizationLifecycle.mockResolvedValue({ - status: 'terminal', reason: 'deleted_or_quarantined', - }); - const optionalRequest = makeRequest(cookie); - const optionalNext = vi.fn() as NextFunction; - await optionalAuth(optionalRequest, makeResponse(), optionalNext); +describe.each([['required', requireAuth], ['optional', optionalAuth]] as const)('%s bearer bans', (_label, middleware) => { + it.each(['cold', 'warm'] as const)('checks the banned exact linked credential on a %s cache', async (cacheState) => { + const token = `header.banned${++sequence}.signature`; + mocks.loadAuthorizationSnapshot.mockResolvedValue(snapshot({ canonicalUserId: 'user_unbanned_primary' })); + if (cacheState === 'warm') { + await middleware(request(token, true), response(), vi.fn()); + invalidateBanCache('user', AUTHENTICATED_ID); + } + mocks.checkPlatformBan.mockImplementation(async (userId: string) => userId === AUTHENTICATED_ID + ? { banned: true, ban: { id: 'ban_exact_credential', reason: 'Account suspended' } } + : { banned: false }); + const res = response(); + const next = vi.fn(); + await middleware(request(token, true), res, next); + expect(mocks.checkPlatformBan).toHaveBeenLastCalledWith(AUTHENTICATED_ID); + expect(res.status).toHaveBeenCalledWith(403); + expect(next).not.toHaveBeenCalled(); + }); +}); - expect(mocks.authenticate).toHaveBeenCalledTimes(2); - expect(optionalNext).toHaveBeenCalledTimes(1); - expect(optionalRequest.user).toBeUndefined(); +describe.each([['required', requireAuth], ['optional', optionalAuth]] as const)('%s concurrent bearer hydration', (_label, middleware) => { + it('keeps overlapping cached JWT requests bound to the original authenticated credential', async () => { + const token = `header.overlapping${++sequence}.signature`; + await middleware(request(token, true), response(), vi.fn()); + + let resolveFirst!: (value: AuthorizationSnapshot) => void; + let resolveSecond!: (value: AuthorizationSnapshot) => void; + let markFirstStarted!: () => void; + let markSecondStarted!: () => void; + const firstStarted = new Promise((resolve) => { markFirstStarted = resolve; }); + const secondStarted = new Promise((resolve) => { markSecondStarted = resolve; }); + const firstSnapshot = snapshot({ canonicalUserId: 'user_primary_a' }); + const secondSnapshot = snapshot({ canonicalUserId: 'user_primary_b', authorizationEpoch: '2' }); + mocks.loadAuthorizationSnapshot + .mockImplementationOnce(() => { + markFirstStarted(); + return new Promise((resolve) => { resolveFirst = resolve; }); + }) + .mockImplementationOnce(() => { + markSecondStarted(); + return new Promise((resolve) => { resolveSecond = resolve; }); + }) + .mockResolvedValue(secondSnapshot); + + const first = request(token, true); + const second = request(token, true); + const firstNext = vi.fn(); + const secondNext = vi.fn(); + const firstPending = middleware(first, response(), firstNext); + const secondPending = middleware(second, response(), secondNext); + await Promise.all([firstStarted, secondStarted]); + expect(mocks.loadAuthorizationSnapshot).toHaveBeenNthCalledWith(2, AUTHENTICATED_ID, null); + expect(mocks.loadAuthorizationSnapshot).toHaveBeenNthCalledWith(3, AUTHENTICATED_ID, null); + + resolveFirst(firstSnapshot); + await firstPending; + const firstUser = first.user; + expect(firstUser).toMatchObject({ id: 'user_primary_a', authWorkosUserId: AUTHENTICATED_ID }); + expect(firstUser?.authorizationSnapshot).toBe(firstSnapshot); + expect(secondNext).not.toHaveBeenCalled(); + + resolveSecond(secondSnapshot); + await secondPending; + expect(firstNext).toHaveBeenCalledOnce(); + expect(secondNext).toHaveBeenCalledOnce(); + expect(second.user).not.toBe(firstUser); + expect(second.user).toMatchObject({ id: 'user_primary_b', authWorkosUserId: AUTHENTICATED_ID }); + expect(second.user?.authorizationSnapshot).toBe(secondSnapshot); + expect(first.user).toBe(firstUser); + expect(first.user).toMatchObject({ id: 'user_primary_a', authWorkosUserId: AUTHENTICATED_ID }); + expect(first.user?.authorizationSnapshot).toBe(firstSnapshot); + + const following = request(token, true); + await middleware(following, response(), vi.fn()); + expect(mocks.loadAuthorizationSnapshot).toHaveBeenNthCalledWith(4, AUTHENTICATED_ID, null); + expect(following.user?.authorizationSnapshot?.authenticatedUserId).toBe(AUTHENTICATED_ID); + expect(following.user?.authWorkosUserId).toBe(AUTHENTICATED_ID); + expect(mocks.verifyWorkOSJWT).toHaveBeenCalledTimes(1); }); }); diff --git a/server/tests/unit/resolve-user-org-authorization.test.ts b/server/tests/unit/resolve-user-org-authorization.test.ts index 8d4aa180609..75912943f00 100644 --- a/server/tests/unit/resolve-user-org-authorization.test.ts +++ b/server/tests/unit/resolve-user-org-authorization.test.ts @@ -1,159 +1,197 @@ -import { beforeEach, describe, expect, it, vi } from "vitest"; -import type { WorkOS } from "@workos-inc/node"; - -const queryMock = vi.hoisted(() => vi.fn()); - -vi.mock("../../src/db/client.js", () => ({ query: queryMock })); - -import { - evaluateUserOrgRoleAuthorization, - resolveUserOrgAuthorization, -} from "../../src/utils/resolve-user-org-authorization.js"; - -const ORGANIZATION_ID = "org_authorization_test"; - -function workosWithMemberships(data: unknown[]): WorkOS { +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import type { WorkOS } from '@workos-inc/node'; +import type { AuthorizationSnapshot } from '../../src/db/user-authorization-snapshot-db.js'; + +const snapshotMock = vi.hoisted(() => vi.fn()); +vi.mock('../../src/db/user-authorization-snapshot-db.js', async (importOriginal) => ({ + ...await importOriginal(), + loadAuthorizationSnapshot: snapshotMock, +})); +import { evaluateUserOrgRoleAuthorization, resolveUserOrgAuthorization } from '../../src/utils/resolve-user-org-authorization.js'; + +const ORGANIZATION_ID = 'org_pinnacle'; +const AUTHENTICATED_ID = 'user_authenticated'; +function snapshot(overrides: Partial = {}): AuthorizationSnapshot { + return { + authenticatedUserId: AUTHENTICATED_ID, canonicalUserId: 'user_canonical', + identityId: 'identity_linked', selectedOrganizationId: ORGANIZATION_ID, authorizationEpoch: '1', + credential: { email: 'sam@example.test', firstName: 'Sam', lastName: 'Adeyemi', emailVerified: true }, + credentialGrant: null, ...overrides, + }; +} +function grant(role: 'owner' | 'admin' | 'member' = 'member'): NonNullable { return { - userManagement: { - listOrganizationMemberships: vi.fn().mockResolvedValue({ data }), - }, + id: 'grant_pinnacle', organizationId: ORGANIZATION_ID, role, + effectiveFrom: '2026-01-01T00:00:00.000000Z', effectiveUntil: null, + }; +} +function principal(authorizationSnapshot?: AuthorizationSnapshot) { + return { id: 'user_canonical', authWorkosUserId: AUTHENTICATED_ID, authorizationSnapshot }; +} +function workosWithMemberships(data: unknown[] = []): WorkOS { + return { + userManagement: { listOrganizationMemberships: vi.fn().mockResolvedValue({ data }) }, } as unknown as WorkOS; } +function membership(organizationId = ORGANIZATION_ID, role = 'member', userId = AUTHENTICATED_ID) { + return { userId, organizationId, status: 'active', role: { slug: role } }; +} +const FORBIDDEN = { status: 'forbidden', complete: true, unavailableSources: [] }; +const SNAPSHOT_UNAVAILABLE = { status: 'unavailable', complete: false, unavailableSources: ['authorization_snapshot'] }; -describe("resolveUserOrgAuthorization", () => { +describe('resolveUserOrgAuthorization', () => { beforeEach(() => { - queryMock.mockReset(); - queryMock.mockResolvedValue({ rows: [] }); + snapshotMock.mockReset(); + snapshotMock.mockImplementation(async (id: string, org: string | null) => snapshot({ + authenticatedUserId: id, selectedOrganizationId: org, + })); }); - - it("uses the exact authenticated credential rather than the canonical identity user", async () => { - const workos = workosWithMemberships([ - { - organizationId: ORGANIZATION_ID, - status: "active", - role: { slug: "member" }, - }, - ]); - - await resolveUserOrgAuthorization( - workos, - { id: "user_canonical", authWorkosUserId: "user_authenticated" }, - ORGANIZATION_ID - ); - - expect( - workos.userManagement.listOrganizationMemberships - ).toHaveBeenCalledWith({ - userId: "user_authenticated", - organizationId: ORGANIZATION_ID, + it.each([ + ['secondary credential', 'user_canonical', AUTHENTICATED_ID], + ['primary credential', AUTHENTICATED_ID, undefined], + ])('uses exact %s authority independently of canonical attribution', async (_label, id, authWorkosUserId) => { + const workos = workosWithMemberships([membership()]); + const result = await resolveUserOrgAuthorization(workos, { id, authWorkosUserId }, ORGANIZATION_ID); + expect(result).toMatchObject({ status: 'authorized', membership: { source: 'workos' } }); + expect(workos.userManagement.listOrganizationMemberships).toHaveBeenCalledExactlyOnceWith({ + userId: AUTHENTICATED_ID, organizationId: ORGANIZATION_ID, }); - expect(queryMock).toHaveBeenCalledWith(expect.any(String), [ - "user_authenticated", - ORGANIZATION_ID, - ]); + expect(snapshotMock).toHaveBeenCalledTimes(2); + expect(snapshotMock).toHaveBeenNthCalledWith(1, AUTHENTICATED_ID, ORGANIZATION_ID); + expect(snapshotMock).toHaveBeenNthCalledWith(2, AUTHENTICATED_ID, ORGANIZATION_ID); }); - - it("returns a definitive denial only when both authority sources are available", async () => { - const result = await resolveUserOrgAuthorization( - workosWithMemberships([]), - { id: "user_without_access" }, - ORGANIZATION_ID - ); - - expect(result).toEqual({ - status: "forbidden", - complete: true, - unavailableSources: [], - }); + it('returns a definitive denial only when both authority sources are available', async () => { + expect(await resolveUserOrgAuthorization(workosWithMemberships(), principal(), ORGANIZATION_ID)).toEqual(FORBIDDEN); }); - - it("distinguishes an authority-source outage from a denial", async () => { - queryMock.mockRejectedValue(new Error("database unavailable")); - - const result = await resolveUserOrgAuthorization( - workosWithMemberships([]), - { id: "user_unknown" }, - ORGANIZATION_ID - ); - - expect(result).toEqual({ - status: "unavailable", - complete: false, - unavailableSources: ["credential_grant"], - }); - expect(evaluateUserOrgRoleAuthorization(result)).toEqual({ - status: "unavailable", - unavailableSources: ["credential_grant"], - }); + it.each(['', ' '])('never chooses an implicit organization for missing selection %j', async (org) => { + const workos = workosWithMemberships([membership()]); + expect(await resolveUserOrgAuthorization(workos, principal(), org)).toEqual(FORBIDDEN); + expect(workos.userManagement.listOrganizationMemberships).not.toHaveBeenCalled(); + expect(snapshotMock).not.toHaveBeenCalled(); }); - - it("uses an active exact-credential grant and preserves partial role uncertainty", async () => { - queryMock.mockResolvedValue({ - rows: [{ workos_organization_id: ORGANIZATION_ID, role: "member" }], + it.each([ + ['another linked credential', membership(ORGANIZATION_ID, 'owner', 'user_canonical')], + ['another organization', membership('org_streamhaus', 'owner')], + ])('does not inherit membership from %s', async (_label, otherMembership) => { + const workos = workosWithMemberships([otherMembership]); + expect(await resolveUserOrgAuthorization(workos, principal(), ORGANIZATION_ID)).toEqual(FORBIDDEN); + expect(workos.userManagement.listOrganizationMemberships).toHaveBeenCalledWith({ + userId: AUTHENTICATED_ID, organizationId: ORGANIZATION_ID, }); - const workos = { - userManagement: { - listOrganizationMemberships: vi - .fn() - .mockRejectedValue(new Error("WorkOS unavailable")), - }, - } as unknown as WorkOS; - + }); + it('rejects replaying a selected organization snapshot against another organization', async () => { + const workos = workosWithMemberships([membership('org_streamhaus')]); + const result = await resolveUserOrgAuthorization(workos, principal(snapshot()), 'org_streamhaus'); + expect(result).toEqual(FORBIDDEN); + expect(workos.userManagement.listOrganizationMemberships).not.toHaveBeenCalled(); + }); + it('binds an unselected authenticated snapshot to an explicit organization using fresh authority', async () => { const result = await resolveUserOrgAuthorization( - workos, - { id: "user_granted" }, - ORGANIZATION_ID + workosWithMemberships([membership('org_streamhaus')]), + principal(snapshot({ selectedOrganizationId: null })), 'org_streamhaus', ); - + expect(result).toMatchObject({ status: 'authorized', membership: { organizationId: 'org_streamhaus', source: 'workos' } }); + }); + it('distinguishes snapshot outage from denial without falling back to valid provider membership', async () => { + snapshotMock.mockRejectedValue(new Error('database unavailable')); + const workos = workosWithMemberships([membership(ORGANIZATION_ID, 'owner')]); + const result = await resolveUserOrgAuthorization(workos, principal(), ORGANIZATION_ID); + expect(result).toEqual(SNAPSHOT_UNAVAILABLE); + expect(evaluateUserOrgRoleAuthorization(result)).toEqual({ status: 'unavailable', unavailableSources: ['authorization_snapshot'] }); + expect(workos.userManagement.listOrganizationMemberships).not.toHaveBeenCalled(); + }); + it('fails unavailable if the post-provider primary revalidation fails', async () => { + snapshotMock.mockResolvedValueOnce(snapshot({ credentialGrant: grant('owner') })) + .mockRejectedValueOnce(new Error('database unavailable')); + const result = await resolveUserOrgAuthorization(workosWithMemberships([membership()]), principal(), ORGANIZATION_ID); + expect(result).toEqual(SNAPSHOT_UNAVAILABLE); + }); + it('uses an active exact-credential grant and preserves partial role uncertainty', async () => { + snapshotMock.mockResolvedValue(snapshot({ credentialGrant: grant() })); + const workos = workosWithMemberships(); + vi.mocked(workos.userManagement.listOrganizationMemberships).mockRejectedValue(new Error('WorkOS unavailable')); + const result = await resolveUserOrgAuthorization(workos, principal(), ORGANIZATION_ID); expect(result).toMatchObject({ - status: "authorized", - complete: false, - unavailableSources: ["workos"], - membership: { role: "member", source: "credential_grant" }, - }); - expect(evaluateUserOrgRoleAuthorization(result, "member")).toMatchObject({ - status: "authorized", - membership: { role: "member" }, + status: 'authorized', complete: false, unavailableSources: ['workos'], + membership: { role: 'member', source: 'credential_grant' }, }); - expect(evaluateUserOrgRoleAuthorization(result, "admin")).toEqual({ - status: "unavailable", - unavailableSources: ["workos"], + expect(evaluateUserOrgRoleAuthorization(result, 'member')).toMatchObject({ status: 'authorized' }); + expect(evaluateUserOrgRoleAuthorization(result, 'admin')).toEqual({ status: 'unavailable', unavailableSources: ['workos'] }); + }); + it('returns unavailable without an independent grant when WorkOS cannot answer', async () => { + expect(await resolveUserOrgAuthorization(null, principal(), ORGANIZATION_ID)).toEqual({ + status: 'unavailable', complete: false, unavailableSources: ['workos'], }); }); - - it("definitively denies a complete exact membership below the required role", () => { + it('definitively denies a complete exact membership below the required role', () => { expect(evaluateUserOrgRoleAuthorization({ - status: "authorized", - membership: { - organizationId: ORGANIZATION_ID, - role: "member", - source: "workos", - }, - complete: true, - unavailableSources: [], - }, "admin")).toEqual({ status: "forbidden" }); + status: 'authorized', complete: true, unavailableSources: [], + membership: { organizationId: ORGANIZATION_ID, role: 'member', source: 'workos' }, + }, 'admin')).toEqual({ status: 'forbidden' }); }); - - it("chooses the highest role while retaining its authority source", async () => { - queryMock.mockResolvedValue({ - rows: [{ workos_organization_id: ORGANIZATION_ID, role: "owner" }], - }); - const result = await resolveUserOrgAuthorization( - workosWithMemberships([ - { - organizationId: ORGANIZATION_ID, - status: "active", - role: { slug: "admin" }, - }, - ]), - { id: "user_with_two_sources" }, - ORGANIZATION_ID - ); - - expect(result).toMatchObject({ - status: "authorized", - complete: true, - membership: { role: "owner", source: "credential_grant" }, + it('chooses the highest role while retaining its authority source', async () => { + snapshotMock.mockResolvedValue(snapshot({ credentialGrant: grant('owner') })); + const result = await resolveUserOrgAuthorization(workosWithMemberships([membership(ORGANIZATION_ID, 'admin')]), principal(), ORGANIZATION_ID); + expect(result).toMatchObject({ status: 'authorized', complete: true, membership: { role: 'owner', source: 'credential_grant' } }); + }); + it.each([ + ['epoch advanced', { authorizationEpoch: '2' }], + ['epoch moved backwards', { authorizationEpoch: '0' }], + ['canonical identity changed', { canonicalUserId: 'user_new_primary' }], + ['binding changed without a matching epoch', { identityId: 'identity_new' }], + ['credential changed', { authenticatedUserId: 'user_other' }], + ] as const)('rejects replayed request state when %s', async (_case, change) => { + const workos = workosWithMemberships([membership()]); + snapshotMock.mockResolvedValue(snapshot(change)); + expect(await resolveUserOrgAuthorization(workos, principal(snapshot()), ORGANIZATION_ID)).toEqual(FORBIDDEN); + expect(workos.userManagement.listOrganizationMemberships).not.toHaveBeenCalled(); + }); + it.each([ + ['revoked', null], ['replaced', { ...grant('owner'), id: 'grant_replacement' }], + ])('rejects a %s grant in prior request state even when the epoch is unchanged', async (_label, currentGrant) => { + snapshotMock.mockResolvedValue(snapshot({ credentialGrant: currentGrant })); + const workos = workosWithMemberships([membership(ORGANIZATION_ID, 'owner')]); + const result = await resolveUserOrgAuthorization(workos, principal(snapshot({ credentialGrant: grant('owner') })), ORGANIZATION_ID); + expect(result).toEqual(FORBIDDEN); + expect(workos.userManagement.listOrganizationMemberships).not.toHaveBeenCalled(); + }); + it.each([ + ['identity binding', { canonicalUserId: 'user_new_primary', identityId: 'identity_new', authorizationEpoch: '2' }], + ['epoch alone', { authorizationEpoch: '2' }], + ['grant revoked', { credentialGrant: null }], + ['grant role', { credentialGrant: grant('member') }], + ['grant replaced', { credentialGrant: { ...grant('owner'), id: 'grant_replacement' } }], + ['grant end date', { credentialGrant: { ...grant('owner'), effectiveUntil: '2026-09-13T00:00:00.000000Z' } }], + ['selected organization', { selectedOrganizationId: 'org_streamhaus' }], + ] as const)('rejects a %s change while a provider decision is in flight', async (_case, change) => { + const before = snapshot({ credentialGrant: grant('owner') }); + let current = before; + snapshotMock.mockImplementation(async () => current); + let finishProvider!: (value: { data: ReturnType[] }) => void; + let providerStarted!: () => void; + const started = new Promise((resolve) => { providerStarted = resolve; }); + const workos = workosWithMemberships(); + vi.mocked(workos.userManagement.listOrganizationMemberships).mockImplementation(() => { + providerStarted(); + return new Promise((resolve) => { finishProvider = resolve as typeof finishProvider; }); }); + const pending = resolveUserOrgAuthorization(workos, principal(before), ORGANIZATION_ID); + await started; + current = snapshot({ ...before, ...change }); + finishProvider({ data: [membership(ORGANIZATION_ID, 'owner')] }); + expect(await pending).toEqual(FORBIDDEN); + expect(snapshotMock).toHaveBeenCalledTimes(2); + }); + it('rejects a credential deleted during provider authorization', async () => { + snapshotMock.mockResolvedValueOnce(snapshot()).mockResolvedValueOnce(null); + expect(await resolveUserOrgAuthorization(workosWithMemberships([membership()]), principal(), ORGANIZATION_ID)).toEqual(FORBIDDEN); + }); + it('checks persisted epoch each turn and rejects replayed prior context', async () => { + const before = snapshot(); + const workos = workosWithMemberships([membership()]); + expect(await resolveUserOrgAuthorization(workos, principal(before), ORGANIZATION_ID)).toMatchObject({ status: 'authorized' }); + snapshotMock.mockResolvedValue(snapshot({ authorizationEpoch: '2' })); + expect(await resolveUserOrgAuthorization(workos, principal(before), ORGANIZATION_ID)).toEqual(FORBIDDEN); + expect(workos.userManagement.listOrganizationMemberships).toHaveBeenCalledTimes(1); }); }); diff --git a/server/tests/unit/working-group-admin-global-auth.test.ts b/server/tests/unit/working-group-admin-global-auth.test.ts index 7c2f4eff7b4..978dbb51bb9 100644 --- a/server/tests/unit/working-group-admin-global-auth.test.ts +++ b/server/tests/unit/working-group-admin-global-auth.test.ts @@ -159,9 +159,17 @@ describe('working-group real global-admin boundary', () => { mocks.getWorkingGroupIdBySlug.mockResolvedValue('wg_aao_admin'); mocks.isMember.mockResolvedValue(true); mocks.poolQuery.mockImplementation((sql: string) => { - if (sql.includes('FROM users')) { + if (sql.includes('pg_catalog.pg_is_in_recovery()')) { return Promise.resolve({ - rows: [{ first_name: 'SSO', last_name: 'Admin' }], + rows: [{ + in_recovery: false, + authenticated_user_id: 'user_sso_admin', + canonical_user_id: 'user_sso_admin', + identity_id: 'identity_sso_admin', + authorization_epoch: '0', + email: 'sso-admin@example.test', email_verified: true, + first_name: 'SSO', last_name: 'Admin', grant_id: null, + }], rowCount: 1, }); } From 2cabb2d5dc4824bb0ae9d04c7228dd3a1d30ce0b Mon Sep 17 00:00:00 2001 From: Brian O'Kelley Date: Sun, 13 Sep 2026 14:30:21 +0000 Subject: [PATCH 2/7] fix(auth): bound snapshots and reject conflicting credentials --- .../src/db/user-authorization-snapshot-db.ts | 28 +- server/src/middleware/auth.ts | 78 +++-- .../user-authorization-snapshot.test.ts | 36 ++- .../unit/addie-admin-global-auth.test.ts | 1 + .../unit/authorization-epoch-session.test.ts | 273 +++++++++++++++++- .../unit/network-health-security.test.ts | 24 +- ...ser-authorization-snapshot-timeout.test.ts | 125 ++++++++ .../working-group-admin-global-auth.test.ts | 1 + 8 files changed, 527 insertions(+), 39 deletions(-) create mode 100644 server/tests/unit/user-authorization-snapshot-timeout.test.ts diff --git a/server/src/db/user-authorization-snapshot-db.ts b/server/src/db/user-authorization-snapshot-db.ts index 74d32facc08..d6a4f7cda30 100644 --- a/server/src/db/user-authorization-snapshot-db.ts +++ b/server/src/db/user-authorization-snapshot-db.ts @@ -1,4 +1,6 @@ -import { getPool } from './client.js'; +import { isTransientConnectionError, queryWithTimeout } from './client.js'; + +const SNAPSHOT_TIMEOUT_MS = 2_000; /** The exact credential and its authority, observed in one primary-database statement. */ export interface AuthorizationSnapshot { @@ -48,11 +50,29 @@ interface SnapshotRow { grant_effective_until: string | null; } +/** A retry may replace a broken connection, never renew the statement budget. */ +async function querySnapshot(statement: string, parameters: [string, string | null]) { + const deadlineMs = Date.now() + SNAPSHOT_TIMEOUT_MS; + for (let attempt = 0; attempt < 2; attempt++) { + const remainingMs = deadlineMs - Date.now(); + if (remainingMs <= 0) throw new AuthorizationSnapshotUnavailableError(); + try { + const result = await queryWithTimeout(statement, parameters, remainingMs); + if (Date.now() >= deadlineMs) throw new AuthorizationSnapshotUnavailableError(); + return result; + } catch (error) { + if (attempt === 1 || !isTransientConnectionError(error) || Date.now() >= deadlineMs) throw error; + } + } + throw new AuthorizationSnapshotUnavailableError(); +} + /** * Never hydrate identity and epoch separately: a binding change can commit * between the reads and stamp old attribution with the new epoch. One SELECT - * gives every field the same PostgreSQL MVCC snapshot. The pool is the primary - * database; a connection to a recovering replica fails closed in this statement. + * gives every field the same PostgreSQL MVCC snapshot. The bounded query wrapper + * uses the primary pool with transaction-local statement and lock timeouts; + * a recovering replica fails closed in this statement. * * Organization selection must be explicit. Neither linked credentials nor the * organization_memberships cache supplies authority on a cold start/cache miss. @@ -63,7 +83,7 @@ export async function loadAuthorizationSnapshot( ): Promise { const organizationId = selectedOrganizationId || null; try { - const result = await getPool().query( + const result = await querySnapshot( `SELECT pg_catalog.pg_is_in_recovery() AS in_recovery, credential.workos_user_id AS authenticated_user_id, primary_binding.workos_user_id AS canonical_user_id, diff --git a/server/src/middleware/auth.ts b/server/src/middleware/auth.ts index d6df5c6d0bf..ac40f881a5c 100644 --- a/server/src/middleware/auth.ts +++ b/server/src/middleware/auth.ts @@ -182,6 +182,14 @@ function isTransientAuthError(err: unknown): boolean { if (!(err instanceof Error)) return false; const name = err.name; const code = (err as { code?: unknown }).code; + const status = (err as { status?: unknown; statusCode?: unknown }).status + ?? (err as { statusCode?: unknown }).statusCode; + if (typeof status === 'number' && (status === 408 || status === 429 || (status >= 500 && status < 600))) return true; + // jose does not retain the HTTP status when the remote JWKS endpoint fails. + // This exact error denotes key-service failure, not token signature failure. + if (name === 'JOSEError' && code === 'ERR_JOSE_GENERIC' + && (err.message === 'Expected 200 OK from the JSON Web Key Set HTTP response' + || err.message === 'Failed to parse the JSON Web Key Set HTTP response as JSON')) return true; if (name === 'JWKSTimeout' || name === 'TimeoutError' || name === 'AbortError') return true; if (typeof code === 'string') { if (code === 'ERR_JWKS_TIMEOUT') return true; @@ -364,6 +372,7 @@ export async function validateWorkOSBearerJWT(req: Request): Promise value !== undefined && value !== null); + ].filter((value) => value !== undefined); if (selectors.some((value) => typeof value !== 'string' || !value.trim())) { throw new ConflictingOrganizationSelectionError(); } const ids = new Set(selectors.map((value) => (value as string).trim())); if (ids.size > 1) throw new ConflictingOrganizationSelectionError(); - return ids.values().next().value ?? providerOrg ?? null; + return ids.values().next().value ?? null; } /** Hydrate a new request object; never mutate the provider object in a cache. */ @@ -746,7 +757,12 @@ async function hydrateDevUser(user: WorkOSUser): Promise { } } -function sendAuthorizationStateError(error: unknown, res: Response): boolean { +function sendAuthorizationStateError(error: unknown, res: Response, includeTransientErrors = false): boolean { + if (includeTransientErrors && isTransientAuthError(error)) { + logger.warn({ err: error }, 'Authentication service unavailable'); + res.status(503).json({ error: 'Authentication service temporarily unavailable' }); + return true; + } if (error instanceof AuthorizationSnapshotUnavailableError) { logger.warn({ err: error }, 'Primary authorization snapshot unavailable'); res.status(503).json({ error: 'Authorization service temporarily unavailable' }); @@ -821,7 +837,7 @@ export async function requireAuth(req: Request, res: Response, next: NextFunctio try { jwtAuth = await validateWorkOSBearerJWT(req); } catch (error) { - if (sendAuthorizationStateError(error, res)) return; + if (sendAuthorizationStateError(error, res, true)) return; throw error; } if (jwtAuth) { @@ -1764,29 +1780,26 @@ export function createRequireWorkingGroupMember( * Native apps send: Authorization: Bearer */ function extractSealedSession(req: Request): string | undefined { - // First check for cookie (web browsers) - const sessionCookie = req.cookies['wos-session']; - if (sessionCookie) { - return sessionCookie; - } - - // Then check Authorization header (native apps like Tauri) + // An explicitly supplied bearer credential takes precedence over a cookie. + // A failed JWT must never be retried as a sealed session or replaced by a cookie. // Format: Authorization: Bearer - // Note: We differentiate from WorkOS API keys by checking prefix const authHeader = req.headers.authorization; - if (authHeader?.startsWith('Bearer ')) { + if (authHeader !== undefined) { + if (!authHeader.startsWith('Bearer ')) return undefined; const token = authHeader.slice(7); - // WorkOS API keys use known prefixes, sealed sessions don't - if (!isWorkOSApiKeyFormat(token)) { + if (token.trim() && !isWorkOSApiKeyFormat(token) && !looksLikeJWT(token)) { return token; } + return undefined; } - - return undefined; + const sessionCookie = req.cookies?.['wos-session']; + return typeof sessionCookie === 'string' && sessionCookie ? sessionCookie : undefined; } /** - * Optional auth middleware - loads user if authenticated, but doesn't require it + * Optional auth permits anonymous requests only when no credential is supplied. + * Invalid presented credentials return 401; verification or authorization + * outages return 503. Neither case falls back to anonymous or another credential. * Uses in-memory cache to reduce WorkOS API calls for session refresh * Automatically refreshes expired access tokens using the refresh token * Supports both cookie-based auth (web) and Authorization header (native apps) @@ -1831,7 +1844,7 @@ export async function optionalAuth(req: Request, res: Response, next: NextFuncti try { jwtAuth = await validateWorkOSBearerJWT(req); } catch (error) { - if (sendAuthorizationStateError(error, res)) return; + if (sendAuthorizationStateError(error, res, true)) return; throw error; } if (jwtAuth) { @@ -1866,14 +1879,16 @@ export async function optionalAuth(req: Request, res: Response, next: NextFuncti if (devConfig) { (req.user as unknown as Record).isMember = devConfig.isMember; } + return next(); } - // No dev session = not logged in (which is fine for optional auth) - return next(); } const sessionCookie = extractSealedSession(req); if (!sessionCookie) { + if (req.headers.authorization !== undefined || req.cookies?.['wos-session'] !== undefined) { + return res.status(401).json({ error: 'Invalid session', login_url: '/auth/login' }); + } return next(); } @@ -1887,7 +1902,7 @@ export async function optionalAuth(req: Request, res: Response, next: NextFuncti const deadAt = deadSessionCache.get(cacheKey); if (deadAt) { if (now - deadAt < DEAD_SESSION_TTL_MS) { - return next(); // optional auth: just proceed without user + return res.status(401).json({ error: 'Invalid session', login_url: '/auth/login' }); } deadSessionCache.delete(cacheKey); } @@ -1928,6 +1943,9 @@ export async function optionalAuth(req: Request, res: Response, next: NextFuncti cookiePassword: WORKOS_COOKIE_PASSWORD, }); + if (!refreshResult.authenticated && 'retryable' in refreshResult && refreshResult.retryable) { + return res.status(503).json({ error: 'Authentication service temporarily unavailable' }); + } if (refreshResult.authenticated && refreshResult.sealedSession) { logger.debug('Session refreshed successfully (optional auth)'); newSealedSession = refreshResult.sealedSession; @@ -1945,6 +1963,7 @@ export async function optionalAuth(req: Request, res: Response, next: NextFuncti refreshFailed = true; } } catch (refreshError) { + if (isTransientAuthError(refreshError)) throw refreshError; logger.debug({ err: refreshError }, 'Optional auth refresh failed'); refreshFailed = true; } @@ -1966,6 +1985,9 @@ export async function optionalAuth(req: Request, res: Response, next: NextFuncti const sharedRefresh = await sharedSessionObj.refresh({ cookiePassword: WORKOS_COOKIE_PASSWORD, }); + if (!sharedRefresh.authenticated && 'retryable' in sharedRefresh && sharedRefresh.retryable) { + return res.status(503).json({ error: 'Authentication service temporarily unavailable' }); + } if (sharedRefresh.authenticated && sharedRefresh.sealedSession) { newSealedSession = sharedRefresh.sealedSession; setSessionCookie(res, sharedRefresh.sealedSession); @@ -1979,6 +2001,7 @@ export async function optionalAuth(req: Request, res: Response, next: NextFuncti result = await refreshedObj.authenticate(); } } catch (innerRefreshErr) { + if (isTransientAuthError(innerRefreshErr)) throw innerRefreshErr; logger.debug({ err: innerRefreshErr }, 'Shared session refresh also failed (optional auth)'); } } else { @@ -1988,6 +2011,7 @@ export async function optionalAuth(req: Request, res: Response, next: NextFuncti } } catch (dbError) { logger.debug({ err: dbError }, 'Failed to look up shared session (optional auth)'); + return res.status(503).json({ error: 'Authentication service temporarily unavailable' }); } } } @@ -2001,6 +2025,8 @@ export async function optionalAuth(req: Request, res: Response, next: NextFuncti if (oldest) deadSessionCache.delete(oldest); } deadSessionCache.set(cacheKey, Date.now()); + sessionCache.delete(cacheKey); + return res.status(401).json({ error: 'Invalid session', login_url: '/auth/login' }); } if (result.authenticated && 'user' in result && result.user) { @@ -2045,9 +2071,9 @@ export async function optionalAuth(req: Request, res: Response, next: NextFuncti req.accessToken = result.accessToken; } } catch (error) { - if (sendAuthorizationStateError(error, res)) return; - // Silently fail for optional auth + if (sendAuthorizationStateError(error, res, true)) return; logger.debug({ err: error }, 'Optional auth failed'); + return res.status(401).json({ error: 'Invalid session', login_url: '/auth/login' }); } next(); diff --git a/server/tests/integration/user-authorization-snapshot.test.ts b/server/tests/integration/user-authorization-snapshot.test.ts index 52e4d037e17..dd00ef27621 100644 --- a/server/tests/integration/user-authorization-snapshot.test.ts +++ b/server/tests/integration/user-authorization-snapshot.test.ts @@ -1,6 +1,7 @@ import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; import type { Pool, PoolClient } from 'pg'; import { closeDatabase, initializeDatabase } from '../../src/db/client.js'; +import * as database from '../../src/db/client.js'; import { runMigrations } from '../../src/db/migrate.js'; import { AuthorizationSnapshotUnavailableError, @@ -250,7 +251,7 @@ describe('primary database authorization snapshots', () => { const readBarrier = new Promise(resolve => { readCompleted = resolve; }); const deliveryBarrier = new Promise(resolve => { releaseRead = resolve; }); let pendingRead: Promise | undefined; - const actualQuery = pool.query.bind(pool); + const actualQuery = database.queryWithTimeout; try { await writer.query('BEGIN'); await link(primaryId, secondaryId, writer); @@ -262,8 +263,8 @@ describe('primary database authorization snapshots', () => { // Hold delivery of the first real SQL result while the writer commits. // A split identity/epoch loader would then issue its epoch query against // the new commit and return old identity + new epoch. No rows are mocked. - const readSpy = vi.spyOn(pool, 'query').mockImplementationOnce(async (...args: any[]) => { - const result = await (actualQuery as (...queryArgs: any[]) => Promise)(...args); + const readSpy = vi.spyOn(database, 'queryWithTimeout').mockImplementationOnce(async (...args) => { + const result = await actualQuery(...args); readCompleted(); await deliveryBarrier; return result; @@ -314,7 +315,7 @@ describe('primary database authorization snapshots', () => { it('fails unavailable on database errors instead of replaying a warm snapshot', async () => { await snapshot(PERSONAL_ID, PERSONAL_ORG); - vi.spyOn(pool, 'query').mockRejectedValueOnce(new Error('private database connection details')); + vi.spyOn(database, 'queryWithTimeout').mockRejectedValueOnce(new Error('private database connection details')); await expect(loadAuthorizationSnapshot(PERSONAL_ID, PERSONAL_ORG)) .rejects.toThrow(new AuthorizationSnapshotUnavailableError()); }); @@ -326,7 +327,7 @@ describe('primary database authorization snapshots', () => { const primary = await snapshot(PERSONAL_ID, PERSONAL_ORG); // The integration database is a primary. Simulate only the single query // result from a recovering replica, including its no-user anchor row. - const readSpy = vi.spyOn(pool, 'query').mockResolvedValueOnce({ + const readSpy = vi.spyOn(database, 'queryWithTimeout').mockResolvedValueOnce({ rows: [{ in_recovery: true, authenticated_user_id: hasUser ? primary.authenticatedUserId : null, @@ -351,4 +352,29 @@ describe('primary database authorization snapshots', () => { .rejects.toBeInstanceOf(AuthorizationSnapshotUnavailableError); expect(readSpy).toHaveBeenCalledTimes(1); }); + + it('expires a blocked statement, releases its transaction, and loads fresh authority after recovery', async () => { + const before = await snapshot(PERSONAL_ID, PERSONAL_ORG); + const writer = await pool.connect(); + const boundedQuery = vi.spyOn(database, 'queryWithTimeout'); + try { + await writer.query('BEGIN'); + await writer.query('LOCK TABLE identity_workos_users IN ACCESS EXCLUSIVE MODE'); + const startedAt = Date.now(); + await expect(loadAuthorizationSnapshot(PERSONAL_ID, PERSONAL_ORG)) + .rejects.toBeInstanceOf(AuthorizationSnapshotUnavailableError); + expect(Date.now() - startedAt).toBeLessThan(5_000); + expect(boundedQuery).toHaveBeenCalledTimes(1); + + await bumpAuthorizationEpochs(writer, [PERSONAL_ID]); + await writer.query('COMMIT'); + const after = await snapshot(PERSONAL_ID, PERSONAL_ORG); + expect(after.authorizationEpoch).toBe('1'); + expect(sameAuthorizationSnapshot(before, after)).toBe(false); + expect(boundedQuery).toHaveBeenCalledTimes(2); + } finally { + await writer.query('ROLLBACK'); + writer.release(); + } + }); }); diff --git a/server/tests/unit/addie-admin-global-auth.test.ts b/server/tests/unit/addie-admin-global-auth.test.ts index 8ec2ff778c2..6e321777253 100644 --- a/server/tests/unit/addie-admin-global-auth.test.ts +++ b/server/tests/unit/addie-admin-global-auth.test.ts @@ -52,6 +52,7 @@ vi.mock('../../src/db/org-filters.js', () => ({ vi.mock('../../src/db/client.js', async (importOriginal) => ({ ...(await importOriginal()), getPool: () => ({ query: mocks.poolQuery }), + queryWithTimeout: mocks.poolQuery, })); vi.mock('../../src/db/authorization-epoch-db.js', async (importOriginal) => ({ diff --git a/server/tests/unit/authorization-epoch-session.test.ts b/server/tests/unit/authorization-epoch-session.test.ts index d648f7ed07f..93148d7e0a7 100644 --- a/server/tests/unit/authorization-epoch-session.test.ts +++ b/server/tests/unit/authorization-epoch-session.test.ts @@ -6,6 +6,7 @@ import type { AuthorizationSnapshot } from '../../src/db/user-authorization-snap const mocks = vi.hoisted(() => ({ authenticate: vi.fn(), loadSealedSession: vi.fn(), checkPlatformBan: vi.fn(), loadAuthorizationSnapshot: vi.fn(), verifyWorkOSJWT: vi.fn(), poolQuery: vi.fn(), + refresh: vi.fn(), getRefreshedSession: vi.fn(), })); vi.hoisted(() => { process.env.DEV_USER_EMAIL = ''; @@ -36,6 +37,11 @@ vi.mock('../../src/auth/workos-jwt.js', () => ({ vi.mock('../../src/db/client.js', () => ({ getPool: () => ({ query: mocks.poolQuery }), query: mocks.poolQuery, isDatabaseInitialized: () => true, })); +vi.mock('../../src/db/session-refresh-db.js', () => ({ + getRefreshedSession: mocks.getRefreshedSession, + storeRefreshedSession: vi.fn().mockResolvedValue(undefined), + cleanExpiredRefreshes: vi.fn().mockResolvedValue(0), +})); import { AuthorizationSnapshotUnavailableError } from '../../src/db/user-authorization-snapshot-db.js'; import { invalidateBanCache, optionalAuth, requireAuth, stopAuthTimers } from '../../src/middleware/auth.js'; @@ -74,9 +80,274 @@ beforeEach(() => { invalidateBanCache('user', AUTHENTICATED_ID); mocks.checkPlatformBan.mockResolvedValue({ banned: false }); mocks.authenticate.mockResolvedValue({ authenticated: true, user: { ...PROVIDER_USER }, accessToken: 'access-token' }); - mocks.loadSealedSession.mockReturnValue({ authenticate: mocks.authenticate, refresh: vi.fn() }); + mocks.refresh.mockReset().mockResolvedValue({ authenticated: false }); + mocks.getRefreshedSession.mockReset().mockResolvedValue(null); + mocks.loadSealedSession.mockReturnValue({ authenticate: mocks.authenticate, refresh: mocks.refresh }); mocks.verifyWorkOSJWT.mockResolvedValue({ sub: AUTHENTICATED_ID, email: PROVIDER_USER.email, isM2M: false }); }); + +const organizationSelectors: Array<[string, (req: Request, organizationId: string) => void]> = [ + ['header', (req, org) => { req.headers['x-organization-id'] = org; }], + ['query org', (req, org) => { req.query.org = org; }], + ['query organization_id', (req, org) => { req.query.organization_id = org; }], + ['body organization_id', (req, org) => { req.body.organization_id = org; }], + ['body organizationId', (req, org) => { req.body.organizationId = org; }], + ['path orgId', (req, org) => { req.params.orgId = org; }], + ['path organizationId', (req, org) => { req.params.organizationId = org; }], +]; + +describe.each([ + ['required cookie', requireAuth, false], ['optional cookie', optionalAuth, false], + ['required bearer', requireAuth, true], ['optional bearer', optionalAuth, true], +] as const)('%s provider organization binding', (_label, middleware, bearer) => { + beforeEach(() => { + mocks.authenticate.mockResolvedValue({ + authenticated: true, user: { ...PROVIDER_USER }, accessToken: 'access-token', organizationId: 'org_pinnacle', + }); + mocks.verifyWorkOSJWT.mockResolvedValue({ + sub: AUTHENTICATED_ID, email: PROVIDER_USER.email, isM2M: false, orgId: 'org_pinnacle', + }); + }); + + describe.each(['cold', 'warm'] as const)('%s cache', (cacheState) => { + it.each(organizationSelectors)('rejects a mismatched %s selector and permits a matching selector', async (_location, select) => { + for (const selectedOrg of ['org_streamhaus', 'org_pinnacle']) { + const token = bearer ? `header.org${++sequence}.signature` : `sealed-org-${++sequence}`; + if (cacheState === 'warm') await middleware(request(token, bearer), response(), vi.fn()); + const req = request(token, bearer); + select(req, selectedOrg); + const res = response(); + const next = vi.fn(); + const snapshotsBefore = mocks.loadAuthorizationSnapshot.mock.calls.length; + await middleware(req, res, next); + if (selectedOrg === 'org_streamhaus') { + expect(res.status).toHaveBeenCalledWith(403); + expect(next).not.toHaveBeenCalled(); + expect(req.user).toBeUndefined(); + expect(mocks.loadAuthorizationSnapshot).toHaveBeenCalledTimes(snapshotsBefore); + } else { + expect(next).toHaveBeenCalledOnce(); + expect(res.status).not.toHaveBeenCalled(); + expect(req.user?.authorizationSnapshot?.selectedOrganizationId).toBe('org_pinnacle'); + } + } + }); + }); + + it.each(['', ' ', null, [], {}])('rejects malformed supplied provider organization %j', async (providerOrganization) => { + mocks.authenticate.mockResolvedValue({ + authenticated: true, user: { ...PROVIDER_USER }, accessToken: 'access-token', organizationId: providerOrganization, + }); + mocks.verifyWorkOSJWT.mockResolvedValue({ + sub: AUTHENTICATED_ID, email: PROVIDER_USER.email, isM2M: false, orgId: providerOrganization, + }); + const token = bearer ? `header.malformed${++sequence}.signature` : `sealed-malformed-${++sequence}`; + const req = request(token, bearer); + req.headers['x-organization-id'] = 'org_pinnacle'; + const res = response(); + const next = vi.fn(); + await middleware(req, res, next); + expect(res.status).toHaveBeenCalledWith(403); + expect(next).not.toHaveBeenCalled(); + expect(mocks.loadAuthorizationSnapshot).not.toHaveBeenCalled(); + }); +}); + +describe('optional authentication credential presence', () => { + function expectRejected(req: Request, res: Response, next: ReturnType, status: number): void { + expect(res.status).toHaveBeenCalledWith(status); + expect(next).not.toHaveBeenCalled(); + expect(req.user).toBeUndefined(); + expect(res.redirect).not.toHaveBeenCalled(); + } + + it('permits anonymous access only when no credential is supplied', async () => { + const req = request('unused'); + req.cookies = {}; + const res = response(); + const next = vi.fn(); + await optionalAuth(req, res, next); + expect(next).toHaveBeenCalledOnce(); + expect(req.user).toBeUndefined(); + expect(res.status).not.toHaveBeenCalled(); + expect(mocks.loadSealedSession).not.toHaveBeenCalled(); + expect(mocks.loadAuthorizationSnapshot).not.toHaveBeenCalled(); + }); + + it.each([false, true])('rejects a failed JWT without anonymous or cookie fallback (cookie present: %s)', async (cookiePresent) => { + const req = request(`header.invalid${++sequence}.signature`, true); + if (cookiePresent) req.cookies['wos-session'] = 'otherwise-valid-cookie'; + mocks.verifyWorkOSJWT.mockRejectedValue(new Error('Invalid JWT signature')); + const res = response(); + const next = vi.fn(); + await optionalAuth(req, res, next); + expectRejected(req, res, next, 401); + expect(mocks.loadSealedSession).not.toHaveBeenCalled(); + expect(mocks.loadAuthorizationSnapshot).not.toHaveBeenCalled(); + }); + + it.each(['Bearer ', 'Bearer ', 'Basic invalid', ''])('rejects malformed authorization header %j without cookie fallback', async (header) => { + const req = request('otherwise-valid-cookie'); + req.headers.authorization = header; + const res = response(); + const next = vi.fn(); + await optionalAuth(req, res, next); + expectRejected(req, res, next, 401); + expect(mocks.loadSealedSession).not.toHaveBeenCalled(); + }); + + it('rejects an unrecognized bearer after sealed-session validation instead of using a valid cookie', async () => { + const token = `invalid-opaque-${++sequence}`; + const req = request(token, true); + req.cookies['wos-session'] = 'otherwise-valid-cookie'; + mocks.authenticate.mockResolvedValue({ authenticated: false }); + const res = response(); + const next = vi.fn(); + await optionalAuth(req, res, next); + expectRejected(req, res, next, 401); + expect(mocks.loadSealedSession).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ sessionData: token })); + }); + + it('continues to authenticate native sealed bearer sessions', async () => { + const token = `native-sealed-${++sequence}`; + const req = request(token, true); + const next = vi.fn(); + await optionalAuth(req, response(), next); + expect(next).toHaveBeenCalledOnce(); + expect(mocks.loadSealedSession).toHaveBeenCalledWith(expect.objectContaining({ sessionData: token })); + expect(req.user?.authorizationSnapshot?.authenticatedUserId).toBe(AUTHENTICATED_ID); + }); + + it.each(['', null, {}])('rejects malformed supplied session cookie %j', async (cookie) => { + const req = request('unused'); + req.cookies['wos-session'] = cookie; + const res = response(); + const next = vi.fn(); + await optionalAuth(req, res, next); + expectRejected(req, res, next, 401); + expect(mocks.loadSealedSession).not.toHaveBeenCalled(); + }); + + it('rejects expired sessions and their dead-cache replay instead of making them anonymous', async () => { + const token = `dead-session-${++sequence}`; + mocks.authenticate.mockResolvedValue({ authenticated: false }); + for (let attempt = 0; attempt < 2; attempt += 1) { + const req = request(token); + const res = response(); + const next = vi.fn(); + await optionalAuth(req, res, next); + expectRejected(req, res, next, 401); + } + expect(mocks.authenticate).toHaveBeenCalledOnce(); + expect(mocks.refresh).toHaveBeenCalledOnce(); + }); + + it('rejects a provider result claiming authentication without a user', async () => { + mocks.authenticate.mockResolvedValue({ authenticated: true }); + const req = request(`missing-user-${++sequence}`); + const res = response(); + const next = vi.fn(); + await optionalAuth(req, res, next); + expectRejected(req, res, next, 401); + }); + + it.each([false, true])('rejects a missing local credential on cold authentication (bearer: %s)', async (bearer) => { + mocks.loadAuthorizationSnapshot.mockResolvedValue(null); + const req = request(bearer ? `header.missing${++sequence}.signature` : `sealed-missing-${++sequence}`, bearer); + const res = response(); + const next = vi.fn(); + await optionalAuth(req, res, next); + expectRejected(req, res, next, 401); + }); + + it.each(['JWT verification', 'session authentication', 'session refresh'] as const)('returns 503 on transient %s failure without anonymous fallback', async (stage) => { + const transient = Object.assign(new Error('Authentication upstream timed out'), { code: 'ETIMEDOUT' }); + const bearer = stage === 'JWT verification'; + const req = request(bearer ? `header.timeout${++sequence}.signature` : `sealed-timeout-${++sequence}`, bearer); + if (stage === 'JWT verification') mocks.verifyWorkOSJWT.mockRejectedValue(transient); + if (stage === 'session authentication') mocks.authenticate.mockRejectedValue(transient); + if (stage === 'session refresh') { + mocks.authenticate.mockResolvedValue({ authenticated: false }); + mocks.refresh.mockRejectedValue(transient); + } + const res = response(); + const next = vi.fn(); + await optionalAuth(req, res, next); + expectRejected(req, res, next, 503); + expect(mocks.loadAuthorizationSnapshot).not.toHaveBeenCalled(); + }); + + it('returns 503 if shared session recovery is unavailable', async () => { + mocks.authenticate.mockResolvedValue({ authenticated: false }); + mocks.getRefreshedSession.mockRejectedValue(new Error('Session database unavailable')); + const req = request(`sealed-recovery-${++sequence}`); + const res = response(); + const next = vi.fn(); + await optionalAuth(req, res, next); + expectRejected(req, res, next, 503); + }); + + it.each([ + ['JWKS HTTP response', Object.assign(new Error('Expected 200 OK from the JSON Web Key Set HTTP response'), { + name: 'JOSEError', code: 'ERR_JOSE_GENERIC', + })], + ['malformed JWKS response', Object.assign(new Error('Failed to parse the JSON Web Key Set HTTP response as JSON'), { + name: 'JOSEError', code: 'ERR_JOSE_GENERIC', + })], + ['provider HTTP 503', Object.assign(new Error('Service unavailable'), { status: 503 })], + ['provider HTTP 429', Object.assign(new Error('Rate limited'), { statusCode: 429 })], + ])('returns 503 for unavailable %s during JWT verification', async (_label, error) => { + mocks.verifyWorkOSJWT.mockRejectedValue(error); + const req = request(`header.unavailable${++sequence}.signature`, true); + req.cookies['wos-session'] = 'otherwise-valid-cookie'; + const res = response(); + const next = vi.fn(); + await optionalAuth(req, res, next); + expectRejected(req, res, next, 503); + expect(mocks.loadSealedSession).not.toHaveBeenCalled(); + }); + + it.each(['initial', 'shared'] as const)('returns 503 for a retryable %s refresh result and permits a later retry', async (stage) => { + const token = `retryable-${stage}-${++sequence}`; + mocks.authenticate.mockResolvedValue({ authenticated: false }); + if (stage === 'shared') { + mocks.getRefreshedSession.mockResolvedValueOnce('shared-sealed-session'); + mocks.refresh.mockResolvedValueOnce({ authenticated: false, reason: 'invalid_grant', retryable: false }); + } + mocks.refresh.mockResolvedValue({ authenticated: false, reason: 'server_error', retryable: true }); + const req = request(token); + const res = response(); + const next = vi.fn(); + await optionalAuth(req, res, next); + expectRejected(req, res, next, 503); + + mocks.authenticate.mockResolvedValue({ authenticated: true, user: { ...PROVIDER_USER }, accessToken: 'access-token' }); + const retry = request(token); + const retryNext = vi.fn(); + await optionalAuth(retry, response(), retryNext); + expect(retryNext).toHaveBeenCalledOnce(); + expect(retry.user?.authorizationSnapshot?.authenticatedUserId).toBe(AUTHENTICATED_ID); + }); + + it('returns 503 on a thrown provider HTTP 503 during session refresh', async () => { + mocks.authenticate.mockResolvedValue({ authenticated: false }); + mocks.refresh.mockRejectedValue(Object.assign(new Error('Service unavailable'), { status: 503 })); + const req = request(`refresh-503-${++sequence}`); + const res = response(); + const next = vi.fn(); + await optionalAuth(req, res, next); + expectRejected(req, res, next, 503); + }); + + it('returns 401 when sealed-session decoding rejects the presented credential', async () => { + mocks.loadSealedSession.mockImplementationOnce(() => { throw new Error('Invalid sealed session'); }); + const req = request(`invalid-sealed-${++sequence}`); + const res = response(); + const next = vi.fn(); + await optionalAuth(req, res, next); + expectRejected(req, res, next, 401); + }); +}); afterAll(stopAuthTimers); describe.each([ diff --git a/server/tests/unit/network-health-security.test.ts b/server/tests/unit/network-health-security.test.ts index 3c134c8f98a..c2c2add7b11 100644 --- a/server/tests/unit/network-health-security.test.ts +++ b/server/tests/unit/network-health-security.test.ts @@ -55,6 +55,7 @@ vi.mock('../../src/db/org-filters.js', () => ({ vi.mock('../../src/db/client.js', async (importOriginal) => ({ ...(await importOriginal()), getPool: () => ({ query: mocks.poolQuery }), + queryWithTimeout: mocks.poolQuery, })); vi.mock('../../src/db/authorization-epoch-db.js', async (importOriginal) => ({ @@ -217,10 +218,27 @@ describe('network-health global authorization boundary', () => { }), ); mocks.isWebUserAAOAdmin.mockResolvedValue(false); - mocks.poolQuery.mockImplementation((sql: string) => { - if (sql.includes('FROM users')) { + mocks.poolQuery.mockImplementation((sql: string, params: unknown[]) => { + if (sql.includes('pg_catalog.pg_is_in_recovery()')) { + const userId = params[0] as string; + const isAdmin = userId === 'user_platform_admin'; return Promise.resolve({ - rows: [{ first_name: 'Regular', last_name: 'User' }], + rows: [{ + in_recovery: false, + authenticated_user_id: userId, + canonical_user_id: userId, + identity_id: `identity_${userId}`, + authorization_epoch: '0', + email: isAdmin ? 'platform-admin@example.test' : 'user@example.test', + email_verified: true, + first_name: isAdmin ? 'Platform' : 'Regular', + last_name: isAdmin ? 'Admin' : 'User', + grant_id: null, + grant_organization_id: null, + grant_role: null, + grant_effective_from: null, + grant_effective_until: null, + }], rowCount: 1, }); } diff --git a/server/tests/unit/user-authorization-snapshot-timeout.test.ts b/server/tests/unit/user-authorization-snapshot-timeout.test.ts new file mode 100644 index 00000000000..b798eb59e37 --- /dev/null +++ b/server/tests/unit/user-authorization-snapshot-timeout.test.ts @@ -0,0 +1,125 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { queryWithTimeout } from '../../src/db/client.js'; +import { + AuthorizationSnapshotUnavailableError, + loadAuthorizationSnapshot, +} from '../../src/db/user-authorization-snapshot-db.js'; + +vi.mock('../../src/db/client.js', async importOriginal => ({ + ...await importOriginal(), + queryWithTimeout: vi.fn(), +})); + +const boundedQuery = vi.mocked(queryWithTimeout); +const USER_ID = 'user_snapshot_timeout'; +const ORGANIZATION_ID = 'org_snapshot_timeout'; + +function queryResult(overrides: Record = {}) { + return { + rows: [{ + in_recovery: false, + authenticated_user_id: USER_ID, + canonical_user_id: USER_ID, + identity_id: 'fd3043f7-cb4f-43c7-9b81-22ac97576150', + authorization_epoch: '7', + email: 'sam@pinnacle.example', + email_verified: true, + first_name: 'Sam', + last_name: 'Adeyemi', + grant_id: null, + grant_organization_id: null, + grant_role: null, + grant_effective_from: null, + grant_effective_until: null, + ...overrides, + }], + rowCount: 1, + command: 'SELECT', + oid: 0, + fields: [], + }; +} + +describe('authorization snapshot query deadline and connection retry', () => { + beforeEach(() => { + boundedQuery.mockReset(); + vi.useFakeTimers(); + vi.setSystemTime(new Date('2026-01-01T00:00:00Z')); + }); + + afterEach(() => { + vi.useRealTimers(); + }); + + it('retries one transient connection failure with only the remaining absolute budget', async () => { + boundedQuery.mockImplementationOnce(async () => { + vi.setSystemTime(Date.now() + 700); + throw Object.assign(new Error('connection reset'), { code: 'ECONNRESET' }); + }).mockResolvedValueOnce(queryResult()); + + await expect(loadAuthorizationSnapshot(USER_ID, ORGANIZATION_ID)).resolves.toMatchObject({ + authenticatedUserId: USER_ID, + selectedOrganizationId: ORGANIZATION_ID, + authorizationEpoch: '7', + }); + expect(boundedQuery).toHaveBeenCalledTimes(2); + const [first, second] = boundedQuery.mock.calls; + expect(first[2]).toBe(2_000); + expect(second).toEqual([first[0], first[1], 1_300]); + expect(first[1]).toEqual([USER_ID, ORGANIZATION_ID]); + }); + + it('stops after two connection failures and permits a clean request after recovery', async () => { + boundedQuery.mockRejectedValue(Object.assign(new Error('private connection details'), { code: 'EPIPE' })); + await expect(loadAuthorizationSnapshot(USER_ID, ORGANIZATION_ID)) + .rejects.toThrow(new AuthorizationSnapshotUnavailableError()); + expect(boundedQuery).toHaveBeenCalledTimes(2); + + boundedQuery.mockResolvedValueOnce(queryResult()); + await expect(loadAuthorizationSnapshot(USER_ID, ORGANIZATION_ID)).resolves.toMatchObject({ + authenticatedUserId: USER_ID, authorizationEpoch: '7', + }); + expect(boundedQuery).toHaveBeenCalledTimes(3); + }); + + it('does not start a retry after the first attempt exhausts the deadline', async () => { + boundedQuery.mockImplementationOnce(async () => { + vi.setSystemTime(Date.now() + 2_000); + throw Object.assign(new Error('connection reset'), { code: 'ECONNRESET' }); + }); + await expect(loadAuthorizationSnapshot(USER_ID, ORGANIZATION_ID)) + .rejects.toBeInstanceOf(AuthorizationSnapshotUnavailableError); + expect(boundedQuery).toHaveBeenCalledTimes(1); + }); + + it('rejects a result delivered after the absolute deadline without retrying', async () => { + boundedQuery.mockImplementationOnce(async () => { + vi.setSystemTime(Date.now() + 2_001); + return queryResult(); + }); + await expect(loadAuthorizationSnapshot(USER_ID, ORGANIZATION_ID)) + .rejects.toBeInstanceOf(AuthorizationSnapshotUnavailableError); + expect(boundedQuery).toHaveBeenCalledTimes(1); + }); + + it.each([ + { code: '57014', message: 'canceling statement due to statement timeout' }, + { code: '55P03', message: 'canceling statement due to lock timeout' }, + { code: '42P01', message: 'relation does not exist' }, + ])('does not retry a non-connection database error ($code)', async ({ code, message }) => { + boundedQuery.mockRejectedValue(Object.assign(new Error(message), { code })); + await expect(loadAuthorizationSnapshot(USER_ID, ORGANIZATION_ID)) + .rejects.toThrow(new AuthorizationSnapshotUnavailableError()); + expect(boundedQuery).toHaveBeenCalledTimes(1); + }); + + it.each([ + { state: 'replica', row: { in_recovery: true } }, + { state: 'missing primary identity', row: { canonical_user_id: null } }, + ])('does not retry an unavailable $state snapshot', async ({ row }) => { + boundedQuery.mockResolvedValue(queryResult(row)); + await expect(loadAuthorizationSnapshot(USER_ID, ORGANIZATION_ID)) + .rejects.toBeInstanceOf(AuthorizationSnapshotUnavailableError); + expect(boundedQuery).toHaveBeenCalledTimes(1); + }); +}); diff --git a/server/tests/unit/working-group-admin-global-auth.test.ts b/server/tests/unit/working-group-admin-global-auth.test.ts index 978dbb51bb9..3b312fc9719 100644 --- a/server/tests/unit/working-group-admin-global-auth.test.ts +++ b/server/tests/unit/working-group-admin-global-auth.test.ts @@ -58,6 +58,7 @@ vi.mock('../../src/db/org-filters.js', () => ({ vi.mock('../../src/db/client.js', async (importOriginal) => ({ ...(await importOriginal()), getPool: () => ({ query: mocks.poolQuery }), + queryWithTimeout: mocks.poolQuery, })); vi.mock('../../src/db/authorization-epoch-db.js', async (importOriginal) => ({ From 88e3abed7f39a8d1b8a9a0f6b53a23306ecb699d Mon Sep 17 00:00:00 2001 From: Brian O'Kelley Date: Sun, 13 Sep 2026 14:43:13 +0000 Subject: [PATCH 3/7] test(auth): document optional credential guard and cover HTTP failures --- server/src/middleware/auth.ts | 1 + .../unit/authorization-epoch-session.test.ts | 76 +++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/server/src/middleware/auth.ts b/server/src/middleware/auth.ts index ac40f881a5c..23127486ab3 100644 --- a/server/src/middleware/auth.ts +++ b/server/src/middleware/auth.ts @@ -1885,6 +1885,7 @@ export async function optionalAuth(req: Request, res: Response, next: NextFuncti const sessionCookie = extractSealedSession(req); + // codeql[js/user-controlled-bypass] - optional auth permits anonymous access only when both credentials are absent; presented credentials fail closed if (!sessionCookie) { if (req.headers.authorization !== undefined || req.cookies?.['wos-session'] !== undefined) { return res.status(401).json({ error: 'Invalid session', login_url: '/auth/login' }); diff --git a/server/tests/unit/authorization-epoch-session.test.ts b/server/tests/unit/authorization-epoch-session.test.ts index 93148d7e0a7..691d62c18a1 100644 --- a/server/tests/unit/authorization-epoch-session.test.ts +++ b/server/tests/unit/authorization-epoch-session.test.ts @@ -1,5 +1,8 @@ /** Provider caches must never become identity, organization, or epoch authority. */ import type { NextFunction, Request, Response } from 'express'; +import express from 'express'; +import cookieParser from 'cookie-parser'; +import supertest from 'supertest'; import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'; import type { AuthorizationSnapshot } from '../../src/db/user-authorization-snapshot-db.js'; @@ -44,6 +47,7 @@ vi.mock('../../src/db/session-refresh-db.js', () => ({ })); import { AuthorizationSnapshotUnavailableError } from '../../src/db/user-authorization-snapshot-db.js'; import { invalidateBanCache, optionalAuth, requireAuth, stopAuthTimers } from '../../src/middleware/auth.js'; +import { csrfProtection } from '../../src/middleware/csrf.js'; const AUTHENTICATED_ID = 'user_epoch_authenticated'; const PROVIDER_USER = { @@ -563,3 +567,75 @@ describe.each([['required', requireAuth], ['optional', optionalAuth]] as const)( expect(mocks.verifyWorkOSJWT).toHaveBeenCalledTimes(1); }); }); + +describe('optional authentication HTTP route boundary', () => { + function route() { + const app = express(); + // Exercise the real HTTP cookie parser, including an explicitly empty cookie. + // This read-only route has the same middleware chain for anonymous and authenticated callers. + app.use(cookieParser()); + app.use(csrfProtection); + const handler = vi.fn((req: Request, res: Response) => { + res.json({ authenticated: Boolean(req.user) }); + }); + app.get('/optional-auth-test', optionalAuth, handler); + return { app, handler }; + } + + it('reaches the anonymous handler when neither Authorization nor Cookie is sent', async () => { + const { app, handler } = route(); + const result = await supertest(app).get('/optional-auth-test'); + expect(result.status).toBe(200); + expect(result.body).toEqual({ authenticated: false }); + expect(handler).toHaveBeenCalledOnce(); + expect(mocks.loadSealedSession).not.toHaveBeenCalled(); + expect(mocks.loadAuthorizationSnapshot).not.toHaveBeenCalled(); + }); + + it.each(['', 'Basic invalid', 'Bearer header.invalid.signature'])('rejects supplied Authorization %j without using an otherwise valid cookie', async (authorization) => { + mocks.verifyWorkOSJWT.mockRejectedValue(new Error('Invalid JWT signature')); + const { app, handler } = route(); + const result = await supertest(app).get('/optional-auth-test') + .set('Authorization', authorization) + .set('Cookie', `wos-session=otherwise-valid-${++sequence}`); + expect(result.status).toBe(401); + expect(handler).not.toHaveBeenCalled(); + expect(mocks.loadSealedSession).not.toHaveBeenCalled(); + expect(mocks.loadAuthorizationSnapshot).not.toHaveBeenCalled(); + }); + + it('rejects an opaque invalid bearer after validating that bearer rather than its companion cookie', async () => { + mocks.authenticate.mockResolvedValue({ authenticated: false }); + const token = `invalid-native-route-${++sequence}`; + const { app, handler } = route(); + const result = await supertest(app).get('/optional-auth-test') + .set('Authorization', `Bearer ${token}`) + .set('Cookie', 'wos-session=otherwise-valid'); + expect(result.status).toBe(401); + expect(handler).not.toHaveBeenCalled(); + expect(mocks.loadSealedSession).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ sessionData: token })); + }); + + it.each(['empty', 'invalid'] as const)('rejects an %s presented wos-session cookie before the handler', async (kind) => { + mocks.authenticate.mockResolvedValue({ authenticated: false }); + const token = kind === 'empty' ? '' : `invalid-cookie-route-${++sequence}`; + const { app, handler } = route(); + const result = await supertest(app).get('/optional-auth-test').set('Cookie', `wos-session=${token}`); + expect(result.status).toBe(401); + expect(handler).not.toHaveBeenCalled(); + expect(mocks.loadAuthorizationSnapshot).not.toHaveBeenCalled(); + if (kind === 'empty') expect(mocks.loadSealedSession).not.toHaveBeenCalled(); + }); + + it.each(['bearer', 'cookie'] as const)('returns 503 for unavailable authorization on a presented %s without running the handler', async (kind) => { + mocks.loadAuthorizationSnapshot.mockRejectedValue(new AuthorizationSnapshotUnavailableError()); + const { app, handler } = route(); + const httpRequest = supertest(app).get('/optional-auth-test'); + if (kind === 'bearer') httpRequest.set('Authorization', `Bearer header.route${++sequence}.signature`); + else httpRequest.set('Cookie', `wos-session=unavailable-route-${++sequence}`); + const result = await httpRequest; + expect(result.status).toBe(503); + expect(handler).not.toHaveBeenCalled(); + expect(mocks.loadAuthorizationSnapshot).toHaveBeenCalledOnce(); + }); +}); From 95f4246ed0d4fa004ed1f29710921f439d406d23 Mon Sep 17 00:00:00 2001 From: Brian O'Kelley Date: Sun, 13 Sep 2026 14:56:17 +0000 Subject: [PATCH 4/7] fix(auth): make optional credential guard an explicit early return --- server/src/middleware/auth.ts | 11 ++++---- .../unit/authorization-epoch-session.test.ts | 28 +++++++++++-------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/server/src/middleware/auth.ts b/server/src/middleware/auth.ts index 23127486ab3..7c55bfcc1d5 100644 --- a/server/src/middleware/auth.ts +++ b/server/src/middleware/auth.ts @@ -1885,12 +1885,13 @@ export async function optionalAuth(req: Request, res: Response, next: NextFuncti const sessionCookie = extractSealedSession(req); - // codeql[js/user-controlled-bypass] - optional auth permits anonymous access only when both credentials are absent; presented credentials fail closed + // Only complete credential absence permits anonymous access. Keep one return + // for the entire branch so CodeQL recognizes this as an early-exit guard; + // supplied but unusable credentials still return 401 without a fallback. if (!sessionCookie) { - if (req.headers.authorization !== undefined || req.cookies?.['wos-session'] !== undefined) { - return res.status(401).json({ error: 'Invalid session', login_url: '/auth/login' }); - } - return next(); + return req.headers.authorization !== undefined || req.cookies?.['wos-session'] !== undefined + ? res.status(401).json({ error: 'Invalid session', login_url: '/auth/login' }) + : next(); } try { diff --git a/server/tests/unit/authorization-epoch-session.test.ts b/server/tests/unit/authorization-epoch-session.test.ts index 691d62c18a1..f99feded240 100644 --- a/server/tests/unit/authorization-epoch-session.test.ts +++ b/server/tests/unit/authorization-epoch-session.test.ts @@ -1,7 +1,6 @@ /** Provider caches must never become identity, organization, or epoch authority. */ import type { NextFunction, Request, Response } from 'express'; import express from 'express'; -import cookieParser from 'cookie-parser'; import supertest from 'supertest'; import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'; import type { AuthorizationSnapshot } from '../../src/db/user-authorization-snapshot-db.js'; @@ -569,11 +568,16 @@ describe.each([['required', requireAuth], ['optional', optionalAuth]] as const)( }); describe('optional authentication HTTP route boundary', () => { - function route() { + function route(parsedCookies: Record = {}) { const app = express(); - // Exercise the real HTTP cookie parser, including an explicitly empty cookie. - // This read-only route has the same middleware chain for anonymous and authenticated callers. - app.use(cookieParser()); + // Match network-health-security.test.ts: authentication is under test, not + // cookie-parser. Inject its parsed output from a fixture owned by this app; + // the HTTP requests below send the corresponding Cookie header. An absent + // cookie remains {}, while a presented empty cookie retains its empty value. + app.use((req, _res, next) => { + req.cookies = { ...parsedCookies }; + next(); + }); app.use(csrfProtection); const handler = vi.fn((req: Request, res: Response) => { res.json({ authenticated: Boolean(req.user) }); @@ -594,10 +598,11 @@ describe('optional authentication HTTP route boundary', () => { it.each(['', 'Basic invalid', 'Bearer header.invalid.signature'])('rejects supplied Authorization %j without using an otherwise valid cookie', async (authorization) => { mocks.verifyWorkOSJWT.mockRejectedValue(new Error('Invalid JWT signature')); - const { app, handler } = route(); + const cookie = `otherwise-valid-${++sequence}`; + const { app, handler } = route({ 'wos-session': cookie }); const result = await supertest(app).get('/optional-auth-test') .set('Authorization', authorization) - .set('Cookie', `wos-session=otherwise-valid-${++sequence}`); + .set('Cookie', `wos-session=${cookie}`); expect(result.status).toBe(401); expect(handler).not.toHaveBeenCalled(); expect(mocks.loadSealedSession).not.toHaveBeenCalled(); @@ -607,7 +612,7 @@ describe('optional authentication HTTP route boundary', () => { it('rejects an opaque invalid bearer after validating that bearer rather than its companion cookie', async () => { mocks.authenticate.mockResolvedValue({ authenticated: false }); const token = `invalid-native-route-${++sequence}`; - const { app, handler } = route(); + const { app, handler } = route({ 'wos-session': 'otherwise-valid' }); const result = await supertest(app).get('/optional-auth-test') .set('Authorization', `Bearer ${token}`) .set('Cookie', 'wos-session=otherwise-valid'); @@ -619,7 +624,7 @@ describe('optional authentication HTTP route boundary', () => { it.each(['empty', 'invalid'] as const)('rejects an %s presented wos-session cookie before the handler', async (kind) => { mocks.authenticate.mockResolvedValue({ authenticated: false }); const token = kind === 'empty' ? '' : `invalid-cookie-route-${++sequence}`; - const { app, handler } = route(); + const { app, handler } = route({ 'wos-session': token }); const result = await supertest(app).get('/optional-auth-test').set('Cookie', `wos-session=${token}`); expect(result.status).toBe(401); expect(handler).not.toHaveBeenCalled(); @@ -629,10 +634,11 @@ describe('optional authentication HTTP route boundary', () => { it.each(['bearer', 'cookie'] as const)('returns 503 for unavailable authorization on a presented %s without running the handler', async (kind) => { mocks.loadAuthorizationSnapshot.mockRejectedValue(new AuthorizationSnapshotUnavailableError()); - const { app, handler } = route(); + const cookie = `unavailable-route-${++sequence}`; + const { app, handler } = route(kind === 'cookie' ? { 'wos-session': cookie } : {}); const httpRequest = supertest(app).get('/optional-auth-test'); if (kind === 'bearer') httpRequest.set('Authorization', `Bearer header.route${++sequence}.signature`); - else httpRequest.set('Cookie', `wos-session=unavailable-route-${++sequence}`); + else httpRequest.set('Cookie', `wos-session=${cookie}`); const result = await httpRequest; expect(result.status).toBe(503); expect(handler).not.toHaveBeenCalled(); From 4e1bc2130deeff1d4f89fbe8650a643768a20e3d Mon Sep 17 00:00:00 2001 From: Brian O'Kelley Date: Mon, 14 Sep 2026 01:02:07 +0000 Subject: [PATCH 5/7] fix(db): bound authorization snapshot pool checkout --- server/src/db/client.ts | 67 ++++++++++- .../src/db/user-authorization-snapshot-db.ts | 6 +- server/tests/unit/db-client-checkout.test.ts | 110 ++++++++++++++++++ ...ser-authorization-snapshot-timeout.test.ts | 3 +- 4 files changed, 181 insertions(+), 5 deletions(-) diff --git a/server/src/db/client.ts b/server/src/db/client.ts index cfc6dfb77f9..172688bccfa 100644 --- a/server/src/db/client.ts +++ b/server/src/db/client.ts @@ -147,8 +147,65 @@ export function withDatabaseDeadline( return queryDeadline.run({ deadlineMs, readOnly: options.readOnly ?? true }, work); } +function databaseDeadlineExceededError(): Error & { code: '57014' } { + return Object.assign(new Error('Database query deadline exceeded'), { code: '57014' as const }); +} + +/** + * Check out a pooled client without allowing pool saturation to outlive the + * caller's absolute deadline. A client delivered after the timer wins is + * released immediately so a timed-out request cannot leak a pool slot. + */ +async function checkoutClientBeforeDeadline(deadlineMs: number): Promise { + const remainingMs = deadlineMs - Date.now(); + if (remainingMs <= 0) throw databaseDeadlineExceededError(); + + const checkout = getPool().connect(); + let timeout: ReturnType | undefined; + let timedOut = false; + const deadline = new Promise((_resolve, reject) => { + timeout = setTimeout(() => { + timedOut = true; + reject(databaseDeadlineExceededError()); + }, remainingMs); + }); + + try { + return await Promise.race([checkout, deadline]); + } catch (error) { + if (timedOut) { + void checkout.then( + (lateClient) => lateClient.release(), + () => undefined, + ); + } + throw error; + } finally { + if (timeout !== undefined) clearTimeout(timeout); + } +} + +async function getClientBeforeDeadline( + deadlineMs: number, + retryTransientCheckout: boolean, +): Promise { + const attempts = retryTransientCheckout ? 2 : 1; + for (let attempt = 0; attempt < attempts; attempt++) { + try { + return await checkoutClientBeforeDeadline(deadlineMs); + } catch (error) { + if (attempt === attempts - 1 || !isTransientConnectionError(error) || Date.now() >= deadlineMs) { + throw error; + } + console.warn('Transient DB connection error, retrying client checkout:', (error as Error).message); + } + } + throw databaseDeadlineExceededError(); +} + /** - * Execute one query with server-enforced statement and lock deadlines. + * Execute one query with deadline-bounded pool checkout plus server-enforced + * statement and lock deadlines. * * Use this for public read paths whose inputs can select unusually large * registry fan-outs. The transaction-local settings ensure PostgreSQL stops @@ -159,18 +216,22 @@ export async function queryWithTimeout( text: string, params: any[] | undefined, timeoutMs: number, + options: { retryTransientCheckout?: boolean } = {}, ): Promise> { const inheritedDeadline = queryDeadline.getStore(); const deadlineMs = Math.min( Date.now() + timeoutMs, inheritedDeadline?.deadlineMs ?? Number.POSITIVE_INFINITY, ); - const client = await getClient(); + const client = await getClientBeforeDeadline( + deadlineMs, + options.retryTransientCheckout ?? true, + ); let transactionStarted = false; try { const effectiveTimeoutMs = deadlineMs - Date.now(); if (effectiveTimeoutMs <= 0) { - throw Object.assign(new Error('Database query deadline exceeded'), { code: '57014' }); + throw databaseDeadlineExceededError(); } await client.query(inheritedDeadline?.readOnly === false ? 'BEGIN' : 'BEGIN READ ONLY'); transactionStarted = true; diff --git a/server/src/db/user-authorization-snapshot-db.ts b/server/src/db/user-authorization-snapshot-db.ts index d6a4f7cda30..263982e1281 100644 --- a/server/src/db/user-authorization-snapshot-db.ts +++ b/server/src/db/user-authorization-snapshot-db.ts @@ -57,7 +57,11 @@ async function querySnapshot(statement: string, parameters: [string, string | nu const remainingMs = deadlineMs - Date.now(); if (remainingMs <= 0) throw new AuthorizationSnapshotUnavailableError(); try { - const result = await queryWithTimeout(statement, parameters, remainingMs); + const result = await queryWithTimeout(statement, parameters, remainingMs, { + // querySnapshot owns the one retry so checkout and statement failures + // share this request's single absolute authorization budget. + retryTransientCheckout: false, + }); if (Date.now() >= deadlineMs) throw new AuthorizationSnapshotUnavailableError(); return result; } catch (error) { diff --git a/server/tests/unit/db-client-checkout.test.ts b/server/tests/unit/db-client-checkout.test.ts index b813b903fb6..dc097bc9db9 100644 --- a/server/tests/unit/db-client-checkout.test.ts +++ b/server/tests/unit/db-client-checkout.test.ts @@ -311,6 +311,116 @@ describe('db client checkout and health checks', () => { await db.closeDatabase(); }); + it('fails an exhausted snapshot pool at the absolute deadline and releases a late client', async () => { + vi.useFakeTimers(); + vi.setSystemTime(new Date('2026-01-01T00:00:00Z')); + const pg = mockPg(); + let deliverClient!: (client: { query: ReturnType; release: ReturnType }) => void; + pg.poolConnect.mockReturnValue(new Promise((resolve) => { + deliverClient = resolve; + })); + + const db = await import('../../src/db/client.js'); + const snapshots = await import('../../src/db/user-authorization-snapshot-db.js'); + db.initializeDatabase({ connectionString: 'postgresql://localhost/test' }); + + const pending = snapshots.loadAuthorizationSnapshot('user_exhausted_pool', 'org_exhausted_pool'); + const rejection = expect(pending).rejects.toBeInstanceOf( + snapshots.AuthorizationSnapshotUnavailableError, + ); + await vi.advanceTimersByTimeAsync(1_999); + expect(pg.poolConnect).toHaveBeenCalledTimes(1); + await vi.advanceTimersByTimeAsync(1); + await rejection; + + const lateClient = { query: vi.fn(), release: vi.fn() }; + deliverClient(lateClient); + await Promise.resolve(); + await Promise.resolve(); + expect(lateClient.query).not.toHaveBeenCalled(); + expect(lateClient.release).toHaveBeenCalledTimes(1); + expect(pg.poolConnect).toHaveBeenCalledTimes(1); + + await db.closeDatabase(); + }); + + it('retries one transient snapshot checkout with only the remaining absolute budget', async () => { + vi.useFakeTimers(); + vi.setSystemTime(new Date('2026-01-01T00:00:00Z')); + const pg = mockPg(); + const release = vi.fn(); + const query = vi.fn().mockImplementation(async (text: string) => { + if (text.includes('pg_catalog.pg_is_in_recovery')) { + return { + rows: [{ + in_recovery: false, + authenticated_user_id: 'user_retry_checkout', + canonical_user_id: 'user_retry_checkout', + identity_id: 'fd3043f7-cb4f-43c7-9b81-22ac97576150', + authorization_epoch: '11', + email: 'sam@pinnacle.example', + email_verified: true, + first_name: 'Sam', + last_name: 'Adeyemi', + grant_id: null, + grant_organization_id: null, + grant_role: null, + grant_effective_from: null, + grant_effective_until: null, + }], + }; + } + return { rows: [] }; + }); + pg.poolConnect + .mockImplementationOnce(async () => { + vi.setSystemTime(Date.now() + 700); + throw Object.assign(new Error('connection reset'), { code: 'ECONNRESET' }); + }) + .mockResolvedValueOnce({ query, release }); + + const db = await import('../../src/db/client.js'); + const snapshots = await import('../../src/db/user-authorization-snapshot-db.js'); + db.initializeDatabase({ connectionString: 'postgresql://localhost/test' }); + + await expect(snapshots.loadAuthorizationSnapshot('user_retry_checkout', 'org_retry_checkout')) + .resolves.toMatchObject({ + authenticatedUserId: 'user_retry_checkout', + selectedOrganizationId: 'org_retry_checkout', + authorizationEpoch: '11', + }); + + expect(pg.poolConnect).toHaveBeenCalledTimes(2); + expect(query).toHaveBeenCalledWith( + "SELECT set_config('statement_timeout', $1, true)", + ['1300ms'], + ); + expect(release).toHaveBeenCalledTimes(1); + + await db.closeDatabase(); + }); + + it('does not multiply snapshot retries inside pool checkout', async () => { + const pg = mockPg(); + const unexpectedClient = { query: vi.fn(), release: vi.fn() }; + pg.poolConnect + .mockRejectedValueOnce(Object.assign(new Error('first reset'), { code: 'ECONNRESET' })) + .mockRejectedValueOnce(Object.assign(new Error('second reset'), { code: 'ECONNRESET' })) + .mockResolvedValueOnce(unexpectedClient); + + const db = await import('../../src/db/client.js'); + const snapshots = await import('../../src/db/user-authorization-snapshot-db.js'); + db.initializeDatabase({ connectionString: 'postgresql://localhost/test' }); + + await expect(snapshots.loadAuthorizationSnapshot('user_two_attempts', 'org_two_attempts')) + .rejects.toBeInstanceOf(snapshots.AuthorizationSnapshotUnavailableError); + expect(pg.poolConnect).toHaveBeenCalledTimes(2); + expect(unexpectedClient.query).not.toHaveBeenCalled(); + expect(unexpectedClient.release).not.toHaveBeenCalled(); + + await db.closeDatabase(); + }); + it('rolls back and releases the client after a timed query fails', async () => { const pg = mockPg(); const release = vi.fn(); diff --git a/server/tests/unit/user-authorization-snapshot-timeout.test.ts b/server/tests/unit/user-authorization-snapshot-timeout.test.ts index b798eb59e37..aa295be55f0 100644 --- a/server/tests/unit/user-authorization-snapshot-timeout.test.ts +++ b/server/tests/unit/user-authorization-snapshot-timeout.test.ts @@ -65,7 +65,8 @@ describe('authorization snapshot query deadline and connection retry', () => { expect(boundedQuery).toHaveBeenCalledTimes(2); const [first, second] = boundedQuery.mock.calls; expect(first[2]).toBe(2_000); - expect(second).toEqual([first[0], first[1], 1_300]); + expect(first[3]).toEqual({ retryTransientCheckout: false }); + expect(second).toEqual([first[0], first[1], 1_300, { retryTransientCheckout: false }]); expect(first[1]).toEqual([USER_ID, ORGANIZATION_ID]); }); From a1d7537832d163ea95f8d53b89520334fc514f01 Mon Sep 17 00:00:00 2001 From: Brian O'Kelley Date: Mon, 14 Sep 2026 18:09:21 +0000 Subject: [PATCH 6/7] fix(db): bound authorization transaction controls --- server/src/db/client.ts | 114 ++++++-- .../src/db/user-authorization-snapshot-db.ts | 3 + server/tests/unit/db-client-checkout.test.ts | 263 ++++++++++++++++++ ...ser-authorization-snapshot-timeout.test.ts | 10 +- 4 files changed, 372 insertions(+), 18 deletions(-) diff --git a/server/src/db/client.ts b/server/src/db/client.ts index 172688bccfa..e2434b6eab2 100644 --- a/server/src/db/client.ts +++ b/server/src/db/client.ts @@ -147,8 +147,52 @@ export function withDatabaseDeadline( return queryDeadline.run({ deadlineMs, readOnly: options.readOnly ?? true }, work); } -function databaseDeadlineExceededError(): Error & { code: '57014' } { - return Object.assign(new Error('Database query deadline exceeded'), { code: '57014' as const }); +export class DatabaseQueryDeadlineExceededError extends Error { + readonly code = '57014' as const; + readonly retryable = true as const; + + constructor() { + super('Database query deadline exceeded'); + this.name = 'DatabaseQueryDeadlineExceededError'; + } +} + +function databaseDeadlineExceededError(): DatabaseQueryDeadlineExceededError { + return new DatabaseQueryDeadlineExceededError(); +} + +/** + * Await one client operation for no longer than the caller's absolute + * deadline. Promise.race keeps observing a late rejection, while callers + * destroy the client because a late result leaves its protocol/transaction + * state unknowable. + */ +async function clientOperationBeforeDeadline( + operation: () => Promise, + deadlineMs: number, +): Promise { + const remainingMs = deadlineMs - Date.now(); + if (remainingMs <= 0) throw databaseDeadlineExceededError(); + + const pending = Promise.resolve().then(operation); + let timeout: ReturnType | undefined; + const deadline = new Promise((_resolve, reject) => { + timeout = setTimeout(() => reject(databaseDeadlineExceededError()), remainingMs); + }); + + try { + return await Promise.race([pending, deadline]); + } finally { + if (timeout !== undefined) clearTimeout(timeout); + } +} + +function isDefinitivePostgresError(error: unknown): boolean { + if (!(error instanceof Error) || error instanceof DatabaseQueryDeadlineExceededError) return false; + const code = (error as { code?: unknown }).code; + return typeof code === 'string' + && /^[0-9A-Z]{5}$/.test(code) + && !isTransientConnectionError(error); } /** @@ -216,11 +260,11 @@ export async function queryWithTimeout( text: string, params: any[] | undefined, timeoutMs: number, - options: { retryTransientCheckout?: boolean } = {}, + options: { retryTransientCheckout?: boolean; deadlineMs?: number } = {}, ): Promise> { const inheritedDeadline = queryDeadline.getStore(); const deadlineMs = Math.min( - Date.now() + timeoutMs, + options.deadlineMs ?? Date.now() + timeoutMs, inheritedDeadline?.deadlineMs ?? Number.POSITIVE_INFINITY, ); const client = await getClientBeforeDeadline( @@ -228,31 +272,69 @@ export async function queryWithTimeout( options.retryTransientCheckout ?? true, ); let transactionStarted = false; + let clientStateUncertain = false; + + const run = async ( + operation: () => Promise, + phase: 'begin' | 'configuration' | 'statement' | 'commit' | 'rollback', + ): Promise => { + try { + return await clientOperationBeforeDeadline(operation, deadlineMs); + } catch (error) { + // A client-side deadline can win while pg is still processing any query. + // Connection-style failures have no trustworthy server acknowledgement. + // COMMIT failures are always ambiguous: the transaction may be durable + // even though its acknowledgement never reached this process. + if (error instanceof DatabaseQueryDeadlineExceededError + || !isDefinitivePostgresError(error) + || phase === 'commit' + || phase === 'rollback') { + clientStateUncertain = true; + } + throw error; + } + }; + try { const effectiveTimeoutMs = deadlineMs - Date.now(); if (effectiveTimeoutMs <= 0) { throw databaseDeadlineExceededError(); } - await client.query(inheritedDeadline?.readOnly === false ? 'BEGIN' : 'BEGIN READ ONLY'); + await run( + () => client.query(inheritedDeadline?.readOnly === false ? 'BEGIN' : 'BEGIN READ ONLY'), + 'begin', + ); transactionStarted = true; - await client.query("SELECT set_config('statement_timeout', $1, true)", [ - `${effectiveTimeoutMs}ms`, - ]); - await client.query("SELECT set_config('lock_timeout', $1, true)", [ - `${Math.min(effectiveTimeoutMs, 2_000)}ms`, - ]); - const result = await client.query(text, params); - await client.query('COMMIT'); + await run( + () => client.query("SELECT set_config('statement_timeout', $1, true)", [ + `${effectiveTimeoutMs}ms`, + ]), + 'configuration', + ); + await run( + () => client.query("SELECT set_config('lock_timeout', $1, true)", [ + `${Math.min(effectiveTimeoutMs, 2_000)}ms`, + ]), + 'configuration', + ); + const result = await run(() => client.query(text, params), 'statement'); + await run(() => client.query('COMMIT'), 'commit'); + transactionStarted = false; return result; } catch (error) { - if (transactionStarted) { - await client.query('ROLLBACK').catch((rollbackError) => { + // Never queue ROLLBACK behind an operation that may still be in flight. + // If the prior outcome is definite, cleanup must still finish inside the + // same deadline before this client can be considered reusable. + if (transactionStarted && !clientStateUncertain) { + await run(() => client.query('ROLLBACK'), 'rollback').catch((rollbackError) => { logger.warn({ err: rollbackError }, 'Timed query rollback failed'); }); + if (!clientStateUncertain) transactionStarted = false; } throw error; } finally { - client.release(); + if (clientStateUncertain || transactionStarted) client.release(true); + else client.release(); } } diff --git a/server/src/db/user-authorization-snapshot-db.ts b/server/src/db/user-authorization-snapshot-db.ts index 263982e1281..d0a2b25dfb0 100644 --- a/server/src/db/user-authorization-snapshot-db.ts +++ b/server/src/db/user-authorization-snapshot-db.ts @@ -27,6 +27,8 @@ export interface AuthorizationSnapshot { /** Distinct from a missing credential or denied organization access. */ export class AuthorizationSnapshotUnavailableError extends Error { + readonly retryable = true as const; + constructor(message = 'Authorization snapshot unavailable') { super(message); this.name = 'AuthorizationSnapshotUnavailableError'; @@ -61,6 +63,7 @@ async function querySnapshot(statement: string, parameters: [string, string | nu // querySnapshot owns the one retry so checkout and statement failures // share this request's single absolute authorization budget. retryTransientCheckout: false, + deadlineMs, }); if (Date.now() >= deadlineMs) throw new AuthorizationSnapshotUnavailableError(); return result; diff --git a/server/tests/unit/db-client-checkout.test.ts b/server/tests/unit/db-client-checkout.test.ts index dc097bc9db9..b1e9dcb8877 100644 --- a/server/tests/unit/db-client-checkout.test.ts +++ b/server/tests/unit/db-client-checkout.test.ts @@ -307,6 +307,269 @@ describe('db client checkout and health checks', () => { expect(query.mock.calls[3]).toEqual(['SELECT $1 AS value', [1]]); expect(query.mock.calls[4]).toEqual(['COMMIT']); expect(release).toHaveBeenCalledTimes(1); + expect(release).toHaveBeenCalledWith(); + + await db.closeDatabase(); + }); + + const deadlinePhases = [ + { label: 'BEGIN', index: 0 }, + { label: 'statement_timeout set_config', index: 1 }, + { label: 'lock_timeout set_config', index: 2 }, + { label: 'authorization SELECT', index: 3 }, + { label: 'COMMIT', index: 4 }, + ] as const; + + const uncertainBehaviors = ['hung', 'rejected', 'late-resolving'] as const; + + it.each(deadlinePhases.flatMap(phase => uncertainBehaviors.map(behavior => ({ + ...phase, + behavior, + }))))( + 'destroys the client when $label is $behavior', + async ({ index, behavior }) => { + vi.useFakeTimers(); + const pg = mockPg(); + const release = vi.fn(); + let settleLate: (() => void) | undefined; + const targetError = new Error('ambiguous client operation failure'); + const query = vi.fn().mockImplementation((text: string) => { + const callIndex = query.mock.calls.length - 1; + if (callIndex === index) { + if (behavior === 'rejected') return Promise.reject(targetError); + return new Promise((resolve) => { + if (behavior === 'late-resolving') { + settleLate = () => resolve({ rows: [{ principal: 'late-principal', epoch: 'late-epoch' }] }); + } + }); + } + return Promise.resolve({ + rows: text === 'SELECT authorization_snapshot' ? [{ principal: 'accepted-principal' }] : [], + }); + }); + pg.poolConnect.mockResolvedValue({ query, release }); + + const db = await import('../../src/db/client.js'); + db.initializeDatabase({ connectionString: 'postgresql://localhost/test' }); + + const pending = db.queryWithTimeout('SELECT authorization_snapshot', undefined, 50); + const rejection = behavior === 'rejected' + ? expect(pending).rejects.toBe(targetError) + : expect(pending).rejects.toBeInstanceOf(db.DatabaseQueryDeadlineExceededError); + await vi.advanceTimersByTimeAsync(0); + expect(query).toHaveBeenCalledTimes(index + 1); + if (behavior !== 'rejected') await vi.advanceTimersByTimeAsync(50); + await rejection; + + expect(release).toHaveBeenCalledTimes(1); + expect(release).toHaveBeenCalledWith(true); + expect(query).toHaveBeenCalledTimes(index + 1); + + settleLate?.(); + await Promise.resolve(); + await Promise.resolve(); + expect(query).toHaveBeenCalledTimes(index + 1); + expect(release).toHaveBeenCalledTimes(1); + + await db.closeDatabase(); + }, + ); + + it('destroys a client after a lost COMMIT acknowledgement', async () => { + const pg = mockPg(); + const release = vi.fn(); + let committed = false; + const lostAcknowledgement = Object.assign(new Error('connection reset after commit'), { + code: 'ECONNRESET', + }); + const query = vi.fn().mockImplementation(async (text: string) => { + if (text === 'COMMIT') { + committed = true; + throw lostAcknowledgement; + } + return { rows: [] }; + }); + pg.poolConnect.mockResolvedValue({ query, release }); + + const db = await import('../../src/db/client.js'); + db.initializeDatabase({ connectionString: 'postgresql://localhost/test' }); + + await expect(db.queryWithTimeout('SELECT authorization_snapshot', undefined, 5_000)) + .rejects.toBe(lostAcknowledgement); + expect(committed).toBe(true); + expect(query).not.toHaveBeenCalledWith('ROLLBACK'); + expect(release).toHaveBeenCalledTimes(1); + expect(release).toHaveBeenCalledWith(true); + + await db.closeDatabase(); + }); + + it.each(uncertainBehaviors)( + 'bounds a $behavior ROLLBACK and destroys the uncertain client', + async (behavior) => { + vi.useFakeTimers(); + const pg = mockPg(); + const release = vi.fn(); + let settleLate: (() => void) | undefined; + const statementError = Object.assign(new Error('definite statement rejection'), { code: '42P01' }); + const rollbackError = new Error('rollback acknowledgement unavailable'); + const query = vi.fn().mockImplementation((text: string) => { + if (text === 'SELECT authorization_snapshot') return Promise.reject(statementError); + if (text === 'ROLLBACK') { + if (behavior === 'rejected') return Promise.reject(rollbackError); + return new Promise((resolve) => { + if (behavior === 'late-resolving') settleLate = () => resolve({ rows: [] }); + }); + } + return Promise.resolve({ rows: [] }); + }); + pg.poolConnect.mockResolvedValue({ query, release }); + + const db = await import('../../src/db/client.js'); + db.initializeDatabase({ connectionString: 'postgresql://localhost/test' }); + + const pending = db.queryWithTimeout('SELECT authorization_snapshot', undefined, 50); + const rejection = expect(pending).rejects.toBe(statementError); + await vi.advanceTimersByTimeAsync(0); + expect(query).toHaveBeenLastCalledWith('ROLLBACK'); + if (behavior !== 'rejected') await vi.advanceTimersByTimeAsync(50); + await rejection; + + expect(release).toHaveBeenCalledTimes(1); + expect(release).toHaveBeenCalledWith(true); + settleLate?.(); + await Promise.resolve(); + await Promise.resolve(); + expect(release).toHaveBeenCalledTimes(1); + + await db.closeDatabase(); + }, + ); + + it('reuses a client only after a definite statement failure and confirmed rollback', async () => { + const pg = mockPg(); + const release = vi.fn(); + const statementError = Object.assign(new Error('relation does not exist'), { code: '42P01' }); + const query = vi.fn().mockImplementation(async (text: string) => { + if (text === 'SELECT authorization_snapshot') throw statementError; + return { rows: [] }; + }); + pg.poolConnect.mockResolvedValue({ query, release }); + + const db = await import('../../src/db/client.js'); + db.initializeDatabase({ connectionString: 'postgresql://localhost/test' }); + + await expect(db.queryWithTimeout('SELECT authorization_snapshot', undefined, 5_000)) + .rejects.toBe(statementError); + expect(query).toHaveBeenLastCalledWith('ROLLBACK'); + expect(release).toHaveBeenCalledTimes(1); + expect(release).toHaveBeenCalledWith(); + + await db.closeDatabase(); + }); + + it('does not starve an eight-client pool across nine timed-out attempts', async () => { + vi.useFakeTimers(); + const pg = mockPg(); + let checkedOut = 0; + let maximumCheckedOut = 0; + const releases: Array> = []; + pg.poolConnect.mockImplementation(() => { + if (checkedOut >= 8) return new Promise(() => undefined); + checkedOut += 1; + maximumCheckedOut = Math.max(maximumCheckedOut, checkedOut); + const release = vi.fn(() => { + checkedOut -= 1; + }); + releases.push(release); + return Promise.resolve({ query: vi.fn(() => new Promise(() => undefined)), release }); + }); + + const db = await import('../../src/db/client.js'); + db.initializeDatabase({ connectionString: 'postgresql://localhost/test', maxPoolSize: 8 }); + + for (let attempt = 0; attempt < 9; attempt++) { + const pending = db.queryWithTimeout('SELECT authorization_snapshot', undefined, 10); + const rejection = expect(pending).rejects.toBeInstanceOf(db.DatabaseQueryDeadlineExceededError); + await vi.advanceTimersByTimeAsync(10); + await rejection; + } + + expect(pg.poolConnect).toHaveBeenCalledTimes(9); + expect(releases).toHaveLength(9); + expect(releases.every(release => release.mock.calls.length === 1 + && release.mock.calls[0]?.[0] === true)).toBe(true); + expect(checkedOut).toBe(0); + expect(maximumCheckedOut).toBe(1); + + await db.closeDatabase(); + }); + + it('does not accept a principal or epoch from a late authorization result', async () => { + vi.useFakeTimers(); + const pg = mockPg(); + const lateRelease = vi.fn(); + const freshRelease = vi.fn(); + let resolveLate!: (value: { rows: Array> }) => void; + const snapshotRow = (suffix: string, epoch: string) => ({ + in_recovery: false, + authenticated_user_id: `user_${suffix}`, + canonical_user_id: `canonical_${suffix}`, + identity_id: `identity_${suffix}`, + authorization_epoch: epoch, + email: `${suffix}@example.test`, + email_verified: true, + first_name: suffix, + last_name: 'Snapshot', + grant_id: null, + grant_organization_id: null, + grant_role: null, + grant_effective_from: null, + grant_effective_until: null, + }); + const lateQuery = vi.fn().mockImplementation((text: string) => { + if (text.includes('pg_catalog.pg_is_in_recovery')) { + return new Promise((resolve) => { + resolveLate = resolve; + }); + } + return Promise.resolve({ rows: [] }); + }); + const freshQuery = vi.fn().mockImplementation((text: string) => Promise.resolve({ + rows: text.includes('pg_catalog.pg_is_in_recovery') ? [snapshotRow('fresh', '22')] : [], + })); + pg.poolConnect + .mockResolvedValueOnce({ query: lateQuery, release: lateRelease }) + .mockResolvedValueOnce({ query: freshQuery, release: freshRelease }); + + const db = await import('../../src/db/client.js'); + const snapshots = await import('../../src/db/user-authorization-snapshot-db.js'); + db.initializeDatabase({ connectionString: 'postgresql://localhost/test' }); + + const lateAttempt = snapshots.loadAuthorizationSnapshot('user_late', 'org_snapshot'); + const unavailable = expect(lateAttempt).rejects.toMatchObject({ + name: 'AuthorizationSnapshotUnavailableError', + retryable: true, + }); + await vi.advanceTimersByTimeAsync(2_000); + await unavailable; + expect(lateRelease).toHaveBeenCalledTimes(1); + expect(lateRelease).toHaveBeenCalledWith(true); + + resolveLate({ rows: [snapshotRow('late', '999')] }); + await Promise.resolve(); + await Promise.resolve(); + + await expect(snapshots.loadAuthorizationSnapshot('user_fresh', 'org_snapshot')) + .resolves.toMatchObject({ + authenticatedUserId: 'user_fresh', + canonicalUserId: 'canonical_fresh', + identityId: 'identity_fresh', + authorizationEpoch: '22', + }); + expect(lateRelease).toHaveBeenCalledTimes(1); + expect(freshRelease).toHaveBeenCalledTimes(1); + expect(freshRelease).toHaveBeenCalledWith(); await db.closeDatabase(); }); diff --git a/server/tests/unit/user-authorization-snapshot-timeout.test.ts b/server/tests/unit/user-authorization-snapshot-timeout.test.ts index aa295be55f0..c244ab1a9be 100644 --- a/server/tests/unit/user-authorization-snapshot-timeout.test.ts +++ b/server/tests/unit/user-authorization-snapshot-timeout.test.ts @@ -65,8 +65,14 @@ describe('authorization snapshot query deadline and connection retry', () => { expect(boundedQuery).toHaveBeenCalledTimes(2); const [first, second] = boundedQuery.mock.calls; expect(first[2]).toBe(2_000); - expect(first[3]).toEqual({ retryTransientCheckout: false }); - expect(second).toEqual([first[0], first[1], 1_300, { retryTransientCheckout: false }]); + expect(first[3]).toEqual({ + retryTransientCheckout: false, + deadlineMs: Date.parse('2026-01-01T00:00:02Z'), + }); + expect(second).toEqual([first[0], first[1], 1_300, { + retryTransientCheckout: false, + deadlineMs: first[3]?.deadlineMs, + }]); expect(first[1]).toEqual([USER_ID, ORGANIZATION_ID]); }); From 0c14d9fd9b49a90faec3f17899aa2e44f686d2a3 Mon Sep 17 00:00:00 2001 From: Brian O'Kelley Date: Thu, 17 Sep 2026 13:36:22 +0000 Subject: [PATCH 7/7] fix(auth): preserve credential lifecycle in primary snapshots --- .../src/db/user-authorization-snapshot-db.ts | 29 ++++++++--- ...thorization-deletion-cross-replica.test.ts | 3 +- .../user-authorization-snapshot.test.ts | 40 ++++++++++++-- .../unit/addie-admin-global-auth.test.ts | 52 +++++++------------ .../unit/authorization-epoch-session.test.ts | 52 +++++++++++++++++++ server/tests/unit/db-client-checkout.test.ts | 4 +- .../unit/network-health-security.test.ts | 21 +------- ...ser-authorization-snapshot-timeout.test.ts | 12 ++++- .../working-group-admin-global-auth.test.ts | 21 +------- 9 files changed, 144 insertions(+), 90 deletions(-) diff --git a/server/src/db/user-authorization-snapshot-db.ts b/server/src/db/user-authorization-snapshot-db.ts index d0a2b25dfb0..68af315dcb2 100644 --- a/server/src/db/user-authorization-snapshot-db.ts +++ b/server/src/db/user-authorization-snapshot-db.ts @@ -37,6 +37,8 @@ export class AuthorizationSnapshotUnavailableError extends Error { interface SnapshotRow { in_recovery: boolean; + terminal_marker: boolean; + primary_count: string; authenticated_user_id: string | null; canonical_user_id: string | null; identity_id: string | null; @@ -95,6 +97,14 @@ export async function loadAuthorizationSnapshot( credential.workos_user_id AS authenticated_user_id, primary_binding.workos_user_id AS canonical_user_id, binding.identity_id, + COALESCE(primary_binding.primary_count, 0)::text AS primary_count, + EXISTS ( + SELECT 1 FROM registry_audit_log audit + WHERE audit.workos_user_id = $1 + AND audit.action IN ( + 'identity_credential_deleted', 'identity_primary_deletion_quarantined' + ) + ) AS terminal_marker, COALESCE(epoch.epoch, 0)::text AS authorization_epoch, credential.email, credential.email_verified, credential.first_name, credential.last_name, credential_grant.id AS grant_id, @@ -108,8 +118,11 @@ export async function loadAuthorizationSnapshot( LEFT JOIN users credential ON credential.workos_user_id = $1 LEFT JOIN identity_workos_users binding ON binding.workos_user_id = credential.workos_user_id - LEFT JOIN identity_workos_users primary_binding - ON primary_binding.identity_id = binding.identity_id AND primary_binding.is_primary + LEFT JOIN LATERAL ( + SELECT COUNT(*) AS primary_count, MIN(candidate.workos_user_id) AS workos_user_id + FROM identity_workos_users candidate + WHERE candidate.identity_id = binding.identity_id AND candidate.is_primary + ) primary_binding ON TRUE LEFT JOIN authorization_epochs epoch ON epoch.workos_user_id = credential.workos_user_id LEFT JOIN organization_credential_grants credential_grant @@ -125,12 +138,12 @@ export async function loadAuthorizationSnapshot( if (!row || row.in_recovery) { throw new AuthorizationSnapshotUnavailableError(); } - if (!row.authenticated_user_id) return null; - // Migration 460 backfills bindings and creates them on every user insert. - // Missing attribution is an integrity failure, not a singleton fallback. - if (!row.identity_id || !row.canonical_user_id) { - throw new AuthorizationSnapshotUnavailableError(); - } + // Preserve the credential lifecycle boundary even if a stale provider event + // recreates a users row. Durable deletion/quarantine markers are terminal. + // Missing or ambiguous identity routing is also terminal, never a fallback + // to a linked credential or an implicit singleton identity. + if (row.terminal_marker || !row.authenticated_user_id || !row.identity_id + || row.primary_count !== '1' || !row.canonical_user_id) return null; const credentialGrant = row.grant_id ? Object.freeze({ diff --git a/server/tests/integration/authorization-deletion-cross-replica.test.ts b/server/tests/integration/authorization-deletion-cross-replica.test.ts index 14ea78e8ffe..c2bfbde77fa 100644 --- a/server/tests/integration/authorization-deletion-cross-replica.test.ts +++ b/server/tests/integration/authorization-deletion-cross-replica.test.ts @@ -157,7 +157,8 @@ describe('durable deletion fingerprint across independent replicas', () => { await requireAuthB(requestFor(cookieB), replicaBResponse, nextB); expect(nextB).toHaveBeenCalledTimes(1); - expect(mocks.authenticate).toHaveBeenCalledTimes(3); + // Provider authentication stays cached; fresh local authority denies reuse. + expect(mocks.authenticate).toHaveBeenCalledTimes(2); expect(replicaBResponse.status).toHaveBeenCalledWith(401); }); }); diff --git a/server/tests/integration/user-authorization-snapshot.test.ts b/server/tests/integration/user-authorization-snapshot.test.ts index dd00ef27621..d95cb8ac288 100644 --- a/server/tests/integration/user-authorization-snapshot.test.ts +++ b/server/tests/integration/user-authorization-snapshot.test.ts @@ -31,6 +31,7 @@ describe('primary database authorization snapshots', () => { }, 60000); async function cleanup() { + await pool.query('DELETE FROM registry_audit_log WHERE workos_user_id = ANY($1)', [USER_IDS]); await pool.query('DELETE FROM organization_memberships WHERE workos_organization_id = ANY($1)', [ORG_IDS]); await pool.query('DELETE FROM organizations WHERE workos_organization_id = ANY($1)', [ORG_IDS]); const identities = await pool.query<{ identity_id: string }>( @@ -303,16 +304,49 @@ describe('primary database authorization snapshots', () => { expect((await snapshot(CORPORATE_ID)).canonicalUserId).toBe(cached.canonicalUserId); }); - it.each(['binding', 'primary'])('fails unavailable for a missing identity %s', async missing => { + it.each(['binding', 'primary'])('returns terminal denial for a missing identity %s', async missing => { if (missing === 'binding') { await pool.query('DELETE FROM identity_workos_users WHERE workos_user_id = $1', [PERSONAL_ID]); } else { await pool.query('UPDATE identity_workos_users SET is_primary = FALSE WHERE workos_user_id = $1', [PERSONAL_ID]); } - await expect(loadAuthorizationSnapshot(PERSONAL_ID, null)) - .rejects.toBeInstanceOf(AuthorizationSnapshotUnavailableError); + expect(await loadAuthorizationSnapshot(PERSONAL_ID, null)).toBeNull(); }); + it.each(['identity_credential_deleted', 'identity_primary_deletion_quarantined'])( + 'rejects a durable %s marker even with live rows and epoch zero', async action => { + await link(CORPORATE_ID, PERSONAL_ID); + await grant(PERSONAL_ID, PERSONAL_ORG); + const before = await snapshot(PERSONAL_ID, PERSONAL_ORG); + expect(before.authorizationEpoch).toBe('0'); + const writer = await pool.connect(); + try { + await writer.query('BEGIN'); + await writer.query( + `INSERT INTO registry_audit_log + (workos_organization_id, workos_user_id, action, resource_type, resource_id) + VALUES ($1, $2::text, $3, 'user', $2::text)`, + [PERSONAL_ORG, PERSONAL_ID, action], + ); + // The uncommitted marker cannot leak into the MVCC snapshot. + expect(await snapshot(PERSONAL_ID, PERSONAL_ORG)).toEqual(before); + await writer.query('COMMIT'); + expect(await loadAuthorizationSnapshot(PERSONAL_ID, PERSONAL_ORG)).toBeNull(); + expect((await snapshot(CORPORATE_ID)).authenticatedUserId).toBe(CORPORATE_ID); + // Even a recreated user/binding/epoch cannot erase the durable marker. + await pool.query('DELETE FROM users WHERE workos_user_id = $1', [PERSONAL_ID]); + await pool.query( + `INSERT INTO users (workos_user_id, email, workos_created_at, workos_updated_at) + VALUES ($1, 'sam.personal@example.test', NOW(), NOW())`, [PERSONAL_ID], + ); + expect(await loadAuthorizationSnapshot(PERSONAL_ID, PERSONAL_ORG)).toBeNull(); + } finally { + await writer.query('ROLLBACK'); + writer.release(); + } + }, + ); + it('fails unavailable on database errors instead of replaying a warm snapshot', async () => { await snapshot(PERSONAL_ID, PERSONAL_ORG); vi.spyOn(database, 'queryWithTimeout').mockRejectedValueOnce(new Error('private database connection details')); diff --git a/server/tests/unit/addie-admin-global-auth.test.ts b/server/tests/unit/addie-admin-global-auth.test.ts index 6e321777253..891b393f1e8 100644 --- a/server/tests/unit/addie-admin-global-auth.test.ts +++ b/server/tests/unit/addie-admin-global-auth.test.ts @@ -13,11 +13,11 @@ const mocks = vi.hoisted(() => ({ getAdminWorkingGroupIdBySlug: vi.fn(), isAdminGroupMember: vi.fn(), poolQuery: vi.fn(), + snapshotQuery: vi.fn(), getWebConversations: vi.fn(), getModelExecutionReadiness: vi.fn(), getRouterShadowSummary: vi.fn(), serveHtmlWithConfig: vi.fn(), - readCredentialAuthorizationLifecycle: vi.fn(), })); vi.hoisted(() => { @@ -52,13 +52,9 @@ vi.mock('../../src/db/org-filters.js', () => ({ vi.mock('../../src/db/client.js', async (importOriginal) => ({ ...(await importOriginal()), getPool: () => ({ query: mocks.poolQuery }), - queryWithTimeout: mocks.poolQuery, + queryWithTimeout: mocks.snapshotQuery, })); -vi.mock('../../src/db/authorization-epoch-db.js', async (importOriginal) => ({ - ...(await importOriginal()), - readCredentialAuthorizationLifecycle: mocks.readCredentialAuthorizationLifecycle, -})); vi.mock('../../src/db/working-group-db.js', () => ({ WorkingGroupDatabase: class WorkingGroupDatabase { @@ -123,6 +119,7 @@ describe('Addie real global-admin boundary', () => { beforeEach(() => { vi.clearAllMocks(); + mocks.snapshotQuery.mockReset().mockImplementation((...args) => mocks.poolQuery(...args)); mocks.createValidation.mockImplementation( ({ value }: { value: string }) => Promise.resolve( validatedTenantKey(value.includes('read') ? 'admin:read' : 'admin:*'), @@ -131,27 +128,13 @@ describe('Addie real global-admin boundary', () => { mocks.resolveEffectiveMembership.mockResolvedValue({ is_member: true }); mocks.checkPlatformBanForApiKey.mockResolvedValue({ banned: false }); mocks.checkPlatformBan.mockResolvedValue({ banned: false }); - mocks.readCredentialAuthorizationLifecycle.mockImplementation((workosUserId: string) => - Promise.resolve({ - status: 'active', - snapshot: { - workos_user_id: workosUserId, - email: 'sso-admin@example.test', - first_name: 'SSO', - last_name: 'Admin', - identity_id: '00000000-0000-4000-8000-000000000001', - primary_workos_user_id: workosUserId, - fingerprint: '', - }, - }), - ); mocks.getAdminWorkingGroupIdBySlug.mockResolvedValue('wg_aao_admin'); mocks.isAdminGroupMember.mockResolvedValue(true); mocks.poolQuery.mockImplementation((sql: string) => { if (sql.includes('pg_catalog.pg_is_in_recovery()')) { return Promise.resolve({ rows: [{ - in_recovery: false, + in_recovery: false, terminal_marker: false, primary_count: '1', authenticated_user_id: 'user_sso_admin', canonical_user_id: 'user_sso_admin', identity_id: 'identity_sso_admin', @@ -287,18 +270,13 @@ describe('Addie real global-admin boundary', () => { }); it.each([true, false])('keeps credential admin authority %s after lifecycle routing to a linked primary', async (credentialIsAdmin) => { - mocks.readCredentialAuthorizationLifecycle.mockResolvedValue({ - status: 'active', - snapshot: { - workos_user_id: 'user_sso_admin', - email: 'sso-admin@example.test', - first_name: 'SSO', - last_name: 'Admin', - identity_id: '00000000-0000-4000-8000-000000000001', - primary_workos_user_id: 'user_linked_primary', - fingerprint: '', - }, - }); + mocks.snapshotQuery.mockResolvedValue({ rows: [{ + in_recovery: false, terminal_marker: false, primary_count: '1', + authenticated_user_id: 'user_sso_admin', canonical_user_id: 'user_linked_primary', + identity_id: '00000000-0000-4000-8000-000000000001', authorization_epoch: '0', + email: 'sso-admin@example.test', email_verified: true, + first_name: 'SSO', last_name: 'Admin', grant_id: null, + }] }); mocks.isAdminGroupMember.mockImplementation(async (_groupId, userId) => userId === 'user_sso_admin' ? credentialIsAdmin : !credentialIsAdmin, ); @@ -326,7 +304,13 @@ describe('Addie real global-admin boundary', () => { expect(warm.status).toBe(200); mocks.getWebConversations.mockClear(); mocks.isAdminGroupMember.mockClear(); - mocks.readCredentialAuthorizationLifecycle.mockResolvedValue({ status: 'terminal', reason }); + mocks.snapshotQuery.mockResolvedValue({ rows: [{ + in_recovery: false, authenticated_user_id: 'user_sso_admin', + identity_id: '00000000-0000-4000-8000-000000000001', + canonical_user_id: reason === 'missing_primary' ? null : 'user_sso_admin', + primary_count: reason === 'missing_primary' ? '0' : '1', + terminal_marker: reason === 'deleted_or_quarantined', + }] }); const denied = await request(app).get('/api/admin/addie/conversations').set('Cookie', cookie); expect(denied.status).toBe(401); diff --git a/server/tests/unit/authorization-epoch-session.test.ts b/server/tests/unit/authorization-epoch-session.test.ts index f99feded240..43044c6d819 100644 --- a/server/tests/unit/authorization-epoch-session.test.ts +++ b/server/tests/unit/authorization-epoch-session.test.ts @@ -89,6 +89,58 @@ beforeEach(() => { mocks.verifyWorkOSJWT.mockResolvedValue({ sub: AUTHENTICATED_ID, email: PROVIDER_USER.email, isM2M: false }); }); +describe('deletion across provider caches', () => { + it.each([ + ['optional to required', optionalAuth, requireAuth], + ['required to optional', requireAuth, optionalAuth], + ] as const)('rejects epoch-zero deletion across %s authentication', async (_label, warm, check) => { + const token = `sealed-shared-deletion-${++sequence}`; + mocks.loadAuthorizationSnapshot.mockResolvedValue(snapshot({ authorizationEpoch: '0' })); + const warmNext = vi.fn(); + await warm(request(token), response(), warmNext); + expect(warmNext).toHaveBeenCalledOnce(); + mocks.loadAuthorizationSnapshot.mockResolvedValue(null); + const req = request(token); + const res = response(); + const next = vi.fn(); + await check(req, res, next); + expect(res.status).toHaveBeenCalledWith(401); + expect(req.user).toBeUndefined(); + expect(next).not.toHaveBeenCalled(); + expect(mocks.authenticate).toHaveBeenCalledTimes(1); + }); + + it('rejects deleted credentials in independent replicas when only replica A receives eviction', async () => { + vi.resetModules(); + const replicaA = await import('../../src/middleware/auth.js'); + vi.resetModules(); + const replicaB = await import('../../src/middleware/auth.js'); + try { + const tokenA = `sealed-replica-a-${++sequence}`; + const tokenB = `sealed-replica-b-${++sequence}`; + mocks.loadAuthorizationSnapshot.mockResolvedValue(snapshot({ authorizationEpoch: '0' })); + const nextA = vi.fn(); + const nextB = vi.fn(); + await replicaA.requireAuth(request(tokenA), response(), nextA); + await replicaB.requireAuth(request(tokenB), response(), nextB); + expect(nextA).toHaveBeenCalledOnce(); + expect(nextB).toHaveBeenCalledOnce(); + replicaA.invalidateSessionsForUsers([AUTHENTICATED_ID]); + mocks.loadAuthorizationSnapshot.mockResolvedValue(null); + const req = request(tokenB); + const res = response(); + await replicaB.requireAuth(req, res, nextB); + expect(res.status).toHaveBeenCalledWith(401); + expect(req.user).toBeUndefined(); + expect(nextB).toHaveBeenCalledOnce(); + expect(mocks.authenticate).toHaveBeenCalledTimes(2); + } finally { + replicaA.stopAuthTimers(); + replicaB.stopAuthTimers(); + } + }); +}); + const organizationSelectors: Array<[string, (req: Request, organizationId: string) => void]> = [ ['header', (req, org) => { req.headers['x-organization-id'] = org; }], ['query org', (req, org) => { req.query.org = org; }], diff --git a/server/tests/unit/db-client-checkout.test.ts b/server/tests/unit/db-client-checkout.test.ts index b1e9dcb8877..2629a2d0ee3 100644 --- a/server/tests/unit/db-client-checkout.test.ts +++ b/server/tests/unit/db-client-checkout.test.ts @@ -512,7 +512,7 @@ describe('db client checkout and health checks', () => { const freshRelease = vi.fn(); let resolveLate!: (value: { rows: Array> }) => void; const snapshotRow = (suffix: string, epoch: string) => ({ - in_recovery: false, + in_recovery: false, terminal_marker: false, primary_count: '1', authenticated_user_id: `user_${suffix}`, canonical_user_id: `canonical_${suffix}`, identity_id: `identity_${suffix}`, @@ -616,7 +616,7 @@ describe('db client checkout and health checks', () => { if (text.includes('pg_catalog.pg_is_in_recovery')) { return { rows: [{ - in_recovery: false, + in_recovery: false, terminal_marker: false, primary_count: '1', authenticated_user_id: 'user_retry_checkout', canonical_user_id: 'user_retry_checkout', identity_id: 'fd3043f7-cb4f-43c7-9b81-22ac97576150', diff --git a/server/tests/unit/network-health-security.test.ts b/server/tests/unit/network-health-security.test.ts index c2c2add7b11..9ad5ce4f92a 100644 --- a/server/tests/unit/network-health-security.test.ts +++ b/server/tests/unit/network-health-security.test.ts @@ -20,7 +20,6 @@ const mocks = vi.hoisted(() => ({ upsertAlertRule: vi.fn(), resolveAlert: vi.fn(), serveHtmlWithConfig: vi.fn(), - readCredentialAuthorizationLifecycle: vi.fn(), })); vi.hoisted(() => { @@ -58,10 +57,6 @@ vi.mock('../../src/db/client.js', async (importOriginal) => ({ queryWithTimeout: mocks.poolQuery, })); -vi.mock('../../src/db/authorization-epoch-db.js', async (importOriginal) => ({ - ...(await importOriginal()), - readCredentialAuthorizationLifecycle: mocks.readCredentialAuthorizationLifecycle, -})); vi.mock('../../src/addie/mcp/admin-tools.js', () => ({ isWebUserAAOAdmin: mocks.isWebUserAAOAdmin, @@ -203,20 +198,6 @@ describe('network-health global authorization boundary', () => { mocks.resolveEffectiveMembership.mockResolvedValue({ is_member: true }); mocks.checkPlatformBanForApiKey.mockResolvedValue({ banned: false }); mocks.checkPlatformBan.mockResolvedValue({ banned: false }); - mocks.readCredentialAuthorizationLifecycle.mockImplementation((workosUserId: string) => - Promise.resolve({ - status: 'active', - snapshot: { - workos_user_id: workosUserId, - email: 'user@example.test', - first_name: 'Regular', - last_name: 'User', - identity_id: '00000000-0000-4000-8000-000000000001', - primary_workos_user_id: workosUserId, - fingerprint: '', - }, - }), - ); mocks.isWebUserAAOAdmin.mockResolvedValue(false); mocks.poolQuery.mockImplementation((sql: string, params: unknown[]) => { if (sql.includes('pg_catalog.pg_is_in_recovery()')) { @@ -224,7 +205,7 @@ describe('network-health global authorization boundary', () => { const isAdmin = userId === 'user_platform_admin'; return Promise.resolve({ rows: [{ - in_recovery: false, + in_recovery: false, terminal_marker: false, primary_count: '1', authenticated_user_id: userId, canonical_user_id: userId, identity_id: `identity_${userId}`, diff --git a/server/tests/unit/user-authorization-snapshot-timeout.test.ts b/server/tests/unit/user-authorization-snapshot-timeout.test.ts index c244ab1a9be..610eb4a4302 100644 --- a/server/tests/unit/user-authorization-snapshot-timeout.test.ts +++ b/server/tests/unit/user-authorization-snapshot-timeout.test.ts @@ -17,7 +17,7 @@ const ORGANIZATION_ID = 'org_snapshot_timeout'; function queryResult(overrides: Record = {}) { return { rows: [{ - in_recovery: false, + in_recovery: false, terminal_marker: false, primary_count: '1', authenticated_user_id: USER_ID, canonical_user_id: USER_ID, identity_id: 'fd3043f7-cb4f-43c7-9b81-22ac97576150', @@ -51,6 +51,15 @@ describe('authorization snapshot query deadline and connection retry', () => { vi.useRealTimers(); }); + it.each([ + { terminal_marker: true }, { primary_count: '0' }, { primary_count: '2' }, + { identity_id: null }, { canonical_user_id: null }, + ])('denies terminal lifecycle state without retrying: %j', async invalid => { + boundedQuery.mockResolvedValue(queryResult(invalid)); + expect(await loadAuthorizationSnapshot(USER_ID, ORGANIZATION_ID)).toBeNull(); + expect(boundedQuery).toHaveBeenCalledOnce(); + }); + it('retries one transient connection failure with only the remaining absolute budget', async () => { boundedQuery.mockImplementationOnce(async () => { vi.setSystemTime(Date.now() + 700); @@ -122,7 +131,6 @@ describe('authorization snapshot query deadline and connection retry', () => { it.each([ { state: 'replica', row: { in_recovery: true } }, - { state: 'missing primary identity', row: { canonical_user_id: null } }, ])('does not retry an unavailable $state snapshot', async ({ row }) => { boundedQuery.mockResolvedValue(queryResult(row)); await expect(loadAuthorizationSnapshot(USER_ID, ORGANIZATION_ID)) diff --git a/server/tests/unit/working-group-admin-global-auth.test.ts b/server/tests/unit/working-group-admin-global-auth.test.ts index 3b312fc9719..659a8f9fffb 100644 --- a/server/tests/unit/working-group-admin-global-auth.test.ts +++ b/server/tests/unit/working-group-admin-global-auth.test.ts @@ -23,7 +23,6 @@ const mocks = vi.hoisted(() => ({ removeMembership: vi.fn(), invalidateMemberContextCache: vi.fn(), invalidateWebAdminStatusCache: vi.fn(), - readCredentialAuthorizationLifecycle: vi.fn(), })); vi.hoisted(() => { @@ -61,10 +60,6 @@ vi.mock('../../src/db/client.js', async (importOriginal) => ({ queryWithTimeout: mocks.poolQuery, })); -vi.mock('../../src/db/authorization-epoch-db.js', async (importOriginal) => ({ - ...(await importOriginal()), - readCredentialAuthorizationLifecycle: mocks.readCredentialAuthorizationLifecycle, -})); vi.mock('../../src/addie/mcp/admin-tools.js', async () => { const { isWebUserAAOAdmin } = await import( @@ -138,20 +133,6 @@ describe('working-group real global-admin boundary', () => { mocks.resolveEffectiveMembership.mockResolvedValue({ is_member: true }); mocks.checkPlatformBanForApiKey.mockResolvedValue({ banned: false }); mocks.checkPlatformBan.mockResolvedValue({ banned: false }); - mocks.readCredentialAuthorizationLifecycle.mockImplementation((workosUserId: string) => - Promise.resolve({ - status: 'active', - snapshot: { - workos_user_id: workosUserId, - email: 'sso-admin@example.test', - first_name: 'SSO', - last_name: 'Admin', - identity_id: '00000000-0000-4000-8000-000000000001', - primary_workos_user_id: workosUserId, - fingerprint: '', - }, - }), - ); mocks.getWorkingGroupBySlug.mockImplementation((slug: string) => Promise.resolve(slug === 'aao-admin' ? { id: 'wg_aao_admin', slug: 'aao-admin' } @@ -163,7 +144,7 @@ describe('working-group real global-admin boundary', () => { if (sql.includes('pg_catalog.pg_is_in_recovery()')) { return Promise.resolve({ rows: [{ - in_recovery: false, + in_recovery: false, terminal_marker: false, primary_count: '1', authenticated_user_id: 'user_sso_admin', canonical_user_id: 'user_sso_admin', identity_id: 'identity_sso_admin',