Skip to content

Commit 07d1fed

Browse files
fix(auth): keep secondary desktop sessions on standard scopes
Dropping the requested scope list from the bootstrap exchange let secondary desktop backends inherit the seed grant's administrative scopes. Request the standard set there, treat an empty scope request like an omitted one, and accept a trailing slash on explicit /pair links. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 5cdf59f commit 07d1fed

7 files changed

Lines changed: 57 additions & 11 deletions

File tree

‎apps/server/src/auth/EnvironmentAuth.test.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,10 @@ it.layer(NodeServices.layer)("EnvironmentAuth.layer", (it) => {
181181
}).pipe(Effect.provide(makeEnvironmentAuthLayer())),
182182
);
183183

184-
it.effect("inherits a constrained pairing grant when token exchange omits scope", () =>
184+
it.effect.each([
185+
{ label: "omits scope", requestedScopes: undefined },
186+
{ label: "requests no scopes", requestedScopes: [] },
187+
])("inherits a constrained pairing grant when token exchange $label", ({ requestedScopes }) =>
185188
Effect.gen(function* () {
186189
const serverAuth = yield* EnvironmentAuth.EnvironmentAuth;
187190
const pairingCredential = yield* serverAuth.issuePairingCredential({
@@ -190,7 +193,7 @@ it.layer(NodeServices.layer)("EnvironmentAuth.layer", (it) => {
190193

191194
const token = yield* serverAuth.exchangeBootstrapCredentialForAccessToken(
192195
pairingCredential.credential,
193-
undefined,
196+
requestedScopes,
194197
requestMetadata,
195198
);
196199

‎apps/server/src/auth/EnvironmentAuth.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,8 +733,14 @@ export const make = Effect.gen(function* () {
733733
});
734734

735735
const exchangeBootstrapCredentialForAccessToken: EnvironmentAuth["Service"]["exchangeBootstrapCredentialForAccessToken"] =
736-
(credential, requestedScopes, requestMetadata, input) =>
737-
bootstrapCredentials
736+
(credential, requestedScopesInput, requestMetadata, input) => {
737+
// An empty request would pass the grant check and issue a session with no
738+
// scopes at all; treat it like an omitted request and use the grant's.
739+
const requestedScopes =
740+
requestedScopesInput !== undefined && requestedScopesInput.length > 0
741+
? requestedScopesInput
742+
: undefined;
743+
return bootstrapCredentials
738744
.consume(credential, {
739745
...input,
740746
...(requestedScopes !== undefined ? { requestedScopes } : {}),
@@ -795,6 +801,7 @@ export const make = Effect.gen(function* () {
795801
),
796802
Effect.withSpan("EnvironmentAuth.exchangeBootstrapCredentialForAccessToken"),
797803
);
804+
};
798805

799806
const issuePairingCredentialForSubject = (input: {
800807
readonly scopes: ReadonlyArray<AuthEnvironmentScope>;

‎apps/server/src/auth/PairingGrantStore.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,9 @@ export const make = Effect.gen(function* () {
533533
return yield* seededResult.error;
534534
}
535535

536+
// The scope check is part of the UPDATE's WHERE clause so a rejected
537+
// request cannot consume a one-time link. The re-check below only
538+
// explains why nothing matched.
536539
const consumed = yield* pairingLinks
537540
.consumeAvailable({
538541
credential,

‎apps/server/src/server.test.ts‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hos
66

77
import {
88
AuthAccessTokenType,
9+
AuthAdministrativeScopes,
910
AuthStandardClientScopes,
1011
AuthEnvironmentBootstrapTokenType,
1112
AuthTokenExchangeGrantType,
@@ -1297,7 +1298,8 @@ const exchangeAccessToken = (
12971298
credential = defaultDesktopBootstrapToken,
12981299
options?: {
12991300
readonly headers?: Record<string, string>;
1300-
readonly scope?: string;
1301+
/** Pass `undefined` explicitly to omit `scope` and receive the grant's scopes. */
1302+
readonly scope?: string | undefined;
13011303
readonly clientMetadata?: {
13021304
readonly label?: string;
13031305
readonly deviceType?: string;
@@ -1318,9 +1320,14 @@ const exchangeAccessToken = (
13181320
subject_token: credential,
13191321
subject_token_type: AuthEnvironmentBootstrapTokenType,
13201322
requested_token_type: AuthAccessTokenType,
1321-
scope:
1322-
options?.scope ??
1323-
"orchestration:read orchestration:operate terminal:operate review:write relay:read access:read access:write relay:write",
1323+
...(options !== undefined && "scope" in options
1324+
? options.scope !== undefined
1325+
? { scope: options.scope }
1326+
: {}
1327+
: {
1328+
scope:
1329+
"orchestration:read orchestration:operate terminal:operate review:write relay:read access:read access:write relay:write",
1330+
}),
13241331
...(options?.clientMetadata?.label ? { client_label: options.clientMetadata.label } : {}),
13251332
...(options?.clientMetadata?.deviceType
13261333
? { client_device_type: options.clientMetadata.deviceType }
@@ -4519,6 +4526,27 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
45194526
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
45204527
);
45214528

4529+
it.effect("issues the grant's scopes when an exchange omits scope", () =>
4530+
Effect.gen(function* () {
4531+
yield* buildAppUnderTest();
4532+
const owner = yield* exchangeAccessToken(defaultDesktopBootstrapToken, {
4533+
scope: undefined,
4534+
});
4535+
assert.equal(owner.response.status, 200);
4536+
assert.equal(owner.body.scope, AuthAdministrativeScopes.join(" "));
4537+
4538+
const createdResponse = yield* HttpClient.post("/api/auth/pairing-token", {
4539+
headers: { cookie: yield* getAuthenticatedSessionCookieHeader() },
4540+
body: yield* HttpBody.json({ scopes: ["orchestration:read", "relay:read"] }),
4541+
});
4542+
assert.equal(createdResponse.status, 200);
4543+
const created = (yield* createdResponse.json) as { credential: string };
4544+
const paired = yield* exchangeAccessToken(created.credential, { scope: undefined });
4545+
assert.equal(paired.response.status, 200);
4546+
assert.equal(paired.body.scope, "orchestration:read relay:read");
4547+
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
4548+
);
4549+
45224550
it.effect("returns only pairing metadata to access-read HTTP sessions", () =>
45234551
Effect.gen(function* () {
45244552
yield* buildAppUnderTest();

‎apps/web/src/authBootstrap.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ describe("resolveInitialServerAuthGateState", () => {
304304
{ suffix: "#token=replacement-token", cached: false },
305305
{ suffix: "#token=replacement-token", cached: true },
306306
{ suffix: "?token=replacement-token", cached: true },
307+
{ suffix: "/#token=replacement-token", cached: true },
307308
])(
308309
"re-pairs an authenticated browser with $suffix when cached=$cached",
309310
async ({ suffix, cached }) => {
@@ -318,11 +319,11 @@ describe("resolveInitialServerAuthGateState", () => {
318319
});
319320
const testWindow = installTestBrowser("http://localhost/");
320321
const {
321-
fetchSessionState,
322322
resolveInitialServerAuthGateState,
323323
submitServerAuthCredential,
324324
takePairingTokenFromUrl,
325325
} = await import("./environments/primary");
326+
const { fetchSessionState } = await import("./environments/primary/auth");
326327

327328
if (cached) {
328329
await expect(resolveInitialServerAuthGateState()).resolves.toEqual({
@@ -376,11 +377,11 @@ describe("resolveInitialServerAuthGateState", () => {
376377
});
377378
const testWindow = installTestBrowser("http://localhost/");
378379
const {
379-
fetchSessionState,
380380
resolveInitialServerAuthGateState,
381381
submitServerAuthCredential,
382382
takePairingTokenFromUrl,
383383
} = await import("./environments/primary");
384+
const { fetchSessionState } = await import("./environments/primary/auth");
384385
if (cached) {
385386
await resolveInitialServerAuthGateState();
386387
}

‎apps/web/src/connection/platform.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { fetchRemoteEnvironmentDescriptor } from "@t3tools/client-runtime/enviro
2626
import { managedRelayAccountChanges, managedRelaySessionAtom } from "@t3tools/client-runtime/relay";
2727
import { EnvironmentRpcRequestObserver } from "@t3tools/client-runtime/rpc";
2828
import {
29+
AuthStandardClientScopes,
2930
type DesktopBridge,
3031
type DesktopEnvironmentBootstrap,
3132
type DesktopSshEnvironmentTarget,
@@ -327,9 +328,12 @@ const loadSecondaryConnectionRegistration = Effect.fn(
327328
Effect.mapError(mapRemoteEnvironmentError),
328329
);
329330
const issuedAtEpochMs = yield* Clock.currentTimeMillis;
331+
// The desktop seed grant is administrative so the primary window can manage
332+
// access; a secondary backend session only needs to operate its environment.
330333
const access = yield* bootstrapRemoteBearerSession({
331334
httpBaseUrl,
332335
credential: entry.bootstrapToken,
336+
scopes: AuthStandardClientScopes,
333337
clientMetadata: clientMetadata(),
334338
}).pipe(Effect.mapError(mapRemoteEnvironmentError));
335339
// Keep the desktop pool's stable backend id in the connection id. The

‎apps/web/src/environments/primary/auth.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ export async function resolveInitialServerAuthGateState(): Promise<ServerAuthGat
433433
// An explicit pairing link replaces this browser's grant, even when the
434434
// current cookie or a cached gate already authenticates it. Keep that intent
435435
// after stripping the token, which causes the router to load this gate again.
436-
if (window.location.pathname !== "/pair") {
436+
if (window.location.pathname.replace(/\/+$/, "") !== "/pair") {
437437
explicitPairingRequested = false;
438438
} else if (peekPairingTokenFromUrl()) {
439439
explicitPairingRequested = true;

0 commit comments

Comments
 (0)