TypeScript
feat(auth): pairing grants access and T3 Connect management by default (#20432)
Julius Marminge19108aa7b4f8
diff --git a/apps/server/src/auth/EnvironmentAuth.test.ts b/apps/server/src/auth/EnvironmentAuth.test.tsindex 9777096524d..4ec8eff2bfe 100644--- a/apps/server/src/auth/EnvironmentAuth.test.ts+++ b/apps/server/src/auth/EnvironmentAuth.test.ts@@ -1,9 +1,5 @@import * as NodeServices from "@effect/platform-node/NodeServices";-import {- authScopeResponse,- AuthAdministrativeScopes,- AuthStandardClientScopes,-} from "@t3tools/contracts";+import { authScopeResponse, AuthStandardClientScopes } from "@t3tools/contracts";import { expect, it } from "@effect/vitest";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";@@ -103,7 +99,7 @@ it.layer(NodeServices.layer)("EnvironmentAuth.layer", (it) => {const authenticated = yield* serverAuth.authenticateHttpRequest(request);expect(devExchange.cookieName).toMatch(/^t3_dev_session_/);expect(devExchange.expireNormalCookie).toBe(true);- expect(devExchange.response).toMatchObject(authScopeResponse(AuthAdministrativeScopes));+ expect(devExchange.response).toMatchObject(authScopeResponse(AuthStandardClientScopes));expect(authenticated.scopes).toEqual(["orchestration:read"]);}).pipe(Effect.provide(@@ -253,7 +249,7 @@ it.layer(NodeServices.layer)("EnvironmentAuth.layer", (it) => {expect((yield* Effect.flip(sessions.verify(token)))._tag).toBe("SessionTokenRevokedError");expect((yield* serverAuth.createBrowserSession(recovery.credential, requestMetadata)).response,- ).toMatchObject({ authenticated: true, ...authScopeResponse(AuthAdministrativeScopes) });+ ).toMatchObject({ authenticated: true, ...authScopeResponse(AuthStandardClientScopes) });}).pipe(Effect.provide(layerEnvironmentAuth({@@ -362,7 +358,9 @@ it.layer(NodeServices.layer)("EnvironmentAuth.layer", (it) => {it.effect("preserves pairing grants after rejecting scopes they do not grant", () =>Effect.gen(function* () {const serverAuth = yield* EnvironmentAuth.EnvironmentAuth;- const pairingCredential = yield* serverAuth.issuePairingCredential();+ const pairingCredential = yield* serverAuth.issuePairingCredential({+ scopes: ["orchestration:read", "orchestration:operate"],+ });const error = yield* serverAuth.exchangeBootstrapCredentialForAccessToken(@@ -514,7 +512,7 @@ it.layer(NodeServices.layer)("EnvironmentAuth.layer", (it) => {Effect.gen(function* () {const serverAuth = yield* EnvironmentAuth.EnvironmentAuth;const pairingCredential = yield* serverAuth.issuePairingCredential({- scopes: AuthAdministrativeScopes,+ scopes: AuthStandardClientScopes,});const listedPairingLinks = yield* serverAuth.listPairingLinks();@@ -544,7 +542,7 @@ it.layer(NodeServices.layer)("EnvironmentAuth.layer", (it) => {makeCookieRequest(sessions.cookieName, exchanged.sessionToken),);- expect(verified.scopes).toEqual(AuthAdministrativeScopes);+ expect(verified.scopes).toEqual(AuthStandardClientScopes);expect(verified.subject).toBe("administrative-bootstrap");}).pipe(Effect.provide(layerEnvironmentAuth())),);diff --git a/apps/server/src/auth/EnvironmentAuth.ts b/apps/server/src/auth/EnvironmentAuth.tsindex e4b48f3c5fb..91bc0703521 100644--- a/apps/server/src/auth/EnvironmentAuth.ts+++ b/apps/server/src/auth/EnvironmentAuth.ts@@ -1,7 +1,6 @@import {AuthAccessTokenType,AuthAccessWriteScope,- AuthAdministrativeScopes,AuthOrchestrationOperateScope,AuthOrchestrationReadScope,AuthSettingsWriteScope,@@ -1096,7 +1095,7 @@ export const make = Effect.gen(function* () {.issue({subject: input?.subject ?? DEFAULT_SESSION_SUBJECT,method: "bearer-access-token",- scopes: input?.scopes ?? AuthAdministrativeScopes,+ scopes: input?.scopes ?? AuthStandardClientScopes,client: {...(input?.label ? { label: input.label } : {}),deviceType: "bot",@@ -1151,7 +1150,7 @@ export const make = Effect.gen(function* () {const issueStartupPairingCredential: EnvironmentAuth["Service"]["issueStartupPairingCredential"] =() => {const fallback = issuePairingCredentialForSubject({- scopes: AuthAdministrativeScopes,+ scopes: AuthStandardClientScopes,subject: INTERNAL_ADMINISTRATIVE_BOOTSTRAP_SUBJECT,purpose: "startup",});diff --git a/apps/server/src/auth/EnvironmentAuthAdmin.test.ts b/apps/server/src/auth/EnvironmentAuthAdmin.test.tsindex 350ed0305c3..7e1ead393a8 100644--- a/apps/server/src/auth/EnvironmentAuthAdmin.test.ts+++ b/apps/server/src/auth/EnvironmentAuthAdmin.test.ts@@ -1,4 +1,4 @@-import { AuthAdministrativeScopes } from "@t3tools/contracts";+import { AuthStandardClientScopes } from "@t3tools/contracts";import * as NodeServices from "@effect/platform-node/NodeServices";import { expect, it } from "@effect/vitest";import * as Effect from "effect/Effect";@@ -80,11 +80,11 @@ it.layer(NodeServices.layer)("EnvironmentAuth administrative operations", (it) =const listedAfterRevoke = yield* environmentAuth.listSessions();expect(issued.method).toBe("bearer-access-token");- expect(issued.scopes).toEqual(AuthAdministrativeScopes);+ expect(issued.scopes).toEqual(AuthStandardClientScopes);expect(issued.client.deviceType).toBe("bot");expect(issued.client.label).toBe("deploy-bot");expect(verified.sessionId).toBe(issued.sessionId);- expect(verified.scopes).toEqual(AuthAdministrativeScopes);+ expect(verified.scopes).toEqual(AuthStandardClientScopes);expect(verified.method).toBe("bearer-access-token");expect(listedBeforeRevoke).toHaveLength(1);expect(listedBeforeRevoke[0]?.sessionId).toBe(issued.sessionId);diff --git a/apps/server/src/auth/McpOAuth.test.ts b/apps/server/src/auth/McpOAuth.test.tsindex 3e2e668b5bb..0846056dafe 100644--- a/apps/server/src/auth/McpOAuth.test.ts+++ b/apps/server/src/auth/McpOAuth.test.ts@@ -1,6 +1,6 @@import * as NodeServices from "@effect/platform-node/NodeServices";import {- AuthAdministrativeScopes,+ AuthStandardClientScopes,type AuthCreatePairingCredentialInput,EnvironmentHttpApi,} from "@t3tools/contracts";@@ -502,7 +502,7 @@ it.live("one-click approves only access the browser session holds the scopes forEffect.flatMap(json<{ csrfToken?: string; oneClickAccess?: ReadonlyArray<string> }>),);- const admin = yield* browserCookie([...AuthAdministrativeScopes]);+ const admin = yield* browserCookie([...AuthStandardClientScopes]);const adminDetails = yield* details(admin);expect(adminDetails.csrfToken).toEqual(expect.any(String));expect(adminDetails.oneClickAccess).toEqual([diff --git a/apps/server/src/auth/PairingGrantStore.test.ts b/apps/server/src/auth/PairingGrantStore.test.tsindex 738650a3596..1047f43489d 100644--- a/apps/server/src/auth/PairingGrantStore.test.ts+++ b/apps/server/src/auth/PairingGrantStore.test.ts@@ -1,4 +1,4 @@-import { AuthAdministrativeScopes, AuthStandardClientScopes } from "@t3tools/contracts";+import { AuthStandardClientScopes } from "@t3tools/contracts";import * as NodeServices from "@effect/platform-node/NodeServices";import { expect, it } from "@effect/vitest";import * as Duration from "effect/Duration";@@ -163,7 +163,7 @@ it.layer(NodeServices.layer)("PairingGrantStore.layer", (it) => {const third = yield* bootstrapCredentials.consume("desktop-bootstrap-token");expect(first.method).toBe("desktop-bootstrap");- expect(first.scopes).toEqual(AuthAdministrativeScopes);+ expect(first.scopes).toEqual(AuthStandardClientScopes);expect(first.subject).toBe("desktop-bootstrap");expect(second.method).toBe("desktop-bootstrap");expect(third.method).toBe("desktop-bootstrap");diff --git a/apps/server/src/auth/PairingGrantStore.ts b/apps/server/src/auth/PairingGrantStore.tsindex 67c8f82d669..36a2bc34b56 100644--- a/apps/server/src/auth/PairingGrantStore.ts+++ b/apps/server/src/auth/PairingGrantStore.ts@@ -1,5 +1,4 @@import {- AuthAdministrativeScopes,AuthStandardClientScopes,type AuthEnvironmentScope,type AuthPairingLink,@@ -316,7 +315,7 @@ export const make = Effect.gen(function* () {const now = yield* DateTime.now;yield* seedGrant(config.desktopBootstrapToken, {method: "desktop-bootstrap",- scopes: AuthAdministrativeScopes,+ scopes: AuthStandardClientScopes,subject: "desktop-bootstrap",expiresAt: DateTime.add(now, {milliseconds: Duration.toMillis(DESKTOP_BOOTSTRAP_TTL_HOURS),@@ -441,7 +440,7 @@ export const make = Effect.gen(function* () {if (consumeRotatingDesktopToken(credential, now.epochMilliseconds)) {return {method: "desktop-bootstrap",- scopes: AuthAdministrativeScopes,+ scopes: AuthStandardClientScopes,subject: "desktop-bootstrap",expiresAt: DateTime.add(now, {milliseconds: DESKTOP_BOOTSTRAP_TOKEN_WINDOW_MS,diff --git a/apps/server/src/auth/SessionStore.ts b/apps/server/src/auth/SessionStore.tsindex 3585d44b9c4..b9fda7a67e4 100644--- a/apps/server/src/auth/SessionStore.ts+++ b/apps/server/src/auth/SessionStore.ts@@ -1,6 +1,5 @@import {AuthSessionId,- AuthAdministrativeScopes,AuthStandardClientScopes,AuthEnvironmentScopes,type AuthClientMetadata,@@ -508,7 +507,7 @@ export const make = Effect.gen(function* () {.createIfAbsent({sessionId: devAuth.sessionId,subject: "reusable-dev-token",- scopes: AuthAdministrativeScopes,+ scopes: AuthStandardClientScopes,method: "browser-session-cookie",client: {label: "Reusable dev token",diff --git a/apps/server/src/cli/auth.ts b/apps/server/src/cli/auth.tsindex 32934a97ca4..8195538a55c 100644--- a/apps/server/src/cli/auth.ts+++ b/apps/server/src/cli/auth.ts@@ -1,8 +1,4 @@-import {- AuthAdministrativeScopes,- AuthSessionId,- AuthStandardClientScopes,-} from "@t3tools/contracts";+import { AuthSessionId, AuthStandardClientScopes } from "@t3tools/contracts";import * as Console from "effect/Console";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";@@ -163,7 +159,7 @@ const pairingCommand = Command.make("pairing").pipe(const sessionIssueCommand = Command.make("issue", {...authLocationFlags,- scopes: authScopesFlag(AuthAdministrativeScopes),+ scopes: authScopesFlag(AuthStandardClientScopes),ttl: ttlFlag,label: labelFlag,subject: subjectFlag,diff --git a/apps/server/src/cli/project.ts b/apps/server/src/cli/project.tsindex 25bebd48dfa..8f352489b88 100644--- a/apps/server/src/cli/project.ts+++ b/apps/server/src/cli/project.ts@@ -1,5 +1,5 @@import {- AuthAdministrativeScopes,+ AuthStandardClientScopes,CommandId,EnvironmentHttpApi,EnvironmentHttpCommonError,@@ -221,7 +221,7 @@ const withProjectCliSessionToken = <A, E, R>() =>Effect.acquireUseRelease(environmentAuth.issueSession({- scopes: AuthAdministrativeScopes,+ scopes: AuthStandardClientScopes,label: "t3 project cli",}),(issued) => run(issued.token),diff --git a/apps/web/src/components/cloud/ConnectOnboardingDialog.tsx b/apps/web/src/components/cloud/ConnectOnboardingDialog.tsxindex a11f092978c..22a60a65dbc 100644--- a/apps/web/src/components/cloud/ConnectOnboardingDialog.tsx+++ b/apps/web/src/components/cloud/ConnectOnboardingDialog.tsx@@ -1,11 +1,6 @@import { useAuth } from "@clerk/react";import { useAtomValue } from "@effect/atom-react";-import {- AuthAdministrativeScopes,- AuthRelayReadScope,- AuthRelayWriteScope,- type AuthSessionState,-} from "@t3tools/contracts";+import { AuthRelayReadScope, AuthRelayWriteScope, type AuthSessionState } from "@t3tools/contracts";import { AsyncResult, Atom } from "effect/reactivity";import { useEffect, useRef, useState } from "react";diff --git a/docs/internals/environment-auth.md b/docs/internals/environment-auth.mdindex 132b6554812..afee0990625 100644--- a/docs/internals/environment-auth.md+++ b/docs/internals/environment-auth.md@@ -7,9 +7,13 @@ identity and relay credentials belong to a separate trust boundary, described in## Authority survives transport changesPairing delegates a set of scopes. Exchanging a bootstrap credential can narrow-that grant but cannot widen it. Ordinary pairing does not grant access-management-or relay-management authority. Creating another pairing link requires both-`access:write` and every scope being delegated. The+that grant but cannot widen it. The default grant includes access and relay+management: `terminal:operate` is a shell as the server's user and+`orchestration:operate` can start a full-access agent, and either can mint a+session with every scope through `t3 auth session issue`, so withholding+management from the default would not be a boundary. A narrower client gets+explicit scopes. Creating another pairing link requires both `access:write` and+every scope being delegated. The[auth handlers](../../apps/server/src/auth/http.ts) enforce this at issuance;client labels and device metadata have no authorization role.diff --git a/docs/user/outside-agents.md b/docs/user/outside-agents.mdindex 86294d68dc3..7cc6692e38f 100644--- a/docs/user/outside-agents.md+++ b/docs/user/outside-agents.md@@ -38,8 +38,8 @@ The page names the agent and where its access goes:Only approve a sign-in you just started. To approve, enter a pairing code from**Settings → Connections** or from `t3 auth pairing create` on the host. A-browser already signed in to the environment as an administrator can approve-without a code.+browser already signed in to the environment with permission to manage access+can approve without a code.Then choose what the agent may do:diff --git a/docs/user/remote-access.md b/docs/user/remote-access.mdindex 7ca2ae41aa8..72af4583501 100644--- a/docs/user/remote-access.md+++ b/docs/user/remote-access.md@@ -255,9 +255,11 @@ npx t3 pair --scope orchestration:read --scope relay:readThe selected scopes replace the default permissions. The same option works with`npx t3 auth pairing create` and `npx t3 auth session issue`; each command's-`--help` lists the available scopes. Without `--scope`, pairing tokens retain-standard client permissions and issued bearer sessions retain administrative-permissions.+`--help` lists the available scopes. Without `--scope`, pairing tokens and issued+bearer sessions get every permission, including managing access and T3 Connect.+A terminal or a full-access agent on the host can reach those as well, so a+client that should not manage access also needs to go without `terminal:operate`+and `orchestration:operate`.To change an existing client's permissions, create a fresh pairing link with thescopes it needs. In a browser opened directly on the environment, open that link@@ -276,12 +278,13 @@ worktrees. It does not restrict what a task does. Starting a task in a newworktree still creates that branch and worktree with `orchestration:operate`,and the agent it runs can use Git however the environment allows.-Settings changes, provider management, and environment maintenance can be granted-separately from access administration. New standard pairings include these-permissions. Existing clients can stay connected after an update, but newly separated-features may require pairing again with the permissions they need. Older clients-may show controls that the server denies. Create a fresh pairing link to change-a client's permissions.+Settings changes, provider management, environment maintenance, access+management, and T3 Connect management can each be granted separately. New+pairings without `--scope` include all of them. Existing clients keep the+permissions they paired with and can stay connected after an update, but newly+separated features may require pairing again with the permissions they need.+Older clients may show controls that the server denies. Create a fresh pairing+link to change a client's permissions.`filesystem:read` allows browsing host files, opening workspace files, and viewinglocal changes. Add `filesystem:write` to allow editing files or saving plans todiff --git a/packages/client-runtime/src/authorization/layer.test.ts b/packages/client-runtime/src/authorization/layer.test.tsindex 21b871eb63a..74d597a6e93 100644--- a/packages/client-runtime/src/authorization/layer.test.ts+++ b/packages/client-runtime/src/authorization/layer.test.ts@@ -1,8 +1,4 @@-import {- AuthAdministrativeScopes,- AuthStandardClientScopes,- EnvironmentId,-} from "@t3tools/contracts";+import { AuthStandardClientScopes, EnvironmentId } from "@t3tools/contracts";import {RelayEnvironmentConnectScope,type RelayEnvironmentConnectResponse,@@ -496,7 +492,7 @@ describe("RemoteEnvironmentAuthorization", () => {it.effect.each([{ name: "read-only", grantScopes: ["orchestration:read"] },- { name: "administrative", grantScopes: AuthAdministrativeScopes },+ { name: "default", grantScopes: AuthStandardClientScopes },] as const)("inherits $name pairing grant scopes for websocket authorization and HTTP renewal",({ grantScopes }) =>diff --git a/packages/client-runtime/src/connection/onboarding.test.ts b/packages/client-runtime/src/connection/onboarding.test.tsindex 7afb3a0bdb4..7aaaf80cee0 100644--- a/packages/client-runtime/src/connection/onboarding.test.ts+++ b/packages/client-runtime/src/connection/onboarding.test.ts@@ -1,5 +1,4 @@import {- AuthAdministrativeScopes,AuthStandardClientScopes,EnvironmentId,type AuthEnvironmentScope,@@ -423,7 +422,7 @@ describe("connection onboarding", () => {);it.effect.each([{ label: "read-only", scopes: ["orchestration:read"] },- { label: "administrative", scopes: AuthAdministrativeScopes },+ { label: "default", scopes: AuthStandardClientScopes },] as const)("preserves the $label grant when pairing a remote environment", ({ scopes }) =>Effect.gen(function* () {const calls: Array<{ readonly url: string; readonly init: RequestInit }> = [];diff --git a/packages/contracts/src/auth.ts b/packages/contracts/src/auth.tsindex 4c3debcc794..b693782771a 100644--- a/packages/contracts/src/auth.ts+++ b/packages/contracts/src/auth.ts@@ -146,6 +146,9 @@ export function sessionHasLegacyPermissions(session: SessionGrantInput): boolean);}+// Access and relay management are included because terminal:operate and+// orchestration:operate already reach that authority through a shell or a+// full-access agent. Narrowing a grant means choosing explicit scopes.export const AuthStandardClientScopes = [AuthOrchestrationReadScope,AuthOrchestrationOperateScope,@@ -160,9 +163,6 @@ export const AuthStandardClientScopes = [AuthFilesystemReadScope,AuthFilesystemWriteScope,AuthRelayReadScope,-] as const;-export const AuthAdministrativeScopes = [- ...AuthStandardClientScopes,AuthAccessReadScope,AuthAccessWriteScope,AuthRelayWriteScope,