Skip to content

Commit 42db6f2

Browse files
fix(clients): address review findings on learned routes
- The server reports only private and tailnet addresses. Learned routes are plain HTTP and carry the client's credential, so a public address would send it unencrypted across the internet. - A route learned over a learned T3 Connect route keeps the T3 Connect credential instead of pointing at a bearer token that does not exist. - Learned route ids use the full origin, and a changed scheme or port is saved, so http and https on one host no longer collide. - A T3 Connect token that a direct address rejects as invalid is replaced, as on the tunnel, rather than reused until it expires. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent a5aca6b commit 42db6f2

7 files changed

Lines changed: 153 additions & 21 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ const INTERFACES: ReturnType<typeof NodeOS.networkInterfaces> = {
3333
scopeid: 4,
3434
},
3535
],
36+
en1: [
37+
{
38+
address: "203.0.113.20",
39+
netmask: "255.255.255.0",
40+
family: "IPv4",
41+
mac: "aa:bb:cc:dd:ee:00",
42+
internal: false,
43+
cidr: "203.0.113.20/24",
44+
},
45+
],
3646
utun4: [
3747
{
3848
address: "100.101.102.103",
@@ -67,4 +77,10 @@ describe("resolveBoundEndpoints", () => {
6777
resolveBoundEndpoints({ host: "100.101.102.103", port: 3773, interfaces: INTERFACES }),
6878
).toEqual([{ kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" }]);
6979
});
80+
81+
it("never reports a public address, which would carry the credential over plain HTTP", () => {
82+
expect(
83+
resolveBoundEndpoints({ host: "203.0.113.20", port: 3773, interfaces: INTERFACES }),
84+
).toEqual([]);
85+
});
7086
});

‎apps/server/src/environment/DirectEndpoints.ts‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import * as Layer from "effect/Layer";
1919
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
2020
import * as NodeOS from "node:os";
2121

22+
import { isPrivateNetworkHost } from "@t3tools/shared/hostClassification";
23+
2224
import * as ServerConfig from "../config.ts";
2325
import { formatHostForUrl, isLoopbackHost, isWildcardHost } from "../startupAccess.ts";
2426

@@ -31,13 +33,20 @@ export class DirectEndpoints extends Context.Service<
3133
}
3234
>()("t3/environment/DirectEndpoints") {}
3335

34-
const isUsableAddress = (address: string): boolean =>
35-
!address.startsWith("127.") && !address.startsWith("169.254.") && !address.startsWith("fe80:");
36+
/**
37+
* Only private-network and tailnet addresses are reported. These routes are
38+
* plain HTTP and carry the client's credential, so a public address would
39+
* send it across the internet unencrypted.
40+
*/
41+
const isAdvertisableAddress = (address: string): boolean =>
42+
!address.startsWith("127.") &&
43+
!address.startsWith("169.254.") &&
44+
(isTailscaleIpv4Address(address) || isPrivateNetworkHost(address));
3645

3746
/**
38-
* Plain HTTP endpoints for the addresses a server bound to `host` accepts.
39-
* IPv4 only: link-local and temporary IPv6 addresses change too often to be
40-
* worth saving.
47+
* Plain HTTP endpoints for the private addresses a server bound to `host`
48+
* accepts. IPv4 only: link-local and temporary IPv6 addresses change too
49+
* often to be worth saving.
4150
*/
4251
export function resolveBoundEndpoints(input: {
4352
readonly host: string | undefined;
@@ -49,10 +58,11 @@ export function resolveBoundEndpoints(input: {
4958
? Object.values(input.interfaces)
5059
.flatMap((entries) => entries ?? [])
5160
.filter(
52-
(entry) => !entry.internal && entry.family === "IPv4" && isUsableAddress(entry.address),
61+
(entry) =>
62+
!entry.internal && entry.family === "IPv4" && isAdvertisableAddress(entry.address),
5363
)
5464
.map((entry) => entry.address)
55-
: [input.host!];
65+
: [input.host!].filter(isAdvertisableAddress);
5666
return [...new Set(addresses)].map((address) => ({
5767
kind: isTailscaleIpv4Address(address) ? "tailnet" : "lan",
5868
httpBaseUrl: `http://${formatHostForUrl(address)}:${input.port}/`,

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,46 @@ describe("RemoteEnvironmentAuthorization", () => {
376376
}),
377377
);
378378

379+
it.effect("replaces a token a learned direct address rejects", () =>
380+
Effect.gen(function* () {
381+
const cached = new TokenStore.RemoteDpopAccessToken({
382+
environmentId: ENVIRONMENT_ID,
383+
accountId: "account-1",
384+
label: DESCRIPTOR.label,
385+
endpoint: ENDPOINT,
386+
accessToken: "revoked-access-token",
387+
expiresAtEpochMs: Number.MAX_SAFE_INTEGER,
388+
dpopThumbprint: "thumbprint-1",
389+
});
390+
const harness = yield* makeHarness({
391+
initialToken: cached,
392+
responses: [
393+
authInvalid(),
394+
Response.json(DESCRIPTOR),
395+
accessToken("replacement-access-token"),
396+
websocketTicket("lan-ticket"),
397+
],
398+
});
399+
400+
const authorized = yield* Effect.gen(function* () {
401+
const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
402+
return yield* remote.authorizeDpop({
403+
expectedEnvironmentId: ENVIRONMENT_ID,
404+
directEndpoint: {
405+
httpBaseUrl: "http://192.168.1.10:3773/",
406+
wsBaseUrl: "ws://192.168.1.10:3773/",
407+
},
408+
});
409+
}).pipe(Effect.provide(harness.layer));
410+
411+
expect(authorized.socketUrl).toContain("wsTicket=lan-ticket");
412+
expect(yield* Ref.get(harness.bootstrapCalls)).toBe(1);
413+
expect((yield* Ref.get(harness.tokens)).get(ENVIRONMENT_ID)?.accessToken).toBe(
414+
"replacement-access-token",
415+
);
416+
}),
417+
);
418+
379419
it.effect("refreshes and persists an expired environment token", () =>
380420
Effect.gen(function* () {
381421
const expired = new TokenStore.RemoteDpopAccessToken({

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,8 @@ export const make = Effect.gen(function* () {
496496
}
497497
// An unreachable direct address says nothing about the token, so keep
498498
// it for the T3 Connect route rather than forcing a relay round trip.
499-
if (endpoint !== undefined) {
499+
// A rejected token falls through and is replaced like on the tunnel.
500+
if (endpoint !== undefined && cachedSocket.failure._tag !== "EnvironmentAuthInvalidError") {
500501
return yield* mapDpopSocketError(cachedSocket.failure);
501502
}
502503
selected = yield* getDpopToken({

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

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ describe("learned routes", () => {
9292
reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
9393
allowInsecure: true,
9494
});
95-
expect(ids(routes)).toEqual([`learned:${ENVIRONMENT_ID}:192.168.1.10:3773`, "relay"]);
95+
expect(ids(routes)).toEqual([`learned:${ENVIRONMENT_ID}:http://192.168.1.10:3773`, "relay"]);
9696
expect(profileOf(routes![0]!)).toMatchObject({
9797
learned: true,
9898
authorization: "t3-connect",
@@ -119,7 +119,7 @@ describe("learned routes", () => {
119119
reported: [{ httpBaseUrl: "http://10.0.0.42:3773/" }],
120120
allowInsecure: true,
121121
});
122-
expect(ids(moved)).toEqual([`learned:${ENVIRONMENT_ID}:10.0.0.42:3773`, "relay"]);
122+
expect(ids(moved)).toEqual([`learned:${ENVIRONMENT_ID}:http://10.0.0.42:3773`, "relay"]);
123123
});
124124

125125
it("leaves user routes alone and does not learn an address already saved", () => {
@@ -215,4 +215,56 @@ describe("learned routes", () => {
215215
};
216216
expect(gitHubRoutingConnectionKey(withLearned)).toBe(gitHubRoutingConnectionKey(relayOnly));
217217
});
218+
219+
it("keeps the T3 Connect credential when learning over a learned T3 Connect route", () => {
220+
const first = mergeLearnedRoutes({
221+
entry: relayOnly,
222+
activeRoute: RELAY,
223+
reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
224+
allowInsecure: true,
225+
})!;
226+
const learnedLan = first[0]!;
227+
const entry: ConnectionCatalogEntry = {
228+
...relayOnly,
229+
target: learnedLan.target,
230+
profile: learnedLan.profile,
231+
alternateRoutes: first.slice(1),
232+
};
233+
const next = mergeLearnedRoutes({
234+
entry,
235+
activeRoute: learnedLan,
236+
reported: [
237+
{ httpBaseUrl: "http://192.168.1.10:3773/" },
238+
{ httpBaseUrl: "https://desk.tail1234.ts.net/" },
239+
],
240+
allowInsecure: true,
241+
})!;
242+
for (const route of next.filter((candidate) => connectionRouteKind(candidate) !== "relay")) {
243+
expect(profileOf(route)).toMatchObject({ authorization: "t3-connect" });
244+
expect(connectionRouteId(route.target)).not.toContain("@");
245+
}
246+
});
247+
248+
it("saves a scheme change on the same host as a new address", () => {
249+
const first = mergeLearnedRoutes({
250+
entry: relayOnly,
251+
activeRoute: RELAY,
252+
reported: [{ httpBaseUrl: "http://desk.local:3773/" }],
253+
allowInsecure: true,
254+
})!;
255+
const entry: ConnectionCatalogEntry = {
256+
...relayOnly,
257+
target: first[0]!.target,
258+
profile: first[0]!.profile,
259+
alternateRoutes: first.slice(1),
260+
};
261+
const moved = mergeLearnedRoutes({
262+
entry,
263+
activeRoute: RELAY,
264+
reported: [{ httpBaseUrl: "https://desk.local:3773/" }],
265+
allowInsecure: true,
266+
});
267+
expect(moved).not.toBeNull();
268+
expect(profileOf(moved![0]!)).toMatchObject({ httpBaseUrl: "https://desk.local:3773/" });
269+
});
218270
});

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,17 @@ export function mergeLearnedRoutes(input: {
214214
if (active._tag !== "RelayConnectionTarget" && active._tag !== "BearerConnectionTarget") {
215215
return null;
216216
}
217-
const authorization = active._tag === "RelayConnectionTarget" ? "t3-connect" : undefined;
217+
// A route learned over another learned route inherits what that one uses:
218+
// the T3 Connect credential, or the paired token it borrows.
219+
const activeProfile = Option.getOrNull(input.activeRoute.profile);
220+
const authorization =
221+
active._tag === "RelayConnectionTarget" ||
222+
(activeProfile?._tag === "BearerConnectionProfile" &&
223+
activeProfile.authorization === "t3-connect")
224+
? ("t3-connect" as const)
225+
: undefined;
218226
const sharedCredential =
219-
active._tag === "BearerConnectionTarget"
227+
authorization === undefined && active._tag === "BearerConnectionTarget"
220228
? credentialConnectionId(active.connectionId)
221229
: undefined;
222230

@@ -245,7 +253,7 @@ export function mergeLearnedRoutes(input: {
245253
known.add(normalized(httpBaseUrl));
246254
const connectionId = learnedConnectionId(
247255
entry.target.environmentId,
248-
url.host,
256+
url.origin,
249257
sharedCredential,
250258
);
251259
next = insertRoute(next, {
@@ -267,9 +275,12 @@ export function mergeLearnedRoutes(input: {
267275
),
268276
});
269277
}
270-
const before = saved.map((route) => connectionRouteId(route.target)).join("\n");
271-
const after = next.map((route) => connectionRouteId(route.target)).join("\n");
272-
return before === after ? null : next;
278+
// Compare addresses too: a scheme or port change keeps no id stable.
279+
const signature = (routes: ReadonlyArray<ConnectionRoute>) =>
280+
routes
281+
.map((route) => `${connectionRouteId(route.target)} ${routeHttpBaseUrl(route) ?? ""}`)
282+
.join("\n");
283+
return signature(saved) === signature(next) ? null : next;
273284
}
274285

275286
/**
@@ -278,17 +289,19 @@ export function mergeLearnedRoutes(input: {
278289
*/
279290
function learnedConnectionId(
280291
environmentId: string,
281-
host: string,
292+
origin: string,
282293
sharedCredential: string | undefined,
283294
): string {
284295
return sharedCredential === undefined
285-
? `learned:${environmentId}:${host}`
286-
: `learned:${environmentId}:${host}@${sharedCredential}`;
296+
? `learned:${environmentId}:${origin}`
297+
: `learned:${environmentId}:${origin}@${sharedCredential}`;
287298
}
288299

289300
/** The connection id whose stored credential a bearer route uses. */
290301
export function credentialConnectionId(connectionId: string): string {
291-
const at = connectionId.lastIndexOf("@");
302+
// The borrowed id follows the first "@"; neither an environment id nor an
303+
// origin contains one.
304+
const at = connectionId.indexOf("@");
292305
return connectionId.startsWith("learned:") && at !== -1
293306
? connectionId.slice(at + 1)
294307
: connectionId;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2045,7 +2045,7 @@ describe("EnvironmentSupervisor routes", () => {
20452045
"BearerConnectionTarget",
20462046
);
20472047
expect(yield* Ref.get(learned)).toEqual([
2048-
`learned:${TARGET.environmentId}:192.168.1.10:3773`,
2048+
`learned:${TARGET.environmentId}:http://192.168.1.10:3773`,
20492049
]);
20502050
}),
20512051
);

0 commit comments

Comments
 (0)