Merge pull request #702 from nwparker/cloud/owner-transfer-fix

fix(cloud): OWNER-TRANSFER a workspace is only handed to a current team member, and a transferred workspace stays in shared mode for credentials
This commit is contained in:
Neil
2026-11-13 06:42:12 -08:00
committed by GitHub
22 changed files with 781 additions and 42 deletions
@@ -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';
@@ -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<SandboxRow, 'visibility'>): boolean {
return row.visibility !== 'private'
}
export { isSharedWorkspace } from './shared-workspace'
export const claudeAccount: AccountProvider = {
agent: 'claude',
@@ -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<SandboxRow, 'id' | 'user_id' | 'visibility'>,
row: Pick<SandboxRow, 'id' | 'user_id' | 'visibility' | 'ownership_transferred_at'>,
env: Readonly<Record<string, string>>,
options: {
providerEnabled: Record<CredentialAgent, boolean>
@@ -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,
@@ -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'
@@ -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<WorkspaceRow> {
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, {
+6
View File
@@ -208,6 +208,12 @@ export type Env = {
* GitHub install started by a setup session, the browser goes to <origin>/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). */
+18 -8
View File
@@ -148,7 +148,7 @@ import {
} from './agent-sources'
import { EgressHostsSchema, egressUserHosts, normalizeUserHosts, setEgressUserHosts } from './egress-hosts'
import { egressProxyMissing, 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'
@@ -160,6 +160,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'
@@ -269,6 +270,8 @@ export type Deps = {
onDeviceSessionRevoked?: (deviceSessionId: string) => Promise<void>
/** 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). */
@@ -2314,8 +2317,17 @@ async function workspaceRoute(request: Request, url: URL, env: Env, deps: Deps,
const current = (await getSandbox(env.SANDBOX_DB, row.id)) ?? row
let patched: Awaited<ReturnType<typeof patchWorkspace>> =
Object.keys(rest).length > 0
? await patchWorkspace(db, caller, current, rest, now)
? 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. 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)
}
// 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) {
@@ -2331,7 +2343,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,
@@ -2349,11 +2361,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)
+4 -1
View File
@@ -716,6 +716,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
@@ -812,7 +814,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) => {
+5 -2
View File
@@ -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<string[]> {
// 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)
}
+5 -3
View File
@@ -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<SandboxRow, 'account_id' | 'user_id' | 'repo_url' | 'visibility'>) {
export function ownersForSandbox(row: Pick<SandboxRow, 'account_id' | 'user_id' | 'repo_url' | 'visibility' | 'ownership_transferred_at'>) {
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<string, string>; scopes: Record<stri
export async function resolveSandboxSecrets(
db: D1Database,
keyring: Keyring,
row: Pick<SandboxRow, 'id' | 'account_id' | 'user_id' | 'repo_url' | 'visibility'>
row: Pick<SandboxRow, 'id' | 'account_id' | 'user_id' | 'repo_url' | 'visibility' | 'ownership_transferred_at'>
): Promise<ResolvedSecrets> {
const owners = ownersForSandbox(row)
const pairs: [SecretScope, string][] = []
@@ -30,7 +30,7 @@ export type ProviderEnv = { env: Record<string, string>; scopes: Record<string,
export async function resolvedProviderEnv(
db: D1Database,
keyring: Keyring,
row: Pick<SandboxRow, 'id' | 'account_id' | 'user_id' | 'repo_url' | 'visibility'>
row: Pick<SandboxRow, 'id' | 'account_id' | 'user_id' | 'repo_url' | 'visibility' | 'ownership_transferred_at'>
): Promise<ProviderEnv> {
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
@@ -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<SandboxRow, 'visibility' | 'ownership_transferred_at'>): boolean {
return row.visibility !== 'private' || row.ownership_transferred_at != null
}
+33 -6
View File
@@ -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<Pick<SandboxRow, 'environment_id' | 'environment_build_id' | 'source_snapshot_id' | 'name' | 'repo_branch' | 'visibility' | 'section'>>,
Partial<Pick<SandboxRow, 'environment_id' | 'environment_build_id' | 'source_snapshot_id' | 'name' | 'repo_branch' | 'visibility' | 'section' | 'ownership_transferred_at'>>,
now: number
): Promise<SandboxRow> {
// 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<string>([
])
export type RegistryPatch = Partial<
Pick<SandboxRow, 'name' | 'repo_branch' | 'visibility' | 'section' | 'archived_at' | 'user_id' | 'token_usd_micros'>
Pick<SandboxRow, 'name' | 'repo_branch' | 'visibility' | 'section' | 'archived_at' | 'user_id' | 'token_usd_micros' | 'ownership_transferred_at'>
>
const REGISTRY_PATCHABLE = new Set<string>(['name', 'repo_branch', 'visibility', 'section', 'archived_at', 'user_id', 'token_usd_micros'])
const REGISTRY_PATCHABLE = new Set<string>(['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
@@ -254,6 +261,26 @@ 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<SandboxRow | null> {
const result = await db
// Unlike other registry edits this bumps `generation` on purpose: a wake that read the row just
// before (still the previous owner's, unmarked) must lose its compare-and-set, not boot a
// private environment for them under the new owner.
.prepare(
`UPDATE sandboxes SET user_id = ?, ownership_transferred_at = ?, generation = generation + 1
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<void> {
await db
@@ -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<MembershipAnswer>
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<string> {
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<Env, 'AUTH_INTERNAL_ORIGIN' | 'MEMBER_REMOVED_TOKEN'>,
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 }
}
}
+49 -6
View File
@@ -9,10 +9,11 @@
// 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'
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'
@@ -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<WorkspaceError['code'], number> = {
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,15 @@ 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,
/** Stops a running workspace before it changes owner (the lifecycle suspend). Without one, a running workspace is not handed over. */
stopForTransfer?: (row: SandboxRow) => Promise<unknown>
): Promise<{ row: SandboxRow; revoke: 'none' | 'non_owner' | 'all_but_new_owner' }> {
const metadataOnly = patch.tokenUsdMicros !== undefined && Object.keys(patch).length === 1
if (metadataOnly) {
@@ -372,6 +384,38 @@ export async function patchWorkspace(
if (!row.account_id.includes('org:')) {
throw new WorkspaceError('invalid_request', 'personal_account')
}
// 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 }))
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
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 } : {}),
@@ -381,7 +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 } : {}),
...(patch.ownerUserId !== undefined ? { user_id: patch.ownerUserId } : {}),
...(patch.tokenUsdMicros !== undefined ? { token_usd_micros: patch.tokenUsdMicros } : {})
})
if (!next) {
@@ -393,7 +436,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' }
@@ -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)
}
+3 -1
View File
@@ -64,7 +64,9 @@ export async function createHarness(overrides: Partial<Env> = {}, 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<string, string> = {}) {
// Like Orca auth (service-tokens.ts): iat backdated 30 s for clock skew, exp 5 min from minting.
@@ -0,0 +1,514 @@
// 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<ReturnType<typeof createHarness>>
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<string, string> {
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<string, string> {
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<typeof createHarness>[1] = {}, env: Record<string, string> = {}) {
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<Response>) => {
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<typeof createTeamMembershipCheck>[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.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('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.
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)
const before = await t.row()
expect(await transferOwnership(t.h.env.SANDBOX_DB, t.id, 'mia', t.h.now())).toMatchObject({ user_id: 'mia', ownership_transferred_at: t.h.now() })
// A wake that read the row before the hand-off loses its compare-and-set afterwards.
expect((await t.row()).generation).toBe(before.generation + 1)
const { transition } = await import('../src/store')
expect(await transition(t.h.env.SANDBOX_DB, before, { status: 'resuming' }, t.h.now())).toBeNull()
expect((await t.row()).status).toBe('suspended')
})
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()
})
})
@@ -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()
@@ -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)
})
@@ -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"
}
}