Skip to content

Commit 65c7c6a

Browse files
Merge remote-tracking branch 'origin/main' into fix/issue-28032
2 parents 275bca0 + 1aa46d0 commit 65c7c6a

9 files changed

Lines changed: 269 additions & 27 deletions

File tree

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

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ it.layer(NodeServices.layer)("SessionStore.layer", (it) => {
9898
Effect.forkScoped({ startImmediately: true }),
9999
);
100100
const first = yield* sessions
101-
.awaitInvalidation(client.sessionId)
101+
.awaitInvalidation(client.sessionId, relaySessionInput.proofKeyThumbprint)
102102
.pipe(Effect.forkScoped({ startImmediately: true }));
103103
const second = yield* sessions
104-
.awaitInvalidation(client.sessionId)
104+
.awaitInvalidation(client.sessionId, relaySessionInput.proofKeyThumbprint)
105105
.pipe(Effect.forkScoped({ startImmediately: true }));
106106
if (operation === "revoke") yield* sessions.revoke(client.sessionId);
107107
else if (operation === "revokeAllExcept") yield* sessions.revokeAllExcept(owner.sessionId);
@@ -120,7 +120,7 @@ it.layer(NodeServices.layer)("SessionStore.layer", (it) => {
120120
yield* sessions.markConnected(client.sessionId);
121121
const invalidated = yield* Deferred.make<void>();
122122
const watcher = yield* sessions
123-
.awaitInvalidation(client.sessionId)
123+
.awaitInvalidation(client.sessionId, relaySessionInput.proofKeyThumbprint)
124124
.pipe(
125125
Effect.andThen(Deferred.succeed(invalidated, undefined)),
126126
Effect.forkScoped({ startImmediately: true }),
@@ -133,6 +133,102 @@ it.layer(NodeServices.layer)("SessionStore.layer", (it) => {
133133
}).pipe(Effect.scoped, Effect.provide(Layer.merge(layerSessionStore(), TestClock.layer()))),
134134
);
135135

136+
it.effect.each([
137+
{ name: "a renewal for the same key", renewal: {}, closesAtMinute: 119 },
138+
{
139+
name: "a renewal for another key",
140+
renewal: { proofKeyThumbprint: "other-proof-key" },
141+
closesAtMinute: 60,
142+
},
143+
{
144+
name: "a renewal with fewer scopes",
145+
renewal: { scopes: ["orchestration:read"] as const },
146+
closesAtMinute: 60,
147+
},
148+
{
149+
name: "a renewal, until the original is revoked",
150+
renewal: {},
151+
closesAtMinute: 90,
152+
revoke: "original",
153+
},
154+
{
155+
name: "a renewal, until the renewal is revoked",
156+
renewal: {},
157+
closesAtMinute: 90,
158+
revoke: "renewal",
159+
},
160+
] as const)("closes a DPoP socket at minute $closesAtMinute after $name", (testCase) =>
161+
Effect.gen(function* () {
162+
const sessions = yield* SessionStore.SessionStore;
163+
const client = yield* sessions.issue(relaySessionInput);
164+
const invalidated = yield* Deferred.make<void>();
165+
const watcher = yield* sessions
166+
.awaitInvalidation(client.sessionId, relaySessionInput.proofKeyThumbprint)
167+
.pipe(
168+
Effect.andThen(Deferred.succeed(invalidated, undefined)),
169+
Effect.forkScoped({ startImmediately: true }),
170+
);
171+
yield* TestClock.adjust(Duration.minutes(59));
172+
const renewal = yield* sessions.issue({ ...relaySessionInput, ...testCase.renewal });
173+
yield* TestClock.adjust(Duration.millis((testCase.closesAtMinute - 59) * 60_000 - 1));
174+
expect(yield* Deferred.isDone(invalidated)).toBe(false);
175+
yield* TestClock.adjust(Duration.millis(1));
176+
if ("revoke" in testCase) {
177+
yield* sessions.revoke(
178+
testCase.revoke === "original" ? client.sessionId : renewal.sessionId,
179+
);
180+
}
181+
yield* Fiber.join(watcher);
182+
}).pipe(Effect.scoped, Effect.provide(Layer.merge(layerSessionStore(), TestClock.layer()))),
183+
);
184+
185+
it.effect("closes a DPoP socket when an earlier renewal in its chain is revoked", () =>
186+
Effect.gen(function* () {
187+
const sessions = yield* SessionStore.SessionStore;
188+
const client = yield* sessions.issue(relaySessionInput);
189+
const invalidated = yield* Deferred.make<void>();
190+
const watcher = yield* sessions
191+
.awaitInvalidation(client.sessionId, relaySessionInput.proofKeyThumbprint)
192+
.pipe(
193+
Effect.andThen(Deferred.succeed(invalidated, undefined)),
194+
Effect.forkScoped({ startImmediately: true }),
195+
);
196+
yield* TestClock.adjust(Duration.minutes(59));
197+
const first = yield* sessions.issue(relaySessionInput);
198+
yield* TestClock.adjust(Duration.minutes(59));
199+
yield* sessions.issue(relaySessionInput);
200+
// Past the first renewal's expiry, so the socket now follows the second.
201+
yield* TestClock.adjust(Duration.minutes(2));
202+
expect(yield* Deferred.isDone(invalidated)).toBe(false);
203+
yield* sessions.revoke(first.sessionId);
204+
yield* Fiber.join(watcher);
205+
}).pipe(Effect.scoped, Effect.provide(Layer.merge(layerSessionStore(), TestClock.layer()))),
206+
);
207+
208+
it.effect("keeps a renewed DPoP socket when another socket on its expired session closes", () =>
209+
Effect.gen(function* () {
210+
const sessions = yield* SessionStore.SessionStore;
211+
const client = yield* sessions.issue(relaySessionInput);
212+
yield* sessions.markConnected(client.sessionId);
213+
const invalidated = yield* Deferred.make<void>();
214+
const watcher = yield* sessions
215+
.awaitInvalidation(client.sessionId, relaySessionInput.proofKeyThumbprint)
216+
.pipe(
217+
Effect.andThen(Deferred.succeed(invalidated, undefined)),
218+
Effect.forkScoped({ startImmediately: true }),
219+
);
220+
yield* TestClock.adjust(Duration.minutes(59));
221+
yield* sessions.issue(relaySessionInput);
222+
yield* TestClock.adjust(Duration.minutes(2));
223+
// An expired session that loses its last socket is reported as removed, not revoked.
224+
yield* sessions.markDisconnected(client.sessionId);
225+
yield* TestClock.adjust(Duration.millis(58 * 60_000 - 1));
226+
expect(yield* Deferred.isDone(invalidated)).toBe(false);
227+
yield* TestClock.adjust(Duration.millis(1));
228+
yield* Fiber.join(watcher);
229+
}).pipe(Effect.scoped, Effect.provide(Layer.merge(layerSessionStore(), TestClock.layer()))),
230+
);
231+
136232
it.effect("keys remote cookies by environment identity instead of state directory", () =>
137233
Effect.gen(function* () {
138234
const cookieName = (stateDir: string, environmentId: EnvironmentId) =>

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

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,10 @@ export class SessionStore extends Context.Service<
409409
SessionCredentialInternalError
410410
>;
411411
readonly streamChanges: Stream.Stream<SessionCredentialChange>;
412+
/** Completes when a live socket's session is revoked or expires without a renewal for its DPoP key. */
412413
readonly awaitInvalidation: (
413414
sessionId: AuthSessionId,
415+
proofKeyThumbprint?: string,
414416
) => Effect.Effect<void, SessionCredentialVerificationError>;
415417
readonly revoke: (
416418
sessionId: AuthSessionId,
@@ -497,6 +499,9 @@ const make = Effect.gen(function* () {
497499
const signingSecret = yield* secretStore.getOrCreateRandom(SIGNING_SECRET_NAME, 32);
498500
const connectedSessionsRef = yield* Ref.make(new Map<AuthSessionId, number>());
499501
const changesPubSub = yield* PubSub.unbounded<SessionCredentialChange>();
502+
// Newest session per DPoP key. A live socket moves to it at expiry, so a
503+
// client that renews its short-lived token keeps its socket.
504+
const latestSessionByProofKey = new Map<string, AuthSessionId>();
500505
const cookieInput = {
501506
mode: serverConfig.mode,
502507
port: serverConfig.port,
@@ -703,6 +708,7 @@ const make = Effect.gen(function* () {
703708
})
704709
: authSessions.create(sessionRecord).pipe(Effect.as([] as ReadonlyArray<AuthSessionId>))
705710
).pipe(Effect.mapError((cause) => new SessionCredentialIssueError({ sessionId, cause })));
711+
if (claims.jkt) latestSessionByProofKey.set(claims.jkt, sessionId);
706712
if (replacedSessionIds.length > 0) {
707713
yield* Ref.update(connectedSessionsRef, (current) => {
708714
const next = new Map(current);
@@ -1034,31 +1040,61 @@ const make = Effect.gen(function* () {
10341040
return revokedSessionIds.length;
10351041
});
10361042

1037-
const awaitInvalidation = Effect.fn(function* (sessionId: AuthSessionId) {
1043+
const awaitInvalidation = Effect.fn(function* (
1044+
sessionId: AuthSessionId,
1045+
proofKeyThumbprint?: string,
1046+
) {
10381047
return yield* Effect.scoped(
10391048
Effect.gen(function* () {
10401049
// Subscribe before reading: revocation can race with the WebSocket upgrade.
10411050
const subscription = yield* PubSub.subscribe(changesPubSub);
1042-
const row = yield* authSessions
1043-
.getById({ sessionId })
1044-
.pipe(
1045-
Effect.mapError(
1046-
(cause) => new SessionCredentialVerificationError({ sessionId, cause }),
1047-
),
1048-
);
1049-
if (Option.isNone(row) || row.value.revokedAt !== null) return;
1050-
const now = yield* DateTime.now;
1051-
const remaining = row.value.expiresAt.epochMilliseconds - now.epochMilliseconds;
1052-
if (remaining <= 0) return;
1053-
yield* Effect.raceFirst(
1054-
Stream.fromSubscription(subscription).pipe(
1055-
Stream.filter(
1056-
(change) => change.type === "clientRemoved" && change.sessionId === sessionId,
1057-
),
1058-
Stream.runHead,
1059-
),
1060-
Effect.sleep(Duration.millis(remaining)),
1061-
);
1051+
const readRow = (id: AuthSessionId) =>
1052+
authSessions
1053+
.getById({ sessionId: id })
1054+
.pipe(
1055+
Effect.mapError(
1056+
(cause) => new SessionCredentialVerificationError({ sessionId: id, cause }),
1057+
),
1058+
);
1059+
const original = yield* readRow(sessionId);
1060+
if (Option.isNone(original)) return;
1061+
// Revoking any session the socket has used closes it. Events only wake this loop
1062+
// and the rows decide: revocations are written before they are published, and
1063+
// clientRemoved also reports an expired session losing its last socket.
1064+
const chain = [sessionId];
1065+
const nextRemoval = Effect.gen(function* () {
1066+
while (true) {
1067+
const change = yield* PubSub.take(subscription);
1068+
if (change.type === "clientRemoved" && chain.includes(change.sessionId)) return;
1069+
}
1070+
});
1071+
let current = original.value;
1072+
while (true) {
1073+
const rows = yield* Effect.forEach(chain, readRow);
1074+
if (rows.some((row) => Option.isNone(row) || row.value.revokedAt !== null)) return;
1075+
const now = yield* DateTime.now;
1076+
const remaining = current.expiresAt.epochMilliseconds - now.epochMilliseconds;
1077+
if (remaining > 0) {
1078+
yield* Effect.raceFirst(nextRemoval, Effect.sleep(Duration.millis(remaining)));
1079+
continue;
1080+
}
1081+
const successorId =
1082+
proofKeyThumbprint === undefined
1083+
? undefined
1084+
: latestSessionByProofKey.get(proofKeyThumbprint);
1085+
if (successorId === undefined || successorId === current.sessionId) return;
1086+
const successor = yield* readRow(successorId);
1087+
// The socket keeps its original scopes, so the renewal must still grant all of them.
1088+
if (
1089+
Option.isNone(successor) ||
1090+
successor.value.subject !== original.value.subject ||
1091+
!original.value.scopes.every((scope) => successor.value.scopes.includes(scope))
1092+
) {
1093+
return;
1094+
}
1095+
chain.push(successorId);
1096+
current = successor.value;
1097+
}
10621098
}),
10631099
);
10641100
});

‎apps/server/src/ws.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3385,7 +3385,7 @@ export const layer = Layer.unwrap(
33853385
() =>
33863386
Effect.raceFirst(
33873387
rpcWebSocketHttpEffect,
3388-
sessions.awaitInvalidation(session.sessionId).pipe(
3388+
sessions.awaitInvalidation(session.sessionId, session.proofKeyThumbprint).pipe(
33893389
Effect.as(HttpServerResponse.empty()),
33903390
Effect.catchTags({
33913391
SessionCredentialVerificationError: (error) =>

‎docs/internals/connection-runtime.md‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,15 @@ RPC sessions authenticate at socket upgrade, while HTTP requests need current
4646
credentials from the
4747
[authorization service](../../packages/client-runtime/src/authorization/service.ts).
4848
Replacing a healthy socket for HTTP renewal would interrupt conversations and
49-
change the transport generation without a transport failure. Credential expiry
50-
does not close the socket, and refresh failure belongs to the HTTP operation.
49+
change the transport generation without a transport failure. Refresh failure
50+
belongs to the HTTP operation.
51+
52+
The server closes a socket when its session is revoked or expires. T3 Connect
53+
access tokens last one hour, so the driver renews a live socket's token in its
54+
last minute. The server then moves the socket to the newer session for the same
55+
DPoP key, subject, and scopes
56+
([`SessionStore.awaitInvalidation`](../../apps/server/src/auth/SessionStore.ts)).
57+
If renewal fails, the socket closes at expiry and the supervisor reconnects.
5158

5259
Session listings must retain unrevoked connected sessions after credential expiry
5360
so an open connection does not disappear from connection management. This does

‎packages/client-runtime/src/authorization/layer.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,59 @@ describe("RemoteEnvironmentAuthorization", () => {
602602
}),
603603
);
604604

605+
it.effect("renews a live socket's token in the minute before each expiry", () =>
606+
Effect.gen(function* () {
607+
const puts = yield* Queue.unbounded<void>();
608+
const relayBlips = yield* Ref.make(1);
609+
const harness = yield* makeHarness({
610+
beforeBootstrap: Ref.getAndUpdate(relayBlips, (count) => count - 1).pipe(
611+
Effect.flatMap((count) =>
612+
count > 0
613+
? Effect.fail(
614+
new ManagedRelay.ManagedRelayRequestTimeoutError({
615+
activity: "Relay environment connection",
616+
timeoutMs: 30_000,
617+
traceId: null,
618+
}),
619+
)
620+
: Effect.void,
621+
),
622+
),
623+
initialToken: persistedToken({ expiresAtEpochMs: 3_600_000 }),
624+
responses: [
625+
Response.json(DESCRIPTOR),
626+
accessToken("renewed-1"),
627+
Response.json(DESCRIPTOR),
628+
accessToken("renewed-2"),
629+
],
630+
beforePut: Queue.offer(puts, undefined).pipe(Effect.asVoid),
631+
});
632+
633+
yield* Effect.gen(function* () {
634+
const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
635+
yield* remote
636+
.renewDpopBeforeExpiry({
637+
expectedEnvironmentId: ENVIRONMENT_ID,
638+
expiresAtEpochMs: 3_600_000,
639+
})
640+
.pipe(Effect.forkScoped);
641+
yield* TestClock.adjust(3_540_000 - 1);
642+
expect(yield* Ref.get(harness.bootstrapCalls)).toBe(0);
643+
// The first try hits a relay blip, and the retry 5 s later succeeds.
644+
yield* TestClock.adjust(1);
645+
expect(yield* Ref.get(harness.bootstrapCalls)).toBe(1);
646+
yield* TestClock.adjust(5_000);
647+
yield* Queue.take(puts);
648+
// Renewed at 0:59:05, so the next renewal is at 1:58:05.
649+
yield* TestClock.adjust(7_085_000 - 3_545_000 - 1);
650+
expect(yield* Ref.get(harness.bootstrapCalls)).toBe(2);
651+
yield* TestClock.adjust(1);
652+
yield* Queue.take(puts);
653+
expect(yield* Ref.get(harness.bootstrapCalls)).toBe(3);
654+
}).pipe(Effect.scoped, Effect.provide(Layer.merge(harness.layer, TestClock.layer())));
655+
}),
656+
);
657+
605658
it.effect("evicts an auth-invalid cached token and obtains a fresh bootstrap", () =>
606659
Effect.gen(function* () {
607660
const cached = new TokenStore.RemoteDpopAccessToken({

‎packages/client-runtime/src/authorization/service.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ export class RemoteEnvironmentAuthorization extends Context.Service<
8282
readonly expectedEnvironmentId: EnvironmentId;
8383
readonly rejectedAccessToken?: string;
8484
}) => Effect.Effect<AuthorizedRemoteHttpEnvironment, ConnectionAttemptError>;
85+
/**
86+
* Renews a live socket's token before it expires, so the server moves the
87+
* socket to the new session instead of closing it. Runs until interrupted.
88+
*/
89+
readonly renewDpopBeforeExpiry: (input: {
90+
readonly expectedEnvironmentId: EnvironmentId;
91+
readonly expiresAtEpochMs: number;
92+
}) => Effect.Effect<void, ConnectionBlockedError>;
8593
}
8694
>()("@t3tools/client-runtime/authorization/service/RemoteEnvironmentAuthorization") {}
8795

@@ -530,6 +538,30 @@ export const make = Effect.gen(function* () {
530538
withRelayClientTracing,
531539
);
532540
}),
541+
// Untraced: a span here would stay open for the socket's lifetime. Each renewal has its own.
542+
renewDpopBeforeExpiry: Effect.fnUntraced(function* (input) {
543+
let expiresAtEpochMs = input.expiresAtEpochMs;
544+
let now = yield* Clock.currentTimeMillis;
545+
// Past expiry the server has closed the socket.
546+
while (now < expiresAtEpochMs) {
547+
// getDpopToken reuses a cached token until it is within the skew of expiry.
548+
// The floor spaces out retries and keeps a wall-clock jump from spinning.
549+
yield* Effect.sleep(
550+
Math.max(5_000, expiresAtEpochMs - DPOP_ACCESS_TOKEN_REFRESH_SKEW_MS - now),
551+
);
552+
const renewed = yield* getDpopToken({
553+
expectedEnvironmentId: input.expectedEnvironmentId,
554+
}).pipe(
555+
Effect.map(({ token }) => token.expiresAtEpochMs),
556+
Effect.withSpan("environment.authorization.renewSocket"),
557+
withRelayClientTracing,
558+
// A relay blip retries until expiry; a blocked credential ends the loop.
559+
Effect.catchTags({ ConnectionTransientError: () => Effect.succeed(expiresAtEpochMs) }),
560+
);
561+
expiresAtEpochMs = Math.max(expiresAtEpochMs, renewed);
562+
now = yield* Clock.currentTimeMillis;
563+
}
564+
}),
533565
});
534566
});
535567

0 commit comments

Comments
 (0)