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)

This commit is contained in:
Neil
2026-11-16 01:04:38 -08:00
parent 05fe1508f2
commit b99fa165c1
3 changed files with 26 additions and 5 deletions
+1 -1
View File
@@ -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<GrantRow>()
@@ -17,8 +17,9 @@ export function isSharedWorkspace(row: Pick<SandboxRow, 'visibility' | 'ownershi
/**
* The rule above, precisely, for callers that can read the database (all of them today). TEAM-SHARING hold: a
* workspace stored as `team` acts private while sharing is held, but keeps shared mode until no one but its
* owner holds a device grant on it (the hold's sweep revokes them, grants.ts sweepHeldTeamGrants): personal
* credentials never reach a VM a teammate may still be paired to. Fails closed on a failed read.
* owner holds a device grant on it (the hold's sweep revokes them, grants.ts sweepHeldTeamGrants), and none was
* revoked during the current session (what a teammate started there may still run): personal credentials never
* reach a VM a teammate may still be paired to or running in. Fails closed on a failed read.
*/
export async function sharedWorkspaceMode(
db: D1Database,
@@ -30,8 +31,15 @@ export async function sharedWorkspaceMode(
if (teamSharingEnabled() || row.visibility !== 'team') {
return true
}
// Review M1: a revoke closes a teammate's connections, not what they left running in this session's
// terminals. A grant revoked during the current session keeps it shared until the next boot.
// The session start is read here, not from the caller's row (which may be a partial or older copy).
const others = await db
.prepare(`SELECT COUNT(*) AS n FROM workspace_grants WHERE sandbox_id = ? AND user_id <> ? 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)
@@ -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 })
})
})