Skip to content

Commit aa619c8

Browse files
fix(clients): verify a learned address before sending it a token
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 5f93105 commit aa619c8

5 files changed

Lines changed: 79 additions & 11 deletions

File tree

‎apps/mobile/src/features/settings/EnvironmentRoutesSection.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ function RouteRow(props: {
212212
accessibilityLabel={[
213213
label,
214214
address,
215+
isLearned(route) ? "Found automatically" : null,
215216
props.inUse ? "In use" : null,
216217
`Route ${props.position} of ${props.count}`,
217218
]

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ describe("resolveBoundEndpoints", () => {
7878
).toEqual([{ kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" }]);
7979
});
8080

81+
it("never reports a host name, which can resolve to another machine per client", () => {
82+
for (const host of ["server.local", "devbox", "devbox.home.arpa"]) {
83+
expect(resolveBoundEndpoints({ host, port: 3773, interfaces: INTERFACES })).toEqual([]);
84+
}
85+
});
86+
8187
it("never reports a public address, which would carry the credential over plain HTTP", () => {
8288
expect(
8389
resolveBoundEndpoints({ host: "203.0.113.20", port: 3773, interfaces: INTERFACES }),

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import * as Context from "effect/Context";
1717
import * as Effect from "effect/Effect";
1818
import * as Layer from "effect/Layer";
1919
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
20+
import * as NodeNet from "node:net";
2021
import * as NodeOS from "node:os";
2122

2223
import { isPrivateNetworkHost } from "@t3tools/shared/hostClassification";
@@ -34,11 +35,13 @@ export class DirectEndpoints extends Context.Service<
3435
>()("t3/environment/DirectEndpoints") {}
3536

3637
/**
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.
38+
* Only numeric private-network and tailnet IPv4 addresses are reported. These
39+
* routes are plain HTTP and carry the client's credential, so a public address
40+
* would send it across the internet unencrypted, and a name (`server.local`)
41+
* can resolve to a different machine on each client's network.
4042
*/
4143
const isAdvertisableAddress = (address: string): boolean =>
44+
NodeNet.isIPv4(address) &&
4245
!address.startsWith("127.") &&
4346
!address.startsWith("169.254.") &&
4447
(isTailscaleIpv4Address(address) || isPrivateNetworkHost(address));

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ describe("RemoteEnvironmentAuthorization", () => {
353353
});
354354
const harness = yield* makeHarness({
355355
initialToken: cached,
356-
responses: [websocketTicket("lan-ticket")],
356+
responses: [Response.json(DESCRIPTOR), websocketTicket("lan-ticket")],
357357
});
358358

359359
const authorized = yield* Effect.gen(function* () {
@@ -370,8 +370,42 @@ describe("RemoteEnvironmentAuthorization", () => {
370370
expect(authorized.httpBaseUrl).toBe("http://192.168.1.10:3773/");
371371
expect(authorized.socketUrl).toMatch(/^ws:\/\/192\.168\.1\.10:3773\/ws\?/);
372372
expect(yield* Ref.get(harness.bootstrapCalls)).toBe(0);
373-
expect(String(harness.fetch.calls[0]?.[0])).toBe(
373+
expect(harness.fetch.calls.map(([url]) => String(url))).toEqual([
374+
"http://192.168.1.10:3773/.well-known/t3/environment",
374375
"http://192.168.1.10:3773/api/auth/websocket-ticket",
376+
]);
377+
}),
378+
);
379+
380+
it.effect("sends no token to a direct address that answers as another machine", () =>
381+
Effect.gen(function* () {
382+
const harness = yield* makeHarness({
383+
initialToken: persistedToken({ accessToken: "cached-access-token" }),
384+
responses: [
385+
Response.json({ ...DESCRIPTOR, environmentId: EnvironmentId.make("someone-else") }),
386+
],
387+
});
388+
389+
const error = yield* Effect.gen(function* () {
390+
const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
391+
return yield* remote
392+
.authorizeDpop({
393+
expectedEnvironmentId: ENVIRONMENT_ID,
394+
directEndpoint: {
395+
httpBaseUrl: "http://192.168.1.10:3773/",
396+
wsBaseUrl: "ws://192.168.1.10:3773/",
397+
},
398+
})
399+
.pipe(Effect.flip);
400+
}).pipe(Effect.provide(harness.layer));
401+
402+
expect(error).toMatchObject({ _tag: "ConnectionBlockedError", reason: "configuration" });
403+
expect(harness.fetch.calls.map(([url]) => String(url))).toEqual([
404+
"http://192.168.1.10:3773/.well-known/t3/environment",
405+
]);
406+
// The token stays saved for the T3 Connect route.
407+
expect((yield* Ref.get(harness.tokens)).get(ENVIRONMENT_ID)?.accessToken).toBe(
408+
"cached-access-token",
375409
);
376410
}),
377411
);
@@ -390,6 +424,7 @@ describe("RemoteEnvironmentAuthorization", () => {
390424
const harness = yield* makeHarness({
391425
initialToken: cached,
392426
responses: [
427+
Response.json(DESCRIPTOR),
393428
authInvalid(),
394429
Response.json(DESCRIPTOR),
395430
accessToken("replacement-access-token"),

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,15 @@ export const make = Effect.gen(function* () {
139139
>
140140
>(new Map());
141141

142-
const authorizeBearer = Effect.fn("clientRuntime.connection.remote.authorizeBearer")(
142+
/**
143+
* Confirms a direct address still serves the expected environment before a
144+
* credential is sent there. A saved LAN address can belong to another
145+
* machine on a different network.
146+
*/
147+
const verifyDirectEndpoint = Effect.fn("clientRuntime.connection.remote.verifyDirectEndpoint")(
143148
function* (input: {
144-
readonly expectedEnvironmentId: Parameters<
145-
RemoteEnvironmentAuthorization["Service"]["authorizeBearer"]
146-
>[0]["expectedEnvironmentId"];
149+
readonly expectedEnvironmentId: EnvironmentId;
147150
readonly httpBaseUrl: string;
148-
readonly wsBaseUrl: string;
149-
readonly bearerToken: string;
150151
readonly connectionMethod: ClientConnectionMethod;
151152
}) {
152153
const now = yield* Clock.currentTimeMillis;
@@ -176,6 +177,21 @@ export const make = Effect.gen(function* () {
176177
return next;
177178
});
178179
}
180+
return descriptor;
181+
},
182+
);
183+
184+
const authorizeBearer = Effect.fn("clientRuntime.connection.remote.authorizeBearer")(
185+
function* (input: {
186+
readonly expectedEnvironmentId: Parameters<
187+
RemoteEnvironmentAuthorization["Service"]["authorizeBearer"]
188+
>[0]["expectedEnvironmentId"];
189+
readonly httpBaseUrl: string;
190+
readonly wsBaseUrl: string;
191+
readonly bearerToken: string;
192+
readonly connectionMethod: ClientConnectionMethod;
193+
}) {
194+
const descriptor = yield* verifyDirectEndpoint(input);
179195
const socketUrl = yield* resolveRemoteWebSocketConnectionUrl({
180196
wsBaseUrl: input.wsBaseUrl,
181197
httpBaseUrl: input.httpBaseUrl,
@@ -476,6 +492,13 @@ export const make = Effect.gen(function* () {
476492
input: Parameters<RemoteEnvironmentAuthorization["Service"]["authorizeDpop"]>[0],
477493
) {
478494
const endpoint = input.directEndpoint;
495+
if (endpoint !== undefined) {
496+
yield* verifyDirectEndpoint({
497+
expectedEnvironmentId: input.expectedEnvironmentId,
498+
httpBaseUrl: endpoint.httpBaseUrl,
499+
connectionMethod: "direct",
500+
});
501+
}
479502
const authorized = (token: TokenStore.RemoteDpopAccessToken, socketUrl: string) => ({
480503
...httpAuthorization(token, endpoint?.httpBaseUrl),
481504
socketUrl,

0 commit comments

Comments
 (0)