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

routes.test.ts

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