Skip to content

Commit aa46f7a

Browse files
abdulsalamgsclaudemaria-rcks
authored
fix(client-runtime): re-pairing at a new address uses the new token (#25591)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 6856500 commit aa46f7a

3 files changed

Lines changed: 53 additions & 5 deletions

File tree

‎packages/client-runtime/src/connection/registry.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,38 @@ describe("EnvironmentRegistry routes", () => {
20902090
}),
20912091
);
20922092

2093+
it.effect("re-pairing at another loopback address prefers the new route", () =>
2094+
Effect.gen(function* () {
2095+
const loopback = (connectionId: string, host: string) =>
2096+
[
2097+
new BearerConnectionTarget({ ...LAN_TARGET, connectionId }),
2098+
new BearerConnectionProfile({
2099+
...LAN_PROFILE,
2100+
connectionId,
2101+
httpBaseUrl: `http://${host}:3773/`,
2102+
wsBaseUrl: `ws://${host}:3773/`,
2103+
}),
2104+
] as const;
2105+
const [savedTarget, savedProfile] = loopback("bearer:ip", "127.0.0.1");
2106+
const [pastedTarget, pastedProfile] = loopback("bearer:localhost", "localhost");
2107+
const harness = yield* makeHarness(
2108+
[savedTarget, RELAY_TARGET],
2109+
[savedProfile],
2110+
[[savedTarget.connectionId, BEARER_CREDENTIAL]],
2111+
);
2112+
yield* Effect.gen(function* () {
2113+
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
2114+
yield* registry.register(lanRegistration(pastedTarget, pastedProfile));
2115+
2116+
expect(routesOf(yield* Ref.get(harness.storedTargets), LAN_TARGET.environmentId)).toEqual([
2117+
pastedTarget,
2118+
savedTarget,
2119+
RELAY_TARGET,
2120+
]);
2121+
}).pipe(Effect.provide(harness.layer), Effect.scoped);
2122+
}),
2123+
);
2124+
20932125
it.effect(
20942126
"adding a saved SSH host again replaces its route, whatever id it was saved under",
20952127
() =>

‎packages/client-runtime/src/connection/routes.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ describe("connection routes", () => {
8989
expect(upsertRoute([RELAY, LAN], repaired)).toEqual([RELAY, repaired]);
9090
});
9191

92+
it("puts a newly paired route ahead of saved routes of the same kind", () => {
93+
const pasted = direct("lan-2", "http://192.168.1.11:3773/");
94+
expect(upsertRoute([LAN, TAILNET], pasted)).toEqual([pasted, LAN, TAILNET]);
95+
});
96+
97+
it("keeps a newly paired route behind a different kind of the same rank", () => {
98+
const vpn = direct("vpn", "http://100.101.102.103:3773/");
99+
const pasted = direct("ts-2", "http://[fd7a:115c:a1e0::2]:3773/");
100+
expect(upsertRoute([vpn, TAILNET], pasted)).toEqual([vpn, pasted, TAILNET]);
101+
});
102+
92103
it("shows a dragged order only until the saved routes catch up or change", () => {
93104
const dragged = ["tailnet", "lan"];
94105
expect(pendingRouteOrder(dragged, ["lan", "tailnet"])).toBe(dragged);

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,14 @@ const ROUTE_KIND_RANK: Record<ConnectionRouteKind, number> = {
115115
export function insertRoute(
116116
routes: ReadonlyArray<ConnectionRoute>,
117117
route: ConnectionRoute,
118+
{ aheadOfSameKind = false }: { readonly aheadOfSameKind?: boolean } = {},
118119
): ReadonlyArray<ConnectionRoute> {
119-
const rank = ROUTE_KIND_RANK[connectionRouteKind(route)];
120-
const index = routes.findIndex(
121-
(existing) => ROUTE_KIND_RANK[connectionRouteKind(existing)] > rank,
122-
);
120+
const kind = connectionRouteKind(route);
121+
const rank = ROUTE_KIND_RANK[kind];
122+
const index = routes.findIndex((existing) => {
123+
const existingKind = connectionRouteKind(existing);
124+
return ROUTE_KIND_RANK[existingKind] > rank || (aheadOfSameKind && existingKind === kind);
125+
});
123126
return index === -1
124127
? [...routes, route]
125128
: [...routes.slice(0, index), route, ...routes.slice(index)];
@@ -132,7 +135,9 @@ export function upsertRoute(
132135
const id = connectionRouteId(route.target);
133136
return routes.some((existing) => connectionRouteId(existing.target) === id)
134137
? routes.map((existing) => (connectionRouteId(existing.target) === id ? route : existing))
135-
: insertRoute(routes, route);
138+
: // A route paired at a new address, such as localhost beside a saved
139+
// 127.0.0.1, goes first among its kind so its fresh credential connects.
140+
insertRoute(routes, route, { aheadOfSameKind: true });
136141
}
137142

138143
export function findRouteToSameAddress(

0 commit comments

Comments
 (0)