@devries-cameron20 / t3code Mirror · read-only / commits / 4040146bbf
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 03:00 UTC

fix(desktop): exclude unrelated VPN addresses from Tailscale pairing (#15753)

Co-authored-by: Arthur Katcher <192321283+arthurkatcher@users.noreply.github.com>

Arthur Katcher committed Oct 18, 2026 at 22:08 UTC 4040146bbfe8e544b10ef369bfe028f62af471ab
4 files changed +174 -31
apps/desktop/src/backend/DesktopServerExposure.test.ts
+34 -5
@@ -40,12 +40,13 @@ const tailnetNetworkInterfaces: DesktopNetworkInterfaces.NetworkInterfaces = {
40 ],
41 };
42
43 -function layerMockSpawner(statusJson = "{}") {
43 +function layerMockSpawner(statusJson = "{}", onSpawn?: () => void) {
44 return Layer.succeed(
45 ChildProcessSpawner.ChildProcessSpawner,
46 ChildProcessSpawner.make(() =>
47 - Effect.succeed(
48 - ChildProcessSpawner.makeHandle({
47 + Effect.sync(() => {
48 + onSpawn?.();
49 + return ChildProcessSpawner.makeHandle({
50 pid: ChildProcessSpawner.ProcessId(1),
51 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
52 isRunning: Effect.succeed(false),
@@ -57,8 +58,8 @@ function layerMockSpawner(statusJson = "{}") {
58 all: Stream.empty,
59 getInputFd: () => Sink.drain,
60 getOutputFd: () => Stream.empty,
60 - }),
61 - ),
61 + });
62 + }),
63 ),
64 );
65 }
@@ -146,6 +147,34 @@ const withHarness = <A, E, R>(
147 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped);
148
149 describe("DesktopServerExposure", () => {
150 + it.effect("uses cached Tailscale identity to exclude another VPN's pairing address", () => {
151 + let statusReads = 0;
152 + return withHarness(
153 + {
154 + ...lanNetworkInterfaces,
155 + pvpnksintrf1: [{ address: "100.85.0.1", family: "IPv4", internal: false }],
156 + vpn0: [{ address: "100.74.126.34", family: "IPv4", internal: false }],
157 + },
158 + Effect.gen(function* () {
159 + const serverExposure = yield* DesktopServerExposure.DesktopServerExposure;
160 + yield* serverExposure.configureFromSettings({ port: 4173 });
161 + yield* serverExposure.setMode("network-accessible");
162 +
163 + const endpoints = yield* serverExposure.getAdvertisedEndpoints;
164 + assert.deepEqual(
165 + endpoints.map((endpoint) => endpoint.httpBaseUrl),
166 + ["http://127.0.0.1:4173/", "http://192.168.1.20:4173/", "http://100.74.126.34:4173/"],
167 + );
168 + assert.deepEqual(yield* serverExposure.getAdvertisedEndpoints, endpoints);
169 + assert.equal(statusReads, 1);
170 + }),
171 + {},
172 + layerMockSpawner(`{"Self":{"TailscaleIPs":["100.74.126.34"]}}`, () => {
173 + statusReads += 1;
174 + }),
175 + );
176 + });
177 +
178 it.effect("falls back to local-only without losing the requested network preference", () =>
179 withHarness(
180 emptyNetworkInterfaces,
apps/desktop/src/backend/DesktopServerExposure.ts
+2 -3
@@ -437,9 +437,8 @@ export const make = Effect.gen(function* () {
437 // Cache the `tailscale status` spawn for the TTL. On macOS, the Mac App
438 // Store Tailscale CLI lives inside Tailscale's sandbox container, so each
439 // spawn re-triggers the "Other apps" TCC prompt.
440 - const cachedReadMagicDnsName = yield* Effect.cachedWithTTL(
440 + const cachedReadTailscaleStatus = yield* Effect.cachedWithTTL(
441 readTailscaleStatus.pipe(
442 - Effect.map((status) => status.magicDnsName),
442 Effect.orElseSucceed(() => null),
443 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, childProcessSpawner),
444 ),
@@ -565,7 +564,7 @@ export const make = Effect.gen(function* () {
564 serveEnabled: state.tailscaleServeEnabled,
565 servePort: state.tailscaleServePort,
566 networkInterfaces: currentNetworkInterfaces,
568 - readMagicDnsName: cachedReadMagicDnsName,
567 + readStatus: cachedReadTailscaleStatus,
568 }).pipe(
569 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, childProcessSpawner),
570 Effect.provideService(HttpClient.HttpClient, httpClient),
apps/desktop/src/backend/tailscaleEndpointProvider.test.ts
+111 -6
@@ -20,7 +20,109 @@ const layerUnusedTailscaleExternalServices = Layer.mergeAll(
20 ),
21 );
22
23 +const protonAndTailscaleInterfaces = {
24 + pvpnksintrf1: [{ address: "100.85.0.1", family: "IPv4", internal: false }],
25 + tailscale0: [{ address: "100.74.126.34", family: "IPv4", internal: false }],
26 +};
27 +
28 describe("tailscale endpoint provider", () => {
29 + it.effect("does not advertise Proton's CGNAT address as a Tailscale pairing endpoint", () =>
30 + Effect.gen(function* () {
31 + const endpoints = yield* resolveTailscaleAdvertisedEndpoints({
32 + port: 3773,
33 + networkInterfaces: protonAndTailscaleInterfaces,
34 + statusJson: `{"Self":{"TailscaleIPs":["100.74.126.34"]}}`,
35 + });
36 + assert.deepEqual(
37 + endpoints.map((endpoint) => endpoint.httpBaseUrl),
38 + ["http://100.74.126.34:3773/"],
39 + );
40 + }).pipe(Effect.provide(layerUnusedTailscaleExternalServices)),
41 + );
42 +
43 + it.effect(
44 + "does not advertise CGNAT interfaces when Tailscale reports no assigned addresses",
45 + () =>
46 + Effect.gen(function* () {
47 + const endpoints = yield* resolveTailscaleAdvertisedEndpoints({
48 + port: 3773,
49 + networkInterfaces: { pvpnksintrf1: protonAndTailscaleInterfaces.pvpnksintrf1 },
50 + statusJson: `{"BackendState":"NoState","Self":{"TailscaleIPs":null}}`,
51 + });
52 + assert.deepEqual(endpoints, []);
53 + }).pipe(Effect.provide(layerUnusedTailscaleExternalServices)),
54 + );
55 +
56 + it.effect("keeps native Tailscale interfaces with missing, malformed, or stale status", () =>
57 + Effect.gen(function* () {
58 + for (const interfaceName of ["tailscale0", "Tailscale"]) {
59 + for (const statusJson of [null, "not-json", `{"Self":{"TailscaleIPs":[]}}`]) {
60 + const endpoints = yield* resolveTailscaleAdvertisedEndpoints({
61 + port: 3773,
62 + networkInterfaces: {
63 + pvpnksintrf1: protonAndTailscaleInterfaces.pvpnksintrf1,
64 + [interfaceName]: protonAndTailscaleInterfaces.tailscale0,
65 + },
66 + statusJson,
67 + });
68 + assert.deepEqual(
69 + endpoints.map((endpoint) => endpoint.httpBaseUrl),
70 + ["http://100.74.126.34:3773/"],
71 + );
72 + }
73 + }
74 + }).pipe(Effect.provide(layerUnusedTailscaleExternalServices)),
75 + );
76 +
77 + it.effect(
78 + "recognizes a macOS Tailscale tunnel by its IPv6 address when status is unavailable",
79 + () =>
80 + Effect.gen(function* () {
81 + const endpoints = yield* resolveTailscaleAdvertisedEndpoints({
82 + port: 3773,
83 + networkInterfaces: {
84 + utun3: [{ address: "100.85.0.1", family: "IPv4", internal: false }],
85 + utun4: [
86 + { address: "100.74.126.34", family: "IPv4", internal: false },
87 + { address: "fd7a:115c:a1e0::1234", family: "IPv6", internal: false },
88 + ],
89 + },
90 + statusJson: null,
91 + });
92 + assert.deepEqual(
93 + endpoints.map((endpoint) => endpoint.httpBaseUrl),
94 + ["http://100.74.126.34:3773/"],
95 + );
96 + }).pipe(Effect.provide(layerUnusedTailscaleExternalServices)),
97 + );
98 +
99 + it.effect("uses assigned addresses for renamed adapters without IPv6", () =>
100 + Effect.gen(function* () {
101 + const endpoints = yield* resolveTailscaleAdvertisedEndpoints({
102 + port: 3773,
103 + networkInterfaces: {
104 + vpn0: [{ address: "100.74.126.34", family: "IPv4", internal: false }],
105 + },
106 + statusJson: `{"Self":{"TailscaleIPs":["100.74.126.34"]}}`,
107 + });
108 + assert.deepEqual(
109 + endpoints.map((endpoint) => endpoint.httpBaseUrl),
110 + ["http://100.74.126.34:3773/"],
111 + );
112 + }).pipe(Effect.provide(layerUnusedTailscaleExternalServices)),
113 + );
114 +
115 + it.effect("does not advertise cached Tailscale addresses absent from local interfaces", () =>
116 + Effect.gen(function* () {
117 + const endpoints = yield* resolveTailscaleAdvertisedEndpoints({
118 + port: 3773,
119 + networkInterfaces: { pvpnksintrf1: protonAndTailscaleInterfaces.pvpnksintrf1 },
120 + statusJson: `{"Self":{"TailscaleIPs":["100.74.126.35"]}}`,
121 + });
122 + assert.deepEqual(endpoints, []);
123 + }).pipe(Effect.provide(layerUnusedTailscaleExternalServices)),
124 + );
125 +
126 it.effect("parses MagicDNS names from tailscale status", () =>
127 Effect.gen(function* () {
128 const dnsName = yield* parseTailscaleMagicDnsName(
@@ -49,7 +151,7 @@ describe("tailscale endpoint provider", () => {
151 },
152 ],
153 },
52 - statusJson: `{"Self":{"DNSName":"desktop.tail.ts.net."}}`,
154 + statusJson: `{"Self":{"DNSName":"desktop.tail.ts.net.","TailscaleIPs":["100.100.100.100"]}}`,
155 });
156 assert.deepEqual(endpoints, [
157 {
@@ -96,21 +198,24 @@ describe("tailscale endpoint provider", () => {
198 }).pipe(Effect.provide(layerUnusedTailscaleExternalServices)),
199 );
200
99 - it.effect("uses an injected magic DNS name reader instead of spawning tailscale", () =>
201 + it.effect("uses one injected status reader for both IP and MagicDNS endpoints", () =>
202 Effect.gen(function* () {
203 let readerCalls = 0;
204 const endpoints = yield* resolveTailscaleAdvertisedEndpoints({
205 port: 3773,
104 - networkInterfaces: {},
105 - readMagicDnsName: Effect.sync(() => {
206 + networkInterfaces: protonAndTailscaleInterfaces,
207 + readStatus: Effect.sync(() => {
208 readerCalls += 1;
107 - return "desktop.tail.ts.net";
209 + return {
210 + magicDnsName: "desktop.tail.ts.net",
211 + tailnetIpv4Addresses: ["100.74.126.34"],
212 + };
213 }),
214 });
215 assert.equal(readerCalls, 1);
216 assert.deepEqual(
217 endpoints.map((endpoint) => endpoint.httpBaseUrl),
113 - ["https://desktop.tail.ts.net/"],
218 + ["http://100.74.126.34:3773/", "https://desktop.tail.ts.net/"],
219 );
220 }).pipe(Effect.provide(layerUnusedTailscaleExternalServices)),
221 );
apps/desktop/src/backend/tailscaleEndpointProvider.ts
+27 -17
@@ -3,9 +3,10 @@ import type { AdvertisedEndpoint, AdvertisedEndpointProvider } from "@t3tools/co
3 import {
4 buildTailscaleHttpsBaseUrl,
5 isTailscaleIpv4Address,
6 - parseTailscaleMagicDnsName,
6 + parseTailscaleStatus,
7 probeTailscaleHttpsEndpoint,
8 readTailscaleStatus,
9 + type TailscaleStatus,
10 } from "@t3tools/tailscale";
11 import * as Effect from "effect/Effect";
12 import * as Option from "effect/Option";
@@ -26,17 +27,32 @@ const TAILSCALE_ENDPOINT_PROVIDER: AdvertisedEndpointProvider = {
27 function resolveTailscaleIpAdvertisedEndpoints(input: {
28 readonly port: number;
29 readonly networkInterfaces: NetworkInterfaces;
30 + readonly status: TailscaleStatus | null;
31 }): readonly AdvertisedEndpoint[] {
32 const seen = new Set<string>();
33 const endpoints: AdvertisedEndpoint[] = [];
34
33 - for (const interfaceAddresses of Object.values(input.networkInterfaces)) {
35 + for (const [interfaceName, interfaceAddresses] of Object.entries(input.networkInterfaces)) {
36 if (!interfaceAddresses) continue;
37
38 + // CGNAT space is shared with other VPNs. Native adapter identity also
39 + // works when the CLI is missing or its cached status predates a connection.
40 + const isTailscaleInterface =
41 + /^tailscale(?:\d+|ipv[46])?$/iu.test(interfaceName) ||
42 + interfaceAddresses.some(
43 + (address) =>
44 + !address.internal &&
45 + address.family === "IPv6" &&
46 + /^fd7a:115c:a1e0:/iu.test(address.address),
47 + );
48 +
49 for (const address of interfaceAddresses) {
50 if (address.internal) continue;
51 if (address.family !== "IPv4") continue;
52 if (!isTailscaleIpv4Address(address.address)) continue;
53 + if (!isTailscaleInterface && !input.status?.tailnetIpv4Addresses.includes(address.address)) {
54 + continue;
55 + }
56 if (seen.has(address.address)) continue;
57 seen.add(address.address);
58
@@ -105,8 +121,8 @@ export const resolveTailscaleAdvertisedEndpoints = Effect.fn("resolveTailscaleAd
121 readonly servePort?: number;
122 readonly networkInterfaces: NetworkInterfaces;
123 readonly statusJson?: string | null;
108 - readonly readMagicDnsName?: Effect.Effect<
109 - string | null,
124 + readonly readStatus?: Effect.Effect<
125 + TailscaleStatus | null,
126 never,
127 ChildProcessSpawner.ChildProcessSpawner
128 >;
@@ -116,23 +132,17 @@ export const resolveTailscaleAdvertisedEndpoints = Effect.fn("resolveTailscaleAd
132 never,
133 ChildProcessSpawner.ChildProcessSpawner | HttpClient.HttpClient
134 > {
119 - const ipEndpoints = resolveTailscaleIpAdvertisedEndpoints(input);
120 - const readDnsName =
121 - input.readMagicDnsName ??
122 - readTailscaleStatus.pipe(
123 - Effect.map((status) => status.magicDnsName),
124 - Effect.orElseSucceed((): string | null => null),
125 - );
126 - const dnsName =
135 + const readStatus =
136 + input.readStatus ?? readTailscaleStatus.pipe(Effect.orElseSucceed(() => null));
137 + const status =
138 input.statusJson === undefined
128 - ? yield* readDnsName
139 + ? yield* readStatus
140 : input.statusJson
130 - ? yield* parseTailscaleMagicDnsName(input.statusJson).pipe(
131 - Effect.orElseSucceed(() => null),
132 - )
141 + ? yield* parseTailscaleStatus(input.statusJson).pipe(Effect.orElseSucceed(() => null))
142 : null;
143 + const ipEndpoints = resolveTailscaleIpAdvertisedEndpoints({ ...input, status });
144 const magicDnsEndpoint = yield* resolveTailscaleMagicDnsAdvertisedEndpoint({
135 - dnsName,
145 + dnsName: status?.magicDnsName ?? null,
146 serveEnabled: input.serveEnabled === true,
147 ...(input.servePort === undefined ? {} : { servePort: input.servePort }),
148 ...(input.probe === undefined ? {} : { probe: input.probe }),