Files
archived-orca/cloud/apps/sandbox-control/test/member-git.test.ts
T
Neil 777f078c76 feat(cloud): W4 team collaboration on cloud workspaces
## W4: team collaboration on Orca Cloud workspaces

### Control plane (`cloud/apps/sandbox-control`)
- **Registry** (`0024_workspace_registry.sql`, applied on dev D1): owner (reassignable), `created_by`, branch, `visibility` (private default / team), section, last activity, token spend. Reuses 0020's `name`/`archived_at`.
- **Routes under `/v1/team/*`** (W5 owns `/v1/workspaces`): list with Mine/Team/All, By, In, Archived filters; PATCH (rename, share or make private, section, archive, reassign owner); presence heartbeats; wake and lease for teammates; links resolve; org prompts/PR instructions (compare-and-set).
- **Security fix:** `/v1/sandboxes/:id` used to be account-scoped, so any org member could resume a teammate's sandbox and receive its orcad pairing code. It is now owner-only; admins can still suspend and destroy.
- **Per-member, per-device E2EE grants:**
  - The VM mints a single-device orcad offer and seals it to the requesting device's X25519 key.
  - The control plane stores only the orcad device id.
  - Revocation runs `orca pair --revoke`, which closes the device's live sockets, plus an on-disk registry removal.
  - When the VM is asleep, the revoke is queued and applied at boot before orcad starts.
  - Triggers: explicit revoke, making a workspace private, archive, reassign, member removal (internal route), and phone device-session revoke (W8).
  - VMs from older images get the entry points installed on demand.
- **Git as the sender:** a member's own GitHub user-to-server token (App OAuth, envelope-encrypted) is swapped in at egress only for the workspace repo. The VM gets per-member git configs and `orca-git-as`.
- **Cost:** metered compute × billing rate, plus optional token spend.

### orcad (generic)
- New owner-only RPC `pairing.revokeDevice` and `orca pair --revoke <device-id>`: revokes a device and closes its open connections immediately.

### Desktop
- A "Cloud workspaces" section on Home: Team/Mine, By, In and Archived filters; sections; live presence; cost; last activity. Single-click Open on a teammate workspace pairs this device through an E2EE grant. Also a row menu (share/private, move to section, reassign, archive) and copy link.
- Deep links `orca://cloud/w/<id>[/c/<chat>]` (open-url, argv, second instance, sidecar handoff) and the `cloudWorkspace.copyLink` keybinding.
- Messages to agents in shared workspaces carry a sender envelope.
- Uses the W10 shared primitives.
- Two rounds of adversarial design and frontend review.

### Evidence
Real Vercel VMs; see the W4 report. Numbers:
- grant issued in 0.55 s, paired in 1.35 s
- revoke closes the live desktop socket ~30 ms after the PATCH returns
- two members' pushes land on GitHub with their own author identities (shared dev Worker, preview alias w4); a push to another repo gets 403
2026-11-09 06:53:24 -08:00

132 lines
6.9 KiB
TypeScript

import { SignJWT, createLocalJWKSet, exportJWK, generateKeyPair } from 'jose'
import { describe, expect, it } from 'vitest'
import { generateGrantRecipientKey } from '../../../../src/main/cloud-workspaces/sealed-grant-node'
import { memberPlaceholder } from '../src/github-user-tokens'
import { MEMBERS_COMMAND } from '../src/lifecycle'
import { gitBasicAuthorization } from '../src/network-policy'
import { createHarness } from './harness'
const PROXY_URL = 'https://sandbox.test/v1/egress/proxy'
const ISSUER = 'https://oidc.vercel.com/team_orca'
const KEK = Buffer.alloc(32, 7).toString('base64')
const OP = 'o'.repeat(40)
const REPO = 'https://github.com/acme/app'
async function setup(memberGit: string | undefined = 'on') {
const { privateKey, publicKey } = await generateKeyPair('RS256')
const jwks = { keys: [{ ...(await exportJWK(publicKey)), kid: 'v1', alg: 'RS256' }] }
const upstream: { url: string; authorization: string | null }[] = []
const h = await createHarness(
{ EGRESS_PROXY_URL: PROXY_URL, SECRETS_KEK: KEK, OPERATOR_TOKEN: OP, ...(memberGit ? { MEMBER_GIT_IDENTITY: memberGit } : {}) },
{
egressKeysForIssuer: () => createLocalJWKSet(jwks),
credentials: { authorizationFor: async () => 'Bearer ghs_app' },
github: {
tokenForRepo: async (url: string) => ({ token: `ghs_member_stand_in_for_${url.split('/').at(-1)}`, expiresAt: h.now() + 3_600_000 }),
installationForRepo: async () => null
} as never,
egressFetch: async (input, init) => {
upstream.push({ url: String(input), authorization: new Headers(init?.headers).get('authorization') })
return new Response('ok', { status: 200 })
}
}
)
const as = (sub: string, role = 'member') => h.token(sub, 'acme', 'sandbox', { org_role: role })
const created = await h.call('POST', '/v1/sandboxes', {
bearer: await as('alice'),
body: { workspaceId: 'ws-1', machineSize: 'standard', repo: { url: REPO }, visibility: 'team' }
})
const id: string = created.body.sandbox.id
const seed = (userId: string, login: string, githubUserId: number) =>
h.call('POST', '/v1/internal/dev/member-github', {
bearer: OP,
body: { accountId: 'org:acme', userId, repoUrl: REPO, login, githubUserId, name: `${login} name` }
})
async function oidc() {
return new SignJWT({ team_id: 'team_orca', project_id: 'prj_orca', sandbox_id: 'sbx-v', sandbox_name: `orca-test-${id}` })
.setProtectedHeader({ alg: 'RS256', kid: 'v1' })
.setIssuer(ISSUER)
.setAudience(PROXY_URL)
.setIssuedAt()
.setExpirationTime('5m')
.sign(privateKey)
}
async function proxied(host: string, path: string, authorization: string) {
const response = await h.send(
new Request(`${PROXY_URL}${path}`, {
method: 'POST',
body: 'pack',
headers: {
'vercel-forwarded-host': host,
'vercel-forwarded-scheme': 'https',
'vercel-forwarded-port': '443',
'vercel-forwarded-path': path,
'vercel-sandbox-oidc-token': await oidc(),
authorization
}
})
)
return { status: response.status, body: await response.text() }
}
return { h, id, as, seed, proxied, upstream }
}
describe('git as the sender', () => {
it('routes each connected member placeholder to their own token, only for the workspace repo', async () => {
const t = await setup()
expect((await t.seed('alice', 'alice-gh', 11)).body.stored).toBe(true)
const alicePh = await memberPlaceholder(t.id, 'alice')
const push = await t.proxied('github.com', '/acme/app.git/git-receive-pack', gitBasicAuthorization(alicePh))
expect(push.status).toBe(200)
expect(t.upstream.at(-1)).toEqual({
url: 'https://github.com/acme/app.git/git-receive-pack',
authorization: gitBasicAuthorization('ghs_member_stand_in_for_app')
})
// The member token is pinned to the workspace repository.
expect(await t.proxied('github.com', '/acme/other.git/git-receive-pack', gitBasicAuthorization(alicePh))).toEqual({ status: 403, body: 'path_not_allowed' })
expect(await t.proxied('github.com', '/acme/app.git/../other.git/info/refs', gitBasicAuthorization(alicePh))).toEqual({ status: 403, body: 'path_not_allowed' })
expect((await t.proxied('api.github.com', '/repos/acme/app/pulls', `Bearer ${alicePh}`)).status).toBe(200)
expect(await t.proxied('api.github.com', '/user/repos', `Bearer ${alicePh}`)).toEqual({ status: 403, body: 'path_not_allowed' })
// A teammate without a grant has no route at all, even if connected.
await t.seed('bob', 'bob-gh', 22)
const bobPh = await memberPlaceholder(t.id, 'bob')
expect(await t.proxied('github.com', '/acme/app.git/git-receive-pack', gitBasicAuthorization(bobPh))).toEqual({ status: 403, body: 'placeholder_missing' })
})
it('adds a member to the policy and the VM git configs when they get a grant, and drops them on revoke', async () => {
const t = await setup()
await t.seed('alice', 'alice-gh', 11)
await t.seed('bob', 'bob-gh', 22)
const bob = await t.as('bob')
const issued = await t.h.call('POST', `/v1/team/workspaces/${t.id}/grants`, {
bearer: bob,
body: { recipientPublicKeyB64: generateGrantRecipientKey().publicKeyB64, deviceLabel: 'b', deviceKind: 'desktop' }
})
expect(issued.status).toBe(200)
const box = [...t.h.vercel.boxes.values()][0]!
const bobPh = gitBasicAuthorization(await memberPlaceholder(t.id, 'bob'))
const githubRules = JSON.stringify(box.policy.allow['github.com'])
expect(githubRules).toContain(bobPh)
const sync = t.h.vercel.commands.filter((c) => c.command === MEMBERS_COMMAND).at(-1)!
const members = JSON.parse(Buffer.from(sync.env.ORCA_MEMBERS_B64!, 'base64').toString())
expect(members.map((m: { userId: string; email: string }) => [m.userId, m.email])).toEqual([
['alice', '11+alice-gh@users.noreply.github.com'],
['bob', '22+bob-gh@users.noreply.github.com']
])
expect(JSON.stringify(members)).not.toContain('ghs_')
expect((await t.proxied('github.com', '/acme/app.git/info/refs', bobPh)).status).toBe(200)
await t.h.call('DELETE', `/v1/team/workspaces/${t.id}/grants/${issued.body.grant.id}`, { bearer: bob })
expect(JSON.stringify(box.policy.allow['github.com'])).not.toContain(bobPh)
expect(await t.proxied('github.com', '/acme/app.git/info/refs', bobPh)).toEqual({ status: 403, body: 'placeholder_missing' })
})
it('is off unless the org opted in: member placeholders get no credential, the VM no member configs', async () => {
const t = await setup('off')
await t.seed('alice', 'alice-gh', 11)
const alicePh = await memberPlaceholder(t.id, 'alice')
expect(await t.proxied('github.com', '/acme/app.git/git-receive-pack', gitBasicAuthorization(alicePh))).toEqual({ status: 403, body: 'credential_unavailable' })
expect(t.h.vercel.commands.some((c) => c.command === MEMBERS_COMMAND)).toBe(false)
})
})