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

browserTargetResolver.test.ts

217 lines7.2 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import { isPrivateNetworkHost, isPublicFaviconHost } from "@t3tools/shared/hostClassification";
3import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
4​
5const readPreparedConnection = vi.fn();
6const env = vi.hoisted(() => ({ isElectron: false }));
7​
8vi.mock("~/state/session", () => ({ readPreparedConnection }));
9vi.mock("~/env", () => env);
10​
11describe("browser target resolver", () => {
12 beforeEach(() => readPreparedConnection.mockReset());
13​
14 it("refuses public relay hosts until the authenticated gateway exists", async () => {
15 readPreparedConnection.mockReturnValue({ httpBaseUrl: "https://relay.example.com" });
16 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
17 expect(
18 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "http://localhost:5173"),
19 ).toBe("http://localhost:5173");
20 });
21​
22 it("refuses a system-browser localhost URL the client has no route to", async () => {
23 const { resolveReachableExternalServerUrl } = await import("./browserTargetResolver");
24 const environmentId = EnvironmentId.make("environment-1");
25 readPreparedConnection.mockReturnValue({ httpBaseUrl: "https://relay.example.com" });
26 expect(() =>
27 resolveReachableExternalServerUrl(environmentId, "http://localhost:3000/docs"),
28 ).toThrow();
29 readPreparedConnection.mockReturnValue(undefined);
30 expect(() =>
31 resolveReachableExternalServerUrl(environmentId, "http://localhost:3000/docs"),
32 ).toThrow();
33 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://192.168.1.25:3773" });
34 expect(
35 resolveReachableExternalServerUrl(environmentId, "http://localhost:3000/docs?x=1#top"),
36 ).toBe("http://192.168.1.25:3000/docs?x=1#top");
37 });
38​
39 it("normalizes schemeless localhost server-picker values", async () => {
40 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://localhost:3773" });
41 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
42 expect(resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:5173")).toBe(
43 "http://localhost:5173/",
44 );
45 expect(
46 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "0.0.0.0:3000/app"),
47 ).toBe("http://localhost:3000/app");
48 });
49​
50 it("maps discovered loopback servers onto a remote environment host", async () => {
51 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://192.168.1.25:3773" });
52 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
53 expect(
54 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:3000/app"),
55 ).toBe("http://192.168.1.25:3000/app");
56 });
57​
58 it("keeps localhost for servers on the desktop's own WSL or primary backend", async () => {
59 // WSL NAT advertises a distro IP, but loopback-bound servers only answer on localhost.
60 const { resolveDiscoveredServerUrl, resolveExternalServerUrl } =
61 await import("./browserTargetResolver");
62 env.isElectron = true;
63 try {
64 for (const target of [
65 { _tag: "BearerConnectionTarget", connectionId: "local:wsl:Ubuntu" },
66 { _tag: "PrimaryConnectionTarget" },
67 ]) {
68 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://172.24.66.27:3773", target });
69 expect(
70 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:3001/"),
71 ).toBe("http://localhost:3001/");
72 }
73 readPreparedConnection.mockReturnValue({
74 httpBaseUrl: "http://192.168.1.25:3773",
75 target: { _tag: "BearerConnectionTarget", connectionId: "saved:remote" },
76 });
77 // The webview's partition tunnels to the environment; the system browser has no tunnel.
78 expect(
79 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:3001/"),
80 ).toBe("http://localhost:3001/");
81 expect(resolveExternalServerUrl(EnvironmentId.make("environment-1"), "localhost:3001/")).toBe(
82 "http://192.168.1.25:3001/",
83 );
84 } finally {
85 env.isElectron = false;
86 }
87 });
88​
89 it("classifies exact private IPv4 and IPv6 boundaries", async () => {
90 const privateHosts = [
91 "0.0.0.0",
92 "10.0.0.0",
93 "10.255.255.255",
94 "100.64.0.0",
95 "100.127.255.255",
96 "127.0.0.0",
97 "127.255.255.255",
98 "169.254.0.0",
99 "169.254.255.255",
100 "172.16.0.0",
101 "172.31.255.255",
102 "192.168.0.0",
103 "192.168.255.255",
104 "198.18.0.0",
105 "198.19.255.255",
106 "fc00::",
107 "fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
108 "fe80::",
109 "febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
110 "::ffff:192.168.1.1",
111 "localhost.",
112 "localhost..",
113 "devbox.",
114 "devbox..",
115 "printer.local.",
116 "printer.local..",
117 "printer.home.arpa.",
118 "printer.home.arpa..",
119 "devbox.example.ts.net.",
120 "devbox.example.ts.net..",
121 ];
122 const publicHosts = [
123 "1.0.0.0",
124 "100.63.255.255",
125 "100.128.0.0",
126 "169.253.255.255",
127 "169.255.0.0",
128 "172.15.255.255",
129 "172.32.0.0",
130 "192.167.255.255",
131 "192.169.0.0",
132 "198.17.255.255",
133 "198.20.0.0",
134 "fbff:ffff::",
135 "fec0::",
136 "2001:4860:4860::8888",
137 "::ffff:8.8.8.8",
138 "example.com.",
139 ];
140 expect(privateHosts.filter((host) => !isPrivateNetworkHost(host))).toEqual([]);
141 expect(publicHosts.filter(isPrivateNetworkHost)).toEqual([]);
142 });
143​
144 it("allows only globally routable hosts to reach a public favicon provider", async () => {
145 const nonPublic = [
146 "192.0.0.0",
147 "192.0.0.255",
148 "192.0.2.0",
149 "192.0.2.255",
150 "192.88.99.0",
151 "192.88.99.255",
152 "198.51.100.0",
153 "198.51.100.255",
154 "203.0.113.0",
155 "203.0.113.255",
156 "224.0.0.0",
157 "255.255.255.255",
158 "::2",
159 "100::",
160 "100::ffff:ffff:ffff:ffff",
161 "100:0:0:1::",
162 "100:0:0:1:ffff:ffff:ffff:ffff",
163 "64:ff9b:1::1",
164 "64:ff9b::a00:1",
165 "64:ff9b::7f00:1",
166 "64:ff9b::c0a8:101",
167 "64:ff9b::c000:201",
168 "2001:5::1",
169 "2001:2::",
170 "2001:2:0:ffff:ffff:ffff:ffff:ffff",
171 "2001:db8::",
172 "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff",
173 "3fff::",
174 "3fff:fff:ffff:ffff:ffff:ffff:ffff:ffff",
175 "5f00::1",
176 "fec0::",
177 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
178 "::ffff:192.0.2.1",
179 "app.test",
180 "app.test..",
181 "printer.local..",
182 "printer.home.arpa..",
183 "devbox.example.ts.net..",
184 "127.0.0.1..",
185 "127.1..",
186 "10.1..",
187 "172.16.1..",
188 "192.168.1..",
189 "service.internal",
190 "hidden.onion",
191 ];
192 const publicHosts = [
193 "191.255.255.255",
194 "192.0.1.255",
195 "192.0.3.0",
196 "198.51.99.255",
197 "198.51.101.0",
198 "203.0.112.255",
199 "203.0.114.0",
200 "223.255.255.255",
201 "1.1.1.1",
202 "2001:4860:4860::8888",
203 "2606:4700:4700::1111",
204 "64:ff9b::808:808",
205 "2001:1::1",
206 "2001:3::1",
207 "2001:4:112::1",
208 "2001:20::1",
209 "2001:30::1",
210 "::ffff:8.8.8.8",
211 "example.com",
212 "example.com.",
213 ];
214 expect(nonPublic.filter(isPublicFaviconHost)).toEqual([]);
215 expect(publicHosts.filter((host) => !isPublicFaviconHost(host))).toEqual([]);
216 });
217});