Repository navigation
Expand file tree
/
Copy pathmanagedLimits.ts
More file actions
239 lines (222 loc) · 10.3 KB
/
Copy pathmanagedLimits.ts
File metadata and controls
239 lines (222 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* managedLimits - what `limits.clients` and `limits.connect` in the managed policy allow, and the
* one error every refusal uses so each transport can show the administrator's organization.
*
* @module managedLimits
*/
import type {
ManagedPolicyClient,
ManagedPolicyDocument,
ServerAuthPolicy,
} from "@t3tools/contracts";
import {
disableTailscaleServe,
disableTailscaleServeTcp,
readTailscaleServeRoutesTo,
tailscaleServeOffArgs,
readTailscaleStatus,
type TailscaleServeRoute,
type TailscaleStderrDiagnostic,
} from "@t3tools/tailscale";
import * as Effect from "effect/Effect";
import * as PlatformError from "effect/PlatformError";
import * as Schema from "effect/Schema";
import * as NetService from "@t3tools/shared/Net";
import { loopbackBindAddress } from "../auth/utils.ts";
type PolicyDocument = { readonly document: ManagedPolicyDocument | undefined };
export class ManagedPolicyRefusedError extends Schema.TaggedError<ManagedPolicyRefusedError>()(
"ManagedPolicyRefusedError",
{
organization: Schema.String,
/** What the policy does not allow, for example "T3 Connect". */
action: Schema.String,
/** What that means for this request, for example "so the server cannot listen on 0.0.0.0". */
detail: Schema.optionalKey(Schema.String),
},
) {
override get message(): string {
return `${this.organization}'s policy does not allow ${this.action}${
this.detail === undefined ? "" : `, ${this.detail}`
}.`;
}
}
const CLIENT_AUTH_POLICY: Record<ManagedPolicyClient, ServerAuthPolicy> = {
desktop: "desktop-managed-local",
"local-browser": "loopback-browser",
remote: "remote-reachable",
};
const AUTH_POLICY_ACTION: Record<ServerAuthPolicy, string> = {
"desktop-managed-local": "the desktop app",
"loopback-browser": "browsers on this machine",
"remote-reachable": "remote clients",
"unsafe-no-auth": "running without authentication",
};
/** Whether the server may run with `authPolicy`. Any policy file rules out `unsafe-no-auth`. */
export function allowsAuthPolicy(policy: PolicyDocument, authPolicy: ServerAuthPolicy): boolean {
if (policy.document === undefined) return true;
if (authPolicy === "unsafe-no-auth") return false;
const clients = policy.document.limits?.clients;
return (
clients === undefined || clients.some((client) => CLIENT_AUTH_POLICY[client] === authPolicy)
);
}
/** Whether anything off this machine may pair, by LAN, Tailscale, or a minted link. */
export const allowsRemoteClients = (policy: PolicyDocument) =>
allowsAuthPolicy(policy, "remote-reachable");
/** T3 Connect is a way for remote clients in, so a policy without `remote` turns it off too. */
export const allowsConnect = (policy: PolicyDocument) =>
policy.document?.limits?.connect !== false && allowsRemoteClients(policy);
const refuse = (policy: PolicyDocument, action: string, detail?: string) =>
new ManagedPolicyRefusedError({
organization: policy.document?.organization ?? "Your organization",
action,
...(detail === undefined ? {} : { detail }),
});
export const refuseConnect = (policy: PolicyDocument) => refuse(policy, "T3 Connect");
// Node throws ENOTDIR, outside the spawner's error mapping, when PATH ends in a file.
const isMissingCli = (cause: unknown) =>
(PlatformError.isPlatformError(cause) && cause.reason._tag === "NotFound") ||
(cause instanceof Error && "code" in cause && cause.code === "ENOTDIR");
const NOTHING_FORWARDED: ReadonlyArray<TailscaleServeRoute> = [];
/** Tailscale answers that leave nothing forwarding here right now. */
const NOTHING_TO_CLOSE: ReadonlySet<TailscaleStderrDiagnostic | undefined> = new Set([
"not-running",
"not-logged-in",
]);
/**
* Turns off the Tailscale Serve routes that forward to the server at `target`, such as one
* `t3 pair --tailscale` left from before the policy, and keeps every other route. Runs before the
* server binds, and fails unless a fresh read shows no route here, since `off` can exit 0 and
* leave it. Without the CLI, or with Tailscale down or logged out, nothing forwards.
*/
export const closeTailscaleServeTo = Effect.fn("managedLimits.closeTailscaleServeTo")(function* (
policy: PolicyDocument,
target: { readonly host: string; readonly port: number },
) {
if (allowsRemoteClients(policy)) return;
// Another app holds the port, so its routes are not ours to remove. It could let go before the
// bind, which would then take over those routes, so the server does not start.
if (!(yield* (yield* NetService.NetService).canListenOnHost(target.port, target.host))) {
return yield* refuseRemoteClients(
policy,
`so the server does not start while it cannot bind ${target.host}:${target.port}; if another app holds that port, the server would take over its Tailscale Serve routes`,
);
}
const readRoutes = readTailscaleServeRoutesTo(target).pipe(
Effect.catchTags({
// Only a CLI that is not there; one that will not run might still front a daemon.
TailscaleCommandSpawnError: (error) =>
isMissingCli(error.cause) ? Effect.succeed(NOTHING_FORWARDED) : Effect.fail(error),
TailscaleCommandExitError: (error) =>
NOTHING_TO_CLOSE.has(error.stderrDiagnostic)
? Effect.succeed(NOTHING_FORWARDED)
: Effect.fail(error),
}),
// Tailscale errors carry only labelled diagnostics, never raw stderr, so they are safe to log.
Effect.tapError((cause) =>
Effect.logWarning("Tailscale Serve could not be checked", { cause }),
),
Effect.mapError((error) =>
refuseRemoteClients(
policy,
error._tag === "TailscaleCommandTimeoutError"
? // Seen on a Mac where Tailscale is installed but was never set up.
"so the server does not start until Tailscale Serve can be checked, and Tailscale did not answer; open Tailscale and finish setting it up, or remove it"
: "so the server does not start until Tailscale Serve can be checked",
),
),
);
const found = yield* readRoutes;
if (found.length === 0) return;
// `off` acts on the machine's current name, so a web route under a former name stays.
const currentName = found.some((route) => route.mount !== undefined && route.owner === undefined)
? yield* readTailscaleStatus.pipe(
Effect.map((status) => status.magicDnsName?.toLowerCase() ?? null),
Effect.orElseSucceed(() => null),
)
: null;
const formerName = (route: TailscaleServeRoute) =>
route.owner === undefined &&
route.host !== undefined &&
currentName !== null &&
route.host.toLowerCase() !== currentName;
// A Service is shared across the tailnet, and a foreground route lives as long as its command.
const removable = (route: TailscaleServeRoute) =>
route.owner === undefined &&
route.ambiguous === undefined &&
(route.mount === undefined || (currentName !== null && !formerName(route)));
for (const route of found.filter(removable)) {
yield* (
route.mount === undefined
? disableTailscaleServeTcp(route.servePort)
: disableTailscaleServe(route)
).pipe(
Effect.catch((cause) =>
Effect.logWarning("Tailscale Serve route could not be turned off", { ...route, cause }),
),
);
}
const left = yield* readRoutes;
if (left.length > 0) {
const describe = (route: TailscaleServeRoute) => {
const where = `port ${route.servePort}${route.mount === undefined ? "" : ` at ${route.mount}`} here`;
const off = tailscaleServeOffArgs(route);
if (off === undefined) return `${where}; stop the "tailscale serve" command that runs it`;
if (formerName(route)) {
return `${where} under ${route.host}, a former name of this machine, which "tailscale serve ... off" cannot reach; run "tailscale serve reset" and set up your other routes again`;
}
const command = `"tailscale ${off.join(" ")}"`;
// Without the current name, `off` might act on another name's route at this path.
if (currentName === null && route.owner === undefined && route.host !== undefined) {
return `${where} under ${route.host}; if that is this machine's current name, turn it off with ${command}`;
}
return route.ambiguous !== undefined
? `${where}, and its target ${route.ambiguous} might be another service on this port; check it, then turn it off with ${command}`
: `${where}; turn it off with ${command}`;
};
return yield* refuseRemoteClients(
policy,
`so the server does not start while Tailscale Serve forwards ${left.map(describe).join(", and ")}`,
);
}
yield* Effect.logInfo("Turned off Tailscale Serve the managed policy does not allow", {
routes: found,
});
});
export const refuseRemoteClients = (policy: PolicyDocument, detail?: string) =>
refuse(policy, AUTH_POLICY_ACTION["remote-reachable"], detail);
/**
* Fails when the server would accept clients the policy leaves out; otherwise returns the address
* to bind. Runs before the server binds, so a refused configuration never listens, not even
* briefly. Without `remote` that address is the loopback literal the check approved, never a name.
*/
export const checkServerReachability = Effect.fn("managedLimits.checkServerReachability")(
function* (
policy: PolicyDocument,
input: {
readonly authPolicy: ServerAuthPolicy;
readonly host: string | undefined;
readonly tailscaleServeEnabled: boolean;
/** The background service chose `host` to reach private networks; the user did not. */
readonly privateNetworkOnly?: boolean | undefined;
},
) {
const cannotListen = () =>
refuseRemoteClients(
policy,
input.privateNetworkOnly === true
? "so the background service cannot listen on private networks; set T3CODE_HOST=127.0.0.1 in its service.env"
: `so the server cannot listen on ${input.host === "" ? "every interface" : (input.host ?? "this host")}`,
);
if (!allowsAuthPolicy(policy, input.authPolicy)) {
return yield* input.authPolicy === "remote-reachable"
? cannotListen()
: refuse(policy, AUTH_POLICY_ACTION[input.authPolicy]);
}
if (allowsRemoteClients(policy)) return input.host ?? "127.0.0.1";
if (input.tailscaleServeEnabled) {
return yield* refuseRemoteClients(policy, "so Tailscale Serve cannot be turned on");
}
return loopbackBindAddress(input.host) ?? (yield* cannotListen());
},
);