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