From 93f206681218cde84380b341ab61a63355fc3335 Mon Sep 17 00:00:00 2001 From: Neil Date: Fri, 13 Nov 2026 06:09:22 -0800 Subject: [PATCH 1/3] fix(cloud): OWNER-TRANSFER a workspace is only handed to a current team member, and a transferred workspace is in shared mode for credentials for good --- .../migrations/0059_ownership_transferred.sql | 9 + .../sandbox-control/src/account-providers.ts | 6 +- .../apps/sandbox-control/src/agent-sources.ts | 2 +- cloud/apps/sandbox-control/src/api/router.ts | 5 + .../apps/sandbox-control/src/api/settings.ts | 8 + .../sandbox-control/src/api/workspaces.ts | 3 + cloud/apps/sandbox-control/src/env.ts | 6 + cloud/apps/sandbox-control/src/index.ts | 32 +- cloud/apps/sandbox-control/src/lifecycle.ts | 5 +- cloud/apps/sandbox-control/src/member-git.ts | 7 +- cloud/apps/sandbox-control/src/secrets.ts | 8 +- cloud/apps/sandbox-control/src/session-env.ts | 2 +- .../sandbox-control/src/shared-workspace.ts | 14 + cloud/apps/sandbox-control/src/store.ts | 19 +- .../sandbox-control/src/team-membership.ts | 85 ++++ cloud/apps/sandbox-control/src/workspaces.ts | 33 +- .../test/agent-accounts.test.ts | 4 +- cloud/apps/sandbox-control/test/harness.ts | 4 +- .../test/owner-transfer.test.ts | 443 ++++++++++++++++++ .../test/shared-workspace-secrets.test.ts | 2 +- .../sandbox-control/test/workspaces.test.ts | 16 +- cloud/apps/sandbox-control/wrangler.dev.jsonc | 3 + 22 files changed, 676 insertions(+), 40 deletions(-) create mode 100644 cloud/apps/sandbox-control/migrations/0059_ownership_transferred.sql create mode 100644 cloud/apps/sandbox-control/src/shared-workspace.ts create mode 100644 cloud/apps/sandbox-control/src/team-membership.ts create mode 100644 cloud/apps/sandbox-control/test/owner-transfer.test.ts diff --git a/cloud/apps/sandbox-control/migrations/0059_ownership_transferred.sql b/cloud/apps/sandbox-control/migrations/0059_ownership_transferred.sql new file mode 100644 index 00000000000..f14bfb72818 --- /dev/null +++ b/cloud/apps/sandbox-control/migrations/0059_ownership_transferred.sql @@ -0,0 +1,9 @@ +-- OWNER-TRANSFER: a workspace handed to another owner is in shared mode for credentials from then +-- on (no personal secrets, personal agent accounts or owner-only git identity), because files the +-- previous owner left on its disk run again on wake. Set in the same write as the owner change and +-- never cleared; the new owner creates a fresh workspace to use personal credentials. Additive. +ALTER TABLE sandboxes ADD COLUMN ownership_transferred_at INTEGER; + +-- Workspaces transferred before this existed: the owner is not who created it. Fail closed. +UPDATE sandboxes SET ownership_transferred_at = updated_at + WHERE created_by IS NOT NULL AND created_by <> user_id AND status <> 'destroyed'; diff --git a/cloud/apps/sandbox-control/src/account-providers.ts b/cloud/apps/sandbox-control/src/account-providers.ts index 4d8f15c14a6..771a2a59ffa 100644 --- a/cloud/apps/sandbox-control/src/account-providers.ts +++ b/cloud/apps/sandbox-control/src/account-providers.ts @@ -79,11 +79,7 @@ async function verifyClaudeToken(credential: string, fetchImpl: typeof fetch): P return type === 'authentication_error' ? 'invalid' : 'unknown' } -/** D13: a workspace shared with teammates never uses a personal account. Fails closed: only a row - * known to be private counts as unshared. */ -export function isSharedWorkspace(row: Pick): boolean { - return row.visibility !== 'private' -} +export { isSharedWorkspace } from './shared-workspace' export const claudeAccount: AccountProvider = { agent: 'claude', diff --git a/cloud/apps/sandbox-control/src/agent-sources.ts b/cloud/apps/sandbox-control/src/agent-sources.ts index 5b465363efd..784bf675bbe 100644 --- a/cloud/apps/sandbox-control/src/agent-sources.ts +++ b/cloud/apps/sandbox-control/src/agent-sources.ts @@ -207,7 +207,7 @@ export async function deleteAgentSourcesForSandbox(db: D1Database, sandboxId: st export type AgentMode = 'account' | 'omit' | 'keep' export async function agentModesFor( db: D1Database, - row: Pick, + row: Pick, env: Readonly>, options: { providerEnabled: Record diff --git a/cloud/apps/sandbox-control/src/api/router.ts b/cloud/apps/sandbox-control/src/api/router.ts index 75aa4cc8110..5af20fe9cb5 100644 --- a/cloud/apps/sandbox-control/src/api/router.ts +++ b/cloud/apps/sandbox-control/src/api/router.ts @@ -454,8 +454,13 @@ async function route(request: Request, url: URL, caller: ApiCaller, ctx: ApiCont const environmentBuildId = body.environment ? await resolveEnvironmentBuild(ctx.db, caller.accountId, body.environment) : undefined await consumeCreateToken(life.db, life.config.guardrails!, caller.accountId, now) const { origin, originRef } = originFor(caller) + // OWNER-TRANSFER, belt and braces (a transferred workspace gets no in-VM token at all): a + // workspace made from inside a transferred one is transferred too, never a private one that + // would carry its owner's personal credentials. + const parent = caller.via === 'workspace' && caller.sandboxId ? await getWorkspace(ctx.db, caller.accountId, caller.sandboxId) : null const started = performance.now() const row = await createWorkspace(life, caller, { + ...(parent?.ownership_transferred_at != null ? { ownershipTransferredAt: parent.ownership_transferred_at } : {}), name: body.name, project, repo: body.repo, diff --git a/cloud/apps/sandbox-control/src/api/settings.ts b/cloud/apps/sandbox-control/src/api/settings.ts index 56ddc8edd7d..ed355b63003 100644 --- a/cloud/apps/sandbox-control/src/api/settings.ts +++ b/cloud/apps/sandbox-control/src/api/settings.ts @@ -1,6 +1,7 @@ // Per-account programmatic-access settings, and the per-boot workspace token for the in-VM CLI. import type { SandboxProviderApi } from '../sandbox-provider' import type { SandboxRow } from '../store' +import { getSandbox } from '../store' import { audit } from './audit' import { WORKSPACE_TOKEN_SCOPES, mintApiKey, revokeWorkspaceTokens } from './keys' import { installWorkspaceToken } from './vm-agent' @@ -54,6 +55,13 @@ export async function provisionWorkspaceToken( ): Promise<'installed' | 'disabled' | 'failed'> { try { await revokeWorkspaceTokens(deps.db, row.id, deps.now()) + // OWNER-TRANSFER: no token for a transferred workspace. It would act as the new owner, and a + // file the previous owner left could use it to create a private sibling with that owner's + // personal credentials. Read from the database, so a boot racing the transfer sees the flag. + const current = await getSandbox(deps.db, row.id) + if (!current || current.ownership_transferred_at != null) { + return 'disabled' + } const settings = await getAccountSettings(deps.db, row.account_id) if (!settings.vmCliEnabled || !deps.apiUrl) { return 'disabled' diff --git a/cloud/apps/sandbox-control/src/api/workspaces.ts b/cloud/apps/sandbox-control/src/api/workspaces.ts index 944fbd2755c..01cae7783d9 100644 --- a/cloud/apps/sandbox-control/src/api/workspaces.ts +++ b/cloud/apps/sandbox-control/src/api/workspaces.ts @@ -154,6 +154,8 @@ export async function createWorkspace( hidden?: boolean /** Caller-chosen sandbox workspace key (routine runs use one derived from the run, so a crash mid-boot is findable). */ workspaceId?: string + /** OWNER-TRANSFER: made from inside a transferred workspace, so it starts transferred (shared mode). */ + ownershipTransferredAt?: number | null } ): Promise { const repo = input.repo ?? (input.project?.repo_url ? { url: input.project.repo_url, ...(input.project.default_ref ? { ref: input.project.default_ref } : {}) } : undefined) @@ -163,6 +165,7 @@ export async function createWorkspace( machineSize: input.machineSize ?? input.project?.machine_size ?? 'standard', ...((input.region ?? input.project?.region) ? { region: (input.region ?? input.project?.region)! } : {}), ...(repo ? { repo } : {}), + ...(input.ownershipTransferredAt != null ? { ownershipTransferredAt: input.ownershipTransferredAt } : {}), ...((input.environmentBuildId ?? input.project?.build_id) ? { environmentBuildId: (input.environmentBuildId ?? input.project?.build_id)! } : {}) }) await setWorkspaceMeta(life.db, ready.row.id, { diff --git a/cloud/apps/sandbox-control/src/env.ts b/cloud/apps/sandbox-control/src/env.ts index 6404ef769a2..f14c368de6a 100644 --- a/cloud/apps/sandbox-control/src/env.ts +++ b/cloud/apps/sandbox-control/src/env.ts @@ -208,6 +208,12 @@ export type Env = { * GitHub install started by a setup session, the browser goes to /cloud/setup/next. */ CLOUD_SETUP_ORIGIN?: string + /** + * Origin of Orca Cloud auth's internal API (https), asked whether someone is in a team before a + * workspace is handed to them (team-membership.ts). Its own setting, so changing the setup pages' + * origin can never redirect an authorization call. Unset: every hand-off is refused (503). + */ + AUTH_INTERNAL_ORIGIN?: string /** Archived workspaces are destroyed this many days after archive (default 30). */ ARCHIVE_TTL_DAYS?: string /** Idle minutes before a sandbox sleeps (default 15; keep equal to billing's plan idleSleepMinutes). */ diff --git a/cloud/apps/sandbox-control/src/index.ts b/cloud/apps/sandbox-control/src/index.ts index 366e7563231..c04efa8fd69 100644 --- a/cloud/apps/sandbox-control/src/index.ts +++ b/cloud/apps/sandbox-control/src/index.ts @@ -159,6 +159,7 @@ import { DesktopCommandError, VmCommandError, startDesktop } from './sandbox-vm- import { forgetShareLink, handleShareGateway } from './share-gateway' import { handleShareRoute, revokeSandboxShares, sharePathMode, shareSecret, shareSecrets } from './share-links' import { SshKeyRequestSchema, updateSshKey } from './ssh-access' +import { createTeamMembershipCheck, type TeamMembershipCheck } from './team-membership' import { audit, auditSession } from './api/audit' import { ALL_EVENT_FAMILIES, AUTH_EVENT_FAMILIES, AuditIngestError, AuditIngestSchema, BILLING_EVENT_FAMILIES, ingestAuditEvents } from './api/audit-ingest' import { getSandbox, getSandboxByProviderName, listAccountSandboxes, markUserSlept, type SandboxRow } from './store' @@ -268,6 +269,8 @@ export type Deps = { onDeviceSessionRevoked?: (deviceSessionId: string) => Promise /** Share-gateway fetch to the VM's share proxy (tests). */ shareFetch?: typeof fetch + /** Is this person in this team now? Default: auth's live answer (team-membership.ts). Tests inject it. */ + teamMembership?: TeamMembershipCheck /** Billing setup-session notices (tests collect them). */ setupNotifier?: SetupNotifier /** Billing trial abuse-key notices after a bind (tests collect them). */ @@ -2289,8 +2292,25 @@ async function workspaceRoute(request: Request, url: URL, env: Env, deps: Deps, const current = (await getSandbox(env.SANDBOX_DB, row.id)) ?? row let patched: Awaited> = Object.keys(rest).length > 0 - ? await patchWorkspace(db, caller, current, rest, now) + ? await patchWorkspace(db, caller, current, rest, now, deps.teamMembership ?? createTeamMembershipCheck(env)) : { row: current, revoke: 'none' } + const transferred = patched.row.user_id !== current.user_id + if (transferred) { + // OWNER-TRANSFER. The owner and the transferred mark are already written together. Now + // stop the VM, so nothing the previous owner left running carries on (a stop keeps the + // disk only: the next wake is a new session with a freshly built, shared-mode env), and + // end everything that acted for them: its share links and its in-VM API token. + if (patched.row.status === 'running') { + const stopped = await suspendSandbox(life, patched.row).catch((error: unknown) => { + // Already marked transferred, so nothing personal can attach; reconcile stops it. + console.log(JSON.stringify({ event: 'owner_transfer_suspend_failed', sandboxId: row.id, error: String(error).slice(0, 120) })) + return null + }) + patched = { ...patched, row: stopped ?? (await getSandbox(env.SANDBOX_DB, row.id)) ?? patched.row } + } + await revokeSandboxShares(env.SANDBOX_DB, row.id, now) + await revokeWorkspaceTokens(env.SANDBOX_DB, row.id, now) + } // Who can open a workspace decides who its agents act as and whose secrets it gets, so both // directions of a share, and a change of owner, go in the team's audit log. if (patched.row.visibility !== current.visibility) { @@ -2306,7 +2326,7 @@ async function workspaceRoute(request: Request, url: URL, env: Env, deps: Deps, now ) } - if (patched.row.user_id !== current.user_id) { + if (transferred) { await auditSession( db, caller, @@ -2324,11 +2344,9 @@ async function workspaceRoute(request: Request, url: URL, env: Env, deps: Deps, } const revoked = patched.revoke === 'none' ? null : await revokeForAccessChange(life, patched.row, patched.revoke, caller.userId) - if ( - patched.row.status === 'running' && - (patched.row.user_id !== current.user_id || patched.row.visibility !== current.visibility) - ) { - // A new owner or a share changes which account (if any) the agents use (D10/D13): rebuild the env now. + if (patched.row.status === 'running' && !transferred && patched.row.visibility !== current.visibility) { + // A share changes which account (if any) the agents use (D10/D13): rebuild the env now. + // Never for a new owner: a transferred workspace is stopped, and its env is not rebuilt for them. const work = refreshSandboxEnv(life, patched.row).catch(() => false) if (deps.waitUntil) { deps.waitUntil(work) diff --git a/cloud/apps/sandbox-control/src/lifecycle.ts b/cloud/apps/sandbox-control/src/lifecycle.ts index ed5426094a7..0bbd9d0f0fe 100644 --- a/cloud/apps/sandbox-control/src/lifecycle.ts +++ b/cloud/apps/sandbox-control/src/lifecycle.ts @@ -671,6 +671,8 @@ export async function createSandbox( useEnvironment?: boolean title?: string visibility?: 'private' | 'team' + /** Set when an in-VM token of a transferred workspace creates this one: it is transferred too. */ + ownershipTransferredAt?: number | null section?: string /** * The desktop shows the clone as its own step (GET /v1/sandboxes/:id/clone): the repo clones @@ -767,7 +769,8 @@ export async function createSandbox( name: request.title ?? null, // A personal account has no team: its workspaces are always private. visibility: request.visibility === 'team' && caller.accountId.includes('org:') ? 'team' : 'private', - section: request.section ?? null + section: request.section ?? null, + ownership_transferred_at: request.ownershipTransferredAt ?? null }, deps.now() ).catch(async (error: unknown) => { diff --git a/cloud/apps/sandbox-control/src/member-git.ts b/cloud/apps/sandbox-control/src/member-git.ts index 87ff1518e49..92234efb2a1 100644 --- a/cloud/apps/sandbox-control/src/member-git.ts +++ b/cloud/apps/sandbox-control/src/member-git.ts @@ -41,13 +41,16 @@ export type MemberGitDeps = { /** Members whose GitHub identity this workspace may use: owner + active grantees, connected only. */ export async function connectedMembers(db: D1Database, row: SandboxRow): Promise { + // OWNER-TRANSFER: on a transferred workspace the owner has no entry of their own. They count + // only while they hold an active grant, exactly like any member of a shared workspace. + const ownerToo = row.ownership_transferred_at == null const { results } = await db .prepare( - `SELECT t.user_id FROM github_user_tokens t WHERE t.account_id = ? AND (t.user_id = ? OR t.user_id IN + `SELECT t.user_id FROM github_user_tokens t WHERE t.account_id = ? AND ((? = 1 AND t.user_id = ?) OR t.user_id IN (SELECT g.user_id FROM workspace_grants g WHERE g.sandbox_id = ? AND g.status = 'active')) ORDER BY t.user_id` ) - .bind(row.account_id, row.user_id, row.id) + .bind(row.account_id, ownerToo ? 1 : 0, row.user_id, row.id) .all<{ user_id: string }>() return results.map((r) => r.user_id) } diff --git a/cloud/apps/sandbox-control/src/secrets.ts b/cloud/apps/sandbox-control/src/secrets.ts index 11b8a6f3416..3fb6931f28e 100644 --- a/cloud/apps/sandbox-control/src/secrets.ts +++ b/cloud/apps/sandbox-control/src/secrets.ts @@ -13,6 +13,7 @@ import { createKeyring, type Keyring, type SealFormat, type SecretBinding } from import { createKekUnwrapper, SecretsKmsError } from './secrets-kms' import { AUTH_IAT_BACKDATE_MS } from './member-changes' import { BASE_URL_NAMES, baseUrlHost } from './provider-env' +import { isSharedWorkspace } from './shared-workspace' import type { SandboxRow } from './store' import { secretLog } from './secret-log' import { @@ -195,11 +196,12 @@ function repoOwner(accountId: string, repo: string): string { * never its owner's personal ones, the same rule as personal accounts (D13). Fails closed: only a row * known to be private counts as unshared. */ -export function ownersForSandbox(row: Pick) { +export function ownersForSandbox(row: Pick) { const repo = normalizeRepo(row.repo_url) return { org: ORG_ACCOUNT.test(row.account_id) ? row.account_id : null, - personal: row.visibility === 'private' ? personalOwner(row.user_id, row.account_id.startsWith('dev:')) : null, + // The one definition of "shared" (visibility, or ever transferred to another owner). + personal: isSharedWorkspace(row) ? null : personalOwner(row.user_id, row.account_id.startsWith('dev:')), repo: repo ? repoOwner(row.account_id, repo) : null } } @@ -358,7 +360,7 @@ export type ResolvedSecrets = { env: Record; scopes: Record + row: Pick ): Promise { const owners = ownersForSandbox(row) const pairs: [SecretScope, string][] = [] diff --git a/cloud/apps/sandbox-control/src/session-env.ts b/cloud/apps/sandbox-control/src/session-env.ts index c20fbebaa02..55f737d8e90 100644 --- a/cloud/apps/sandbox-control/src/session-env.ts +++ b/cloud/apps/sandbox-control/src/session-env.ts @@ -29,7 +29,7 @@ export type ProviderEnv = { env: Record; scopes: Record + row: Pick ): Promise { const { env: stored, scopes } = await resolveSandboxSecrets(db, keyring, row) // The proxy only needs the keys; dropping those of agents not on 'keep' happens at request time diff --git a/cloud/apps/sandbox-control/src/shared-workspace.ts b/cloud/apps/sandbox-control/src/shared-workspace.ts new file mode 100644 index 00000000000..8fc679ec56d --- /dev/null +++ b/cloud/apps/sandbox-control/src/shared-workspace.ts @@ -0,0 +1,14 @@ +// What counts as a shared workspace when deciding whose credentials a session may carry. One +// definition, used by secrets, agent accounts, the egress proxy and the member git rules. +import type { SandboxRow } from './store' + +/** + * D13: a workspace shared with teammates never uses a personal account. Fails closed: only a row + * known to be private counts as unshared. + * OWNER-TRANSFER: a workspace whose owner was ever changed is in shared mode for good. Its disk was + * set up by someone else, and whatever they left there runs when the new owner uses it, so nothing + * personal to the new owner is ever attached. A fresh workspace is the way to use personal credentials. + */ +export function isSharedWorkspace(row: Pick): boolean { + return row.visibility !== 'private' || row.ownership_transferred_at != null +} diff --git a/cloud/apps/sandbox-control/src/store.ts b/cloud/apps/sandbox-control/src/store.ts index 65a1407f5e8..2a156b40a80 100644 --- a/cloud/apps/sandbox-control/src/store.ts +++ b/cloud/apps/sandbox-control/src/store.ts @@ -63,6 +63,11 @@ export type SandboxRow = { archive_expires_at: number | null /** Registry (0006). `user_id` is the current owner (reassignable); `created_by` never changes. */ created_by: string | null + /** + * Set when the owner was changed, in the same write, and never cleared (0059). From then on the + * workspace is in shared mode for credentials: see isSharedWorkspace and ownersForSandbox. + */ + ownership_transferred_at: number | null /** Display name (0020's `name`; the API calls it `title`). */ name: string | null repo_branch: string | null @@ -119,7 +124,7 @@ export async function insertSandbox( SandboxRow, 'id' | 'account_id' | 'user_id' | 'workspace_id' | 'provider' | 'provider_name' | 'machine_size' | 'region' | 'repo_url' > & - Partial>, + Partial>, now: number ): Promise { // One transaction: bump the account's counter, then take its value as this row's number (W-123). @@ -135,14 +140,16 @@ export async function insertSandbox( .prepare( `INSERT INTO sandboxes (id, account_id, user_id, workspace_id, provider, provider_name, status, machine_size, region, generation, created_at, updated_at, repo_url, environment_id, environment_build_id, - source_snapshot_id, created_by, name, repo_branch, visibility, section, last_activity_at, last_activity_by, number) - VALUES (?, ?, ?, ?, ?, ?, 'creating', ?, ?, 0, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, + source_snapshot_id, created_by, name, repo_branch, visibility, section, last_activity_at, last_activity_by, + ownership_transferred_at, number) + VALUES (?, ?, ?, ?, ?, ?, 'creating', ?, ?, 0, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, (SELECT last_number FROM org_workspace_counters WHERE account_id = ?))` ) .bind(row.id, row.account_id, row.user_id, row.workspace_id, row.provider, row.provider_name, row.machine_size, row.region, now, now, row.repo_url, row.environment_id ?? null, row.environment_build_id ?? null, row.source_snapshot_id ?? null, row.user_id, row.name ?? null, - row.repo_branch ?? null, row.visibility ?? 'private', row.section ?? null, now, row.user_id, row.account_id) + row.repo_branch ?? null, row.visibility ?? 'private', row.section ?? null, now, row.user_id, + row.ownership_transferred_at ?? null, row.account_id) ]) const inserted = await getSandbox(db, row.id) if (!inserted) { @@ -234,9 +241,9 @@ const PATCHABLE = new Set([ ]) export type RegistryPatch = Partial< - Pick + Pick > -const REGISTRY_PATCHABLE = new Set(['name', 'repo_branch', 'visibility', 'section', 'archived_at', 'user_id', 'token_usd_micros']) +const REGISTRY_PATCHABLE = new Set(['name', 'repo_branch', 'visibility', 'section', 'archived_at', 'user_id', 'token_usd_micros', 'ownership_transferred_at']) /** * Registry metadata (title, visibility, owner, ...). Deliberately not a `transition`: it never diff --git a/cloud/apps/sandbox-control/src/team-membership.ts b/cloud/apps/sandbox-control/src/team-membership.ts new file mode 100644 index 00000000000..9a77fe933f9 --- /dev/null +++ b/cloud/apps/sandbox-control/src/team-membership.ts @@ -0,0 +1,85 @@ +// Is this person in this team right now? Asked of Orca Cloud auth, which answers from the live +// roster (auth's POST /v1/internal/org-membership). The control plane holds no member list of its +// own that may decide access: the names it stores are labels, and auth's pushes are best effort. +// +// Used before a workspace is handed to a new owner (OWNER-TRANSFER). Fails closed: anything but a +// clear "yes" refuses, and nothing here falls back to stored data. +// +// The bearer is derived, never the shared token itself: HMAC-SHA256 keyed with the token auth and +// the control plane already share (MEMBER_REMOVED_TOKEN here), over a fixed purpose string. Auth +// refuses the raw token on this route, and the derived key opens nothing on the control plane. +import { z } from 'zod' +import type { Env } from './env' + +export const MEMBERSHIP_KEY_PURPOSE = 'orca-cloud:control-plane-to-auth:org-membership:v1' +/** Auth's own roster read is capped at 4 s. */ +const TIMEOUT_MS = 6_000 + +export type MembershipAnswer = + | { member: true } + | { member: false } + /** Auth could not say (down, slow, rate limited, misconfigured): refuse, and say try again. */ + | { unavailable: string } + +export type TeamMembershipCheck = (accountId: string, userId: string) => Promise + +const Answer = z.looseObject({ member: z.boolean() }) + +function base64Url(bytes: Uint8Array): string { + let text = '' + for (const byte of bytes) { + text += String.fromCharCode(byte) + } + return btoa(text).replaceAll('+', '-').replaceAll('/', '_').replace(/=+$/, '') +} + +/** The bearer for auth's membership route, from the shared control-plane token. */ +export async function membershipKey(sharedToken: string): Promise { + const key = await crypto.subtle.importKey('raw', new TextEncoder().encode(sharedToken.trim()), { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']) + return base64Url(new Uint8Array(await crypto.subtle.sign('HMAC', key, new TextEncoder().encode(MEMBERSHIP_KEY_PURPOSE)))) +} + +/** + * The live check. `unavailable` when it is not configured (no AUTH_INTERNAL_ORIGIN or no shared + * token), for accounts auth does not know (personal and dev operator accounts), and for every + * answer from auth other than 200 with `member` true or false. + */ +export function createTeamMembershipCheck( + env: Pick, + fetchImpl: typeof fetch = fetch +): TeamMembershipCheck { + return async (accountId, userId) => { + const origin = env.AUTH_INTERNAL_ORIGIN?.replace(/\/+$/, '') + const token = env.MEMBER_REMOVED_TOKEN?.trim() + if (!origin || !origin.startsWith('https://') || !token || token.length < 32) { + return { unavailable: 'not_configured' } + } + // Only real teams: a dev operator account (`dev:org:…`) is not in auth, and is refused. + if (!accountId.startsWith('org:')) { + return { unavailable: 'unknown_account' } + } + let response: Response + try { + response = await fetchImpl(`${origin}/v1/internal/org-membership`, { + method: 'POST', + headers: { authorization: `Bearer ${await membershipKey(token)}`, 'content-type': 'application/json' }, + body: JSON.stringify({ orgId: accountId.slice('org:'.length), userId }), + redirect: 'manual', + signal: AbortSignal.timeout(TIMEOUT_MS) + }) + } catch { + return { unavailable: 'unreachable' } + } + if (response.status !== 200) { + await response.body?.cancel().catch(() => undefined) + // 401/400: our bug. 404: auth has no such route (or no token). 429: rate limited. 502: its + // directory could not answer. None of them is a "yes". + return { unavailable: `auth_${response.status}` } + } + const parsed = Answer.safeParse(await response.json().catch(() => null)) + if (!parsed.success) { + return { unavailable: 'unreadable_answer' } + } + return { member: parsed.data.member } + } +} diff --git a/cloud/apps/sandbox-control/src/workspaces.ts b/cloud/apps/sandbox-control/src/workspaces.ts index 20d67a837b7..958d1a69ff3 100644 --- a/cloud/apps/sandbox-control/src/workspaces.ts +++ b/cloud/apps/sandbox-control/src/workspaces.ts @@ -9,6 +9,7 @@ // Access rule: a workspace is visible to its owner, and to every member of the owning org when // visibility is `team`. Org owners/admins additionally see private rows' metadata (scope=all) so a // departed member's workspaces can be reassigned or archived — metadata only, never access. +import type { TeamMembershipCheck } from './team-membership' import { z } from 'zod' import type { SandboxCaller } from './auth' import type { MachineSize } from './entitlements' @@ -32,7 +33,7 @@ export type WorkspaceAccess = 'owner' | 'granted' | 'can_open' | 'none' export class WorkspaceError extends Error { constructor( - readonly code: 'not_found' | 'forbidden' | 'invalid_request' | 'conflict', + readonly code: 'not_found' | 'forbidden' | 'invalid_request' | 'conflict' | 'unavailable', readonly reason?: string ) { super(reason ? `${code}:${reason}` : code) @@ -44,7 +45,8 @@ export const WORKSPACE_ERROR_STATUS: Record = { not_found: 404, forbidden: 403, invalid_request: 400, - conflict: 409 + conflict: 409, + unavailable: 503 } export function isOrgAdmin(caller: SandboxCaller): boolean { @@ -214,6 +216,8 @@ export async function workspaceViews(db: D1Database, caller: SandboxCaller, rows archivedAt: row.archived_at, createdBy: row.created_by ?? row.user_id, ownerUserId: row.user_id, + // Set once the owner was ever changed: the workspace runs in shared mode for credentials. + ownershipTransferredAt: row.ownership_transferred_at, lastActivityAt: row.last_activity_at ?? row.updated_at, lastActivityBy: row.last_activity_by, cost: costView(row, compute.get(row.id) ?? 0, now), @@ -347,7 +351,13 @@ export async function patchWorkspace( caller: SandboxCaller, row: SandboxRow, patch: WorkspacePatch, - now: number + now: number, + /** + * Asked once the caller is known to be allowed to hand the workspace over, and before anything + * is written: is the new owner in this team right now? (team-membership.ts) Without one, no + * hand-off happens. + */ + confirmNewOwner?: TeamMembershipCheck ): Promise<{ row: SandboxRow; revoke: 'none' | 'non_owner' | 'all_but_new_owner' }> { const metadataOnly = patch.tokenUsdMicros !== undefined && Object.keys(patch).length === 1 if (metadataOnly) { @@ -372,7 +382,18 @@ export async function patchWorkspace( if (!row.account_id.includes('org:')) { throw new WorkspaceError('invalid_request', 'personal_account') } + // OWNER-TRANSFER: only to someone who is in the team now, by auth's live answer. Anything but + // a clear yes refuses, and nothing has been changed yet. + const answer = confirmNewOwner ? await confirmNewOwner(row.account_id, patch.ownerUserId) : { unavailable: 'not_configured' } + if ('unavailable' in answer) { + console.log(JSON.stringify({ event: 'owner_transfer_refused', sandboxId: row.id, reason: answer.unavailable })) + throw new WorkspaceError('unavailable', 'membership_unavailable') + } + if (!answer.member) { + throw new WorkspaceError('forbidden', 'not_a_member') + } } + const transferring = patch.ownerUserId !== undefined && patch.ownerUserId !== row.user_id const next = await updateRegistry(db, row.id, { ...(patch.title !== undefined ? { name: patch.title } : {}), ...(patch.branch !== undefined ? { repo_branch: patch.branch } : {}), @@ -381,7 +402,9 @@ export async function patchWorkspace( // Archiving is a lifecycle change (archiveSandbox stops the VM, then sets archived_at); this edit // can only clear a label left by an older label-only archive. ...(patch.archived === false ? { archived_at: null } : {}), - ...(patch.ownerUserId !== undefined ? { user_id: patch.ownerUserId } : {}), + // One statement: the new owner and the transferred mark land together, so there is never a + // moment where the workspace has a new owner and still counts as private to them. + ...(transferring ? { user_id: patch.ownerUserId, ownership_transferred_at: now } : {}), ...(patch.tokenUsdMicros !== undefined ? { token_usd_micros: patch.tokenUsdMicros } : {}) }) if (!next) { @@ -393,7 +416,7 @@ export async function patchWorkspace( console.warn('member_usage_write_failed', String(error)) ) } - const ownerChanged = patch.ownerUserId !== undefined && patch.ownerUserId !== row.user_id + const ownerChanged = transferring const wentPrivate = row.visibility === 'team' && next.visibility === 'private' const archived = patch.archived === true && row.archived_at === null return { row: next, revoke: ownerChanged ? 'all_but_new_owner' : wentPrivate || archived ? 'non_owner' : 'none' } diff --git a/cloud/apps/sandbox-control/test/agent-accounts.test.ts b/cloud/apps/sandbox-control/test/agent-accounts.test.ts index 61107afb573..63e9261d9f4 100644 --- a/cloud/apps/sandbox-control/test/agent-accounts.test.ts +++ b/cloud/apps/sandbox-control/test/agent-accounts.test.ts @@ -236,8 +236,8 @@ describe('egress-time Source (D10) and shared workspaces (D13)', () => { describe('D13 fails closed', () => { it('treats any visibility other than private as shared', () => { - expect(isSharedWorkspace({ visibility: 'private' })).toBe(false) - expect(isSharedWorkspace({ visibility: 'team' })).toBe(true) + expect(isSharedWorkspace({ visibility: 'private', ownership_transferred_at: null })).toBe(false) + expect(isSharedWorkspace({ visibility: 'team', ownership_transferred_at: null })).toBe(true) for (const visibility of ['', 'org', null, undefined] as unknown as string[]) { expect(isSharedWorkspace({ visibility } as never)).toBe(true) } diff --git a/cloud/apps/sandbox-control/test/harness.ts b/cloud/apps/sandbox-control/test/harness.ts index 945c243cebf..ac7f630f9fc 100644 --- a/cloud/apps/sandbox-control/test/harness.ts +++ b/cloud/apps/sandbox-control/test/harness.ts @@ -64,7 +64,9 @@ export async function createHarness(overrides: Partial = {}, extraDeps: Par ...overrides } // No live github.com from tests: repos count as public unless a test says otherwise. - const deps: Deps = { provider: vercel, entitlements, now: clock, publicRepoCheck: async () => true, ...extraDeps } + // Hand-offs ask auth whether the new owner is in the team; tests answer yes unless they say otherwise + // (pass `teamMembership: undefined` to get the real check, which refuses when unconfigured). + const deps: Deps = { provider: vercel, entitlements, now: clock, publicRepoCheck: async () => true, teamMembership: async () => ({ member: true }), ...extraDeps } async function token(sub: string, org?: string, purpose = 'sandbox', extra: Record = {}) { // Like Orca auth (service-tokens.ts): iat backdated 30 s for clock skew, exp 5 min from minting. diff --git a/cloud/apps/sandbox-control/test/owner-transfer.test.ts b/cloud/apps/sandbox-control/test/owner-transfer.test.ts new file mode 100644 index 00000000000..771bff32b9c --- /dev/null +++ b/cloud/apps/sandbox-control/test/owner-transfer.test.ts @@ -0,0 +1,443 @@ +// OWNER-TRANSFER (P0). Handing a workspace to another owner used to accept any user id and rebuild +// a running VM's environment for them at once, so their personal secrets and agent account landed +// in a VM the previous owner had just been using. Now: +// - the new owner must be in the team, by auth's live answer; anything but a clear yes refuses; +// - the owner and a permanent "transferred" mark are written together, and the VM is stopped; +// - a transferred workspace is in shared mode for credentials for good: nothing personal to the +// new owner is ever attached, on transfer, wake or refresh; +// - what acted for the previous owner (share links, the in-VM token) is revoked. +import { readFileSync } from 'node:fs' +import { fileURLToPath } from 'node:url' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { mintApiKey, WORKSPACE_TOKEN_SCOPES } from '../src/api/keys' +import { connectedMembers } from '../src/member-git' +import { ENV_COMMAND, START_COMMAND } from '../src/sandbox-vm-commands' +import { ownersForSandbox } from '../src/secrets' +import { isSharedWorkspace } from '../src/shared-workspace' +import { getSandbox } from '../src/store' +import { createTeamMembershipCheck, membershipKey, MEMBERSHIP_KEY_PURPOSE, type MembershipAnswer } from '../src/team-membership' +import { createHarness } from './harness' + +type Harness = Awaited> + +const KEK = Buffer.alloc(32, 7).toString('base64') +const AUTH_TOKEN = 'a'.repeat(40) +const OPERATOR = 'o'.repeat(40) +// Fake values only. None of the new owner's may ever reach the VM or a log line. +const TEAM_KEY = 'sk-ant-api03-fake-team-key-0000' +const MIA_VALUES = ['sk-proj-fake-mia-openai-1111', 'mia-db-password', 'mia-aws-secret-value'] +const MAL_VALUE = 'mallory-own-db-password' + +let logs: string[] = [] +beforeEach(() => { + logs = [] + for (const method of ['log', 'info', 'warn', 'error', 'debug'] as const) { + vi.spyOn(console, method).mockImplementation((...args: unknown[]) => { + logs.push(args.map((arg) => (typeof arg === 'string' ? arg : JSON.stringify(arg))).join(' ')) + }) + } +}) +afterEach(() => { + for (const value of [...MIA_VALUES, MAL_VALUE, TEAM_KEY, AUTH_TOKEN]) { + expect(logs.join('\n')).not.toContain(value) + } + vi.restoreAllMocks() +}) + +function decodeEnv(encoded: string | undefined): Record { + return Object.fromEntries( + (encoded ?? '') + .split('\n') + .filter(Boolean) + .map((line) => { + const at = line.indexOf('=') + return [line.slice(0, at), Buffer.from(line.slice(at + 1), 'base64').toString('utf8')] + }) + ) +} + +/** Everything the control plane sent the VM from command `from` on, secrets decoded, as one string. */ +function everythingSent(h: Harness, from = 0): string { + return JSON.stringify(h.vercel.commands.slice(from).map((c) => ({ ...c, env: { ...c.env, ORCA_SECRET_ENV: decodeEnv(c.env.ORCA_SECRET_ENV) } }))) +} + +function lastEnv(h: Harness, command: string = START_COMMAND): Record { + return decodeEnv(h.vercel.commands.filter((c) => c.command === command).at(-1)?.env.ORCA_SECRET_ENV) +} + +/** Mallory's private, running workspace in Acme; Mia is a teammate with personal secrets of her own. */ +async function setup(deps: Parameters[1] = {}, env: Record = {}) { + const h = await createHarness( + { SECRETS_KEK: KEK, MEMBER_REMOVED_TOKEN: AUTH_TOKEN, OPERATOR_TOKEN: OPERATOR, PUBLIC_API_URL: 'https://api.test', SHARE_LINK_SECRET: 's'.repeat(40), SHARE_HOST_SUFFIX: 'share.test', ...env }, + deps + ) + h.billing.features = { auditLog: true } + const admin = await h.token('ada', 'acme', 'sandbox', { orgRole: 'admin' }) + const mallory = await h.token('mallory', 'acme', 'sandbox', { orgRole: 'member' }) + const mia = await h.token('mia', 'acme', 'sandbox', { orgRole: 'member' }) + const put = (scope: string, name: string, value: string, bearer: string) => h.call('PUT', `/v1/secrets/${scope}/${name}`, { body: { value }, bearer }) + await put('org', 'ANTHROPIC_API_KEY', TEAM_KEY, admin) + await put('personal', 'OPENAI_API_KEY', MIA_VALUES[0]!, mia) + await put('personal', 'DB_PASSWORD', MIA_VALUES[1]!, mia) + await put('personal', 'AWS_SECRET_ACCESS_KEY', MIA_VALUES[2]!, mia) + await put('personal', 'DB_PASSWORD', MAL_VALUE, mallory) + const created = await h.call('POST', '/v1/sandboxes', { bearer: mallory, body: { workspaceId: 'ws-1', machineSize: 'standard', repo: { url: 'https://github.com/acme/app' } } }) + expect(created.status).toBe(200) + const id: string = created.body.sandbox.id + const transfer = (bearer: string, ownerUserId: string) => h.call('PATCH', `/v1/team/workspaces/${id}`, { bearer, body: { ownerUserId } }) + const row = async () => (await getSandbox(h.env.SANDBOX_DB, id))! + return { h, admin, mallory, mia, id, transfer, row, put } +} + +const answers = (answer: MembershipAnswer) => ({ teamMembership: async () => answer }) + +describe('who a workspace may be handed to', () => { + it('refuses someone who is not in the team, and changes nothing', async () => { + const asked: string[][] = [] + const t = await setup({ teamMembership: async (accountId, userId) => (asked.push([accountId, userId]), { member: false }) }) + const sent = t.h.vercel.commands.length + const refused = await t.transfer(t.mallory, 'usr_outsider') + expect([refused.status, refused.body]).toEqual([403, { error: 'forbidden', reason: 'not_a_member' }]) + expect(asked).toEqual([['org:acme', 'usr_outsider']]) + expect(await t.row()).toMatchObject({ user_id: 'mallory', ownership_transferred_at: null, status: 'running' }) + // The VM was not touched: no stop, no command. + expect(t.h.vercel.commands.length).toBe(sent) + expect(t.h.raw.prepare("SELECT COUNT(*) AS n FROM audit_log WHERE action = 'workspace.owner_changed'").get()).toEqual({ n: 0 }) + }) + + it('refuses when auth cannot say, with a "try again" answer, and changes nothing', async () => { + const t = await setup(answers({ unavailable: 'auth_502' })) + const refused = await t.transfer(t.mallory, 'mia') + expect([refused.status, refused.body]).toEqual([503, { error: 'unavailable', reason: 'membership_unavailable' }]) + expect(await t.row()).toMatchObject({ user_id: 'mallory', ownership_transferred_at: null, status: 'running' }) + }) + + it('never asks about membership for a caller who may not hand the workspace over', async () => { + const asked: string[] = [] + const t = await setup({ teamMembership: async (_account, userId) => (asked.push(userId), { member: true }) }) + // Mia is a teammate, not the owner or an admin: a private workspace does not exist for her. + expect((await t.transfer(t.mia, 'mia')).status).toBe(404) + expect(asked).toEqual([]) + expect(await t.row()).toMatchObject({ user_id: 'mallory', ownership_transferred_at: null }) + }) + + it('with no check configured, every hand-off is refused (the interim "transfers off")', async () => { + const t = await setup({ teamMembership: undefined }) + expect((await t.transfer(t.mallory, 'mia')).body).toEqual({ error: 'unavailable', reason: 'membership_unavailable' }) + expect((await t.transfer(t.admin, 'ada')).status).toBe(503) + expect(await t.row()).toMatchObject({ user_id: 'mallory', ownership_transferred_at: null }) + }) +}) + +describe("auth's live membership answer", () => { + const env = { AUTH_INTERNAL_ORIGIN: 'https://auth.test/', MEMBER_REMOVED_TOKEN: AUTH_TOKEN } + const auth = (respond: () => Response | Promise) => { + const calls: Array<{ url: string; authorization: string | null; body: unknown; redirect: unknown }> = [] + const fetchImpl = (async (input: string | URL | Request, init?: RequestInit) => { + calls.push({ url: String(input), authorization: new Headers(init?.headers).get('authorization'), body: JSON.parse(String(init?.body)), redirect: init?.redirect }) + return respond() + }) as typeof fetch + return { calls, fetchImpl } + } + + it('asks with a key derived from the shared token, never the token itself', async () => { + const a = auth(() => Response.json({ member: true, role: 'member' })) + expect(await createTeamMembershipCheck(env, a.fetchImpl)('org:acme', 'usr_mia')).toEqual({ member: true }) + const key = await membershipKey(AUTH_TOKEN) + expect(a.calls).toEqual([{ url: 'https://auth.test/v1/internal/org-membership', authorization: `Bearer ${key}`, body: { orgId: 'acme', userId: 'usr_mia' }, redirect: 'manual' }]) + expect(key).toMatch(/^[A-Za-z0-9_-]{43}$/) + expect(key).not.toContain(AUTH_TOKEN) + // The contract with auth (orca-cloud#798): HMAC-SHA256(token, purpose), base64url, no padding. + expect(MEMBERSHIP_KEY_PURPOSE).toBe('orca-cloud:control-plane-to-auth:org-membership:v1') + expect(await membershipKey(` ${AUTH_TOKEN}\n`)).toBe(key) + expect(await membershipKey('b'.repeat(40))).not.toBe(key) + }) + + it('reads "not a member" only from a clear 200', async () => { + expect(await createTeamMembershipCheck(env, auth(() => Response.json({ member: false })).fetchImpl)('org:acme', 'u')).toEqual({ member: false }) + }) + + it.each([ + [401, { error: 'unauthorized' }], + [400, { error: 'invalid_request' }], + [404, { error: 'not_found' }], + [429, { error: 'rate_limited' }], + [500, { error: 'boom' }], + [502, { error: 'directory_unavailable' }], + // A redirect is never followed, and never a yes. + [302, null] + ])('treats HTTP %i as "cannot say", even with member:true in the body', async (status, body) => { + const a = auth(() => new Response(JSON.stringify({ ...(body ?? {}), member: true }), { status })) + expect(await createTeamMembershipCheck(env, a.fetchImpl)('org:acme', 'u')).toEqual({ unavailable: `auth_${status}` }) + }) + + it('treats a network failure, a timeout and an unreadable 200 as "cannot say"', async () => { + const down = auth(() => Promise.reject(new TypeError('fetch failed'))) + expect(await createTeamMembershipCheck(env, down.fetchImpl)('org:acme', 'u')).toEqual({ unavailable: 'unreachable' }) + for (const body of ['not json', '{}', '{"member":"yes"}', '{"member":1}', 'null']) { + const odd = auth(() => new Response(body, { status: 200 })) + expect([body, await createTeamMembershipCheck(env, odd.fetchImpl)('org:acme', 'u')]).toEqual([body, { unavailable: 'unreadable_answer' }]) + } + }) + + it('refuses without calling auth when it is not configured, or the account is not a real team', async () => { + const a = auth(() => Response.json({ member: true })) + const cases: Array<[Parameters[0], string, string]> = [ + [{ MEMBER_REMOVED_TOKEN: AUTH_TOKEN }, 'org:acme', 'not_configured'], + [{ AUTH_INTERNAL_ORIGIN: 'https://auth.test' }, 'org:acme', 'not_configured'], + [{ AUTH_INTERNAL_ORIGIN: 'https://auth.test', MEMBER_REMOVED_TOKEN: 'short' }, 'org:acme', 'not_configured'], + [{ AUTH_INTERNAL_ORIGIN: 'http://auth.test', MEMBER_REMOVED_TOKEN: AUTH_TOKEN }, 'org:acme', 'not_configured'], + // Dev operator accounts are not in auth: refused, no bypass. Personal accounts have no team. + [env, 'dev:org:w6test', 'unknown_account'], + [env, 'user:mallory', 'unknown_account'] + ] + for (const [config, account, reason] of cases) { + expect([account, await createTeamMembershipCheck(config, a.fetchImpl)(account, 'u')]).toEqual([account, { unavailable: reason }]) + } + expect(a.calls).toEqual([]) + }) + + it('through the route: a member is accepted, everything else is refused', async () => { + let next: () => Response = () => Response.json({ member: true }) + const a = auth(() => next()) + const t = await setup({ teamMembership: createTeamMembershipCheck(env, a.fetchImpl) }) + for (const [respond, status] of [ + [() => Response.json({ member: false }), 403], + [() => Response.json({ error: 'rate_limited' }, { status: 429 }), 503], + [() => Response.json({ error: 'directory_unavailable' }, { status: 502 }), 503], + [() => Response.json({ error: 'not_found' }, { status: 404 }), 503], + [() => Response.json({ error: 'unauthorized' }, { status: 401 }), 503] + ] as Array<[() => Response, number]>) { + next = respond + expect((await t.transfer(t.mallory, 'mia')).status).toBe(status) + expect(await t.row()).toMatchObject({ user_id: 'mallory', ownership_transferred_at: null, status: 'running' }) + } + next = () => Response.json({ member: true }) + expect((await t.transfer(t.mallory, 'mia')).status).toBe(200) + expect(a.calls.at(-1)!.body).toEqual({ orgId: 'acme', userId: 'mia' }) + }) + + it("the derived key opens nothing on the control plane's own internal routes", async () => { + const t = await setup() + const key = await membershipKey(AUTH_TOKEN) + const post = (path: string, body: unknown, bearer: string) => t.h.call('POST', path, { body, bearer }) + const routes: Array<[string, unknown]> = [ + ['/v1/internal/org-members/removed', { accountId: 'org:acme', userId: 'mia' }], + ['/v1/internal/members/role-changed', { accountId: 'org:acme', userId: 'mia', role: 'owner' }], + ['/v1/internal/member-names', { accountId: 'org:acme', members: [{ userId: 'mia', name: 'Mia' }] }], + ['/v1/internal/audit-events', { accountId: 'org:acme', events: [{ eventId: 'evt_00000001', action: 'member.invited' }] }] + ] + for (const [path, body] of routes) { + expect([path, (await post(path, body, key)).status]).toEqual([path, 404]) + // The real shared token still works there, so the 404 above is the key being refused. + expect([path, (await post(path, body, AUTH_TOKEN)).status]).toEqual([path, 200]) + } + expect((await t.h.call('GET', '/v1/internal/status', { bearer: key })).status).toBe(404) + }) +}) + +describe('a hand-off to a teammate', () => { + it('writes the owner and the transferred mark together, stops the VM, and rebuilds nothing for the new owner', async () => { + const t = await setup() + const session = (await t.row()).session_id + // Mallory's own private workspace had her personal secret, as before. + expect(lastEnv(t.h).DB_PASSWORD).toBe(MAL_VALUE) + const sent = t.h.vercel.commands.length + const moved = await t.transfer(t.mallory, 'mia') + expect(moved.status).toBe(200) + expect(moved.body.workspace).toMatchObject({ ownerUserId: 'mia', createdBy: 'mallory', status: 'suspended', visibility: 'private' }) + expect(moved.body.workspace.ownershipTransferredAt).toBe(t.h.now()) + const row = await t.row() + expect(row).toMatchObject({ user_id: 'mia', created_by: 'mallory', status: 'suspended', session_id: null, visibility: 'private' }) + expect(row.ownership_transferred_at).toBe(t.h.now()) + expect(session).toBeTruthy() + // Nothing was sent to the VM for Mia: no env refresh, no start. And none of her values, ever. + const after = t.h.vercel.commands.slice(sent).map((c) => c.command) + expect(after).not.toContain(ENV_COMMAND) + expect(after).not.toContain(START_COMMAND) + for (const value of MIA_VALUES) { + expect(everythingSent(t.h)).not.toContain(value) + } + // Recorded, with both people. + const audit = t.h.raw.prepare("SELECT user_id, detail FROM audit_log WHERE action = 'workspace.owner_changed'").all() as Array<{ user_id: string; detail: string }> + expect(audit.map((entry) => [entry.user_id, JSON.parse(entry.detail)])).toEqual([['mallory', { fromUserId: 'mallory', toUserId: 'mia' }]]) + }) + + it('treats the workspace as shared for credentials from then on, though it stays private to see', async () => { + const t = await setup() + await t.transfer(t.mallory, 'mia') + const row = await t.row() + expect(isSharedWorkspace(row)).toBe(true) + expect(ownersForSandbox(row)).toEqual({ org: 'org:acme', personal: null, repo: 'org:acme#github.com/acme/app' }) + // Still Mia's alone to open: the mark changes credentials, not who can see it. + expect((await t.h.call('GET', `/v1/team/workspaces/${t.id}`, { bearer: t.mallory })).status).toBe(404) + expect((await t.h.call('GET', `/v1/team/workspaces/${t.id}`, { bearer: t.mia })).status).toBe(200) + }) + + it('a wake after the hand-off is a new session with team secrets only: nothing of the new owner, nothing of the old', async () => { + const t = await setup() + const before = (await t.row()).session_id + await t.transfer(t.mallory, 'mia') + const sent = t.h.vercel.commands.length + const woke = await t.h.call('POST', `/v1/sandboxes/${t.id}/resume`, { bearer: t.mia }) + expect(woke.status).toBe(200) + const row = await t.row() + expect(row.status).toBe('running') + // A fresh session (the stop kept the disk only), started with a freshly built env. + expect(row.session_id).toBeTruthy() + expect(row.session_id).not.toBe(before) + const env = lastEnv(t.h) + expect(env.ANTHROPIC_API_KEY).toBeDefined() + expect(env).not.toHaveProperty('DB_PASSWORD') + expect(env).not.toHaveProperty('OPENAI_API_KEY') + expect(env).not.toHaveProperty('AWS_SECRET_ACCESS_KEY') + const since = everythingSent(t.h, sent) + for (const value of [...MIA_VALUES, MAL_VALUE]) { + expect(since).not.toContain(value) + } + }) + + it('an env refresh of a transferred workspace (the path refreshOwnersSandboxes uses) attaches nothing personal', async () => { + const t = await setup() + await t.transfer(t.mallory, 'mia') + await t.h.call('POST', `/v1/sandboxes/${t.id}/resume`, { bearer: t.mia }) + const sent = t.h.vercel.commands.length + // Mia saves another personal secret: her running sandboxes are refreshed, this one included. + await t.put('personal', 'NEW_ONE', 'mia-new-secret-value', t.mia) + expect((await t.h.call('POST', `/v1/sandboxes/${t.id}/env/refresh`, { bearer: t.mia })).status).toBe(200) + const since = everythingSent(t.h, sent) + for (const value of [...MIA_VALUES, 'mia-new-secret-value']) { + expect(since).not.toContain(value) + } + expect(lastEnv(t.h, ENV_COMMAND)).not.toHaveProperty('NEW_ONE') + }) + + it('handing it back does not clear the mark: still shared mode, even for its creator', async () => { + const t = await setup() + await t.transfer(t.mallory, 'mia') + const first = (await t.row()).ownership_transferred_at + t.h.advance(60_000) + expect((await t.transfer(t.mia, 'mallory')).status).toBe(200) + const row = await t.row() + expect(row).toMatchObject({ user_id: 'mallory', created_by: 'mallory' }) + expect(row.ownership_transferred_at).toBeGreaterThan(first!) + expect(ownersForSandbox(row).personal).toBeNull() + await t.h.call('POST', `/v1/sandboxes/${t.id}/resume`, { bearer: t.mallory }) + expect(lastEnv(t.h)).not.toHaveProperty('DB_PASSWORD') + }) + + it('an admin taking a workspace for themselves follows the same rule', async () => { + const t = await setup() + await t.put('personal', 'ADMIN_ONLY', 'ada-personal-secret', t.admin) + const taken = await t.transfer(t.admin, 'ada') + expect(taken.status).toBe(200) + const row = await t.row() + expect(row).toMatchObject({ user_id: 'ada', status: 'suspended' }) + expect(row.ownership_transferred_at).not.toBeNull() + await t.h.call('POST', `/v1/sandboxes/${t.id}/resume`, { bearer: t.admin }) + expect(everythingSent(t.h)).not.toContain('ada-personal-secret') + }) + + it('a hand-off of a sleeping workspace needs no stop, and is marked the same', async () => { + const t = await setup() + await t.h.call('POST', `/v1/sandboxes/${t.id}/suspend`, { bearer: t.mallory }) + expect((await t.transfer(t.mallory, 'mia')).status).toBe(200) + expect(await t.row()).toMatchObject({ user_id: 'mia', status: 'suspended' }) + expect((await t.row()).ownership_transferred_at).not.toBeNull() + }) + + it('naming the current owner again is not a transfer', async () => { + const t = await setup(answers({ member: false })) + expect((await t.transfer(t.mallory, 'mallory')).status).toBe(200) + expect(await t.row()).toMatchObject({ user_id: 'mallory', ownership_transferred_at: null, status: 'running' }) + }) +}) + +describe('what acted for the previous owner', () => { + it('share links on the workspace are turned off', async () => { + const t = await setup() + const share = await t.h.call('POST', `/v1/sandboxes/${t.id}/shares`, { bearer: t.mallory, body: { port: 3000, audience: 'team', expiresIn: '1h' } }) + expect(share.status).toBe(200) + await t.transfer(t.mallory, 'mia') + expect(t.h.raw.prepare('SELECT COUNT(*) AS live FROM share_links WHERE sandbox_id = ? AND revoked_at IS NULL').get(t.id)).toEqual({ live: 0 }) + }) + + it('the in-VM API token is revoked, and none is ever minted for a transferred workspace', async () => { + const t = await setup() + await t.h.call('PUT', '/v1/settings', { bearer: t.admin, body: { vmCliEnabled: true } }) + // A fresh boot mints one for the owner, as before. + await t.h.call('POST', `/v1/sandboxes/${t.id}/suspend`, { bearer: t.mallory }) + await t.h.call('POST', `/v1/sandboxes/${t.id}/resume`, { bearer: t.mallory }) + const live = () => (t.h.raw.prepare("SELECT COUNT(*) AS n FROM api_keys WHERE sandbox_id = ? AND kind = 'workspace' AND revoked_at IS NULL").get(t.id) as { n: number }).n + expect(live()).toBe(1) + await t.transfer(t.mallory, 'mia') + expect(live()).toBe(0) + await t.h.call('POST', `/v1/sandboxes/${t.id}/resume`, { bearer: t.mia }) + expect((await t.row()).status).toBe('running') + expect(live()).toBe(0) + }) + + it('a workspace made from inside a transferred one starts transferred, never private with personal secrets', async () => { + const t = await setup() + await t.h.call('PUT', '/v1/settings', { bearer: t.admin, body: { vmCliEnabled: true } }) + await t.transfer(t.mallory, 'mia') + // No token is minted for it now; this is one that somehow exists (belt and braces). + const minted = await mintApiKey(t.h.env.SANDBOX_DB, { accountId: 'org:acme', createdBy: 'mia', name: 'left behind', scopes: WORKSPACE_TOKEN_SCOPES, kind: 'workspace', sandboxId: t.id, expiresAt: t.h.now() + 3_600_000 }, t.h.now()) + const sent = t.h.vercel.commands.length + const sibling = await t.h.call('POST', '/v1/workspaces', { bearer: minted.secret, body: { name: 'sibling', repo: { url: 'https://github.com/acme/app' } } }) + expect(sibling.status).toBe(201) + const row = (await getSandbox(t.h.env.SANDBOX_DB, sibling.body.workspace.id))! + expect(row).toMatchObject({ user_id: 'mia', visibility: 'private' }) + expect(row.ownership_transferred_at).not.toBeNull() + for (const value of MIA_VALUES) { + expect(everythingSent(t.h, sent)).not.toContain(value) + } + }) + + it("the owner's GitHub identity has no entry of its own on a transferred workspace", async () => { + const t = await setup() + const db = t.h.env.SANDBOX_DB + const connect = (userId: string) => + t.h.raw + .prepare(`INSERT INTO github_user_tokens (account_id, user_id, github_user_id, github_login, kek_id, wrapped_dek, ciphertext, updated_at) VALUES ('org:acme', ?, 1, ?, 'k', 'w', 'c', 1)`) + .run(userId, userId) + connect('mallory') + connect('mia') + // Before: the owner counts with no grant. + expect(await connectedMembers(db, await t.row())).toEqual(['mallory']) + await t.transfer(t.mallory, 'mia') + // After: nobody without a grant, the new owner included. + expect(await connectedMembers(db, await t.row())).toEqual([]) + // With an active grant she counts, exactly like any member of a shared workspace. + t.h.raw + .prepare(`INSERT INTO workspace_grants (id, sandbox_id, account_id, user_id, device_kind, device_label, status, created_at) VALUES ('g1', ?, 'org:acme', 'mia', 'desktop', 'Mia', 'active', 1)`) + .run(t.id) + expect(await connectedMembers(db, await t.row())).toEqual(['mia']) + }) +}) + +describe('everything else is unchanged', () => { + it('a workspace that was never handed over keeps its owner’s personal secrets (private) or none (shared)', async () => { + const t = await setup() + expect(isSharedWorkspace(await t.row())).toBe(false) + expect(lastEnv(t.h).DB_PASSWORD).toBe(MAL_VALUE) + expect((await t.h.call('PATCH', `/v1/team/workspaces/${t.id}`, { bearer: t.mallory, body: { visibility: 'team' } })).status).toBe(200) + expect(await t.row()).toMatchObject({ ownership_transferred_at: null, status: 'running', visibility: 'team' }) + // Sharing still rebuilds the running env at once, without the owner's personal secrets. + expect(lastEnv(t.h, ENV_COMMAND)).not.toHaveProperty('DB_PASSWORD') + expect((await t.h.call('PATCH', `/v1/team/workspaces/${t.id}`, { bearer: t.mallory, body: { visibility: 'private' } })).status).toBe(200) + expect(lastEnv(t.h, ENV_COMMAND).DB_PASSWORD).toBe(MAL_VALUE) + }) + + it('the migration marks workspaces whose owner is not their creator, and no others', async () => { + const t = await setup() + const second = await t.h.call('POST', '/v1/sandboxes', { bearer: t.mia, body: { workspaceId: 'ws-2', machineSize: 'standard' } }) + // As a row transferred before the fix looked: a new owner, no mark. + t.h.raw.prepare("UPDATE sandboxes SET user_id = 'mia' WHERE id = ?").run(t.id) + const sql = readFileSync(fileURLToPath(new URL('../migrations/0059_ownership_transferred.sql', import.meta.url).href), 'utf8') + const backfill = sql.slice(sql.indexOf('UPDATE sandboxes')) + t.h.raw.exec(backfill) + expect((await t.row()).ownership_transferred_at).not.toBeNull() + expect((await getSandbox(t.h.env.SANDBOX_DB, second.body.sandbox.id))!.ownership_transferred_at).toBeNull() + }) +}) diff --git a/cloud/apps/sandbox-control/test/shared-workspace-secrets.test.ts b/cloud/apps/sandbox-control/test/shared-workspace-secrets.test.ts index 2f720e35034..45f240815af 100644 --- a/cloud/apps/sandbox-control/test/shared-workspace-secrets.test.ts +++ b/cloud/apps/sandbox-control/test/shared-workspace-secrets.test.ts @@ -82,7 +82,7 @@ const TEAM_AND_REPO = { ANTHROPIC_API_KEY: TEAM_KEY, BAR: 'repo-bar-value', FOO: describe('personal secrets in shared workspaces (SEC-5)', () => { it('resolves no personal owner for a shared row, and fails closed on an unknown visibility', () => { - const row = { account_id: 'org:acme', user_id: 'mia', repo_url: 'https://github.com/acme/app' } + const row = { account_id: 'org:acme', user_id: 'mia', repo_url: 'https://github.com/acme/app', ownership_transferred_at: null } expect(ownersForSandbox({ ...row, visibility: 'private' }).personal).toBe('user:mia') expect(ownersForSandbox({ ...row, visibility: 'team' })).toEqual({ org: 'org:acme', personal: null, repo: 'org:acme#github.com/acme/app' }) expect(ownersForSandbox({ ...row, visibility: undefined as never }).personal).toBeNull() diff --git a/cloud/apps/sandbox-control/test/workspaces.test.ts b/cloud/apps/sandbox-control/test/workspaces.test.ts index 881006ae296..8c7304129b6 100644 --- a/cloud/apps/sandbox-control/test/workspaces.test.ts +++ b/cloud/apps/sandbox-control/test/workspaces.test.ts @@ -314,11 +314,17 @@ describe('workspace grants (E2EE pairing through the control plane)', () => { const t = await team() expect((await t.req(t.bob, 'PATCH', `/v1/team/workspaces/${t.id}`, { ownerUserId: 'bob' })).status).toBe(404) const moved = await t.req(t.alice, 'PATCH', `/v1/team/workspaces/${t.id}`, { ownerUserId: 'bob' }) - expect(moved.body.workspace).toMatchObject({ ownerUserId: 'bob', createdBy: 'alice' }) - expect(moved.body.revoked).toEqual({ revoked: 1, queued: 0 }) - const box = [...t.h.vercel.boxes.values()][0]! - expect(box.devices).toEqual([]) - expect((await t.req(t.alice, 'POST', `/v1/sandboxes/${t.id}/suspend`)).status).toBe(404) + expect(moved.body.workspace).toMatchObject({ ownerUserId: 'bob', createdBy: 'alice', status: 'suspended' }) + expect(moved.body.workspace.ownershipTransferredAt).toEqual(expect.any(Number)) + // OWNER-TRANSFER: the hand-off stops the VM, so the previous owner's devices are revoked + // at its next boot (queued), before orcad starts, instead of inside a VM that kept running. + expect(moved.body.revoked).toEqual({ revoked: 0, queued: 1 }) + expect((await t.req(t.alice, 'POST', `/v1/sandboxes/${t.id}/resume`)).status).toBe(404) + t.h.vercel.commands.length = 0 + expect((await t.req(t.bob, 'POST', `/v1/sandboxes/${t.id}/resume`)).status).toBe(200) + const order = t.h.vercel.commands.map((c) => c.command) + expect(order.indexOf(REVOKE_COMMAND)).toBeGreaterThanOrEqual(0) + expect(order.indexOf(REVOKE_COMMAND)).toBeLessThan(order.lastIndexOf('/opt/orca/bin/orca-sandbox-start')) expect((await t.req(t.bob, 'POST', `/v1/sandboxes/${t.id}/suspend`)).status).toBe(200) }) diff --git a/cloud/apps/sandbox-control/wrangler.dev.jsonc b/cloud/apps/sandbox-control/wrangler.dev.jsonc index 053863da52d..e285ac1cd00 100644 --- a/cloud/apps/sandbox-control/wrangler.dev.jsonc +++ b/cloud/apps/sandbox-control/wrangler.dev.jsonc @@ -70,6 +70,9 @@ "GITHUB_APP_SLUG": "orca-cloud-dev", // Cloud setup pages (staging auth): GitHub installs started from a setup session return there. "CLOUD_SETUP_ORIGIN": "https://auth-staging.onorca.dev", + // Auth's internal API (staging auth is the one that pushes to this Worker): the live "is this + // person in the team" check before a workspace changes owner. Unset = hand-offs refused. + "AUTH_INTERNAL_ORIGIN": "https://auth-staging.onorca.dev", "EGRESS_PROXY_URL": "https://orca-sandbox-control-dev.lovecast.workers.dev/v1/egress/proxy" } } From 90e181af0ff505e1c28d161d20c57236c016abb0 Mon Sep 17 00:00:00 2001 From: Neil Date: Fri, 13 Nov 2026 06:13:02 -0800 Subject: [PATCH 2/3] fix(cloud): OWNER-TRANSFER stop before the write; refuse during a boot or stop, and when the stop fails (sso-scim review) --- cloud/apps/sandbox-control/src/index.ts | 20 +++------- cloud/apps/sandbox-control/src/store.ts | 14 +++++++ cloud/apps/sandbox-control/src/workspaces.ts | 34 ++++++++++++---- .../test/owner-transfer.test.ts | 40 +++++++++++++++++++ 4 files changed, 87 insertions(+), 21 deletions(-) diff --git a/cloud/apps/sandbox-control/src/index.ts b/cloud/apps/sandbox-control/src/index.ts index c04efa8fd69..428de1e6226 100644 --- a/cloud/apps/sandbox-control/src/index.ts +++ b/cloud/apps/sandbox-control/src/index.ts @@ -147,7 +147,7 @@ import { } from './agent-sources' import { EgressHostsSchema, egressUserHosts, normalizeUserHosts, setEgressUserHosts } from './egress-hosts' import { gitBasicAuthorization, parseEgressPreset } from './network-policy' -import { cachedProviderEnv } from './session-env' +import { cachedProviderEnv, forgetProviderEnv } from './session-env' import { credentialTargets, type CredentialTarget } from './provider-env' import type { Keyring } from './secrets-crypto' import { ProviderError, type SandboxProviderApi } from './sandbox-provider' @@ -2292,22 +2292,14 @@ async function workspaceRoute(request: Request, url: URL, env: Env, deps: Deps, const current = (await getSandbox(env.SANDBOX_DB, row.id)) ?? row let patched: Awaited> = Object.keys(rest).length > 0 - ? await patchWorkspace(db, caller, current, rest, now, deps.teamMembership ?? createTeamMembershipCheck(env)) + ? await patchWorkspace(db, caller, current, rest, now, deps.teamMembership ?? createTeamMembershipCheck(env), (running) => suspendSandbox(life, running)) : { row: current, revoke: 'none' } const transferred = patched.row.user_id !== current.user_id if (transferred) { - // OWNER-TRANSFER. The owner and the transferred mark are already written together. Now - // stop the VM, so nothing the previous owner left running carries on (a stop keeps the - // disk only: the next wake is a new session with a freshly built, shared-mode env), and - // end everything that acted for them: its share links and its in-VM API token. - if (patched.row.status === 'running') { - const stopped = await suspendSandbox(life, patched.row).catch((error: unknown) => { - // Already marked transferred, so nothing personal can attach; reconcile stops it. - console.log(JSON.stringify({ event: 'owner_transfer_suspend_failed', sandboxId: row.id, error: String(error).slice(0, 120) })) - return null - }) - patched = { ...patched, row: stopped ?? (await getSandbox(env.SANDBOX_DB, row.id)) ?? patched.row } - } + // OWNER-TRANSFER. patchWorkspace stopped the VM and then wrote the owner and the mark + // together. End what acted for the previous owner: the share links, the in-VM API token, + // and the proxy's cached copy of the keys this workspace's sessions were using. + forgetProviderEnv(row.id) await revokeSandboxShares(env.SANDBOX_DB, row.id, now) await revokeWorkspaceTokens(env.SANDBOX_DB, row.id, now) } diff --git a/cloud/apps/sandbox-control/src/store.ts b/cloud/apps/sandbox-control/src/store.ts index 2a156b40a80..192c1983de0 100644 --- a/cloud/apps/sandbox-control/src/store.ts +++ b/cloud/apps/sandbox-control/src/store.ts @@ -261,6 +261,20 @@ export async function updateRegistry(db: D1Database, id: string, patch: Registry return getSandbox(db, id) } +/** + * OWNER-TRANSFER: the new owner and the transferred mark in one statement, and only while the + * workspace is stopped. A row that is running or mid-boot at this instant is left untouched + * (null), so a hand-off can never land on a live VM or on a boot whose environment was resolved + * for the previous owner. + */ +export async function transferOwnership(db: D1Database, id: string, newOwnerId: string, now: number): Promise { + const result = await db + .prepare(`UPDATE sandboxes SET user_id = ?, ownership_transferred_at = ? WHERE id = ? AND status IN ('suspended', 'archived')`) + .bind(newOwnerId, now, id) + .run() + return (result.meta.changes ?? 0) > 0 ? getSandbox(db, id) : null +} + /** Last activity (open, message, presence) — coarse, at most one write a minute per workspace. */ export async function touchActivity(db: D1Database, id: string, userId: string, now: number): Promise { await db diff --git a/cloud/apps/sandbox-control/src/workspaces.ts b/cloud/apps/sandbox-control/src/workspaces.ts index 958d1a69ff3..ec27a5c1248 100644 --- a/cloud/apps/sandbox-control/src/workspaces.ts +++ b/cloud/apps/sandbox-control/src/workspaces.ts @@ -13,7 +13,7 @@ import type { TeamMembershipCheck } from './team-membership' import { z } from 'zod' import type { SandboxCaller } from './auth' import type { MachineSize } from './entitlements' -import { getSandbox, touchActivity, updateRegistry, type SandboxRow } from './store' +import { getSandbox, touchActivity, transferOwnership, updateRegistry, type SandboxRow } from './store' import { memberNamesFor } from './member-names' import { recordTokenSpend } from './member-usage' import { readSnapshotTimes } from './read-snapshots' @@ -357,7 +357,9 @@ export async function patchWorkspace( * is written: is the new owner in this team right now? (team-membership.ts) Without one, no * hand-off happens. */ - confirmNewOwner?: TeamMembershipCheck + confirmNewOwner?: TeamMembershipCheck, + /** Stops a running workspace before it changes owner (the lifecycle suspend). Without one, a running workspace is not handed over. */ + stopForTransfer?: (row: SandboxRow) => Promise ): Promise<{ row: SandboxRow; revoke: 'none' | 'non_owner' | 'all_but_new_owner' }> { const metadataOnly = patch.tokenUsdMicros !== undefined && Object.keys(patch).length === 1 if (metadataOnly) { @@ -382,8 +384,13 @@ export async function patchWorkspace( if (!row.account_id.includes('org:')) { throw new WorkspaceError('invalid_request', 'personal_account') } - // OWNER-TRANSFER: only to someone who is in the team now, by auth's live answer. Anything but - // a clear yes refuses, and nothing has been changed yet. + // OWNER-TRANSFER: never onto a workspace that is booting or stopping. Its environment may have + // been resolved for the previous owner, and what their boot started would run on under the new one. + if (row.status !== 'running' && row.status !== 'suspended' && row.status !== 'archived') { + throw new WorkspaceError('conflict', `sandbox_${row.status}`) + } + // Only to someone who is in the team now, by auth's live answer. Anything but a clear yes + // refuses, and nothing has been changed yet. const answer = confirmNewOwner ? await confirmNewOwner(row.account_id, patch.ownerUserId) : { unavailable: 'not_configured' } if ('unavailable' in answer) { console.log(JSON.stringify({ event: 'owner_transfer_refused', sandboxId: row.id, reason: answer.unavailable })) @@ -394,6 +401,22 @@ export async function patchWorkspace( } } const transferring = patch.ownerUserId !== undefined && patch.ownerUserId !== row.user_id + if (transferring) { + // Stop first, so nothing the previous owner left running carries on and the next wake is a new + // session with a freshly built, shared-mode environment. A stop that fails refuses the hand-off: + // the workspace stays theirs, stopped or not, and nothing else has changed. + if (row.status === 'running') { + const stopped = stopForTransfer ? await stopForTransfer(row).then(() => true, () => false) : false + if (!stopped) { + throw new WorkspaceError('conflict', 'stop_failed') + } + } + // One statement, and only if it is stopped at this instant (the previous owner may have woken + // it again): the new owner and the transferred mark land together or not at all. + if (!(await transferOwnership(db, row.id, patch.ownerUserId!, now))) { + throw new WorkspaceError('conflict', 'sandbox_not_stopped') + } + } const next = await updateRegistry(db, row.id, { ...(patch.title !== undefined ? { name: patch.title } : {}), ...(patch.branch !== undefined ? { repo_branch: patch.branch } : {}), @@ -402,9 +425,6 @@ export async function patchWorkspace( // Archiving is a lifecycle change (archiveSandbox stops the VM, then sets archived_at); this edit // can only clear a label left by an older label-only archive. ...(patch.archived === false ? { archived_at: null } : {}), - // One statement: the new owner and the transferred mark land together, so there is never a - // moment where the workspace has a new owner and still counts as private to them. - ...(transferring ? { user_id: patch.ownerUserId, ownership_transferred_at: now } : {}), ...(patch.tokenUsdMicros !== undefined ? { token_usd_micros: patch.tokenUsdMicros } : {}) }) if (!next) { diff --git a/cloud/apps/sandbox-control/test/owner-transfer.test.ts b/cloud/apps/sandbox-control/test/owner-transfer.test.ts index 771bff32b9c..88b807d352e 100644 --- a/cloud/apps/sandbox-control/test/owner-transfer.test.ts +++ b/cloud/apps/sandbox-control/test/owner-transfer.test.ts @@ -346,6 +346,46 @@ describe('a hand-off to a teammate', () => { expect((await t.row()).ownership_transferred_at).not.toBeNull() }) + it.each(['creating', 'resuming', 'suspending', 'archiving', 'failed'])('refuses while the workspace is %s: never onto a boot or a stop in flight', async (status) => { + const asked: string[] = [] + const t = await setup({ teamMembership: async (_account, userId) => (asked.push(userId), { member: true }) }) + t.h.raw.prepare('UPDATE sandboxes SET status = ? WHERE id = ?').run(status, t.id) + const sent = t.h.vercel.commands.length + const refused = await t.transfer(t.admin, 'mia') + expect([refused.status, refused.body]).toEqual([409, { error: 'conflict', reason: `sandbox_${status}` }]) + expect(await t.row()).toMatchObject({ user_id: 'mallory', ownership_transferred_at: null, status }) + expect(asked).toEqual([]) + expect(t.h.vercel.commands.length).toBe(sent) + }) + + it('refuses when the VM cannot be stopped: the workspace stays with its owner, unmarked', async () => { + const t = await setup() + const stop = t.h.vercel.stopSession.bind(t.h.vercel) + t.h.vercel.stopSession = async () => { + throw new Error('provider down') + } + const refused = await t.transfer(t.mallory, 'mia') + expect([refused.status, refused.body]).toEqual([409, { error: 'conflict', reason: 'stop_failed' }]) + expect(await t.row()).toMatchObject({ user_id: 'mallory', ownership_transferred_at: null }) + expect(t.h.raw.prepare("SELECT COUNT(*) AS n FROM audit_log WHERE action = 'workspace.owner_changed'").get()).toEqual({ n: 0 }) + // Once it can be stopped, the same request goes through. + t.h.vercel.stopSession = stop + t.h.raw.prepare("UPDATE sandboxes SET status = 'running' WHERE id = ? AND status <> 'suspended'").run(t.id) + expect((await t.transfer(t.mallory, 'mia')).status).toBe(200) + expect(await t.row()).toMatchObject({ user_id: 'mia', status: 'suspended' }) + }) + + it('writes the owner only if the workspace is stopped at that instant', async () => { + const t = await setup() + // As if the previous owner woke it again between the stop and the write. + const { transferOwnership } = await import('../src/store') + t.h.raw.prepare("UPDATE sandboxes SET status = 'running' WHERE id = ?").run(t.id) + expect(await transferOwnership(t.h.env.SANDBOX_DB, t.id, 'mia', t.h.now())).toBeNull() + expect(await t.row()).toMatchObject({ user_id: 'mallory', ownership_transferred_at: null }) + t.h.raw.prepare("UPDATE sandboxes SET status = 'suspended' WHERE id = ?").run(t.id) + expect(await transferOwnership(t.h.env.SANDBOX_DB, t.id, 'mia', t.h.now())).toMatchObject({ user_id: 'mia', ownership_transferred_at: t.h.now() }) + }) + it('naming the current owner again is not a transfer', async () => { const t = await setup(answers({ member: false })) expect((await t.transfer(t.mallory, 'mallory')).status).toBe(200) From 0a9b8a6bde20e7b3f5116e3d89ba4941c963d21a Mon Sep 17 00:00:00 2001 From: Neil Date: Fri, 13 Nov 2026 06:13:47 -0800 Subject: [PATCH 3/3] test(cloud): OWNER-TRANSFER a wake racing the hand-off refuses it and leaves the VM as woken --- .../test/owner-transfer.test.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/cloud/apps/sandbox-control/test/owner-transfer.test.ts b/cloud/apps/sandbox-control/test/owner-transfer.test.ts index 88b807d352e..2bc93dbdabc 100644 --- a/cloud/apps/sandbox-control/test/owner-transfer.test.ts +++ b/cloud/apps/sandbox-control/test/owner-transfer.test.ts @@ -375,6 +375,31 @@ describe('a hand-off to a teammate', () => { expect(await t.row()).toMatchObject({ user_id: 'mia', status: 'suspended' }) }) + it('a wake that races in between the stop and the write refuses the hand-off and leaves the VM as the waker left it', async () => { + const t = await setup() + // The previous owner wakes it after the stop and just before the owner is written. + const db = t.h.env.SANDBOX_DB + const prepare = db.prepare.bind(db) + let raced = 0 + db.prepare = ((sql: string) => { + if (sql.includes('ownership_transferred_at = ?') && sql.startsWith('UPDATE sandboxes SET user_id')) { + raced += 1 + t.h.raw.prepare("UPDATE sandboxes SET status = 'running', session_id = 'sess_woken_again' WHERE id = ?").run(t.id) + } + return prepare(sql) + }) as typeof db.prepare + const refused = await t.transfer(t.mallory, 'mia') + db.prepare = prepare + expect(raced).toBe(1) + expect([refused.status, refused.body]).toEqual([409, { error: 'conflict', reason: 'sandbox_not_stopped' }]) + // Still Mallory's, unmarked, and running as her wake left it. + expect(await t.row()).toMatchObject({ user_id: 'mallory', ownership_transferred_at: null, status: 'running', session_id: 'sess_woken_again' }) + expect(t.h.raw.prepare("SELECT COUNT(*) AS n FROM audit_log WHERE action = 'workspace.owner_changed'").get()).toEqual({ n: 0 }) + for (const value of MIA_VALUES) { + expect(everythingSent(t.h)).not.toContain(value) + } + }) + it('writes the owner only if the workspace is stopped at that instant', async () => { const t = await setup() // As if the previous owner woke it again between the stop and the write.