Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

routes.test.ts

439 lines15.5 KB

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 };
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 };
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("puts a newly paired route ahead of saved routes of the same kind", () => {
93 const pasted = direct("lan-2", "http://192.168.1.11:3773/");
94 expect(upsertRoute([LAN, TAILNET], pasted)).toEqual([pasted, LAN, TAILNET]);
95 });
96​
97 it("keeps a newly paired route behind a different kind of the same rank", () => {
98 const vpn = direct("vpn", "http://100.101.102.103:3773/");
99 const pasted = direct("ts-2", "http://[fd7a:115c:a1e0::2]:3773/");
100 expect(upsertRoute([vpn, TAILNET], pasted)).toEqual([vpn, pasted, TAILNET]);
101 });
102​
103 it("shows a dragged order only until the saved routes catch up or change", () => {
104 const dragged = ["tailnet", "lan"];
105 expect(pendingRouteOrder(dragged, ["lan", "tailnet"])).toBe(dragged);
106 expect(pendingRouteOrder(dragged, ["tailnet", "lan"])).toBeNull();
107 expect(pendingRouteOrder(dragged, ["lan", "public"])).toBeNull();
108 expect(pendingRouteOrder(dragged, ["lan", "tailnet", "public"])).toBeNull();
109 });
110});
111​
112describe("learned routes", () => {
113 const relayOnly: ConnectionCatalogEntry = {
114 target: RELAY.target,
115 profile: RELAY.profile,
116 enabled: true,
117 };
118 const ids = (routes: ReadonlyArray<ConnectionRoute> | null) =>
119 routes?.map((route) => connectionRouteId(route.target)) ?? null;
120 const profileOf = (route: ConnectionRoute) => Option.getOrThrow(route.profile);
121​
122 it("learns a LAN address over T3 Connect, ahead of it, using the T3 Connect credential", () => {
123 const routes = mergeLearnedRoutes({
124 entry: relayOnly,
125 activeRoute: RELAY,
126 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
127 allowInsecure: true,
128 });
129 expect(ids(routes)).toEqual([`learned:${ENVIRONMENT_ID}:http://192.168.1.10:3773`, "relay"]);
130 expect(profileOf(routes![0]!)).toMatchObject({
131 learned: true,
132 authorization: "t3-connect",
133 wsBaseUrl: "ws://192.168.1.10:3773/",
134 });
135 });
136​
137 it("replaces a learned LAN address when the server reports a new one", () => {
138 const first = mergeLearnedRoutes({
139 entry: relayOnly,
140 activeRoute: RELAY,
141 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
142 allowInsecure: true,
143 })!;
144 const entry: ConnectionCatalogEntry = {
145 ...relayOnly,
146 target: first[0]!.target,
147 profile: first[0]!.profile,
148 alternateRoutes: first.slice(1),
149 };
150 const moved = mergeLearnedRoutes({
151 entry,
152 activeRoute: RELAY,
153 reported: [{ httpBaseUrl: "http://10.0.0.42:3773/" }],
154 allowInsecure: true,
155 });
156 expect(ids(moved)).toEqual([`learned:${ENVIRONMENT_ID}:http://10.0.0.42:3773`, "relay"]);
157 });
158​
159 it("keeps a learned route where the user moved it while the server reports it", () => {
160 const lan = { httpBaseUrl: "http://192.168.1.10:3773/" };
161 const first = mergeLearnedRoutes({
162 entry: relayOnly,
163 activeRoute: RELAY,
164 reported: [lan],
165 allowInsecure: true,
166 })!;
167 const reordered = entryWithRoutes(relayOnly, [first[1]!, first[0]!]);
168 expect(
169 mergeLearnedRoutes({
170 entry: reordered,
171 activeRoute: RELAY,
172 reported: [lan],
173 allowInsecure: true,
174 }),
175 ).toBeNull();
176 const next = mergeLearnedRoutes({
177 entry: reordered,
178 activeRoute: RELAY,
179 reported: [lan, { httpBaseUrl: "http://100.101.102.103:3773/" }],
180 allowInsecure: true,
181 });
182 expect(ids(next)).toEqual([
183 `learned:${ENVIRONMENT_ID}:http://100.101.102.103:3773`,
184 "relay",
185 `learned:${ENVIRONMENT_ID}:http://192.168.1.10:3773`,
186 ]);
187 });
188​
189 it("labels a learned address Tailscale only while the server reports it as tailnet", () => {
190 const tailscale = { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" };
191 const mesh = { kind: "lan", httpBaseUrl: "http://100.96.0.1:3773/" };
192 const first = mergeLearnedRoutes({
193 entry: relayOnly,
194 activeRoute: RELAY,
195 reported: [tailscale, mesh],
196 allowInsecure: true,
197 })!;
198 expect(first.map(connectionRouteLabel)).toEqual(["Tailscale", "VPN", "T3 Connect"]);
199​
200 const corrected = mergeLearnedRoutes({
201 entry: entryWithRoutes(relayOnly, first),
202 activeRoute: RELAY,
203 reported: [{ ...tailscale, kind: "lan" }, mesh],
204 allowInsecure: true,
205 })!;
206 expect(ids(corrected)).toEqual(ids(first));
207 expect(corrected.map(connectionRouteLabel)).toEqual(["VPN", "VPN", "T3 Connect"]);
208 expect(
209 mergeLearnedRoutes({
210 entry: entryWithRoutes(relayOnly, corrected),
211 activeRoute: RELAY,
212 reported: [{ ...tailscale, kind: "lan" }, mesh],
213 allowInsecure: true,
214 }),
215 ).toBeNull();
216 });
217​
218 it("leaves user routes alone and does not learn an address already saved", () => {
219 const entry: ConnectionCatalogEntry = {
220 target: LAN.target,
221 profile: LAN.profile,
222 alternateRoutes: [RELAY],
223 enabled: true,
224 };
225 expect(
226 mergeLearnedRoutes({
227 entry,
228 activeRoute: RELAY,
229 reported: [{ httpBaseUrl: "http://192.168.1.10:3773" }],
230 allowInsecure: true,
231 }),
232 ).toBeNull();
233 expect(
234 mergeLearnedRoutes({ entry, activeRoute: RELAY, reported: [], allowInsecure: true }),
235 ).toBeNull();
236 });
237​
238 it("borrows the paired token when learned over a bearer route", () => {
239 const entry: ConnectionCatalogEntry = {
240 ...relayOnly,
241 target: LAN.target,
242 profile: LAN.profile,
243 };
244 const routes = mergeLearnedRoutes({
245 entry,
246 activeRoute: LAN,
247 reported: [{ httpBaseUrl: "https://desk.tail1234.ts.net/" }],
248 allowInsecure: true,
249 })!;
250 const learned = routes.find((route) => connectionRouteKind(route) === "tailnet")!;
251 expect(profileOf(learned)).not.toHaveProperty("authorization");
252 expect(credentialConnectionId(connectionRouteId(learned.target))).toBe("lan");
253 });
254​
255 it("keeps a token paired over HTTPS off plain HTTP addresses outside the tailnet", () => {
256 const entry = entryWithRoutes(relayOnly, [PUBLIC, RELAY]);
257 const routes = mergeLearnedRoutes({
258 entry,
259 activeRoute: PUBLIC,
260 reported: [
261 { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" },
262 { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" },
263 { kind: "tailnet", httpBaseUrl: "https://desk.tail1234.ts.net/" },
264 ],
265 allowInsecure: true,
266 });
267 expect(ids(routes)).toEqual([
268 `learned:${ENVIRONMENT_ID}:http://100.101.102.103:3773@public`,
269 `learned:${ENVIRONMENT_ID}:https://desk.tail1234.ts.net@public`,
270 "public",
271 "relay",
272 ]);
273 });
274​
275 it("drops a stale plain HTTP route borrowing an HTTPS-paired token while another route is active", () => {
276 const lanOrigin = "http://10.0.0.42:3773";
277 const stale = learned(`learned:${ENVIRONMENT_ID}:${lanOrigin}@public`, `${lanOrigin}/`);
278 const entry = entryWithRoutes(relayOnly, [stale, PUBLIC, RELAY]);
279 expect(sendsTokenInClear([stale, PUBLIC, RELAY], stale)).toBe(true);
280 expect(ids(usableRoutes(entry))).toEqual(["public", "relay"]);
281 const routes = mergeLearnedRoutes({
282 entry,
283 activeRoute: RELAY,
284 reported: [{ kind: "lan", httpBaseUrl: `${lanOrigin}/` }],
285 allowInsecure: true,
286 });
287 // The stale route is gone; the address is learned again with T3 Connect's credential.
288 expect(ids(routes)).toEqual([`learned:${ENVIRONMENT_ID}:${lanOrigin}`, "public", "relay"]);
289 expect(profileOf(routes![0]!)).toMatchObject({ authorization: "t3-connect" });
290 });
291​
292 it("keeps plain HTTP routes that don't borrow the active HTTPS-paired token", () => {
293 const lanOrigin = "http://10.0.0.42:3773";
294 const overLan = learned(`learned:${ENVIRONMENT_ID}:${lanOrigin}@lan`, `${lanOrigin}/`);
295 const routes = mergeLearnedRoutes({
296 entry: entryWithRoutes(relayOnly, [overLan, LAN, PUBLIC, RELAY]),
297 activeRoute: PUBLIC,
298 reported: [{ kind: "lan", httpBaseUrl: `${lanOrigin}/` }],
299 allowInsecure: true,
300 });
301 expect(routes).toBeNull();
302 });
303​
304 it("learns plain HTTP LAN addresses over a token paired over plain HTTP", () => {
305 const entry = entryWithRoutes(relayOnly, [LAN, RELAY]);
306 const routes = mergeLearnedRoutes({
307 entry,
308 activeRoute: LAN,
309 reported: [{ kind: "lan", httpBaseUrl: "http://10.0.0.42:3773/" }],
310 allowInsecure: true,
311 });
312 expect(ids(routes)).toEqual([
313 "lan",
314 `learned:${ENVIRONMENT_ID}:http://10.0.0.42:3773@lan`,
315 "relay",
316 ]);
317 });
318​
319 it("skips plain HTTP from an HTTPS page and never learns loopback", () => {
320 expect(
321 mergeLearnedRoutes({
322 entry: relayOnly,
323 activeRoute: RELAY,
324 reported: [
325 { httpBaseUrl: "http://192.168.1.10:3773/" },
326 { httpBaseUrl: "http://127.0.0.1:3773/" },
327 ],
328 allowInsecure: false,
329 }),
330 ).toBeNull();
331 });
332​
333 it("removes learned routes along with the route whose credential they borrow", () => {
334 const overRelay = mergeLearnedRoutes({
335 entry: relayOnly,
336 activeRoute: RELAY,
337 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
338 allowInsecure: true,
339 })!;
340 expect(routesAfterRemoving(overRelay, "relay")).toEqual([]);
341​
342 const paired: ConnectionCatalogEntry = {
343 ...relayOnly,
344 target: LAN.target,
345 profile: LAN.profile,
346 alternateRoutes: [RELAY],
347 };
348 const overLan = mergeLearnedRoutes({
349 entry: paired,
350 activeRoute: LAN,
351 reported: [{ httpBaseUrl: "https://desk.tail1234.ts.net/" }],
352 allowInsecure: true,
353 })!;
354 expect(ids(routesAfterRemoving(overLan, "lan"))).toEqual(["relay"]);
355 expect(ids(routesAfterRemoving(overLan, "relay"))).toHaveLength(2);
356 });
357​
358 it("counts an environment reached only through T3 Connect as removed with it", () => {
359 const learned = mergeLearnedRoutes({
360 entry: relayOnly,
361 activeRoute: RELAY,
362 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
363 allowInsecure: true,
364 })!;
365 expect(removedWithRelay(relayOnly)).toBe(true);
366 expect(removedWithRelay(entryWithRoutes(relayOnly, learned))).toBe(true);
367 expect(removedWithRelay(entryWithRoutes(relayOnly, [LAN, RELAY]))).toBe(false);
368 });
369​
370 it("keeps pull request routing trust when a route is learned", () => {
371 const learned = mergeLearnedRoutes({
372 entry: relayOnly,
373 activeRoute: RELAY,
374 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
375 allowInsecure: true,
376 })!;
377 const withLearned: ConnectionCatalogEntry = {
378 ...relayOnly,
379 target: learned[0]!.target,
380 profile: learned[0]!.profile,
381 alternateRoutes: learned.slice(1),
382 };
383 expect(pullRequestRoutingConnectionKey(withLearned)).toBe(
384 pullRequestRoutingConnectionKey(relayOnly),
385 );
386 });
387​
388 it("keeps the T3 Connect credential when learning over a learned T3 Connect route", () => {
389 const first = mergeLearnedRoutes({
390 entry: relayOnly,
391 activeRoute: RELAY,
392 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
393 allowInsecure: true,
394 })!;
395 const learnedLan = first[0]!;
396 const entry: ConnectionCatalogEntry = {
397 ...relayOnly,
398 target: learnedLan.target,
399 profile: learnedLan.profile,
400 alternateRoutes: first.slice(1),
401 };
402 const next = mergeLearnedRoutes({
403 entry,
404 activeRoute: learnedLan,
405 reported: [
406 { httpBaseUrl: "http://192.168.1.10:3773/" },
407 { httpBaseUrl: "https://desk.tail1234.ts.net/" },
408 ],
409 allowInsecure: true,
410 })!;
411 for (const route of next.filter((candidate) => connectionRouteKind(candidate) !== "relay")) {
412 expect(profileOf(route)).toMatchObject({ authorization: "t3-connect" });
413 expect(connectionRouteId(route.target)).not.toContain("@");
414 }
415 });
416​
417 it("saves a scheme change on the same host as a new address", () => {
418 const first = mergeLearnedRoutes({
419 entry: relayOnly,
420 activeRoute: RELAY,
421 reported: [{ httpBaseUrl: "http://desk.local:3773/" }],
422 allowInsecure: true,
423 })!;
424 const entry: ConnectionCatalogEntry = {
425 ...relayOnly,
426 target: first[0]!.target,
427 profile: first[0]!.profile,
428 alternateRoutes: first.slice(1),
429 };
430 const moved = mergeLearnedRoutes({
431 entry,
432 activeRoute: RELAY,
433 reported: [{ httpBaseUrl: "https://desk.local:3773/" }],
434 allowInsecure: true,
435 });
436 expect(moved).not.toBeNull();
437 expect(profileOf(moved![0]!)).toMatchObject({ httpBaseUrl: "https://desk.local:3773/" });
438 });
439});