diff --git a/apps/server/src/peer/PeerLinks.test.ts b/apps/server/src/peer/PeerLinks.test.ts index 0b1f4f716474c..c92b4f775a657 100644 --- a/apps/server/src/peer/PeerLinks.test.ts +++ b/apps/server/src/peer/PeerLinks.test.ts @@ -2,7 +2,8 @@ import * as NodeFS from "node:fs"; // @effect-diagnostics-next-line nodeBuiltinImport:off - A peer with a certificate this machine does not trust. import * as NodeHttps from "node:https"; -import { NodeHttpServer } from "@effect/platform-node"; +import { NodeHttpClient, NodeHttpServer } from "@effect/platform-node"; +import * as Undici from "@effect/platform-node/Undici"; import { type ExecutionEnvironmentDescriptor, OrchestratorMcpFailure } from "@t3tools/contracts"; import { expect, it } from "@effect/vitest"; import * as Context from "effect/Context"; @@ -17,7 +18,10 @@ import * as Queue from "effect/Queue"; import * as Ref from "effect/Ref"; import * as Schema from "effect/Schema"; import * as HttpRouter from "effect/http/HttpRouter"; +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; +import * as Sink from "effect/Sink"; import * as SqlClient from "effect/sql/SqlClient"; +import * as Stream from "effect/Stream"; import * as TestClock from "effect/testing/TestClock"; import { Tool, Toolkit } from "effect/ai"; import { @@ -89,8 +93,9 @@ const serveB = (descriptor: ExecutionEnvironmentDescriptor) => const makeA = ( descriptor: ExecutionEnvironmentDescriptor, secrets?: typeof ServerSecretStore.layer, + machine?: Layer.Layer, ) => - layerLinkingEnvironment(descriptor, secrets).pipe( + layerLinkingEnvironment(descriptor, secrets, undefined, machine).pipe( Layer.build, Effect.map((context) => ({ links: Context.get(context, PeerLinks.PeerLinks), @@ -332,6 +337,106 @@ const serveRaw = ( ), ); +/** `tailscale status --json` with `peerIps` as another machine on its tailnet. */ +const tailscaleStatus = (backendState: string, peerIps: ReadonlyArray, tun = true) => + JSON.stringify({ + BackendState: backendState, + TUN: tun, + Self: { TailscaleIPs: ["100.101.0.1"] }, + Peer: { "nodekey:box": { TailscaleIPs: peerIps } }, + }); + +/** A's `tailscale`, whose `status --json` prints what `status` holds at each run. */ +const layerTailscale = (status: Ref.Ref) => + Layer.succeed( + ChildProcessSpawner.ChildProcessSpawner, + ChildProcessSpawner.make(() => + Ref.get(status).pipe( + Effect.map((json) => + ChildProcessSpawner.makeHandle({ + pid: ChildProcessSpawner.ProcessId(1), + exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)), + isRunning: Effect.succeed(false), + kill: () => Effect.void, + unref: Effect.succeed(Effect.void), + stdin: Sink.drain, + stdout: Stream.make(new TextEncoder().encode(json)), + stderr: Stream.empty, + all: Stream.empty, + getInputFd: () => Sink.drain, + getOutputFd: () => Stream.empty, + }), + ), + ), + ), + ); + +/** Connections to `host` reach 127.0.0.1 instead, as a tailnet route would reach the peer. */ +const layerRouteHostToLoopback = (host: string) => { + const connect = Undici.buildConnector({}); + return NodeHttpClient.layerUndiciOptions({ + connect: (options, callback) => + connect( + { ...options, hostname: options.hostname === host ? "127.0.0.1" : options.hostname }, + callback, + ), + }); +}; + +it.effect( + "links over plain http to a 100.64.0.0/10 address only while Tailscale routes to it as a peer", + () => + Effect.scoped( + Effect.gen(function* () { + const b = yield* serveB(box); + const port = new URL(b.url).port; + const status = yield* Ref.make(tailscaleStatus("Stopped", ["100.100.1.2"])); + const a = yield* makeA( + laptop, + undefined, + Layer.mergeAll(layerTailscale(status), layerRouteHostToLoopback("100.100.1.2")), + ); + const linkTo = (url: string) => + b.auth + .issuePairingCredential() + .pipe( + Effect.flatMap(({ credential }) => + a.links.link({ url, pairingCode: credential, access: "auto" }), + ), + ); + + // Stopped or in userspace networking, Tailscale still lists its peers, + // but the OS would not send traffic to them through it. + for (const refused of [ + tailscaleStatus("Stopped", ["100.100.1.2"]), + tailscaleStatus("Running", ["100.100.1.2"], false), + // WARP and carrier-grade NAT share the range: an address Tailscale does not list. + tailscaleStatus("Running", ["100.100.9.9"]), + ]) { + yield* Ref.set(status, refused); + const error = yield* linkTo(`http://100.100.1.2:${port}`).pipe(Effect.flip); + expect(error.message).toContain("plain http"); + } + expect(yield* linkedSessions(b)).toEqual([]); + + yield* Ref.set(status, tailscaleStatus("Running", ["100.100.1.2"])); + expect(yield* linkTo(`http://100.100.1.2:${port}/`)).toMatchObject({ + environmentId: box.environmentId, + urls: [`http://100.100.1.2:${port}`], + }); + yield* probe(a); + expect((yield* Ref.get(b.bearers)).length).toBeGreaterThan(0); + + // Once Tailscale stops, the link's token stays here. + yield* Ref.set(status, tailscaleStatus("NeedsLogin", ["100.100.1.2"])); + yield* Ref.set(b.bearers, []); + yield* probe(a).pipe(Effect.flip); + expect(yield* Ref.get(b.bearers)).toEqual([]); + expect((yield* a.links.list).map((link) => link.status)).toEqual(["unreachable"]); + }), + ).pipe(Effect.provide(NodeHttpServer.layerTest)), +); + it.effect("never follows a peer's redirect with the pairing code", () => Effect.scoped( Effect.gen(function* () { diff --git a/apps/server/src/peer/PeerLinks.testkit.ts b/apps/server/src/peer/PeerLinks.testkit.ts index ff09db6cbdf7f..6f67f9d83109f 100644 --- a/apps/server/src/peer/PeerLinks.testkit.ts +++ b/apps/server/src/peer/PeerLinks.testkit.ts @@ -196,7 +196,8 @@ export type ServedPeer = Effect.Success>; /** * The environment that links: its own database and secret store, reaching - * peers over plain HTTP. Provide it to anything built on its links. + * peers over plain HTTP. Provide it to anything built on its links. `machine` + * replaces what this machine provides, such as its `tailscale` or its network. */ export const layerLinkingEnvironment = ( descriptor: ExecutionEnvironmentDescriptor, @@ -205,9 +206,11 @@ export const layerLinkingEnvironment = ( SqlClient.SqlClient, E | Layer.Error > = Sqlite.layerMemory, + machine: Layer.Layer = Layer.empty, ) => PeerMcpClient.layer.pipe( Layer.provideMerge(PeerLinks.layer), + Layer.provide(machine), Layer.provideMerge(ServerSettings.layerTest({ linkedEnvironmentsEnabled: true })), Layer.provideMerge(database), Layer.provide(Layer.mergeAll(secrets, FetchHttpClient.layer)), diff --git a/apps/server/src/peer/PeerLinks.ts b/apps/server/src/peer/PeerLinks.ts index 5325e5745c5d7..d902afb5db391 100644 --- a/apps/server/src/peer/PeerLinks.ts +++ b/apps/server/src/peer/PeerLinks.ts @@ -35,7 +35,13 @@ import * as HttpClient from "effect/http/HttpClient"; import * as HttpClientError from "effect/http/HttpClientError"; import * as HttpApi from "effect/http-api/HttpApi"; import * as HttpApiClient from "effect/http-api/HttpApiClient"; -import { isLocalLoopbackHost, isTailnetHost } from "@t3tools/shared/hostClassification"; +import { + isLocalLoopbackHost, + isSharedAddressSpaceHost, + isTailnetHost, +} from "@t3tools/shared/hostClassification"; +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; +import { readTailnetAddresses } from "@t3tools/tailscale"; import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; import * as ServerEnvironment from "../environment/ServerEnvironment.ts"; @@ -167,37 +173,6 @@ const failStorage = (cause: unknown) => ), ); -/** - * The base URL a peer is reached at, normalised to its origin. The pairing - * code and then the link's token travel to it, so plain http is accepted only - * where the network already encrypts: this machine, or a Tailscale address. - */ -const peerOrigin = (url: string) => { - const parsed = URL.parse(url); - if (parsed === null || (parsed.protocol !== "https:" && parsed.protocol !== "http:")) { - return Effect.fail( - new PeerLinkError({ - reason: "unreachable", - // Never the address as given: it may hold a username, password or query. - message: "That address is not an http or https address.", - }), - ); - } - if ( - parsed.protocol === "http:" && - !isLocalLoopbackHost(parsed.hostname) && - !isTailnetHost(parsed.hostname) - ) { - return Effect.fail( - new PeerLinkError({ - reason: "unreachable", - message: `${parsed.origin} is plain http, which would send the link's credentials unencrypted. Use its https address, or its Tailscale address.`, - }), - ); - } - return Effect.succeed(parsed.origin); -}; - /** The system error under a failed connection, such as ECONNREFUSED or ENOTFOUND. */ const connectionCode = (cause: unknown) => [cause, Predicate.hasProperty(cause, "cause") ? cause.cause : undefined] @@ -249,11 +224,47 @@ const make = Effect.gen(function* () { const environment = yield* ServerEnvironment.ServerEnvironment; const serverSettings = yield* ServerSettings.ServerSettingsService; const isOwnLoopback = yield* LoopbackHost; + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; const enabled = serverSettings.getSettings.pipe( Effect.map((settings) => settings.linkedEnvironmentsEnabled), Effect.orElseSucceed(() => false), ); + const isTailnetAddress = (host: string) => + readTailnetAddresses.pipe( + Effect.map((addresses) => addresses.has(host)), + Effect.orElseSucceed(() => false), + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + ); + + /** + * A peer's base URL as its origin. Its pairing code and token travel there, so + * plain http only where the network encrypts: this machine, or Tailscale. WARP + * and CGNAT share 100.64.0.0/10, so those count only where Tailscale lists them. + */ + const peerOrigin = Effect.fn("PeerLinks.peerOrigin")(function* (url: string) { + const parsed = URL.parse(url); + if (parsed === null || (parsed.protocol !== "https:" && parsed.protocol !== "http:")) { + return yield* new PeerLinkError({ + reason: "unreachable", + // Never the address as given: it may hold a username, password or query. + message: "That address is not an http or https address.", + }); + } + if ( + parsed.protocol === "http:" && + !isLocalLoopbackHost(parsed.hostname) && + !isTailnetHost(parsed.hostname) && + !(isSharedAddressSpaceHost(parsed.hostname) && (yield* isTailnetAddress(parsed.hostname))) + ) { + return yield* new PeerLinkError({ + reason: "unreachable", + message: `${parsed.origin} is plain http, which would send the link's credentials unencrypted. Use its https address, or its Tailscale address.`, + }); + } + return parsed.origin; + }); + /** A fresh name for one link's token; the row records it. */ const newSecretName = random.randomBytes(16).pipe( Effect.map((bytes) => `peer-link-${Hex.encode(bytes)}`), @@ -626,11 +637,18 @@ const make = Effect.gen(function* () { now: DateTime.Utc, ): Effect.fn.Return { if (DateTime.isLessThanOrEqualTo(link.expiresAt, now)) return { ...link, status: "expired" }; + // Listed as reachable only where resolve would still use it. Checked before + // the probe budget starts: `tailscale status` alone can take most of it. + const origins = (yield* Effect.forEach( + link.urls, + (url) => peerOrigin(url).pipe(Effect.option), + { concurrency: "unbounded" }, + )).flatMap((origin) => (Option.isSome(origin) ? [origin.value] : [])); // A race over no addresses never settles. - if (link.urls.length === 0) return { ...link, status: "unreachable" }; + if (origins.length === 0) return { ...link, status: "unreachable" }; const answered = yield* Effect.raceAll( - link.urls.map((url) => - answersAs(url, link.environmentId).pipe(Effect.flatMap(Effect.fromOption)), + origins.map((origin) => + answersAs(origin, link.environmentId).pipe(Effect.flatMap(Effect.fromOption)), ), ).pipe(Effect.timeoutOption(LIST_PROBE_TIMEOUT), Effect.orElseSucceed(Option.none)); if (Option.isNone(answered)) return { ...link, status: "unreachable" }; @@ -710,7 +728,9 @@ const make = Effect.gen(function* () { ? Option.none() : yield* Effect.raceAll( link.urls.map((url) => - answersAs(url, environmentId).pipe( + // A 100.x address is trusted only while Tailscale still lists it. + peerOrigin(url).pipe( + Effect.flatMap((origin) => answersAs(origin, environmentId)), Effect.flatMap(Effect.fromOption), Effect.map((peer) => ({ url, peer })), ), diff --git a/packages/tailscale/src/tailscale.ts b/packages/tailscale/src/tailscale.ts index 0456f95ab4850..0dc63a79915fb 100644 --- a/packages/tailscale/src/tailscale.ts +++ b/packages/tailscale/src/tailscale.ts @@ -308,23 +308,30 @@ export const readTailscaleStatus = readTailscaleOutput("status", ["status", "--j Effect.flatMap(parseTailscaleStatus), ); -const TailscaleExitNodeJson = Schema.Struct({ +const TailscalePeersJson = Schema.Struct({ + BackendState: Schema.optional(Schema.Unknown), + TUN: Schema.optional(Schema.Unknown), + // Go prints a nil Self or Peer as null: logged out, or no other machines. + Self: Schema.optional( + Schema.NullOr(Schema.Struct({ TailscaleIPs: Schema.optional(Schema.Unknown) })), + ), ExitNodeStatus: Schema.optional(Schema.Struct({ Online: Schema.optional(Schema.Unknown) })), Peer: Schema.optional( - Schema.Record( - Schema.String, - Schema.Struct({ - ExitNode: Schema.optional(Schema.Unknown), - HostName: Schema.optional(Schema.Unknown), - DNSName: Schema.optional(Schema.Unknown), - }), + Schema.NullOr( + Schema.Record( + Schema.String, + Schema.Struct({ + ExitNode: Schema.optional(Schema.Unknown), + HostName: Schema.optional(Schema.Unknown), + DNSName: Schema.optional(Schema.Unknown), + TailscaleIPs: Schema.optional(Schema.Unknown), + }), + ), ), ), }); -const decodeTailscaleExitNodeJson = Schema.decodeEffect( - Schema.fromJsonString(TailscaleExitNodeJson), -); +const decodeTailscalePeersJson = Schema.decodeEffect(Schema.fromJsonString(TailscalePeersJson)); export interface TailscaleExitNode { readonly name: string | null; @@ -338,7 +345,7 @@ export interface TailscaleExitNode { export const parseTailscaleExitNode = ( rawStatusJson: string, ): Effect.Effect => - decodeTailscaleExitNodeJson(rawStatusJson).pipe( + decodeTailscalePeersJson(rawStatusJson).pipe( Effect.mapError((cause) => new TailscaleStatusParseError({ cause })), Effect.map((parsed) => { if (parsed.ExitNodeStatus === undefined) return null; @@ -359,6 +366,33 @@ export const readTailscaleExitNode = readTailscaleOutput("status", ["status", "- Effect.flatMap(parseTailscaleExitNode), ); +/** + * The addresses of this machine and every other machine on its tailnet. Empty + * unless Tailscale runs with its TUN device: stopped or in userspace networking, + * it still lists them, but the OS would send traffic to them unencrypted. + */ +const parseTailnetAddresses = ( + rawStatusJson: string, +): Effect.Effect, TailscaleStatusParseError> => + decodeTailscalePeersJson(rawStatusJson).pipe( + Effect.mapError((cause) => new TailscaleStatusParseError({ cause })), + Effect.map((parsed) => { + if (parsed.BackendState !== "Running" || parsed.TUN !== true) return new Set(); + const nodes = [...(parsed.Self ? [parsed.Self] : []), ...Object.values(parsed.Peer ?? {})]; + return new Set( + nodes.flatMap((node) => + Array.isArray(node.TailscaleIPs) + ? node.TailscaleIPs.filter((address): address is string => typeof address === "string") + : [], + ), + ); + }), + ); + +export const readTailnetAddresses = readTailscaleOutput("status", ["status", "--json"]).pipe( + Effect.flatMap(parseTailnetAddresses), +); + const TailscalePrefsJson = Schema.Struct({ RunSSH: Schema.optional(Schema.Unknown), WantRunning: Schema.optional(Schema.Unknown),