Repository navigation
Expand file tree
/
Copy pathmanagedLimits.test.ts
More file actions
411 lines (399 loc) · 17.6 KB
/
Copy pathmanagedLimits.test.ts
File metadata and controls
411 lines (399 loc) · 17.6 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import type { ManagedPolicyDocument } from "@t3tools/contracts";
import { assert, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as PlatformError from "effect/PlatformError";
import * as Sink from "effect/Sink";
import * as Stream from "effect/Stream";
import * as TestClock from "effect/testing/TestClock";
import { ChildProcessSpawner } from "effect/process";
import * as NetService from "@t3tools/shared/Net";
import * as EnvironmentAuthPolicy from "../auth/EnvironmentAuthPolicy.ts";
import * as ManagedLimits from "./managedLimits.ts";
import * as ManagedPolicy from "./ManagedPolicy.ts";
const policy = (limits?: ManagedPolicyDocument["limits"]) => ({
document: {
schemaVersion: 1 as const,
organization: "Example Corp",
...(limits === undefined ? {} : { limits }),
},
});
const desktopAndLocal = policy({ clients: ["desktop", "local-browser"] });
it.effect("refuses to listen on a non-loopback host when remote clients are not allowed", () =>
Effect.gen(function* () {
const error = yield* ManagedLimits.checkServerReachability(desktopAndLocal, {
authPolicy: "remote-reachable",
host: "0.0.0.0",
tailscaleServeEnabled: false,
}).pipe(Effect.flip);
assert.equal(
error.message,
"Example Corp's policy does not allow remote clients, so the server cannot listen on 0.0.0.0.",
);
}),
);
it.effect("binds only the loopback address it approved when remote clients are not allowed", () =>
Effect.gen(function* () {
const bind = (
host: string | undefined,
policy: { readonly document: ManagedPolicyDocument | undefined } = desktopAndLocal,
) =>
ManagedLimits.checkServerReachability(policy, {
authPolicy: EnvironmentAuthPolicy.resolveServerAuthPolicy({ mode: "web", host }),
host,
tailscaleServeEnabled: false,
});
// Node listens on every interface for an empty host, and a name resolves through DNS.
for (const host of ["", "127.office.example", "127.0.0.1e0", "0.0.0.0"]) {
const error = yield* bind(host).pipe(Effect.flip);
assert.include(error.message, "does not allow remote clients", host);
}
for (const [host, address] of [
[undefined, "127.0.0.1"],
["localhost", "127.0.0.1"],
["127.0.0.2", "127.0.0.2"],
["[::1]", "::1"],
] as const) {
assert.equal(yield* bind(host), address);
}
const service = yield* ManagedLimits.checkServerReachability(desktopAndLocal, {
authPolicy: "remote-reachable",
host: "0.0.0.0",
tailscaleServeEnabled: false,
privateNetworkOnly: true,
}).pipe(Effect.flip);
assert.include(service.message, "set T3CODE_HOST=127.0.0.1 in its service.env");
assert.equal(yield* bind("localhost", { document: undefined }), "localhost");
assert.equal(yield* bind(undefined, { document: undefined }), "127.0.0.1");
}),
);
it.effect("refuses Tailscale Serve when remote clients are not allowed", () =>
Effect.gen(function* () {
const error = yield* ManagedLimits.checkServerReachability(desktopAndLocal, {
authPolicy: "loopback-browser",
host: undefined,
tailscaleServeEnabled: true,
}).pipe(Effect.flip);
assert.include(error.message, "Tailscale Serve");
}),
);
it.effect("refuses an auth policy the clients list leaves out", () =>
Effect.gen(function* () {
const error = yield* ManagedLimits.checkServerReachability(policy({ clients: ["desktop"] }), {
authPolicy: "loopback-browser",
host: undefined,
tailscaleServeEnabled: false,
}).pipe(Effect.flip);
assert.equal(error.message, "Example Corp's policy does not allow browsers on this machine.");
}),
);
it.effect("refuses unsafe-no-auth under any policy file, even one without limits", () =>
Effect.gen(function* () {
const error = yield* ManagedLimits.checkServerReachability(policy(), {
authPolicy: "unsafe-no-auth",
host: undefined,
tailscaleServeEnabled: false,
}).pipe(Effect.flip);
assert.include(error.message, "without authentication");
assert.isTrue(ManagedLimits.allowsAuthPolicy({ document: undefined }, "unsafe-no-auth"));
}),
);
it.effect("allows the clients the policy lists", () =>
ManagedLimits.checkServerReachability(policy({ clients: ["remote"] }), {
authPolicy: "remote-reachable",
host: "0.0.0.0",
tailscaleServeEnabled: true,
}),
);
it.effect("tells clients Connect is off when the policy leaves out remote clients", () =>
Effect.gen(function* () {
const managed = yield* ManagedPolicy.fromDocument("/test/policy.json", {
...desktopAndLocal.document,
});
assert.deepEqual(managed.summary?.limits, {
clients: ["desktop", "local-browser"],
connect: false,
});
assert.isTrue(ManagedLimits.allowsConnect(policy({ clients: ["remote"] })));
assert.isFalse(ManagedLimits.allowsConnect(policy({ connect: false })));
}),
);
it.effect(
"turns off only the Tailscale Serve routes that forward here, and refuses to start if one stays",
() => {
type Web = Record<string, Record<string, string>>;
interface Answers {
/** Serve handlers by host:port, then mount, to proxy target. */
readonly web?: Web;
/** TCP forwards by port. */
readonly tcp?: Record<string, string>;
/** The name `off` acts on: the machine's current one. */
readonly currentName?: string;
readonly status?: {
readonly stdout: string;
readonly exitCode: number;
readonly stderr?: string;
};
readonly offExitCode?: number;
/** `serve status` never answers, as on a Mac where Tailscale was never set up. */
readonly statusHangs?: boolean;
/** `off` exits as told but leaves the config alone. */
readonly offIgnored?: boolean;
}
// Acts as the real CLI: `serve status --json` prints the config; `serve --https=<port> off`
// drops every handler on that port of the current name unless `--set-path` names one
// (serve_v2.go removeWebServe), and `serve --tcp=<port> off` drops that port's forward.
const close = (answers: Answers, offCalls: Array<ReadonlyArray<string>> = []) => {
const web: Web = answers.web ?? {
"box.tailnet.ts.net:443": { "/": "http://127.0.0.1:3773" },
};
const tcp = answers.tcp ?? {};
const currentName = answers.currentName ?? "box.tailnet.ts.net";
return ManagedLimits.closeTailscaleServeTo(desktopAndLocal, {
host: "127.0.0.1",
port: 3773,
}).pipe(
Effect.provideService(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make((command) => {
const { args } = command as unknown as { readonly args: ReadonlyArray<string> };
// `tailscale status --json` names the machine; `tailscale serve status --json` routes.
if (args[0] === "status") {
return Effect.succeed(
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.stringify({ Self: { DNSName: `${currentName}.` } }),
),
),
stderr: Stream.empty,
all: Stream.empty,
getInputFd: () => Sink.drain,
getOutputFd: () => Stream.empty,
}),
);
}
const isStatus = args.includes("status");
if (!isStatus) {
offCalls.push(args);
const port = args.find((arg) => arg.startsWith("--https="))?.slice(8);
const tcpPort = args.find((arg) => arg.startsWith("--tcp="))?.slice(6);
const mount = args.find((arg) => arg.startsWith("--set-path="))?.slice(11);
const site = web[`${currentName}:${port}`];
if (answers.offIgnored !== true) {
if (tcpPort !== undefined) delete tcp[tcpPort];
for (const handler of Object.keys(site ?? {})) {
if (mount === undefined || mount === handler) delete site![handler];
}
}
}
const status = answers.status ?? {
stdout: JSON.stringify({
TCP: Object.fromEntries(
Object.entries(tcp).map(([port, forward]) => [port, { TCPForward: forward }]),
),
Web: Object.fromEntries(
Object.entries(web).map(([hostPort, handlers]) => [
hostPort,
{
Handlers: Object.fromEntries(
Object.entries(handlers).map(([mount, proxy]) => [mount, { Proxy: proxy }]),
),
},
]),
),
}),
exitCode: 0,
};
const text = (value: string | undefined) =>
value === undefined ? Stream.empty : Stream.make(new TextEncoder().encode(value));
return Effect.succeed(
ChildProcessSpawner.makeHandle({
pid: ChildProcessSpawner.ProcessId(1),
exitCode:
isStatus && answers.statusHangs === true
? Effect.never
: Effect.succeed(
ChildProcessSpawner.ExitCode(
isStatus ? status.exitCode : (answers.offExitCode ?? 0),
),
),
isRunning: Effect.succeed(false),
kill: () => Effect.void,
unref: Effect.succeed(Effect.void),
stdin: Sink.drain,
stdout: isStatus ? text(status.stdout) : Stream.empty,
stderr: isStatus ? text(status.stderr) : Stream.empty,
all: Stream.empty,
getInputFd: () => Sink.drain,
getOutputFd: () => Stream.empty,
}),
);
}),
),
);
};
const portFree = { ...NetService.make(), canListenOnHost: () => Effect.succeed(true) };
const portHeld = { ...portFree, canListenOnHost: () => Effect.succeed(false) };
return Effect.gen(function* () {
// A route that shares the port with this server's stays, as does one to another loopback
// address on the same port; a TCP forward here goes too.
for (const mount of ["/t3", "/"]) {
const web: Web = {
"box.tailnet.ts.net:443": {
[mount]: "http://127.0.0.1:3773",
"/dashboard": "http://127.0.0.1:9000",
"/other": "http://127.0.0.2:3773",
},
};
const tcp: Record<string, string> = { "7000": "127.0.0.1:3773" };
const offCalls: Array<ReadonlyArray<string>> = [];
yield* close({ web, tcp }, offCalls);
assert.deepEqual(web, {
"box.tailnet.ts.net:443": {
"/dashboard": "http://127.0.0.1:9000",
"/other": "http://127.0.0.2:3773",
},
});
assert.deepEqual(tcp, {});
assert.deepEqual(offCalls, [
["serve", "--tcp=7000", "off"],
["serve", "--https=443", `--set-path=${mount}`, "off"],
]);
}
// `off` acts on the current name: a route under a former one is left, with how to clear it,
// while one under the current name goes even if a former name has routes too.
const renamed: Web = {
"old.tailnet.ts.net:443": { "/": "http://127.0.0.1:3773" },
"new.tailnet.ts.net:443": { "/": "http://127.0.0.1:9000" },
};
const renamedOffCalls: Array<ReadonlyArray<string>> = [];
const former = yield* close(
{ web: renamed, currentName: "new.tailnet.ts.net" },
renamedOffCalls,
).pipe(Effect.flip);
assert.deepEqual(renamedOffCalls, []);
assert.deepEqual(renamed["new.tailnet.ts.net:443"], { "/": "http://127.0.0.1:9000" });
assert.include(
former.message,
'under old.tailnet.ts.net, a former name of this machine, which "tailscale serve ... off" cannot reach; run "tailscale serve reset"',
);
const current: Web = {
"old.tailnet.ts.net:443": { "/": "http://127.0.0.1:9000" },
"new.tailnet.ts.net:443": { "/": "http://127.0.0.1:3773" },
};
yield* close({ web: current, currentName: "new.tailnet.ts.net" });
assert.deepEqual(current, {
"old.tailnet.ts.net:443": { "/": "http://127.0.0.1:9000" },
"new.tailnet.ts.net:443": {},
});
// A `localhost` target might reach another service on the other loopback, so it stays.
const localhostOffCalls: Array<ReadonlyArray<string>> = [];
const localhost = yield* close(
{ web: { "box.tailnet.ts.net:443": { "/": "http://localhost:3773" } } },
localhostOffCalls,
).pipe(Effect.flip);
assert.deepEqual(localhostOffCalls, []);
assert.include(localhost.message, "its target localhost might be another service");
// Only a fresh read confirms the route is gone, whatever `off` exits with.
for (const answers of [{ offIgnored: true }, { offIgnored: true, offExitCode: 1 }]) {
const stuck = yield* close(answers).pipe(Effect.flip);
assert.include(
stuck.message,
'Tailscale Serve forwards port 443 at / here; turn it off with "tailscale serve --https=443 --set-path=/ off"',
);
}
// Routes the server must not remove itself: a Service's, and a foreground command's.
const owners = {
stdout: JSON.stringify({
Foreground: {
session1: {
Web: { "box:443": { Handlers: { "/": { Proxy: "http://127.0.0.1:3773" } } } },
},
},
Services: {
"svc:t3": {
Web: { "t3:443": { Handlers: { "/t3": { Proxy: "http://127.0.0.1:3773" } } } },
},
},
}),
exitCode: 0,
};
const ownedOffCalls: Array<ReadonlyArray<string>> = [];
const owned = yield* close({ status: owners }, ownedOffCalls).pipe(Effect.flip);
assert.deepEqual(ownedOffCalls, []);
assert.include(owned.message, 'stop the "tailscale serve" command that runs it');
assert.include(
owned.message,
'"tailscale serve --service=svc:t3 --https=443 --set-path=/t3 off"',
);
// An answer that cannot be read might hide a mapping, so the server does not start.
const dialFailure = (reason: string) =>
`Failed to connect to local Tailscale daemon for /localapi/v0/serve-config; not running? Error: dial unix /var/run/tailscale/tailscaled.sock: connect: ${reason}`;
for (const status of [
{ stdout: "", exitCode: 1, stderr: "something unexpected" },
{ stdout: "not json", exitCode: 0 },
{ stdout: "", exitCode: 1, stderr: "Access denied: serve config denied" },
{ stdout: "", exitCode: 1, stderr: dialFailure("permission denied") },
]) {
const unread = yield* close({ status }).pipe(Effect.flip);
assert.include(unread.message, "does not start until Tailscale Serve can be checked");
}
// A Tailscale that never answers is not read as nothing to close, and the refusal says why.
const hung = yield* close({ statusHangs: true }).pipe(Effect.flip, Effect.forkChild);
yield* TestClock.adjust("10 seconds");
assert.include((yield* Fiber.join(hung)).message, "Tailscale did not answer");
// Tailscale down, logged out, or not installed forwards nothing now.
for (const stderr of [
dialFailure("connection refused"),
"NeedsLogin: not logged in",
String.raw`Get "http://local-tailscaled.sock/localapi/v0/serve-config": open \\.\pipe\ProtectedPrefix\Administrators\Tailscale\tailscaled: The system cannot find the file specified.`,
]) {
yield* close({ status: { stdout: "", exitCode: 1, stderr } });
}
const spawnFailing = (tag: "NotFound" | "PermissionDenied") =>
ManagedLimits.closeTailscaleServeTo(desktopAndLocal, {
host: "127.0.0.1",
port: 3773,
}).pipe(
Effect.provideService(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make(() =>
Effect.fail(
PlatformError.systemError({ _tag: tag, module: "ChildProcess", method: "spawn" }),
),
),
),
);
yield* spawnFailing("NotFound");
// Node throws this instead when PATH ends in a file.
yield* ManagedLimits.closeTailscaleServeTo(desktopAndLocal, {
host: "127.0.0.1",
port: 3773,
}).pipe(
Effect.provideService(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make(() =>
Effect.die(Object.assign(new Error("spawn tailscale ENOTDIR"), { code: "ENOTDIR" })),
),
),
);
// A CLI that is there but will not run might still front a daemon with a mapping.
yield* spawnFailing("PermissionDenied").pipe(Effect.flip);
// Another app holds the port: its route is not ours, and it could let go before the bind.
const heldOffCalls: Array<ReadonlyArray<string>> = [];
const held = yield* close({}, heldOffCalls).pipe(
Effect.provideService(NetService.NetService, portHeld),
Effect.flip,
);
assert.deepEqual(heldOffCalls, []);
assert.include(held.message, "while it cannot bind 127.0.0.1:3773");
}).pipe(Effect.provideService(NetService.NetService, portFree));
},
);