Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 normalizePreviewUrl,
5 PreviewUrlNormalizationError,
6 resolveAddressBarInput,
7 previewPdfAssetUrl,
8 resolveSearchUrlTemplate,
9} from "./preview.ts";
10
11describe("previewPdfAssetUrl", () => {
12 it.each([
13 "http://localhost:6497",
14 "https://host.tailnet.test",
15 "https://environment.tunnel.test",
16 ])("loads PDF bytes through the client's connection at %s", (base) => {
17 expect(
18 previewPdfAssetUrl(
19 "http://127.0.0.1:14537/api/assets/signed-token/Study%20guide.PDF?revision=2#page=8",
20 base,
21 ),
22 ).toBe(`${base}/api/assets/signed-token/Study%20guide.PDF?revision=2#page=8`);
23 });
24
25 it.each([
26 "http://localhost:6497/api/assets/token/index.html",
27 "https://example.com/paper.pdf",
28 "file:///api/assets/token/paper.pdf",
29 "invalid",
30 ])("leaves non-asset documents on their browser session: %s", (url) => {
31 expect(previewPdfAssetUrl(url, "https://environment.test")).toBeNull();
32 });
33
34 it.each(["another-environment.test", "127.0.0.2.example.com", "128.0.0.1"])(
35 "leaves a matching PDF path on unrelated host %s on its browser session",
36 (host) => {
37 const source = `https://${host}/api/assets/token/guide.pdf?revision=2#page=3`;
38 expect(previewPdfAssetUrl(source, "https://environment.test")).toBeNull();
39 },
40 );
41
42 it("keeps URLs from the connected environment", () => {
43 const source = "https://environment.test/api/assets/token/guide.pdf#page=2";
44 expect(previewPdfAssetUrl(source, "https://environment.test")).toBe(source);
45 });
46
47 it.each(["127.0.0.2", "localhost.", "[::1]"])(
48 "remaps local assets on %s through the client connection",
49 (host) => {
50 expect(
51 previewPdfAssetUrl(
52 `http://${host}:14537/api/assets/token/guide.pdf?revision=2#page=3`,
53 "https://remote.test",
54 ),
55 ).toBe("https://remote.test/api/assets/token/guide.pdf?revision=2#page=3");
56 },
57 );
58});
59
60describe("resolveAddressBarInput", () => {
61 it.each([
62 ["localhost:3000/?next=https://example.com", "http://localhost:3000/?next=https://example.com"],
63 ["127.0.0.1:3000/#https://example.com", "http://127.0.0.1:3000/#https://example.com"],
64 ["example.com/https://example.org", "https://example.com/https://example.org"],
65 ])("opens the outer address when it contains another URL: %s", (input, expected) => {
66 expect(resolveAddressBarInput(input)).toBe(expected);
67 });
68
69 it("opens what users type as addresses", () => {
70 expect(resolveAddressBarInput("//127.0.0.1:44605/health")).toBe(
71 "http://127.0.0.1:44605/health",
72 );
73 expect(resolveAddressBarInput("//example.com/docs")).toBe("https://example.com/docs");
74 expect(resolveAddressBarInput("localhost:5173")).toBe("http://localhost:5173/");
75 expect(resolveAddressBarInput("127.0.0.1:3000/path")).toBe("http://127.0.0.1:3000/path");
76 expect(resolveAddressBarInput("my-box.tailnet.ts.net")).toBe("https://my-box.tailnet.ts.net/");
77 expect(resolveAddressBarInput("cnn.com")).toBe("https://cnn.com/");
78 expect(resolveAddressBarInput("devbox:8080")).toBe("https://devbox:8080/");
79 expect(resolveAddressBarInput(" https://example.com/a b ")).toBe("https://example.com/a%20b");
80 expect(resolveAddressBarInput("example.com:8080")).toBe("https://example.com:8080/");
81 expect(resolveAddressBarInput("[::1]:3000")).toBe("http://[::1]:3000/");
82 expect(resolveAddressBarInput("app.localhost:5173")).toBe("http://app.localhost:5173/");
83 expect(resolveAddressBarInput("localhost./health")).toBe("http://localhost./health");
84 expect(resolveAddressBarInput("localhost.?debug=1")).toBe("http://localhost./?debug=1");
85 expect(resolveAddressBarInput("192.168.1.5:3000")).toBe("https://192.168.1.5:3000/");
86 });
87
88 it.each([
89 ["127.0.0.1:44605/my file?probe=1", "http://127.0.0.1:44605/my%20file?probe=1"],
90 ["//127.0.0.1:44605/my file", "http://127.0.0.1:44605/my%20file"],
91 ["localhost:3000/a b", "http://localhost:3000/a%20b"],
92 ["[::1]:3000/docs/read me.html", "http://[::1]:3000/docs/read%20me.html"],
93 ["192.168.1.5:3000/search?q=a b", "https://192.168.1.5:3000/search?q=a%20b"],
94 ])("opens a local address whose path has spaces: %s", (input, expected) => {
95 expect(resolveAddressBarInput(input)).toBe(expected);
96 });
97
98 it.each([
99 "10.0.0.0/8 vs 172.16.0.0/12",
100 "127.0.0.0/8 loopback range",
101 "1.1.1.1/ dns",
102 "localhost:3000 is not responding",
103 "16:9/4:3 aspect ratio",
104 "user@localhost:3000/a b",
105 "localhost:3000\\a b",
106 "//div/span text",
107 "http: status codes",
108 "https: certificate error",
109 ])("searches for text with spaces that only resembles an address: %s", (input) => {
110 expect(resolveAddressBarInput(input)).toBe(
111 `https://duckduckgo.com/?q=${encodeURIComponent(input)}`,
112 );
113 });
114
115 it("searches for text that is not an address", () => {
116 expect(resolveAddressBarInput("weather")).toBe("https://duckduckgo.com/?q=weather");
117 expect(resolveAddressBarInput("//eslint-disable-next-line")).toBe(
118 "https://duckduckgo.com/?q=%2F%2Feslint-disable-next-line",
119 );
120 expect(resolveAddressBarInput("how to center a div")).toBe(
121 "https://duckduckgo.com/?q=how%20to%20center%20a%20div",
122 );
123 expect(resolveAddressBarInput("what is cnn.com")).toBe(
124 "https://duckduckgo.com/?q=what%20is%20cnn.com",
125 );
126 expect(resolveAddressBarInput("what is https://example.com")).toBe(
127 "https://duckduckgo.com/?q=what%20is%20https%3A%2F%2Fexample.com",
128 );
129 expect(resolveAddressBarInput("what is 10:30")).toBe(
130 "https://duckduckgo.com/?q=what%20is%2010%3A30",
131 );
132 expect(resolveAddressBarInput("note: buy milk")).toBe(
133 "https://duckduckgo.com/?q=note%3A%20buy%20milk",
134 );
135 });
136
137 it("still rejects empty input and unsupported schemes", () => {
138 expect(() => resolveAddressBarInput(" ")).toThrow(PreviewUrlNormalizationError);
139 expect(() => resolveAddressBarInput("ftp://example.com")).toThrow(PreviewUrlNormalizationError);
140 expect(() => resolveAddressBarInput("mailto:alice@example.com")).toThrow(
141 PreviewUrlNormalizationError,
142 );
143 expect(() => resolveAddressBarInput("data:text/plain,hello")).toThrow(
144 PreviewUrlNormalizationError,
145 );
146 for (const input of ["ftp:21", "tel:5551234"]) {
147 expect(() => resolveAddressBarInput(input)).toThrow(PreviewUrlNormalizationError);
148 }
149 expect(resolveAddressBarInput("devbox:8080")).toBe("https://devbox:8080/");
150 });
151
152 it("searches with the given template and still opens addresses directly", () => {
153 const google = resolveSearchUrlTemplate({
154 browserSearchEngine: "google",
155 browserCustomSearchUrl: "",
156 });
157 expect(resolveAddressBarInput("how to center a div", google)).toBe(
158 "https://www.google.com/search?q=how%20to%20center%20a%20div",
159 );
160 expect(resolveAddressBarInput("cnn.com", google)).toBe("https://cnn.com/");
161 expect(resolveAddressBarInput("a&b", "https://search.example/?q=%s&lang=en")).toBe(
162 "https://search.example/?q=a%26b&lang=en",
163 );
164 });
165});
166
167describe("resolveSearchUrlTemplate", () => {
168 it("uses a valid custom template and falls back to DuckDuckGo for an unusable one", () => {
169 expect(
170 resolveSearchUrlTemplate({
171 browserSearchEngine: "custom",
172 browserCustomSearchUrl: " https://search.example/?q=%s ",
173 }),
174 ).toBe("https://search.example/?q=%s");
175 for (const browserCustomSearchUrl of [
176 "https://search.example/?q=",
177 "javascript:alert('%s')",
178 "https://%s.example.com/",
179 "https://%s@search.example/?q=%s",
180 ]) {
181 expect(
182 resolveSearchUrlTemplate({ browserSearchEngine: "custom", browserCustomSearchUrl }),
183 ).toBe("https://duckduckgo.com/?q=%s");
184 }
185 });
186});
187
188describe("normalizePreviewUrl", () => {
189 it.each([
190 ["127.0.0.2:3000/path", "http://127.0.0.2:3000/path"],
191 ["127.255.255.254:443/path", "http://127.255.255.254:443/path"],
192 ["0x7f.0.0.2:3000", "http://127.0.0.2:3000/"],
193 ["127.0.0.2.example.com:3000", "https://127.0.0.2.example.com:3000/"],
194 ["https://127.0.0.2:80/path", "https://127.0.0.2:80/path"],
195 ["app.localhost:5173", "http://app.localhost:5173/"],
196 ["https://app.localhost:5173", "https://app.localhost:5173/"],
197 ["localhost.example.com:3000", "https://localhost.example.com:3000/"],
198 ])("infers the scheme for loopback-looking %s", (input, expected) => {
199 expect(normalizePreviewUrl(input)).toBe(expected);
200 });
201
202 it.each([
203 ["0.0.0.0.:44605/health", "http://0.0.0.0:44605/health"],
204 ["0.0.0.0?debug=1", "http://0.0.0.0/?debug=1"],
205 ["[::]#health", "http://[::]/#health"],
206 ["[::0]:443/health", "http://[::]:443/health"],
207 ])("infers http for wildcard address %s", (input, expected) => {
208 expect(normalizePreviewUrl(input)).toBe(expected);
209 });
210
211 it.each([
212 ["http:/127.0.0.1:44605/health?probe=1", "http://127.0.0.1:44605/health?probe=1"],
213 ["https:/example.com/path", "https://example.com/path"],
214 ])("keeps the host of single-slash %s", (input, expected) => {
215 expect(normalizePreviewUrl(input)).toBe(expected);
216 });
217
218 it.each([
219 ["http:127.0.0.1:44605/health", "http://127.0.0.1:44605/health"],
220 ["https:example.com/path", "https://example.com/path"],
221 ])("keeps the host of slashless %s", (input, expected) => {
222 expect(normalizePreviewUrl(input)).toBe(expected);
223 expect(resolveAddressBarInput(input)).toBe(expected);
224 });
225
226 it.each([
227 ["http:8080", "https://http:8080/"],
228 ["https:8443/health", "https://https:8443/health"],
229 ])("preserves the bare host and port in %s", (input, expected) => {
230 expect(normalizePreviewUrl(input)).toBe(expected);
231 expect(resolveAddressBarInput(input)).toBe(expected);
232 });
233
234 it("tells agents how to open a blank tab and which URLs are accepted", () => {
235 expect(() => normalizePreviewUrl("about:blank")).toThrow(
236 /preview_open, omit url when creating a new tab[^.]*blank tab/i,
237 );
238 expect(() => normalizePreviewUrl("about:blank")).toThrow(/http\(s\)/i);
239 expect(() => normalizePreviewUrl("about:blank")).toThrow(/schemeless host/i);
240 });
241
242 it("rejects unparseable input without retaining credentials or tokens", () => {
243 const rawUrl = "https://user:password@example.com:bad/path?access_token=secret#fragment";
244 try {
245 normalizePreviewUrl(rawUrl);
246 expect.unreachable("expected URL normalization to fail");
247 } catch (error) {
248 expect(error).toBeInstanceOf(PreviewUrlNormalizationError);
249 expect(error).toMatchObject({
250 inputLength: rawUrl.length,
251 reason: "parse",
252 protocol: "https:",
253 });
254 expect(error).not.toHaveProperty("rawUrl");
255 expect((error as PreviewUrlNormalizationError).cause).toBeInstanceOf(Error);
256 expect((error as PreviewUrlNormalizationError).message).not.toContain(
257 ((error as PreviewUrlNormalizationError).cause as Error).message,
258 );
259 expect((error as PreviewUrlNormalizationError).message).not.toMatch(
260 /user|password|access_token|secret|fragment/,
261 );
262 }
263 });
264
265 it("reports explicit non-web schemes instead of reading them as a host", () => {
266 for (const [rawUrl, protocol] of [
267 ["about:blank", "about:"],
268 ["mailto:alice@example.com", "mailto:"],
269 ["javascript:alert(1)", "javascript:"],
270 ] as const) {
271 expect(() => normalizePreviewUrl(rawUrl)).toThrow(
272 expect.objectContaining({ reason: "unsupported-protocol", protocol }),
273 );
274 }
275 expect(normalizePreviewUrl("localhost:3000")).toBe("http://localhost:3000/");
276 expect(normalizePreviewUrl("devbox:8080")).toBe("https://devbox:8080/");
277 });
278});