From 9de47a1b63e5fbe6369c25a65edd119b01c61b98 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 23 Nov 2026 14:52:22 -0800 Subject: [PATCH] fix(server): link codes and guest links require the access they hand out Getting a link code through a linked environment now requires the scopes of the access level requested, and creating a guest link requires orchestration:read, in addition to access:write. The link dialog offers linking through another environment only when the session holds them. Co-Authored-By: Claude Fable 5.2 --- apps/server/src/auth/RpcAuthorization.test.ts | 70 +++++++++++++++++++ .../server/src/peer/PeerClientPairing.test.ts | 32 --------- .../settings/LinkedEnvironmentsSettings.tsx | 14 +++- docs/internals/04_environment-auth.md | 11 ++- docs/internals/06_t3-connect.md | 4 +- docs/user/remote-access.md | 3 +- .../contracts/src/clientRpcPermissions.ts | 11 +++ 7 files changed, 107 insertions(+), 38 deletions(-) diff --git a/apps/server/src/auth/RpcAuthorization.test.ts b/apps/server/src/auth/RpcAuthorization.test.ts index 750ff136ac07d..15ddfb90b08a5 100644 --- a/apps/server/src/auth/RpcAuthorization.test.ts +++ b/apps/server/src/auth/RpcAuthorization.test.ts @@ -355,6 +355,76 @@ it.effect("pairs a client with a linked environment only for one that may hand o }).pipe(Effect.scoped), ); +it.effect("hands out a link code or a guest link only within what the session itself holds", () => + Effect.gen(function* () { + const group = WsRpcGroup.omit( + ...[...WsRpcGroup.requests.keys()].filter( + ( + tag, + ): tag is Exclude< + keyof typeof RPC_REQUIRED_SCOPES, + typeof WS_METHODS.peerLinksPairLink | typeof WS_METHODS.guestShareCreate + > => tag !== WS_METHODS.peerLinksPairLink && tag !== WS_METHODS.guestShareCreate, + ), + ); + let handled = 0; + const environmentId = EnvironmentId.make("environment-box"); + const threadId = ThreadId.make("thread-1"); + const handlers = Layer.mergeAll( + group.toLayerHandler(WS_METHODS.peerLinksPairLink, () => + Effect.sync(() => { + handled++; + return { + type: "unsupported" as const, + reason: "link_access" as const, + environmentId, + label: "Box", + urls: [], + }; + }), + ), + group.toLayerHandler(WS_METHODS.guestShareCreate, () => + Effect.sync(() => handled++).pipe(Effect.andThen(Effect.die("no share in this test"))), + ), + ); + const clientWith = (scopes: Parameters[0]) => + RpcTest.makeClient(group).pipe( + Effect.provide(Layer.merge(handlers, RpcAuthorization.layer(scopes))), + ); + + // Managing access alone is not running agents or reading threads. + const manager = yield* clientWith([AuthAccessWriteScope]); + expect( + yield* manager[WS_METHODS.peerLinksPairLink]({ environmentId, access: "read-only" }).pipe( + Effect.flip, + ), + ).toMatchObject({ requiredPermission: AuthOrchestrationReadScope }); + expect( + yield* manager[WS_METHODS.guestShareCreate]({ threadId }).pipe(Effect.flip), + ).toMatchObject({ requiredPermission: AuthOrchestrationReadScope }); + + const supervisor = yield* clientWith([ + AuthAccessWriteScope, + AuthOrchestrationReadScope, + AuthOrchestrationOperateScope, + ]); + expect( + yield* supervisor[WS_METHODS.peerLinksPairLink]({ + environmentId, + access: "full-access", + }).pipe(Effect.flip), + ).toMatchObject({ requiredPermission: AuthSettingsWriteScope }); + expect(handled).toBe(0); + + expect( + (yield* supervisor[WS_METHODS.peerLinksPairLink]({ environmentId, access: "auto" })).type, + ).toBe("unsupported"); + expect(handled).toBe(1); + yield* supervisor[WS_METHODS.guestShareCreate]({ threadId }).pipe(Effect.exit); + expect(handled).toBe(2); + }).pipe(Effect.scoped), +); + it.effect.each([WS_METHODS.subscribeVcsBranch, WS_METHODS.subscribeVcsStatus] as const)( "enforces read permission on stream %s", (method) => diff --git a/apps/server/src/peer/PeerClientPairing.test.ts b/apps/server/src/peer/PeerClientPairing.test.ts index 17a5df1cb75ff..8dacc53f7f07e 100644 --- a/apps/server/src/peer/PeerClientPairing.test.ts +++ b/apps/server/src/peer/PeerClientPairing.test.ts @@ -257,38 +257,6 @@ it.effect("pairs no client while Linked environments is off on either side", () ).pipe(Effect.provide(NodeHttpServer.layerTest)), ); -it.effect("a full-access link gets a pairing code that links another environment to the box", () => - Effect.scoped( - Effect.gen(function* () { - const b = yield* serveBox(); - const a = yield* makeLaptop; - yield* linkTo(a.links, b, "full-access"); - const phone = yield* a.client([AuthAccessWriteScope]); - - const result = yield* a.pairing.requestForLink( - { environmentId: box.environmentId, access: "approval-required" }, - phone, - ); - if (result.type !== "credential") return yield* Effect.die(result); - expect(result).toMatchObject({ environmentId: box.environmentId, urls: [b.url] }); - - // A third environment links with it, at the access the code was minted for. - const desk = yield* Layer.build( - layerLinkingEnvironment(descriptorOf("environment-desk", "Desk")), - ); - const linked = yield* Context.get(desk, PeerLinks.PeerLinks).link({ - url: b.url, - pairingCode: result.credential, - access: "approval-required", - }); - expect(linked).toMatchObject({ - environmentId: box.environmentId, - access: "approval-required", - }); - }), - ).pipe(Effect.provide(NodeHttpServer.layerTest)), -); - it.effect("a linked environment from before link pairing is never asked for one", () => Effect.scoped( Effect.gen(function* () { diff --git a/apps/web/src/components/settings/LinkedEnvironmentsSettings.tsx b/apps/web/src/components/settings/LinkedEnvironmentsSettings.tsx index 5ab6debec7255..71afddaa33b32 100644 --- a/apps/web/src/components/settings/LinkedEnvironmentsSettings.tsx +++ b/apps/web/src/components/settings/LinkedEnvironmentsSettings.tsx @@ -329,8 +329,20 @@ export function LinkEnvironmentDialog({ const throughCandidate = prefilledThrough ?? (targetId === null || canMint ? null : (routes.get(targetId) ?? null)); const via = useEnvironment(throughCandidate?.viaEnvironmentId ?? null); + // The environment holding the link refuses a level this client's session there does not hold. + const mayPairThrough = useAtomValue( + peerLinkEnvironment.pairLink.permissionAtom( + throughCandidate?.viaEnvironmentId ?? null, + throughCandidate === null + ? undefined + : { environmentId: throughCandidate.environmentId, access }, + ), + ); const through = - throughCandidate !== null && via !== null && !throughFailed.has(throughCandidate.environmentId) + throughCandidate !== null && + via !== null && + mayPairThrough && + !throughFailed.has(throughCandidate.environmentId) ? throughCandidate : null; const linkBack = useLinkBackOffers(environmentId, targetId === null ? [] : [targetId], open); diff --git a/docs/internals/04_environment-auth.md b/docs/internals/04_environment-auth.md index 53c9f60d8f7c6..8b80c65860d60 100644 --- a/docs/internals/04_environment-auth.md +++ b/docs/internals/04_environment-auth.md @@ -8,8 +8,12 @@ model. ## Scoped sessions and delegation - **Narrowing only.** Pairing delegates a set of scopes. Exchanging a bootstrap credential can - narrow a grant, never widen it. Creating another pairing link requires both `access:write` and - every scope being delegated. Client labels and device metadata have no authorization role. + narrow a grant, never widen it. Client labels and device metadata have no authorization role. +- **`access:write` hands out what the session holds and revokes anything.** Every route that + returns a credential to the caller requires `access:write` and every scope that credential + carries: a pairing link, an MCP sign-in approval, a link code from a linked environment, and a + guest link (which reads a thread). Revoking is not capped by the caller's scopes. Admitting a + T3 Connect device is not delegation; see [T3 Connect](./06_t3-connect.md). - **Why the default grant includes management scopes.** `terminal:operate` and `orchestration:operate` already grant OS-user execution (which can invoke `t3 auth session issue`), so withholding access or relay management from the default grant would not be a real boundary. @@ -48,7 +52,8 @@ model. - **Peer-link client pairing.** Only a full-access peer link may mint a one-time client pairing on the linked environment (since full-access agents can already run `t3 auth pairing create`). The linked side enforces this itself, and the operation stays a human-triggered RPC/HTTP route rather - than an MCP tool. + than an MCP tool. The same route mints a code that links another environment at a chosen access + level, and the linking side's RPC requires the caller to hold that level's scopes. ### Reusable dev credential diff --git a/docs/internals/06_t3-connect.md b/docs/internals/06_t3-connect.md index 2962721830a86..dc60a5468b849 100644 --- a/docs/internals/06_t3-connect.md +++ b/docs/internals/06_t3-connect.md @@ -28,7 +28,9 @@ Deployment and Clerk configuration live in the [Connect setup runbook](../operat compromised relay signing key. Enabling `requireConnectDeviceApproval` shifts trust from the relay to approved DPoP key thumbprints: untrusted keys mint credentials that cannot be spent until someone with `access:write` approves the key, and revoking a key revokes all of its sessions and - pending credentials. + pending credentials. Approving admits a device already signed in to the owner's cloud account + and returns no credential to the approver, so `access:write` alone is enough, as it is for + turning the setting off. - **Anti-hijack and anti-SSRF rules.** Managed tunnels expose only the server's own loopback HTTP origin (ignoring caller-supplied origins). The relay refuses to relink an active environment ID under a different key until unlinked, resolves endpoints only from its own managed allocations, diff --git a/docs/user/remote-access.md b/docs/user/remote-access.md index 051445f03eb07..bdcd2281ccd08 100644 --- a/docs/user/remote-access.md +++ b/docs/user/remote-access.md @@ -488,7 +488,8 @@ it needs no pairing code. A machine this app is not connected to, shown only because another environment links to it, can still be linked without a code when that link has full access: drop the plus on it and the environment holding the link gets the pairing code -from it. Your session on that environment must manage access there. The result +from it. Your session on that environment must manage access there and already +have the access you pick. The result is a separate link with the access you pick, listed and revocable on the linked machine like any other. Links still do not chain. diff --git a/packages/contracts/src/clientRpcPermissions.ts b/packages/contracts/src/clientRpcPermissions.ts index b23b4650b20a9..eb61fff1c0abd 100644 --- a/packages/contracts/src/clientRpcPermissions.ts +++ b/packages/contracts/src/clientRpcPermissions.ts @@ -11,7 +11,9 @@ import { AuthSourceControlWriteScope, AuthTerminalOperateScope, type AuthEnvironmentScope, + mcpClientScopes, } from "./auth.ts"; +import { PeerLinkLinkPairingInput } from "./peerLink.ts"; import { ORCHESTRATION_V2_WS_METHODS } from "./orchestrationV2.ts"; import { WS_METHODS } from "./rpc.ts"; @@ -106,6 +108,7 @@ export type ClientGuardedRpcTag = keyof typeof CLIENT_GUARDED_RPC_SCOPES; const decodePrepareThread = Schema.decodeUnknownSync(GitPreparePullRequestThreadInput); const decodeStackedAction = Schema.decodeUnknownSync(GitRunStackedActionInput); +const decodeLinkPairing = Schema.decodeUnknownSync(PeerLinkLinkPairingInput); export function clientRpcRequiredScopes( method: string, @@ -124,6 +127,14 @@ export function clientRpcRequiredScopes( ) return [AuthSourceControlWriteScope, AuthOrchestrationReadScope]; } + // Handing out access takes holding it: the caller gets back a credential it can spend itself. + if (method === WS_METHODS.peerLinksPairLink && input !== undefined) { + const held = mcpClientScopes(decodeLinkPairing(input).access); + return [AuthAccessWriteScope, ...held]; + } + if (method === WS_METHODS.guestShareCreate) { + return [AuthAccessWriteScope, AuthOrchestrationReadScope]; + } return Object.hasOwn(CLIENT_GUARDED_RPC_SCOPES, method) ? [CLIENT_GUARDED_RPC_SCOPES[method as ClientGuardedRpcTag]] : [];