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

routes.test.ts

419 lines14.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 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("leaves user routes alone and does not learn an address already saved", () => {
199 const entry: ConnectionCatalogEntry = {
200 target: LAN.target,
201 profile: LAN.profile,
202 alternateRoutes: [RELAY],
203 enabled: true,
204 };
205 expect(
206 mergeLearnedRoutes({
207 entry,
208 activeRoute: RELAY,
209 reported: [{ httpBaseUrl: "http://192.168.1.10:3773" }],
210 allowInsecure: true,
211 }),
212 ).toBeNull();
213 expect(
214 mergeLearnedRoutes({ entry, activeRoute: RELAY, reported: [], allowInsecure: true }),
215 ).toBeNull();
216 });
217​
218 it("borrows the paired token when learned over a bearer route", () => {
219 const entry: ConnectionCatalogEntry = {
220 ...relayOnly,
221 target: LAN.target,
222 profile: LAN.profile,
223 };
224 const routes = mergeLearnedRoutes({
225 entry,
226 activeRoute: LAN,
227 reported: [{ httpBaseUrl: "https://desk.tail1234.ts.net/" }],
228 allowInsecure: true,
229 })!;
230 const learned = routes.find((route) => connectionRouteKind(route) === "tailnet")!;
231 expect(profileOf(learned)).not.toHaveProperty("authorization");
232 expect(credentialConnectionId(connectionRouteId(learned.target))).toBe("lan");
233 });
234​
235 it("keeps a token paired over HTTPS off plain HTTP addresses outside the tailnet", () => {
236 const entry = entryWithRoutes(relayOnly, [PUBLIC, RELAY]);
237 const routes = mergeLearnedRoutes({
238 entry,
239 activeRoute: PUBLIC,
240 reported: [
241 { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" },
242 { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" },
243 { kind: "tailnet", httpBaseUrl: "https://desk.tail1234.ts.net/" },
244 ],
245 allowInsecure: true,
246 });
247 expect(ids(routes)).toEqual([
248 `learned:${ENVIRONMENT_ID}:http://100.101.102.103:3773@public`,
249 `learned:${ENVIRONMENT_ID}:https://desk.tail1234.ts.net@public`,
250 "public",
251 "relay",
252 ]);
253 });
254​
255 it("drops a stale plain HTTP route borrowing an HTTPS-paired token while another route is active", () => {
256 const lanOrigin = "http://10.0.0.42:3773";
257 const stale = learned(`learned:${ENVIRONMENT_ID}:${lanOrigin}@public`, `${lanOrigin}/`);
258 const entry = entryWithRoutes(relayOnly, [stale, PUBLIC, RELAY]);
259 expect(sendsTokenInClear([stale, PUBLIC, RELAY], stale)).toBe(true);
260 expect(ids(usableRoutes(entry))).toEqual(["public", "relay"]);
261 const routes = mergeLearnedRoutes({
262 entry,
263 activeRoute: RELAY,
264 reported: [{ kind: "lan", httpBaseUrl: `${lanOrigin}/` }],
265 allowInsecure: true,
266 });
267 // The stale route is gone; the address is learned again with T3 Connect's credential.
268 expect(ids(routes)).toEqual([`learned:${ENVIRONMENT_ID}:${lanOrigin}`, "public", "relay"]);
269 expect(profileOf(routes![0]!)).toMatchObject({ authorization: "t3-connect" });
270 });
271​
272 it("keeps plain HTTP routes that don't borrow the active HTTPS-paired token", () => {
273 const lanOrigin = "http://10.0.0.42:3773";
274 const overLan = learned(`learned:${ENVIRONMENT_ID}:${lanOrigin}@lan`, `${lanOrigin}/`);
275 const routes = mergeLearnedRoutes({
276 entry: entryWithRoutes(relayOnly, [overLan, LAN, PUBLIC, RELAY]),
277 activeRoute: PUBLIC,
278 reported: [{ kind: "lan", httpBaseUrl: `${lanOrigin}/` }],
279 allowInsecure: true,
280 });
281 expect(routes).toBeNull();
282 });
283​
284 it("learns plain HTTP LAN addresses over a token paired over plain HTTP", () => {
285 const entry = entryWithRoutes(relayOnly, [LAN, RELAY]);
286 const routes = mergeLearnedRoutes({
287 entry,
288 activeRoute: LAN,
289 reported: [{ kind: "lan", httpBaseUrl: "http://10.0.0.42:3773/" }],
290 allowInsecure: true,
291 });
292 expect(ids(routes)).toEqual([
293 "lan",
294 `learned:${ENVIRONMENT_ID}:http://10.0.0.42:3773@lan`,
295 "relay",
296 ]);
297 });
298​
299 it("skips plain HTTP from an HTTPS page and never learns loopback", () => {
300 expect(
301 mergeLearnedRoutes({
302 entry: relayOnly,
303 activeRoute: RELAY,
304 reported: [
305 { httpBaseUrl: "http://192.168.1.10:3773/" },
306 { httpBaseUrl: "http://127.0.0.1:3773/" },
307 ],
308 allowInsecure: false,
309 }),
310 ).toBeNull();
311 });
312​
313 it("removes learned routes along with the route whose credential they borrow", () => {
314 const overRelay = mergeLearnedRoutes({
315 entry: relayOnly,
316 activeRoute: RELAY,
317 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
318 allowInsecure: true,
319 })!;
320 expect(routesAfterRemoving(overRelay, "relay")).toEqual([]);
321​
322 const paired: ConnectionCatalogEntry = {
323 ...relayOnly,
324 target: LAN.target,
325 profile: LAN.profile,
326 alternateRoutes: [RELAY],
327 };
328 const overLan = mergeLearnedRoutes({
329 entry: paired,
330 activeRoute: LAN,
331 reported: [{ httpBaseUrl: "https://desk.tail1234.ts.net/" }],
332 allowInsecure: true,
333 })!;
334 expect(ids(routesAfterRemoving(overLan, "lan"))).toEqual(["relay"]);
335 expect(ids(routesAfterRemoving(overLan, "relay"))).toHaveLength(2);
336 });
337​
338 it("counts an environment reached only through T3 Connect as removed with it", () => {
339 const learned = mergeLearnedRoutes({
340 entry: relayOnly,
341 activeRoute: RELAY,
342 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
343 allowInsecure: true,
344 })!;
345 expect(removedWithRelay(relayOnly)).toBe(true);
346 expect(removedWithRelay(entryWithRoutes(relayOnly, learned))).toBe(true);
347 expect(removedWithRelay(entryWithRoutes(relayOnly, [LAN, RELAY]))).toBe(false);
348 });
349​
350 it("keeps pull request routing trust when a route is learned", () => {
351 const learned = mergeLearnedRoutes({
352 entry: relayOnly,
353 activeRoute: RELAY,
354 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
355 allowInsecure: true,
356 })!;
357 const withLearned: ConnectionCatalogEntry = {
358 ...relayOnly,
359 target: learned[0]!.target,
360 profile: learned[0]!.profile,
361 alternateRoutes: learned.slice(1),
362 };
363 expect(pullRequestRoutingConnectionKey(withLearned)).toBe(
364 pullRequestRoutingConnectionKey(relayOnly),
365 );
366 });
367​
368 it("keeps the T3 Connect credential when learning over a learned T3 Connect route", () => {
369 const first = mergeLearnedRoutes({
370 entry: relayOnly,
371 activeRoute: RELAY,
372 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
373 allowInsecure: true,
374 })!;
375 const learnedLan = first[0]!;
376 const entry: ConnectionCatalogEntry = {
377 ...relayOnly,
378 target: learnedLan.target,
379 profile: learnedLan.profile,
380 alternateRoutes: first.slice(1),
381 };
382 const next = mergeLearnedRoutes({
383 entry,
384 activeRoute: learnedLan,
385 reported: [
386 { httpBaseUrl: "http://192.168.1.10:3773/" },
387 { httpBaseUrl: "https://desk.tail1234.ts.net/" },
388 ],
389 allowInsecure: true,
390 })!;
391 for (const route of next.filter((candidate) => connectionRouteKind(candidate) !== "relay")) {
392 expect(profileOf(route)).toMatchObject({ authorization: "t3-connect" });
393 expect(connectionRouteId(route.target)).not.toContain("@");
394 }
395 });
396​
397 it("saves a scheme change on the same host as a new address", () => {
398 const first = mergeLearnedRoutes({
399 entry: relayOnly,
400 activeRoute: RELAY,
401 reported: [{ httpBaseUrl: "http://desk.local:3773/" }],
402 allowInsecure: true,
403 })!;
404 const entry: ConnectionCatalogEntry = {
405 ...relayOnly,
406 target: first[0]!.target,
407 profile: first[0]!.profile,
408 alternateRoutes: first.slice(1),
409 };
410 const moved = mergeLearnedRoutes({
411 entry,
412 activeRoute: RELAY,
413 reported: [{ httpBaseUrl: "https://desk.local:3773/" }],
414 allowInsecure: true,
415 });
416 expect(moved).not.toBeNull();
417 expect(profileOf(moved![0]!)).toMatchObject({ httpBaseUrl: "https://desk.local:3773/" });
418 });
419});