Skip to content

Commit 82465ed

Browse files
fix(tailscale): Tailscale HTTPS is advertised only when it reaches this T3 server (#34744)
1 parent ea22b5f commit 82465ed

5 files changed

Lines changed: 53 additions & 14 deletions

File tree

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as Layer from "effect/Layer";
99
import * as Sink from "effect/Sink";
1010
import * as Stream from "effect/Stream";
1111
import * as HttpClient from "effect/http/HttpClient";
12+
import type * as HttpClientRequest from "effect/http/HttpClientRequest";
1213
import * as HttpClientResponse from "effect/http/HttpClientResponse";
1314
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
1415
import { describe, expect, it } from "vite-plus/test";
@@ -250,12 +251,24 @@ const layerTailscaleUp = Layer.succeed(
250251
),
251252
);
252253

254+
const T3_DESCRIPTOR_BODY = JSON.stringify({
255+
environmentId: "serve-test",
256+
label: "Serve test",
257+
platform: { os: "darwin", arch: "arm64" },
258+
serverVersion: "0.0.0-test",
259+
capabilities: { repositoryIdentity: true },
260+
});
261+
262+
const serveProbeResponse = (request: HttpClientRequest.HttpClientRequest, status: number) =>
263+
HttpClientResponse.fromWeb(
264+
request,
265+
new Response(status === 200 ? T3_DESCRIPTOR_BODY : null, { status }),
266+
);
267+
253268
const layerServeProbe = (status: number) =>
254269
Layer.succeed(
255270
HttpClient.HttpClient,
256-
HttpClient.make((request) =>
257-
Effect.succeed(HttpClientResponse.fromWeb(request, new Response(null, { status }))),
258-
),
271+
HttpClient.make((request) => Effect.succeed(serveProbeResponse(request, status))),
259272
);
260273

261274
const layerServeConfig = (host = "127.0.0.1") =>
@@ -320,9 +333,7 @@ describe("DirectEndpoints Tailscale Serve", () => {
320333
)
321334
: Effect.void,
322335
),
323-
Effect.map((status) =>
324-
HttpClientResponse.fromWeb(request, new Response(null, { status })),
325-
),
336+
Effect.map((status) => serveProbeResponse(request, status)),
326337
),
327338
),
328339
),

‎packages/tailscale/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"test": "vp test run --config ../../vite.config.ts --dir ."
1414
},
1515
"dependencies": {
16+
"@t3tools/contracts": "workspace:*",
1617
"@t3tools/shared": "workspace:*",
1718
"effect": "catalog:"
1819
},

‎packages/tailscale/src/tailscale.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as Fiber from "effect/Fiber";
77
import * as Layer from "effect/Layer";
88
import * as Sink from "effect/Sink";
99
import * as Stream from "effect/Stream";
10+
import { HttpClient, HttpClientResponse } from "effect/http";
1011
import * as TestClock from "effect/testing/TestClock";
1112
import { ChildProcessSpawner } from "effect/process";
1213
import * as HostProcess from "@t3tools/shared/HostProcess";
@@ -17,6 +18,7 @@ import {
1718
parseTailscaleServeRoutesTo,
1819
parseTailscaleSshEnabled,
1920
parseTailscaleStatus,
21+
probeTailscaleHttpsEndpoint,
2022
readTailscaleSshEnabled,
2123
readTailscaleStatus,
2224
TailscaleCommandExitError,
@@ -99,6 +101,27 @@ function layerMockSpawner(
99101
}
100102

101103
describe("tailscale", () => {
104+
it.effect("does not treat a non-T3 response on the HTTPS probe path as reachable", () =>
105+
Effect.gen(function* () {
106+
for (const [body, status] of [
107+
["<!doctype html><title>Other application</title>", 200],
108+
[null, 204],
109+
] as const) {
110+
const reachable = yield* probeTailscaleHttpsEndpoint({
111+
baseUrl: "https://desktop.example.ts.net",
112+
}).pipe(
113+
Effect.provideService(
114+
HttpClient.HttpClient,
115+
HttpClient.make((request) =>
116+
Effect.succeed(HttpClientResponse.fromWeb(request, new Response(body, { status }))),
117+
),
118+
),
119+
);
120+
assert.isFalse(reachable, `HTTP ${status} without a T3 descriptor`);
121+
}
122+
}),
123+
);
124+
102125
it("finds the Serve routes that forward to the bound address, and only those", () => {
103126
const status = JSON.stringify({
104127
TCP: {

‎packages/tailscale/src/tailscale.ts‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import { ExecutionEnvironmentDescriptor } from "@t3tools/contracts";
12
import * as HostProcess from "@t3tools/shared/HostProcess";
23
import { normalizeHostname } from "@t3tools/shared/hostClassification";
34
import * as Duration from "effect/Duration";
45
import * as Effect from "effect/Effect";
56
import * as Option from "effect/Option";
67
import * as Schema from "effect/Schema";
78
import * as Stream from "effect/Stream";
8-
import { HttpClient, HttpClientRequest } from "effect/http";
9+
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
910
import { ChildProcess, ChildProcessSpawner } from "effect/process";
1011

1112
export const DEFAULT_TAILSCALE_SERVE_PORT = 443;
@@ -658,16 +659,16 @@ export const readTailscaleServeRoutesTo = (target: {
658659
export const probeTailscaleHttpsEndpoint = Effect.fn("tailscale.probeTailscaleHttpsEndpoint")(
659660
function* (input: { readonly baseUrl: string; readonly timeout?: Duration.Input }) {
660661
const client = yield* HttpClient.HttpClient;
661-
const response = yield* Effect.gen(function* () {
662+
const descriptor = yield* Effect.gen(function* () {
662663
const url = new URL("/.well-known/t3/environment", input.baseUrl);
663-
const request = HttpClientRequest.get(url.toString());
664-
return yield* client.execute(request);
664+
const response = yield* client
665+
.execute(HttpClientRequest.get(url.toString()))
666+
.pipe(Effect.flatMap(HttpClientResponse.filterStatusOk));
667+
// A Serve mapping to another app also answers 2xx; only a T3 descriptor proves this server.
668+
return yield* HttpClientResponse.schemaBodyJson(ExecutionEnvironmentDescriptor)(response);
665669
}).pipe(Effect.timeoutOption(input.timeout ?? TAILSCALE_PROBE_TIMEOUT));
666670

667-
return Option.match(response, {
668-
onNone: () => false,
669-
onSome: (httpResponse) => httpResponse.status >= 200 && httpResponse.status < 300,
670-
});
671+
return Option.isSome(descriptor);
671672
},
672673
Effect.orElseSucceed(() => false),
673674
);

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)