Skip to content

Commit adda6eb

Browse files
committed
fix(client-runtime): revalidate descriptors after route fallback
1 parent 9516f24 commit adda6eb

4 files changed

Lines changed: 113 additions & 2 deletions

File tree

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

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import * as ConnectionCredentialStore from "../connection/credentialStore.ts";
2424
import { BearerConnectionTarget } from "../connection/model.ts";
2525
import * as ConnectionProfileStore from "../connection/profileStore.ts";
2626
import * as ConnectionResolver from "../connection/resolver.ts";
27+
import * as ConnectionDriver from "../connection/driver.ts";
28+
import * as RpcSession from "../rpc/session.ts";
29+
import { ConnectionTransientError } from "../connection/model.ts";
2730
import { CheckedEnvironmentDescriptors } from "../environment/descriptor.ts";
2831

2932
const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
@@ -431,6 +434,108 @@ describe("RemoteEnvironmentAuthorization", () => {
431434
}),
432435
);
433436

437+
it.effect(
438+
"rechecks a LAN descriptor after another route attempt before sending credentials",
439+
() =>
440+
Effect.gen(function* () {
441+
const lanUrl = "http://192.168.1.2:3773";
442+
const route = (connectionId: string, httpBaseUrl: string) => ({
443+
target: new BearerConnectionTarget({
444+
environmentId: ENVIRONMENT_ID,
445+
label: DESCRIPTOR.label,
446+
connectionId,
447+
}),
448+
profile: Option.some(
449+
new BearerConnectionProfile({
450+
connectionId,
451+
environmentId: ENVIRONMENT_ID,
452+
label: DESCRIPTOR.label,
453+
httpBaseUrl,
454+
wsBaseUrl: httpBaseUrl.replace("http", "ws"),
455+
}),
456+
),
457+
});
458+
const harness = yield* makeHarness({
459+
responses: [
460+
Response.json(DESCRIPTOR),
461+
Response.json(DESCRIPTOR),
462+
websocketTicket("first-ticket"),
463+
Response.json({ ...DESCRIPTOR, environmentId: "another-machine" }),
464+
],
465+
});
466+
const result = yield* Effect.gen(function* () {
467+
const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
468+
const driver = yield* ConnectionDriver.ConnectionDriver.pipe(
469+
Effect.provide(
470+
ConnectionDriver.layer.pipe(
471+
Layer.provide(
472+
Layer.mergeAll(
473+
Layer.succeed(ConnectionResolver.ConnectionResolver, {
474+
prepare: (entry) =>
475+
Effect.gen(function* () {
476+
const profile = Option.getOrThrow(entry.profile);
477+
if (profile._tag !== "BearerConnectionProfile")
478+
return yield* Effect.die("Unexpected profile");
479+
return {
480+
...(yield* remote.authorizeBearer({
481+
expectedEnvironmentId: ENVIRONMENT_ID,
482+
httpBaseUrl: profile.httpBaseUrl,
483+
wsBaseUrl: profile.wsBaseUrl,
484+
bearerToken: "bearer-token",
485+
connectionMethod: "direct",
486+
})),
487+
target: entry.target,
488+
};
489+
}),
490+
prepareForUpdate: () => Effect.die("Unexpected update"),
491+
}),
492+
Layer.succeed(RpcSession.RpcSessionFactory, {
493+
connect: () =>
494+
Effect.fail(
495+
new ConnectionTransientError({
496+
reason: "endpoint-unavailable",
497+
detail: "First route disconnected",
498+
}),
499+
),
500+
}),
501+
),
502+
),
503+
),
504+
),
505+
);
506+
return yield* driver
507+
.connect(
508+
{
509+
...route("first", "http://127.0.0.1:3773"),
510+
alternateRoutes: [route("lan", lanUrl)],
511+
enabled: true,
512+
},
513+
() => Effect.void,
514+
)
515+
.pipe(Effect.result);
516+
}).pipe(Effect.provide(harness.layer));
517+
518+
expect(result._tag).toBe("Failure");
519+
for (const [url, init] of harness.fetch.calls.filter(([url]) =>
520+
String(url).startsWith(lanUrl),
521+
)) {
522+
expect(new URL(String(url)).pathname).toBe("/.well-known/t3/environment");
523+
expect(new Headers(init.headers).has("authorization")).toBe(false);
524+
expect(init.cache).toBe("no-store");
525+
}
526+
expect(
527+
harness.fetch.calls
528+
.filter(([url]) => String(url).startsWith(lanUrl))
529+
.map(([url]) => new URL(String(url)).pathname),
530+
).toEqual(["/.well-known/t3/environment", "/.well-known/t3/environment"]);
531+
expect(
532+
harness.fetch.calls.filter(([url]) =>
533+
String(url).endsWith("/api/auth/websocket-ticket/rpc"),
534+
),
535+
).toHaveLength(1);
536+
}),
537+
);
538+
434539
it.effect("reuses a same-account persisted environment token without contacting the relay", () =>
435540
Effect.gen(function* () {
436541
const cached = new TokenStore.RemoteDpopAccessToken({

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import * as Result from "effect/Result";
4040
import * as Scope from "effect/Scope";
4141
import * as Semaphore from "effect/Semaphore";
4242
import * as HttpClient from "effect/http/HttpClient";
43+
import * as FetchHttpClient from "effect/http/FetchHttpClient";
4344

4445
import {
4546
DPOP_ACCESS_TOKEN_REFRESH_SKEW_MS,
@@ -143,6 +144,7 @@ export const make = Effect.gen(function* () {
143144
const descriptor =
144145
checked ??
145146
(yield* fetchDescriptor(input.httpBaseUrl, input.connectionMethod).pipe(
147+
Effect.provideService(FetchHttpClient.RequestInit, { cache: "no-store" }),
146148
Effect.provideService(HttpClient.HttpClient, httpClient),
147149
));
148150
if (descriptor.environmentId !== input.expectedEnvironmentId) {

‎packages/client-runtime/src/connection/driver.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,17 @@ export const make = Effect.gen(function* () {
225225
});
226226
yield* reportProgress({ stage: "preparing" });
227227
const checked = new Map<string, ExecutionEnvironmentDescriptor>();
228+
let firstRouteAttempt = true;
228229
return yield* connectOverRoutes(
229230
entry,
230231
(route) => checkRoute(entry, route, checked),
231232
Effect.fnUntraced(function* (route) {
233+
// Another route attempt may outlive the network where these probes answered.
234+
const descriptors = firstRouteAttempt ? checked : new Map();
235+
firstRouteAttempt = false;
232236
const prepared = yield* resolver
233237
.prepare(routeEntry(entry, route))
234-
.pipe(Effect.provideService(CheckedEnvironmentDescriptors, checked));
238+
.pipe(Effect.provideService(CheckedEnvironmentDescriptors, descriptors));
235239
yield* reportProgress({ stage: "opening", prepared });
236240
const session = yield* sessions.connect(prepared);
237241
yield* reportProgress({ stage: "synchronizing", prepared });

‎packages/client-runtime/src/environment/descriptor.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { executeEnvironmentHttpRequest, makeEnvironmentHttpApiGroupClient } from
88
const DEFAULT_REMOTE_REQUEST_TIMEOUT_MS = 10_000;
99

1010
/**
11-
* Descriptors a connection attempt's route checks fetched moments ago, by HTTP base URL.
11+
* Descriptors fetched by route checks, reusable only before the first route attempt.
1212
* Verifying the same origin again before sending credentials would cost a round trip.
1313
*/
1414
export class CheckedEnvironmentDescriptors extends Context.Reference<

0 commit comments

Comments
 (0)