Skip to content

Commit bf801eb

Browse files
fix(web): Settings shows network access on only once Tailscale Serve answers
The previous change made the server report remote-reachable whenever --tailscale-serve was set, so Settings claimed remote access even when tailscale serve failed (permission denied). Restore the host-only auth policy and the managed-policy check, and have Settings treat a loopback server as network accessible once it advertises a direct endpoint, which a loopback server does only after its Tailscale Serve URL answers as itself. Fixes #35521 AI-assisted; verified with the tests and tools listed above.
1 parent c7051be commit bf801eb

7 files changed

Lines changed: 40 additions & 34 deletions

File tree

‎apps/server/src/auth/EnvironmentAuthPolicy.test.ts‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,5 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))(
8282
),
8383
),
8484
);
85-
86-
it.effect(
87-
"does not report a loopback web host published through Tailscale Serve as local-only",
88-
() =>
89-
Effect.gen(function* () {
90-
const policy = yield* EnvironmentAuthPolicy.EnvironmentAuthPolicy;
91-
const descriptor = yield* policy.getDescriptor();
92-
93-
expect(descriptor.policy).not.toBe("loopback-browser");
94-
}).pipe(
95-
Effect.provide(
96-
layerEnvironmentAuthPolicy({
97-
mode: "web",
98-
host: undefined,
99-
tailscaleServeEnabled: true,
100-
}),
101-
),
102-
),
103-
);
10485
},
10586
);

‎apps/server/src/auth/EnvironmentAuthPolicy.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ export class EnvironmentAuthPolicy extends Context.Service<
2020

2121
/** The auth policy a server started with `config` runs under. */
2222
export function resolveServerAuthPolicy(
23-
config: Pick<ServerConfig.ServerConfig["Service"], "mode" | "host" | "tailscaleServeEnabled">,
23+
config: Pick<ServerConfig.ServerConfig["Service"], "mode" | "host">,
2424
): ServerAuthPolicy {
25-
if (config.tailscaleServeEnabled || isRemoteReachableHost(config.host)) return "remote-reachable";
25+
if (isRemoteReachableHost(config.host)) return "remote-reachable";
2626
return config.mode === "desktop" ? "desktop-managed-local" : "loopback-browser";
2727
}
2828

‎apps/server/src/policy/managedLimits.test.ts‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,7 @@ it.effect("binds only the loopback address it approved when remote clients are n
4545
policy: { readonly document: ManagedPolicyDocument | undefined } = desktopAndLocal,
4646
) =>
4747
ManagedLimits.checkServerReachability(policy, {
48-
authPolicy: EnvironmentAuthPolicy.resolveServerAuthPolicy({
49-
mode: "web",
50-
host,
51-
tailscaleServeEnabled: false,
52-
}),
48+
authPolicy: EnvironmentAuthPolicy.resolveServerAuthPolicy({ mode: "web", host }),
5349
host,
5450
tailscaleServeEnabled: false,
5551
});
@@ -80,10 +76,10 @@ it.effect("binds only the loopback address it approved when remote clients are n
8076

8177
it.effect("refuses Tailscale Serve when remote clients are not allowed", () =>
8278
Effect.gen(function* () {
83-
const config = { mode: "web", host: undefined, tailscaleServeEnabled: true } as const;
8479
const error = yield* ManagedLimits.checkServerReachability(desktopAndLocal, {
85-
...config,
86-
authPolicy: EnvironmentAuthPolicy.resolveServerAuthPolicy(config),
80+
authPolicy: "loopback-browser",
81+
host: undefined,
82+
tailscaleServeEnabled: true,
8783
}).pipe(Effect.flip);
8884
assert.include(error.message, "Tailscale Serve");
8985
}),

‎apps/server/src/policy/managedLimits.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,15 @@ export const checkServerReachability = Effect.fn("managedLimits.checkServerReach
225225
? "so the background service cannot listen on private networks; set T3CODE_HOST=127.0.0.1 in its service.env"
226226
: `so the server cannot listen on ${input.host === "" ? "every interface" : (input.host ?? "this host")}`,
227227
);
228-
if (input.tailscaleServeEnabled && !allowsRemoteClients(policy)) {
229-
return yield* refuseRemoteClients(policy, "so Tailscale Serve cannot be turned on");
230-
}
231228
if (!allowsAuthPolicy(policy, input.authPolicy)) {
232229
return yield* input.authPolicy === "remote-reachable"
233230
? cannotListen()
234231
: refuse(policy, AUTH_POLICY_ACTION[input.authPolicy]);
235232
}
236233
if (allowsRemoteClients(policy)) return input.host ?? "127.0.0.1";
234+
if (input.tailscaleServeEnabled) {
235+
return yield* refuseRemoteClients(policy, "so Tailscale Serve cannot be turned on");
236+
}
237237
return loopbackBindAddress(input.host) ?? (yield* cannotListen());
238238
},
239239
);

‎apps/web/src/components/settings/ConnectionsSettings.logic.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { type DesktopWslState, EnvironmentId } from "@t3tools/contracts";
22
import { describe, expect, it, vi } from "vite-plus/test";
33
import {
44
applyWslEnableSelection,
5+
isServerNetworkAccessible,
56
resolveAccessEnvironmentId,
67
resolveTailscaleServeBaseUrl,
78
selectQrEndpointOption,
@@ -201,3 +202,16 @@ describe("resolveAccessEnvironmentId", () => {
201202
);
202203
});
203204
});
205+
206+
describe("isServerNetworkAccessible", () => {
207+
const served = [{ kind: "tailnet", httpBaseUrl: "https://host.example.ts.net:8443/" }] as const;
208+
it.each([
209+
{ authPolicy: "remote-reachable", directEndpoints: undefined, expected: true },
210+
{ authPolicy: "loopback-browser", directEndpoints: undefined, expected: false },
211+
{ authPolicy: "loopback-browser", directEndpoints: [], expected: false },
212+
{ authPolicy: "loopback-browser", directEndpoints: served, expected: true },
213+
{ authPolicy: null, directEndpoints: served, expected: false },
214+
] as const)("$authPolicy with $directEndpoints.length endpoints", (row) => {
215+
expect(isServerNetworkAccessible(row)).toBe(row.expected);
216+
});
217+
});

‎apps/web/src/components/settings/ConnectionsSettings.logic.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
type DesktopUpdateState,
2121
type DesktopWslState,
2222
type EnvironmentId,
23+
type ServerAuthPolicy,
24+
type ServerDirectEndpoint,
2325
} from "@t3tools/contracts";
2426
import {
2527
getDesktopUpdateDownloadedVersion,
@@ -369,6 +371,15 @@ export function selectPairingEndpoint(
369371
);
370372
}
371373

374+
/** A loopback server advertises a direct endpoint only once its Tailscale Serve URL answers. */
375+
export function isServerNetworkAccessible(input: {
376+
readonly authPolicy: ServerAuthPolicy | null;
377+
readonly directEndpoints: ReadonlyArray<ServerDirectEndpoint> | undefined;
378+
}): boolean {
379+
if (input.authPolicy === "remote-reachable") return true;
380+
return input.authPolicy === "loopback-browser" && (input.directEndpoints?.length ?? 0) > 0;
381+
}
382+
372383
export function isTailscaleHttpsEndpoint(endpoint: AdvertisedEndpoint): boolean {
373384
return endpoint.id.startsWith("tailscale-magicdns:");
374385
}

‎apps/web/src/components/settings/ConnectionsSettings.tsx‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ import {
122122
toDesktopPairingLinkRecord,
123123
toDesktopClientSessionRecord,
124124
selectPairingEndpoint,
125+
isServerNetworkAccessible,
125126
isTailscaleHttpsEndpoint,
126127
endpointDefaultPreferenceKey,
127128
} from "./ConnectionsSettings.logic";
@@ -439,7 +440,10 @@ function ConnectionsSettingsContent({
439440
})();
440441
const isLocalBackendNetworkAccessible = desktopBridge
441442
? desktopServerExposureState?.mode === "network-accessible"
442-
: currentAuthPolicy === "remote-reachable";
443+
: isServerNetworkAccessible({
444+
authPolicy: currentAuthPolicy,
445+
directEndpoints: accessEnvironment?.serverConfig?.directEndpoints,
446+
});
443447
const trimmedTailscaleServePortInput = tailscaleServePortInput.trim();
444448
const parsedTailscaleServePort = Number(trimmedTailscaleServePortInput);
445449
const isTailscaleServePortValid =
@@ -1098,7 +1102,7 @@ function ConnectionsSettingsContent({
10981102
description={
10991103
desktopBridge
11001104
? "This connection cannot view or change network exposure."
1101-
: currentAuthPolicy === "remote-reachable"
1105+
: isLocalBackendNetworkAccessible
11021106
? "Remote access is already configured. Change network exposure where the server starts."
11031107
: currentAuthPolicy === "loopback-browser"
11041108
? "Only this machine can connect. Restart with a non-loopback host for remote pairing."

0 commit comments

Comments
 (0)