From b99fa165c181efc308e8f4e9dc2f601cfb1df43e Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 16 Nov 2026 01:04:38 -0800 Subject: [PATCH] fix(cloud): TEAM-SHARING hold keeps shared credential mode for the session a teammate's grant was revoked in (M1); sweep only team workspaces (L1) --- cloud/apps/sandbox-control/src/grants.ts | 2 +- .../apps/sandbox-control/src/shared-workspace.ts | 14 +++++++++++--- .../test/team-sharing-hold.test.ts | 15 ++++++++++++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/cloud/apps/sandbox-control/src/grants.ts b/cloud/apps/sandbox-control/src/grants.ts index b4e87c6a426..3375ef6f22c 100644 --- a/cloud/apps/sandbox-control/src/grants.ts +++ b/cloud/apps/sandbox-control/src/grants.ts @@ -274,7 +274,7 @@ export async function sweepHeldTeamGrants(deps: LifecycleDeps): Promise<{ worksp const { results } = await deps.db .prepare( `SELECT g.* FROM workspace_grants g JOIN sandboxes s ON s.id = g.sandbox_id - WHERE g.status IN ('issuing', 'active') AND g.user_id <> s.user_id AND s.ownership_transferred_at IS NULL AND s.status <> 'destroyed' + WHERE g.status IN ('issuing', 'active') AND g.user_id <> s.user_id AND s.visibility = 'team' AND s.ownership_transferred_at IS NULL AND s.status <> 'destroyed' ORDER BY g.sandbox_id LIMIT 500` ) .all() diff --git a/cloud/apps/sandbox-control/src/shared-workspace.ts b/cloud/apps/sandbox-control/src/shared-workspace.ts index e9ecb4aefd6..1bf87043013 100644 --- a/cloud/apps/sandbox-control/src/shared-workspace.ts +++ b/cloud/apps/sandbox-control/src/shared-workspace.ts @@ -17,8 +17,9 @@ export function isSharedWorkspace(row: Pick ? AND status IN ('issuing', 'active', 'revoking')`) + .prepare( + `SELECT COUNT(*) AS n FROM workspace_grants g JOIN sandboxes s ON s.id = g.sandbox_id WHERE g.sandbox_id = ? AND g.user_id <> ? + AND (g.status IN ('issuing', 'active', 'revoking') + OR (s.session_started_at IS NOT NULL AND g.revoked_at IS NOT NULL AND g.revoked_at >= s.session_started_at))` + ) .bind(row.id, row.user_id) .first<{ n: number }>() .catch(() => null) diff --git a/cloud/apps/sandbox-control/test/team-sharing-hold.test.ts b/cloud/apps/sandbox-control/test/team-sharing-hold.test.ts index 2411b5867bf..ffe1fa4b836 100644 --- a/cloud/apps/sandbox-control/test/team-sharing-hold.test.ts +++ b/cloud/apps/sandbox-control/test/team-sharing-hold.test.ts @@ -104,7 +104,12 @@ describe('TEAM_SHARING held (the default)', () => { expect((await resolveSandboxSecrets(db, keyring, row)).env.MY_TOKEN).toBeUndefined() expect(await sweepHeldTeamGrants(life)).toEqual({ workspaces: 1, revoked: 1, queued: 0 }) expect(h.raw.prepare('SELECT status, revoke_reason FROM workspace_grants WHERE user_id = ?').get('bob')).toEqual({ status: 'revoked', revoke_reason: 'team_sharing_held' }) - // Cleared: it is the owner's alone, so it acts private for credentials too. + // Review M1: revoked during this session, so what Bob started may still run: shared until the next boot. + expect(await sharedWorkspaceMode(db, row)).toBe(true) + expect((await resolveSandboxSecrets(db, keyring, row)).env.MY_TOKEN).toBeUndefined() + // A new session (restart): the owner's alone, so it acts private for credentials too. + h.advance(1_000) + expect((await h.call('POST', `/v1/sandboxes/${id}/restart`, { bearer: alice })).status).toBe(200) expect(await sharedWorkspaceMode(db, row)).toBe(false) expect((await resolveSandboxSecrets(db, keyring, row)).env.MY_TOKEN).toBe('personal-value') // Nothing left to sweep; and with sharing on again, nothing is swept at all. @@ -112,5 +117,13 @@ describe('TEAM_SHARING held (the default)', () => { configureTeamSharing({ TEAM_SHARING: 'on' }) expect(await sharedWorkspaceMode(db, row)).toBe(true) }) + + it('review L1: the sweep leaves private workspaces alone', async () => { + const t = await held() + t.h.raw + .prepare(`INSERT INTO workspace_grants (id, sandbox_id, account_id, user_id, device_kind, device_label, status, created_at) VALUES ('grt_p', ?, 'org:acme', 'bob', 'desktop', 'b', 'active', 1)`) + .run(t.id) + expect(await sweepHeldTeamGrants((await lifecycleDeps(t.h.env, t.h.deps))!)).toEqual({ workspaces: 0, revoked: 0, queued: 0 }) + }) })