Skip to content

Commit d0aaee1

Browse files
fix(server): advertise a Tailscale Serve URL only when it reaches this server
The Settings fix treats an advertised direct endpoint as proof that a loopback server is reachable over Tailscale Serve, but the Serve probe accepted any T3 environment descriptor. When the Serve port already routed to another T3 server, this one advertised that URL and Settings said remote access was configured. The probe now takes the expected environment ID and DirectEndpoints passes its own. AI-assisted; verified with the tests and tools listed above.
1 parent bf801eb commit d0aaee1

4 files changed

Lines changed: 33 additions & 8 deletions

File tree

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as Ref from "effect/Ref";
88
import * as Layer from "effect/Layer";
99
import * as Sink from "effect/Sink";
1010
import * as Stream from "effect/Stream";
11+
import { EnvironmentId } from "@t3tools/contracts";
1112
import * as HttpClient from "effect/http/HttpClient";
1213
import type * as HttpClientRequest from "effect/http/HttpClientRequest";
1314
import * as HttpClientResponse from "effect/http/HttpClientResponse";
@@ -16,6 +17,7 @@ import { describe, expect, it } from "vite-plus/test";
1617

1718
import * as ServerConfig from "../config.ts";
1819
import * as DirectEndpoints from "./DirectEndpoints.ts";
20+
import * as ServerEnvironment from "./ServerEnvironment.ts";
1921
import { resolveBoundEndpoints } from "./DirectEndpoints.ts";
2022

2123
const INTERFACES: ReturnType<typeof NodeOS.networkInterfaces> = {
@@ -286,12 +288,22 @@ const layerServeConfig = (host = "127.0.0.1") =>
286288
Layer.provide(NodeServices.layer),
287289
);
288290

289-
const resolveWithServe = (probeStatus: number) =>
291+
const layerIdentity = (environmentId = "serve-test") =>
292+
Layer.succeed(ServerEnvironment.ServerEnvironmentIdentity, {
293+
getEnvironmentId: Effect.succeed(EnvironmentId.make(environmentId)),
294+
});
295+
296+
const resolveWithServe = (probeStatus: number, environmentId?: string) =>
290297
Effect.flatMap(DirectEndpoints.DirectEndpoints, (service) => service.resolve()).pipe(
291298
Effect.provide(
292299
DirectEndpoints.layer.pipe(
293300
Layer.provide(
294-
Layer.mergeAll(layerServeConfig(), layerTailscaleUp, layerServeProbe(probeStatus)),
301+
Layer.mergeAll(
302+
layerServeConfig(),
303+
layerTailscaleUp,
304+
layerServeProbe(probeStatus),
305+
layerIdentity(environmentId),
306+
),
295307
),
296308
),
297309
),
@@ -320,6 +332,7 @@ describe("DirectEndpoints Tailscale Serve", () => {
320332
Layer.mergeAll(
321333
layerServeConfig("192.168.1.10"),
322334
layerTailscaleUp,
335+
layerIdentity(),
323336
Layer.succeed(
324337
HttpClient.HttpClient,
325338
HttpClient.make((request) =>
@@ -361,9 +374,10 @@ describe("DirectEndpoints Tailscale Serve", () => {
361374
}).pipe(Effect.scoped),
362375
);
363376

364-
effectIt.effect("leaves the tailnet name out when Serve is not forwarding", () =>
377+
effectIt.effect("leaves the tailnet name out when Serve does not forward to this server", () =>
365378
Effect.gen(function* () {
366379
expect(yield* resolveWithServe(502)).toEqual([]);
380+
expect(yield* resolveWithServe(200, "another-server")).toEqual([]);
367381
}),
368382
);
369383
});

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from "@t3tools/shared/hostClassification";
2424

2525
import * as ServerConfig from "../config.ts";
26+
import * as ServerEnvironment from "./ServerEnvironment.ts";
2627
import { isLoopbackHost, isPrivateNetworkAddress, VIRTUAL_INTERFACE } from "../startupAccess.ts";
2728

2829
type NetworkInterfacesMap = ReturnType<typeof NodeOS.networkInterfaces>;
@@ -120,6 +121,8 @@ export const make = Effect.gen(function* () {
120121
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
121122
const httpClient = yield* HttpClient.HttpClient;
122123
const platform = yield* HostProcess.Platform;
124+
const environmentId = yield* (yield* ServerEnvironment.ServerEnvironmentIdentity)
125+
.getEnvironmentId;
123126
// Only the service default listens on every interface without being asked,
124127
// so only it checks once whether it runs in a cloud. The check starts now so
125128
// the first client does not wait for its timeout.
@@ -155,7 +158,7 @@ export const make = Effect.gen(function* () {
155158
magicDnsName,
156159
servePort: config.tailscaleServePort,
157160
});
158-
return probeTailscaleHttpsEndpoint({ baseUrl: httpBaseUrl }).pipe(
161+
return probeTailscaleHttpsEndpoint({ baseUrl: httpBaseUrl, environmentId }).pipe(
159162
Effect.map((reachable) => (reachable ? httpBaseUrl : null)),
160163
);
161164
}),

‎apps/server/src/server.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ const layerRuntimeDependencies = layerRuntimeCoreDependencies.pipe(
773773
Layer.provideMerge(AnalyticsService.layer.pipe(Layer.provide(ManagedPolicy.layer))),
774774
Layer.provideMerge(ExternalLauncher.layer),
775775
Layer.provideMerge(RemoteOpenTargets.layer),
776-
Layer.provideMerge(DirectEndpoints.layer),
776+
Layer.provideMerge(DirectEndpoints.layer.pipe(Layer.provide(ServerEnvironment.layerIdentity))),
777777
Layer.provideMerge(ServerLifecycleEvents.layer),
778778
Layer.provide(NetService.layer),
779779
);

‎packages/tailscale/src/tailscale.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ExecutionEnvironmentDescriptor } from "@t3tools/contracts";
1+
import { type EnvironmentId, ExecutionEnvironmentDescriptor } from "@t3tools/contracts";
22
import * as HostProcess from "@t3tools/shared/HostProcess";
33
import { isLocalLoopbackHost, normalizeHostname } from "@t3tools/shared/hostClassification";
44
import * as Duration from "effect/Duration";
@@ -711,7 +711,12 @@ export const readTailscaleServeRootHolders = (
711711
);
712712

713713
export const probeTailscaleHttpsEndpoint = Effect.fn("tailscale.probeTailscaleHttpsEndpoint")(
714-
function* (input: { readonly baseUrl: string; readonly timeout?: Duration.Input }) {
714+
function* (input: {
715+
readonly baseUrl: string;
716+
readonly timeout?: Duration.Input;
717+
/** Serve may still route to another T3 server; only its own ID proves this one. */
718+
readonly environmentId?: EnvironmentId;
719+
}) {
715720
const client = yield* HttpClient.HttpClient;
716721
const descriptor = yield* Effect.gen(function* () {
717722
const url = new URL("/.well-known/t3/environment", input.baseUrl);
@@ -722,7 +727,10 @@ export const probeTailscaleHttpsEndpoint = Effect.fn("tailscale.probeTailscaleHt
722727
return yield* HttpClientResponse.schemaBodyJson(ExecutionEnvironmentDescriptor)(response);
723728
}).pipe(Effect.timeoutOption(input.timeout ?? TAILSCALE_PROBE_TIMEOUT));
724729

725-
return Option.isSome(descriptor);
730+
return Option.exists(
731+
descriptor,
732+
(found) => input.environmentId === undefined || found.environmentId === input.environmentId,
733+
);
726734
},
727735
Effect.orElseSucceed(() => false),
728736
);

0 commit comments

Comments
 (0)