From 371702349568c414a31792b8944505b865caddd7 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 19:42:54 +0000 Subject: [PATCH] fix(clients): a new pairing replaces the login on an environment's other addresses Each paired address kept its own login, so pairing again after a login expired fixed only the address used. The other addresses, and the routes learned through them, kept sending the dead one, and removing the newly paired address locked the environment out. A newly issued login is now saved for every address paired for that environment. A plain HTTP address takes it only when the pairing was over plain HTTP too, the rule learned routes already follow. Editing an address registers the login it already has and spreads nothing. Re-pairing an address saved under an older route id also drops the routes that borrowed the old login, so they are learned again over the new one. Refs pingdotgg/t3code#17014 Co-authored-by: Theo Browne Co-authored-by: Maxwell Young Co-Authored-By: Claude Fable 5.2 --- .../src/connection/registry.test.ts | 19 +++++- .../client-runtime/src/connection/registry.ts | 11 ++-- .../client-runtime/src/connection/routes.ts | 38 ++++++++++-- .../src/platform/storageDocument.test.ts | 61 +++++++++++++++++++ .../src/platform/storageDocument.ts | 40 +++++++++++- 5 files changed, 155 insertions(+), 14 deletions(-) diff --git a/packages/client-runtime/src/connection/registry.test.ts b/packages/client-runtime/src/connection/registry.test.ts index f6c305ab6bb90..9a6fdb2145d5a 100644 --- a/packages/client-runtime/src/connection/registry.test.ts +++ b/packages/client-runtime/src/connection/registry.test.ts @@ -2064,9 +2064,24 @@ describe("EnvironmentRegistry routes", () => { it.effect("re-pairing the same address replaces that route rather than adding another", () => Effect.gen(function* () { + // Borrows the login saved under the old route id, which goes away with that route. + const learnedId = `learned:${LAN_TARGET.environmentId}:http://100.101.102.103:3773@${LAN_TARGET.connectionId}`; const harness = yield* makeHarness( - [LAN_TARGET, RELAY_TARGET], - [LAN_PROFILE], + [ + LAN_TARGET, + new BearerConnectionTarget({ ...LAN_TARGET, connectionId: learnedId }), + RELAY_TARGET, + ], + [ + LAN_PROFILE, + new BearerConnectionProfile({ + ...LAN_PROFILE, + connectionId: learnedId, + httpBaseUrl: "http://100.101.102.103:3773/", + wsBaseUrl: "ws://100.101.102.103:3773/", + learned: true, + }), + ], [[LAN_TARGET.connectionId, BEARER_CREDENTIAL]], ); yield* Effect.gen(function* () { diff --git a/packages/client-runtime/src/connection/registry.ts b/packages/client-runtime/src/connection/registry.ts index 12f8aa5ec505f..84af8fc70c534 100644 --- a/packages/client-runtime/src/connection/registry.ts +++ b/packages/client-runtime/src/connection/registry.ts @@ -579,11 +579,12 @@ export const make = Effect.gen(function* () { if (previous !== undefined) { const existing = connectionRoutes(previous); const sameAddress = findRouteToSameAddress(existing, route); - const routes = existing.filter( - (existing) => - existing !== sameAddress || - connectionRouteId(existing.target) === connectionRouteId(route.target), - ); + // An address saved under another id is replaced, along with the routes borrowing its login. + const routes = + sameAddress === undefined || + connectionRouteId(sameAddress.target) === connectionRouteId(route.target) + ? existing + : routesAfterRemoving(existing, connectionRouteId(sameAddress.target)); const next = withRoutes(previous, upsertRoute(routes, route)); if (next.addressesChanged) { yield* forgetRoutingTrust(environmentId, "register-connection"); diff --git a/packages/client-runtime/src/connection/routes.ts b/packages/client-runtime/src/connection/routes.ts index 7a3b4a11e616c..269a4b01ee902 100644 --- a/packages/client-runtime/src/connection/routes.ts +++ b/packages/client-runtime/src/connection/routes.ts @@ -340,17 +340,43 @@ export function mergeLearnedRoutes(input: { // A paired token goes to a plain HTTP address outside the tailnet only when // it was paired that way, so a token paired over HTTPS, loopback, or the // tailnet keeps to the transports it was issued for. +function pairedOverPlainHttp(route: ConnectionRoute): boolean { + const kind = connectionRouteKind(route); + return ( + routeHttpBaseUrl(route)?.startsWith("http:") === true && (kind === "lan" || kind === "public") + ); +} + function tokenAllowsPlainHttp( routes: ReadonlyArray, credentialId: string, ): boolean { const credentialRoute = routes.find((route) => connectionRouteId(route.target) === credentialId); - return ( - credentialRoute !== undefined && - routeHttpBaseUrl(credentialRoute)?.startsWith("http:") === true && - (connectionRouteKind(credentialRoute) === "lan" || - connectionRouteKind(credentialRoute) === "public") - ); + return credentialRoute !== undefined && pairedOverPlainHttp(credentialRoute); +} + +/** + * The paired addresses of `paired`'s environment that take the login just issued over it. The + * server accepts a login at every address, and learned routes borrow one of these. A plain HTTP + * address gets it only under the rule learned routes follow. + */ +export function routesSharingLogin( + routes: ReadonlyArray, + paired: ConnectionRoute, +): ReadonlyArray { + const inClear = pairedOverPlainHttp(paired); + return routes.filter((route) => { + if (route.target._tag !== "BearerConnectionTarget" || isLearned(route)) return false; + if (connectionRouteId(route.target) === connectionRouteId(paired.target)) return true; + const profile = Option.getOrNull(route.profile); + if (profile?._tag !== "BearerConnectionProfile") return false; + return ( + inClear || + !profile.httpBaseUrl.startsWith("http:") || + profile.network === "tailscale" || + connectionRouteKind(route) === "loopback" + ); + }); } /** diff --git a/packages/client-runtime/src/platform/storageDocument.test.ts b/packages/client-runtime/src/platform/storageDocument.test.ts index 60f561e3dae53..072236c26d07e 100644 --- a/packages/client-runtime/src/platform/storageDocument.test.ts +++ b/packages/client-runtime/src/platform/storageDocument.test.ts @@ -394,6 +394,67 @@ describe("ConnectionCatalogDocument", () => { expect(bearerOnly.remoteDpopTokens).toEqual([]); }); + it("a new pairing replaces the login of the environment's other paired addresses", () => { + const route = (connectionId: string, httpBaseUrl: string, learned = false) => ({ + target: new BearerConnectionTarget({ ...BEARER_TARGET, connectionId }), + profile: new BearerConnectionProfile({ + ...BEARER_PROFILE, + connectionId, + httpBaseUrl, + wsBaseUrl: httpBaseUrl.replace(/^http/, "ws"), + ...(learned ? { learned: true } : {}), + }), + }); + const expired = new BearerConnectionCredential({ token: "expired" }); + const https = route("https", "https://remote.example.test/"); + const lan = route("lan", "http://192.168.1.10:3773/"); + const learned = route("learned:tailnet@https", "http://100.101.102.103:3773/", true); + const ssh = new SshConnectionTarget({ + environmentId: ENVIRONMENT_ID, + label: "Remote", + connectionId: "ssh", + }); + const saved: ConnectionCatalogDocument = { + ...EMPTY_CONNECTION_CATALOG_DOCUMENT, + targets: [https.target, lan.target, learned.target, ssh], + profiles: [https.profile, lan.profile, learned.profile], + credentials: [https, lan, { target: ssh }].map(({ target }) => ({ + connectionId: target.connectionId, + credential: expired, + })), + }; + const tokens = (document: ConnectionCatalogDocument) => + Object.fromEntries( + document.credentials.map((value) => [value.connectionId, value.credential.token]), + ); + const pair = (paired: ReturnType, credential = BEARER_CREDENTIAL) => + registerConnectionInCatalog( + saved, + new BearerConnectionRegistration({ ...paired, credential }), + [paired.target, ...saved.targets], + ); + + // Paired over HTTPS: the plain HTTP LAN address keeps to the login it was paired with. + const overHttps = pair(route("https-2", "https://other.example.test/")); + expect(tokens(overHttps)).toEqual({ + ssh: "expired", + lan: "expired", + https: "bearer-token", + "https-2": "bearer-token", + }); + // Removing the newly paired address leaves the others on the working login. + expect(tokens(setRoutesInCatalog(overHttps, ENVIRONMENT_ID, saved.targets))).toMatchObject({ + https: "bearer-token", + }); + + expect(tokens(pair(route("lan-2", "http://10.0.0.5:3773/")))).toMatchObject({ + lan: "bearer-token", + https: "bearer-token", + }); + // Editing an address registers the login it already has. + expect(tokens(pair(lan, expired))).toEqual(tokens(saved)); + }); + it("keeps an environment's position in the catalog when its routes change", () => { const other = new RelayConnectionTarget({ environmentId: EnvironmentId.make("environment-2"), diff --git a/packages/client-runtime/src/platform/storageDocument.ts b/packages/client-runtime/src/platform/storageDocument.ts index 1af8e0e3d67aa..930c20635b0ec 100644 --- a/packages/client-runtime/src/platform/storageDocument.ts +++ b/packages/client-runtime/src/platform/storageDocument.ts @@ -1,5 +1,6 @@ import { EnvironmentId } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; +import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; import { @@ -8,6 +9,7 @@ import { ConnectionProfile, } from "../connection/catalog.ts"; import { type ConnectionTarget, PersistedConnectionTarget } from "../connection/model.ts"; +import { routesSharingLogin } from "../connection/routes.ts"; import * as TokenStore from "../authorization/tokenStore.ts"; import { StoredPullRequestRoutingPermission } from "../connection/pullRequestRoutingPermissions.ts"; @@ -134,7 +136,43 @@ export function registerConnectionInCatalog( switch (registration._tag) { case "RelayConnectionRegistration": return next; - case "BearerConnectionRegistration": + case "BearerConnectionRegistration": { + const { connectionId } = registration.target; + const profiles = replaceCatalogValue( + next.profiles, + (value) => value.connectionId, + registration.profile, + ); + // An edit registers the login it already has, which must not overwrite the other addresses'. + const isNewLogin = + document.credentials.find((value) => value.connectionId === connectionId)?.credential + .token !== registration.credential.token; + const owners = new Set([ + connectionId, + ...(isNewLogin + ? routesSharingLogin( + routes.map((target) => ({ + target, + profile: Option.fromUndefinedOr( + profiles.find((profile) => profile.connectionId === connectionIdOf(target)), + ), + })), + { target: registration.target, profile: Option.some(registration.profile) }, + ).flatMap((route) => connectionIdOf(route.target) ?? []) + : []), + ]); + return { + ...next, + profiles, + credentials: [ + ...next.credentials.filter((value) => !owners.has(value.connectionId)), + ...Array.from(owners, (owner) => ({ + connectionId: owner, + credential: registration.credential, + })), + ], + }; + } case "SshConnectionRegistration": return { ...next,