Open History to see the last commit
1import { EnvironmentId } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3import * as Option from "effect/Option";
4
5import {
6 BearerConnectionProfile,
7 type ConnectionCatalogEntry,
8 type ConnectionRoute,
9} from "./catalog.ts";
10import { pullRequestRoutingConnectionKey } from "./pullRequestRoutingPermissions.ts";
11import { BearerConnectionTarget, RelayConnectionTarget } from "./model.ts";
12import {
13 connectionRouteId,
14 connectionRouteKind,
15 connectionRouteLabel,
16 credentialConnectionId,
17 insertRoute,
18 entryWithRoutes,
19 mergeLearnedRoutes,
20 pendingRouteOrder,
21 removedWithRelay,
22 routesAfterRemoving,
23 sendsTokenInClear,
24 upsertRoute,
25 usableRoutes,
26} from "./routes.ts";
27
28const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
29
30function direct(id: string, httpBaseUrl: string): ConnectionRoute {
31 return {
32 target: new BearerConnectionTarget({
33 environmentId: ENVIRONMENT_ID,
34 label: "Desk",
35 connectionId: id,
36 }),
37 profile: Option.some(
38 new BearerConnectionProfile({
39 connectionId: id,
40 environmentId: ENVIRONMENT_ID,
41 label: "Desk",
42 httpBaseUrl,
43 wsBaseUrl: httpBaseUrl.replace(/^http/, "ws"),
44 }),
45 ),
46 };
47}
48
49const RELAY: ConnectionRoute = {
50 target: new RelayConnectionTarget({ environmentId: ENVIRONMENT_ID, label: "Desk" }),
51 profile: Option.none(),
52};
53const LAN = direct("lan", "http://192.168.1.10:3773/");
54const TAILNET = direct("tailnet", "https://desk.tail1234.ts.net/");
55const PUBLIC = direct("public", "https://desk.example.com/");
56
57function learned(id: string, httpBaseUrl: string): ConnectionRoute {
58 const route = direct(id, httpBaseUrl);
59 const profile = Option.getOrThrow(route.profile) as BearerConnectionProfile;
60 return {
61 target: route.target,
62 profile: Option.some(new BearerConnectionProfile({ ...profile, learned: true })),
63 };
64}
65
66describe("connection routes", () => {
67 it("classifies direct routes by address", () => {
68 expect(connectionRouteKind(LAN)).toBe("lan");
69 // Tailscale, Cloudflare WARP and Mesh, and other VPNs share 100.64.0.0/10.
70 expect(connectionRouteKind(direct("ip", "http://100.101.102.103:3773/"))).toBe("vpn");
71 expect(connectionRouteLabel(direct("mesh", "http://100.96.0.1:3773/"))).toBe("VPN");
72 expect(connectionRouteKind(TAILNET)).toBe("tailnet");
73 expect(connectionRouteKind(PUBLIC)).toBe("public");
74 expect(connectionRouteKind(direct("lo", "http://127.0.0.1:3773/"))).toBe("loopback");
75 expect(connectionRouteKind(direct("ts6", "http://[fd7a:115c:a1e0::1]:3773/"))).toBe("tailnet");
76 expect(connectionRouteLabel(TAILNET)).toBe("Tailscale");
77 expect(connectionRouteLabel(RELAY)).toBe("T3 Connect");
78 });
79
80 it("places a new route after faster kinds and ahead of T3 Connect", () => {
81 expect(insertRoute([RELAY], LAN)).toEqual([LAN, RELAY]);
82 expect(insertRoute([LAN, RELAY], TAILNET)).toEqual([LAN, TAILNET, RELAY]);
83 expect(insertRoute([TAILNET], LAN)).toEqual([LAN, TAILNET]);
84 expect(insertRoute([LAN, TAILNET], RELAY)).toEqual([LAN, TAILNET, RELAY]);
85 });
86
87 it("keeps a user's order when a saved route is replaced", () => {
88 const repaired = direct("lan", "http://192.168.1.11:3773/");
89 expect(upsertRoute([RELAY, LAN], repaired)).toEqual([RELAY, repaired]);
90 });
91
92 it("shows a dragged order only until the saved routes catch up or change", () => {
93 const dragged = ["tailnet", "lan"];
94 expect(pendingRouteOrder(dragged, ["lan", "tailnet"])).toBe(dragged);
95 expect(pendingRouteOrder(dragged, ["tailnet", "lan"])).toBeNull();
96 expect(pendingRouteOrder(dragged, ["lan", "public"])).toBeNull();
97 expect(pendingRouteOrder(dragged, ["lan", "tailnet", "public"])).toBeNull();
98 });
99});
100
101describe("learned routes", () => {
102 const relayOnly: ConnectionCatalogEntry = {
103 target: RELAY.target,
104 profile: RELAY.profile,
105 enabled: true,
106 };
107 const ids = (routes: ReadonlyArray<ConnectionRoute> | null) =>
108 routes?.map((route) => connectionRouteId(route.target)) ?? null;
109 const profileOf = (route: ConnectionRoute) => Option.getOrThrow(route.profile);
110
111 it("learns a LAN address over T3 Connect, ahead of it, using the T3 Connect credential", () => {
112 const routes = mergeLearnedRoutes({
113 entry: relayOnly,
114 activeRoute: RELAY,
115 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
116 allowInsecure: true,
117 });
118 expect(ids(routes)).toEqual([`learned:${ENVIRONMENT_ID}:http://192.168.1.10:3773`, "relay"]);
119 expect(profileOf(routes![0]!)).toMatchObject({
120 learned: true,
121 authorization: "t3-connect",
122 wsBaseUrl: "ws://192.168.1.10:3773/",
123 });
124 });
125
126 it("replaces a learned LAN address when the server reports a new one", () => {
127 const first = mergeLearnedRoutes({
128 entry: relayOnly,
129 activeRoute: RELAY,
130 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
131 allowInsecure: true,
132 })!;
133 const entry: ConnectionCatalogEntry = {
134 ...relayOnly,
135 target: first[0]!.target,
136 profile: first[0]!.profile,
137 alternateRoutes: first.slice(1),
138 };
139 const moved = mergeLearnedRoutes({
140 entry,
141 activeRoute: RELAY,
142 reported: [{ httpBaseUrl: "http://10.0.0.42:3773/" }],
143 allowInsecure: true,
144 });
145 expect(ids(moved)).toEqual([`learned:${ENVIRONMENT_ID}:http://10.0.0.42:3773`, "relay"]);
146 });
147
148 it("keeps a learned route where the user moved it while the server reports it", () => {
149 const lan = { httpBaseUrl: "http://192.168.1.10:3773/" };
150 const first = mergeLearnedRoutes({
151 entry: relayOnly,
152 activeRoute: RELAY,
153 reported: [lan],
154 allowInsecure: true,
155 })!;
156 const reordered = entryWithRoutes(relayOnly, [first[1]!, first[0]!]);
157 expect(
158 mergeLearnedRoutes({
159 entry: reordered,
160 activeRoute: RELAY,
161 reported: [lan],
162 allowInsecure: true,
163 }),
164 ).toBeNull();
165 const next = mergeLearnedRoutes({
166 entry: reordered,
167 activeRoute: RELAY,
168 reported: [lan, { httpBaseUrl: "http://100.101.102.103:3773/" }],
169 allowInsecure: true,
170 });
171 expect(ids(next)).toEqual([
172 `learned:${ENVIRONMENT_ID}:http://100.101.102.103:3773`,
173 "relay",
174 `learned:${ENVIRONMENT_ID}:http://192.168.1.10:3773`,
175 ]);
176 });
177
178 it("labels a learned address Tailscale only while the server reports it as tailnet", () => {
179 const tailscale = { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" };
180 const mesh = { kind: "lan", httpBaseUrl: "http://100.96.0.1:3773/" };
181 const first = mergeLearnedRoutes({
182 entry: relayOnly,
183 activeRoute: RELAY,
184 reported: [tailscale, mesh],
185 allowInsecure: true,
186 })!;
187 expect(first.map(connectionRouteLabel)).toEqual(["Tailscale", "VPN", "T3 Connect"]);
188
189 const corrected = mergeLearnedRoutes({
190 entry: entryWithRoutes(relayOnly, first),
191 activeRoute: RELAY,
192 reported: [{ ...tailscale, kind: "lan" }, mesh],
193 allowInsecure: true,
194 })!;
195 expect(ids(corrected)).toEqual(ids(first));
196 expect(corrected.map(connectionRouteLabel)).toEqual(["VPN", "VPN", "T3 Connect"]);
197 expect(
198 mergeLearnedRoutes({
199 entry: entryWithRoutes(relayOnly, corrected),
200 activeRoute: RELAY,
201 reported: [{ ...tailscale, kind: "lan" }, mesh],
202 allowInsecure: true,
203 }),
204 ).toBeNull();
205 });
206
207 it("leaves user routes alone and does not learn an address already saved", () => {
208 const entry: ConnectionCatalogEntry = {
209 target: LAN.target,
210 profile: LAN.profile,
211 alternateRoutes: [RELAY],
212 enabled: true,
213 };
214 expect(
215 mergeLearnedRoutes({
216 entry,
217 activeRoute: RELAY,
218 reported: [{ httpBaseUrl: "http://192.168.1.10:3773" }],
219 allowInsecure: true,
220 }),
221 ).toBeNull();
222 expect(
223 mergeLearnedRoutes({ entry, activeRoute: RELAY, reported: [], allowInsecure: true }),
224 ).toBeNull();
225 });
226
227 it("borrows the paired token when learned over a bearer route", () => {
228 const entry: ConnectionCatalogEntry = {
229 ...relayOnly,
230 target: LAN.target,
231 profile: LAN.profile,
232 };
233 const routes = mergeLearnedRoutes({
234 entry,
235 activeRoute: LAN,
236 reported: [{ httpBaseUrl: "https://desk.tail1234.ts.net/" }],
237 allowInsecure: true,
238 })!;
239 const learned = routes.find((route) => connectionRouteKind(route) === "tailnet")!;
240 expect(profileOf(learned)).not.toHaveProperty("authorization");
241 expect(credentialConnectionId(connectionRouteId(learned.target))).toBe("lan");
242 });
243
244 it("keeps a token paired over HTTPS off plain HTTP addresses outside the tailnet", () => {
245 const entry = entryWithRoutes(relayOnly, [PUBLIC, RELAY]);
246 const routes = mergeLearnedRoutes({
247 entry,
248 activeRoute: PUBLIC,
249 reported: [
250 { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" },
251 { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" },
252 { kind: "tailnet", httpBaseUrl: "https://desk.tail1234.ts.net/" },
253 ],
254 allowInsecure: true,
255 });
256 expect(ids(routes)).toEqual([
257 `learned:${ENVIRONMENT_ID}:http://100.101.102.103:3773@public`,
258 `learned:${ENVIRONMENT_ID}:https://desk.tail1234.ts.net@public`,
259 "public",
260 "relay",
261 ]);
262 });
263
264 it("drops a stale plain HTTP route borrowing an HTTPS-paired token while another route is active", () => {
265 const lanOrigin = "http://10.0.0.42:3773";
266 const stale = learned(`learned:${ENVIRONMENT_ID}:${lanOrigin}@public`, `${lanOrigin}/`);
267 const entry = entryWithRoutes(relayOnly, [stale, PUBLIC, RELAY]);
268 expect(sendsTokenInClear([stale, PUBLIC, RELAY], stale)).toBe(true);
269 expect(ids(usableRoutes(entry))).toEqual(["public", "relay"]);
270 const routes = mergeLearnedRoutes({
271 entry,
272 activeRoute: RELAY,
273 reported: [{ kind: "lan", httpBaseUrl: `${lanOrigin}/` }],
274 allowInsecure: true,
275 });
276 // The stale route is gone; the address is learned again with T3 Connect's credential.
277 expect(ids(routes)).toEqual([`learned:${ENVIRONMENT_ID}:${lanOrigin}`, "public", "relay"]);
278 expect(profileOf(routes![0]!)).toMatchObject({ authorization: "t3-connect" });
279 });
280
281 it("keeps plain HTTP routes that don't borrow the active HTTPS-paired token", () => {
282 const lanOrigin = "http://10.0.0.42:3773";
283 const overLan = learned(`learned:${ENVIRONMENT_ID}:${lanOrigin}@lan`, `${lanOrigin}/`);
284 const routes = mergeLearnedRoutes({
285 entry: entryWithRoutes(relayOnly, [overLan, LAN, PUBLIC, RELAY]),
286 activeRoute: PUBLIC,
287 reported: [{ kind: "lan", httpBaseUrl: `${lanOrigin}/` }],
288 allowInsecure: true,
289 });
290 expect(routes).toBeNull();
291 });
292
293 it("learns plain HTTP LAN addresses over a token paired over plain HTTP", () => {
294 const entry = entryWithRoutes(relayOnly, [LAN, RELAY]);
295 const routes = mergeLearnedRoutes({
296 entry,
297 activeRoute: LAN,
298 reported: [{ kind: "lan", httpBaseUrl: "http://10.0.0.42:3773/" }],
299 allowInsecure: true,
300 });
301 expect(ids(routes)).toEqual([
302 "lan",
303 `learned:${ENVIRONMENT_ID}:http://10.0.0.42:3773@lan`,
304 "relay",
305 ]);
306 });
307
308 it("skips plain HTTP from an HTTPS page and never learns loopback", () => {
309 expect(
310 mergeLearnedRoutes({
311 entry: relayOnly,
312 activeRoute: RELAY,
313 reported: [
314 { httpBaseUrl: "http://192.168.1.10:3773/" },
315 { httpBaseUrl: "http://127.0.0.1:3773/" },
316 ],
317 allowInsecure: false,
318 }),
319 ).toBeNull();
320 });
321
322 it("removes learned routes along with the route whose credential they borrow", () => {
323 const overRelay = mergeLearnedRoutes({
324 entry: relayOnly,
325 activeRoute: RELAY,
326 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
327 allowInsecure: true,
328 })!;
329 expect(routesAfterRemoving(overRelay, "relay")).toEqual([]);
330
331 const paired: ConnectionCatalogEntry = {
332 ...relayOnly,
333 target: LAN.target,
334 profile: LAN.profile,
335 alternateRoutes: [RELAY],
336 };
337 const overLan = mergeLearnedRoutes({
338 entry: paired,
339 activeRoute: LAN,
340 reported: [{ httpBaseUrl: "https://desk.tail1234.ts.net/" }],
341 allowInsecure: true,
342 })!;
343 expect(ids(routesAfterRemoving(overLan, "lan"))).toEqual(["relay"]);
344 expect(ids(routesAfterRemoving(overLan, "relay"))).toHaveLength(2);
345 });
346
347 it("counts an environment reached only through T3 Connect as removed with it", () => {
348 const learned = mergeLearnedRoutes({
349 entry: relayOnly,
350 activeRoute: RELAY,
351 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
352 allowInsecure: true,
353 })!;
354 expect(removedWithRelay(relayOnly)).toBe(true);
355 expect(removedWithRelay(entryWithRoutes(relayOnly, learned))).toBe(true);
356 expect(removedWithRelay(entryWithRoutes(relayOnly, [LAN, RELAY]))).toBe(false);
357 });
358
359 it("keeps pull request routing trust when a route is learned", () => {
360 const learned = mergeLearnedRoutes({
361 entry: relayOnly,
362 activeRoute: RELAY,
363 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
364 allowInsecure: true,
365 })!;
366 const withLearned: ConnectionCatalogEntry = {
367 ...relayOnly,
368 target: learned[0]!.target,
369 profile: learned[0]!.profile,
370 alternateRoutes: learned.slice(1),
371 };
372 expect(pullRequestRoutingConnectionKey(withLearned)).toBe(
373 pullRequestRoutingConnectionKey(relayOnly),
374 );
375 });
376
377 it("keeps the T3 Connect credential when learning over a learned T3 Connect route", () => {
378 const first = mergeLearnedRoutes({
379 entry: relayOnly,
380 activeRoute: RELAY,
381 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
382 allowInsecure: true,
383 })!;
384 const learnedLan = first[0]!;
385 const entry: ConnectionCatalogEntry = {
386 ...relayOnly,
387 target: learnedLan.target,
388 profile: learnedLan.profile,
389 alternateRoutes: first.slice(1),
390 };
391 const next = mergeLearnedRoutes({
392 entry,
393 activeRoute: learnedLan,
394 reported: [
395 { httpBaseUrl: "http://192.168.1.10:3773/" },
396 { httpBaseUrl: "https://desk.tail1234.ts.net/" },
397 ],
398 allowInsecure: true,
399 })!;
400 for (const route of next.filter((candidate) => connectionRouteKind(candidate) !== "relay")) {
401 expect(profileOf(route)).toMatchObject({ authorization: "t3-connect" });
402 expect(connectionRouteId(route.target)).not.toContain("@");
403 }
404 });
405
406 it("saves a scheme change on the same host as a new address", () => {
407 const first = mergeLearnedRoutes({
408 entry: relayOnly,
409 activeRoute: RELAY,
410 reported: [{ httpBaseUrl: "http://desk.local:3773/" }],
411 allowInsecure: true,
412 })!;
413 const entry: ConnectionCatalogEntry = {
414 ...relayOnly,
415 target: first[0]!.target,
416 profile: first[0]!.profile,
417 alternateRoutes: first.slice(1),
418 };
419 const moved = mergeLearnedRoutes({
420 entry,
421 activeRoute: RELAY,
422 reported: [{ httpBaseUrl: "https://desk.local:3773/" }],
423 allowInsecure: true,
424 });
425 expect(moved).not.toBeNull();
426 expect(profileOf(moved![0]!)).toMatchObject({ httpBaseUrl: "https://desk.local:3773/" });
427 });
428});