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

browserTargetResolver.test.ts

326 lines11.3 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
3​
4const readPreparedConnection = vi.fn();
5​
6vi.mock("~/state/session", () => ({ readPreparedConnection }));
7​
8describe("browser target resolver", () => {
9 beforeEach(() => readPreparedConnection.mockReset());
10​
11 it("maps environment ports onto a private network host", async () => {
12 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://192.168.1.25:3773" });
13 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
14 expect(
15 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
16 kind: "environment-port",
17 port: 5173,
18 path: "/dashboard",
19 }),
20 ).toEqual({
21 requestedUrl: "http://localhost:5173/dashboard",
22 resolvedUrl: "http://192.168.1.25:5173/dashboard",
23 resolutionKind: "direct-private-network",
24 environmentId: "environment-1",
25 });
26 });
27​
28 it("maps localhost URL navigation onto a remote Tailscale IPv4 host", async () => {
29 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://100.65.180.100:3773" });
30 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
31 expect(
32 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
33 kind: "url",
34 url: "http://localhost:5173/dashboard?mode=test#results",
35 }),
36 ).toEqual({
37 requestedUrl: "http://localhost:5173/dashboard?mode=test#results",
38 resolvedUrl: "http://100.65.180.100:5173/dashboard?mode=test#results",
39 resolutionKind: "direct-private-network",
40 environmentId: "environment-1",
41 });
42 });
43​
44 it("preserves URL credentials when mapping localhost onto a remote host", async () => {
45 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://100.65.180.100:3773" });
46 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
47 expect(
48 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
49 kind: "url",
50 url: "http://user:p%40ss@localhost:5173/dashboard",
51 }).resolvedUrl,
52 ).toBe("http://user:p%40ss@100.65.180.100:5173/dashboard");
53 });
54​
55 it("maps credentialed localhost URLs onto private IPv6 hosts", async () => {
56 readPreparedConnection.mockReturnValue({
57 httpBaseUrl: "http://[fd7a:115c:a1e0::53]:3773",
58 });
59 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
60 expect(
61 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
62 kind: "url",
63 url: "http://user:p%40ss@localhost:5173/dashboard?mode=test#results",
64 }).resolvedUrl,
65 ).toBe("http://user:p%40ss@[fd7a:115c:a1e0::53]:5173/dashboard?mode=test#results");
66 });
67​
68 it("maps schemeless localhost navigation onto a remote environment host", async () => {
69 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://192.168.1.25:3773" });
70 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
71 expect(
72 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
73 kind: "url",
74 url: "localhost:3000/app",
75 }).resolvedUrl,
76 ).toBe("http://192.168.1.25:3000/app");
77 });
78​
79 it("keeps localhost navigation local for a local environment", async () => {
80 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://127.0.0.1:3773" });
81 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
82 expect(
83 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
84 kind: "url",
85 url: "localhost:3000/app",
86 }),
87 ).toEqual({
88 requestedUrl: "localhost:3000/app",
89 resolvedUrl: "localhost:3000/app",
90 resolutionKind: "direct",
91 environmentId: "environment-1",
92 });
93 });
94​
95 it("keeps localhost navigation local for the full IPv4 loopback range", async () => {
96 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://127.0.0.2:3773" });
97 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
98 expect(
99 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
100 kind: "url",
101 url: "http://localhost:3000/app",
102 }),
103 ).toEqual({
104 requestedUrl: "http://localhost:3000/app",
105 resolvedUrl: "http://localhost:3000/app",
106 resolutionKind: "direct",
107 environmentId: "environment-1",
108 });
109 });
110​
111 it("refuses public relay hosts until the authenticated gateway exists", async () => {
112 readPreparedConnection.mockReturnValue({ httpBaseUrl: "https://relay.example.com" });
113 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
114 expect(() =>
115 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
116 kind: "environment-port",
117 port: 5173,
118 }),
119 ).toThrow(/authenticated preview gateway/);
120 expect(() =>
121 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
122 kind: "url",
123 url: "http://localhost:5173",
124 }),
125 ).toThrow(/authenticated preview gateway/);
126 });
127​
128 it("normalizes schemeless localhost server-picker values", async () => {
129 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://localhost:3773" });
130 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
131 expect(resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:5173")).toBe(
132 "http://localhost:5173/",
133 );
134 expect(
135 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "0.0.0.0:3000/app"),
136 ).toBe("http://localhost:3000/app");
137 });
138​
139 it("preserves localhost server-picker values when the prepared base is 127.0.0.1", async () => {
140 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://127.0.0.1:3773" });
141 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
142 expect(
143 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:5173/app?x=1#top"),
144 ).toBe("http://localhost:5173/app?x=1#top");
145 });
146​
147 it("normalizes public URLs without treating them as environment ports", async () => {
148 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
149 expect(resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "example.com/app")).toBe(
150 "https://example.com/app",
151 );
152 });
153​
154 it("supports private IPv6 environment hosts", async () => {
155 readPreparedConnection.mockReturnValue({
156 httpBaseUrl: "http://[fd7a:115c:a1e0::53]:3773",
157 });
158 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
159 expect(
160 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
161 kind: "environment-port",
162 port: 5173,
163 path: "/app?mode=test",
164 }).resolvedUrl,
165 ).toBe("http://[fd7a:115c:a1e0::53]:5173/app?mode=test");
166 });
167​
168 it("supports a local IPv6 environment host", async () => {
169 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://[::1]:3773" });
170 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
171 expect(
172 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
173 kind: "environment-port",
174 port: 5173,
175 }).resolvedUrl,
176 ).toBe("http://localhost:5173/");
177 });
178​
179 it("maps local IPv4 environment ports onto localhost for dual-stack guests", async () => {
180 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://127.0.0.1:3773" });
181 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
182 expect(
183 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
184 kind: "environment-port",
185 port: 5173,
186 path: "/app",
187 }).resolvedUrl,
188 ).toBe("http://localhost:5173/app");
189 });
190​
191 it("leaves malformed input for the normal navigation error path", async () => {
192 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
193 expect(resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), " ")).toBe(" ");
194 });
195​
196 it("classifies exact private IPv4 and IPv6 boundaries", async () => {
197 const { isPrivateNetworkHost } = await import("./browserTargetResolver");
198 const privateHosts = [
199 "0.0.0.0",
200 "10.0.0.0",
201 "10.255.255.255",
202 "100.64.0.0",
203 "100.127.255.255",
204 "127.0.0.0",
205 "127.255.255.255",
206 "169.254.0.0",
207 "169.254.255.255",
208 "172.16.0.0",
209 "172.31.255.255",
210 "192.168.0.0",
211 "192.168.255.255",
212 "198.18.0.0",
213 "198.19.255.255",
214 "fc00::",
215 "fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
216 "fe80::",
217 "febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
218 "::ffff:192.168.1.1",
219 "localhost.",
220 "localhost..",
221 "devbox.",
222 "devbox..",
223 "printer.local.",
224 "printer.local..",
225 "printer.home.arpa.",
226 "printer.home.arpa..",
227 "devbox.example.ts.net.",
228 "devbox.example.ts.net..",
229 ];
230 const publicHosts = [
231 "1.0.0.0",
232 "100.63.255.255",
233 "100.128.0.0",
234 "169.253.255.255",
235 "169.255.0.0",
236 "172.15.255.255",
237 "172.32.0.0",
238 "192.167.255.255",
239 "192.169.0.0",
240 "198.17.255.255",
241 "198.20.0.0",
242 "fbff:ffff::",
243 "fec0::",
244 "2001:4860:4860::8888",
245 "::ffff:8.8.8.8",
246 "example.com.",
247 ];
248 expect(privateHosts.filter((host) => !isPrivateNetworkHost(host))).toEqual([]);
249 expect(publicHosts.filter(isPrivateNetworkHost)).toEqual([]);
250 });
251​
252 it("allows only globally routable hosts to reach a public favicon provider", async () => {
253 const { isPublicFaviconHost } = await import("./browserTargetResolver");
254 const nonPublic = [
255 "192.0.0.0",
256 "192.0.0.255",
257 "192.0.2.0",
258 "192.0.2.255",
259 "192.88.99.0",
260 "192.88.99.255",
261 "198.51.100.0",
262 "198.51.100.255",
263 "203.0.113.0",
264 "203.0.113.255",
265 "224.0.0.0",
266 "255.255.255.255",
267 "::2",
268 "100::",
269 "100::ffff:ffff:ffff:ffff",
270 "100:0:0:1::",
271 "100:0:0:1:ffff:ffff:ffff:ffff",
272 "64:ff9b:1::1",
273 "64:ff9b::a00:1",
274 "64:ff9b::7f00:1",
275 "64:ff9b::c0a8:101",
276 "64:ff9b::c000:201",
277 "2001:5::1",
278 "2001:2::",
279 "2001:2:0:ffff:ffff:ffff:ffff:ffff",
280 "2001:db8::",
281 "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff",
282 "3fff::",
283 "3fff:fff:ffff:ffff:ffff:ffff:ffff:ffff",
284 "5f00::1",
285 "fec0::",
286 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
287 "::ffff:192.0.2.1",
288 "app.test",
289 "app.test..",
290 "printer.local..",
291 "printer.home.arpa..",
292 "devbox.example.ts.net..",
293 "127.0.0.1..",
294 "127.1..",
295 "10.1..",
296 "172.16.1..",
297 "192.168.1..",
298 "service.internal",
299 "hidden.onion",
300 ];
301 const publicHosts = [
302 "191.255.255.255",
303 "192.0.1.255",
304 "192.0.3.0",
305 "198.51.99.255",
306 "198.51.101.0",
307 "203.0.112.255",
308 "203.0.114.0",
309 "223.255.255.255",
310 "1.1.1.1",
311 "2001:4860:4860::8888",
312 "2606:4700:4700::1111",
313 "64:ff9b::808:808",
314 "2001:1::1",
315 "2001:3::1",
316 "2001:4:112::1",
317 "2001:20::1",
318 "2001:30::1",
319 "::ffff:8.8.8.8",
320 "example.com",
321 "example.com.",
322 ];
323 expect(nonPublic.filter(isPublicFaviconHost)).toEqual([]);
324 expect(publicHosts.filter((host) => !isPublicFaviconHost(host))).toEqual([]);
325 });
326});