Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions apps/server/src/auth/RpcAuthorization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof RpcAuthorization.layer>[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) =>
Expand Down
32 changes: 0 additions & 32 deletions apps/server/src/peer/PeerClientPairing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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* () {
Expand Down
14 changes: 13 additions & 1 deletion apps/web/src/components/settings/LinkedEnvironmentsSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 8 additions & 3 deletions docs/internals/04_environment-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion docs/internals/06_t3-connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion docs/user/remote-access.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
11 changes: 11 additions & 0 deletions packages/contracts/src/clientRpcPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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,
Expand All @@ -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]]
: [];
Expand Down
Loading