From 34d86a539a0803ef2eaceb60524c42db3082b35a Mon Sep 17 00:00:00 2001 From: Neil Date: Thu, 12 Nov 2026 16:50:53 -0800 Subject: [PATCH] fix(cloud): stale_token on create/secrets/GitHub allows for auth's 30 s iat backdate, so the desktop's one re-mint succeeds --- cloud/apps/sandbox-control/src/index.ts | 10 +++++----- cloud/apps/sandbox-control/src/member-changes.ts | 14 ++++++++++++++ .../sandbox-control/test/org-token-stale.test.ts | 15 +++++++++++++-- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/cloud/apps/sandbox-control/src/index.ts b/cloud/apps/sandbox-control/src/index.ts index 09720b2ea8a..869334bf4a6 100644 --- a/cloud/apps/sandbox-control/src/index.ts +++ b/cloud/apps/sandbox-control/src/index.ts @@ -47,7 +47,7 @@ import { WorkerEntrypoint } from 'cloudflare:workers' import type { RunnerBuild } from './build-runner' import { maintainPool, poolConfigFromEnv } from './pool' import { cachedCreateTiming } from './create-timing' -import { creatorLeftAccount, tokenPredatesMemberChange } from './member-changes' +import { creatorLeftAccount, tokenMintedBeforeMemberChange } from './member-changes' import { authenticateDesktop, OrgMismatchError } from './auth' import { authenticateApi } from './api/caller' import { handleApi, handleApiAs, isApiPath, type ApiContext, type ApiExtension } from './api/router' @@ -1417,7 +1417,7 @@ async function routeRequest(request: Request, env: Env, deps: Deps): Promise { + if (caller.tokenIssuedAt === undefined) { + return false + } + return tokenPredatesMemberChange(db, { ...caller, tokenIssuedAt: caller.tokenIssuedAt + AUTH_IAT_BACKDATE_MS }) +} + export async function tokenPredatesMemberChange(db: D1Database, caller: SandboxCaller): Promise { if (caller.tokenIssuedAt === undefined || caller.deviceSessionId || !caller.orgId) { return false diff --git a/cloud/apps/sandbox-control/test/org-token-stale.test.ts b/cloud/apps/sandbox-control/test/org-token-stale.test.ts index ae17a7b925a..3ddfdcb4bab 100644 --- a/cloud/apps/sandbox-control/test/org-token-stale.test.ts +++ b/cloud/apps/sandbox-control/test/org-token-stale.test.ts @@ -2,7 +2,7 @@ // minted for another team is refused with 409 org_mismatch instead of acting on the wrong team. A token minted // before a role change is refused (401 stale_token) where it would create, read secrets, or reach GitHub. import { describe, expect, it } from 'vitest' -import { recordMemberChange } from '../src/member-changes' +import { AUTH_IAT_BACKDATE_MS, recordMemberChange } from '../src/member-changes' import { createHarness } from './harness' async function setup() { @@ -58,7 +58,8 @@ describe('x-orca-org: a token for another team is refused', () => { describe('a token minted before a role change: 401 stale_token where it matters', () => { it('create, secrets and GitHub refuse it; a fresh token works', async () => { const { h, teamToken } = await setup() - h.advance(2_000) + // Minted well before the change, even allowing for auth's 30 s iat backdate. + h.advance(AUTH_IAT_BACKDATE_MS + 2_000) await recordMemberChange(h.env.SANDBOX_DB, 'org:acme', 'alice', 'role', h.now()) const create = await h.call('POST', '/v1/sandboxes', { bearer: teamToken, body: { workspaceId: 'ws-2', machineSize: 'standard' } }) expect(create.status).toBe(401) @@ -69,4 +70,14 @@ describe('a token minted before a role change: 401 stale_token where it matters' const fresh = await h.token('alice', 'acme', 'sandbox', { org_role: 'member' }) expect((await h.call('GET', '/v1/secrets', { bearer: fresh })).body.reason).not.toBe('stale_token') }) + + it('a token re-minted just after the change (iat backdated 30 s by auth) is fresh, so the retry works', async () => { + const { h } = await setup() + // Auth stamps iat = mint time - 30 s: a token minted 5 s after the change carries iat 25 s before it. + const reminted = await h.token('alice', 'acme', 'sandbox', { org_role: 'member' }) + h.advance(25_000) + await recordMemberChange(h.env.SANDBOX_DB, 'org:acme', 'alice', 'role', h.now()) + const res = await h.call('GET', '/v1/secrets', { bearer: reminted }) + expect(res.body?.reason).not.toBe('stale_token') + }) })