~james/core

core/web/src/hooks.server.test.ts -rw-r--r-- 13.0 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 14 hours ago
                                                                                
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import { isRedirect } from "@sveltejs/kit";
import type { ResolveOptions } from "@sveltejs/kit/hooks";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { createSecureServer, type Http2Session } from "node:http2";
import type { AddressInfo } from "node:net";
import { Agent, fetch as undiciFetch } from "undici";
import { handle, handleFetch, upstreamPool } from "./hooks.server";

const config = vi.hoisted(() => ({
	sitemapUrl: "",
	bobbinUrl: "https://bobbin.test",
	avatarUrl: "https://avatar.test",
	camoUrl: "https://camo.test",
	upstreamTimeoutMs: 15_000
}));
vi.mock("#lib/server/config.ts", () => ({ getConfig: () => config }));
const repos = vi.hoisted(() => ({ resolveRepoByRepoDid: vi.fn() }));
vi.mock("#lib/api/repo.ts", () => repos);

const run = (
	path: string,
	binding?: (request: Request) => Promise<Response>,
	routeId: string | null = null
) => {
	const resolve = vi.fn(async () => new Response("page"));
	const url = new URL(`https://next.tangled.org${path}`);
	const event = {
		url,
		route: { id: routeId },
		fetch: vi.fn(),
		request: new Request(url),
		platform: binding ? { env: { SITEMAP: { fetch: binding } } } : undefined
	};
	return { res: handle({ event, resolve } as unknown as Parameters<typeof handle>[0]), resolve };
};

beforeEach(() => {
	config.sitemapUrl = "";
	repos.resolveRepoByRepoDid.mockReset().mockResolvedValue(null);
	vi.spyOn(console, "warn").mockImplementation(() => {});
});
afterEach(() => vi.unstubAllGlobals());

describe("handle: legacy redirects", () => {
	it.each([
		["/@alice.test", 302, "/alice.test"],
		["/@alice.test/core/tree/main?x=1", 302, "/alice.test/core/tree/main?x=1"],
		["/%40alice.test/core", 302, "/alice.test/core"],
		[
			"/@tangled.org/core/info/refs?service=git-upload-pack",
			302,
			"/tangled.org/core/info/refs?service=git-upload-pack"
		],
		["/tangled.sh", 301, "/tangled.org"],
		["/@tangled.sh", 301, "/tangled.org"],
		["/tangled.sh/core/issues?state=open", 301, "/tangled.org/core/issues?state=open"],
		["/@tangled.sh/core", 301, "/tangled.org/core"]
	])("sends %s to %s %s", async (path, status, location) => {
		const { res, resolve } = run(path);
		const out = await res;
		expect([out.status, out.headers.get("location")]).toEqual([status, location]);
		expect(resolve).not.toHaveBeenCalled();
	});

	it.each([
		"/tangled.shop/core",
		"/tangled.org/core",
		"/did:plc:j5hmlfdrwkvtxm7cjmu7j2is/info/refs",
		"/@did:plc:j5hmlfdrwkvtxm7cjmu7j2is",
		"/@",
		"/@not a handle",
		"/settings",
		"/login",
		"/alice.test/@core",
		"/%E0%A4%A/core",
		"/%zz"
	])("leaves %s to the routes", async (path) => {
		const { res, resolve } = run(path);
		await res;
		expect(resolve).toHaveBeenCalled();
	});
});

describe("handle: repo did permalinks", () => {
	const repoDid = "did:plc:j5hmlfdrwkvtxm7cjmu7j2is";
	const redirected = async (res: ReturnType<typeof handle>) => {
		try {
			await res;
		} catch (thrown) {
			if (isRedirect(thrown)) return [thrown.status, thrown.location];
			throw thrown;
		}
		return null;
	};

	beforeEach(() => {
		repos.resolveRepoByRepoDid.mockImplementation(async (_, did: string) =>
			did === repoDid ? { owner: "tangled.org", name: "core" } : null
		);
	});

	it.each([
		["", "/tangled.org/core"],
		["/issues", "/tangled.org/core/issues"],
		["/issues/12", "/tangled.org/core/issues/12"],
		["/tree/main/web/src?x=1", "/tangled.org/core/tree/main/web/src?x=1"]
	])("sends the repo did%s to the repo", async (rest, location) => {
		const { res, resolve } = run(`/${repoDid}${rest}`);
		expect(await redirected(res)).toEqual([307, location]);
		expect(resolve).not.toHaveBeenCalled();
	});

	it.each(["/did:plc:aaaaaaaaaaaaaaaaaaaaaaaa", "/did:plc:aaaaaaaaaaaaaaaaaaaaaaaa/core"])(
		"leaves %s, a did that isn't a repo, to the routes",
		async (path) => {
			const { res, resolve } = run(path);
			expect(await redirected(res)).toBeNull();
			expect(resolve).toHaveBeenCalled();
		}
	);

	it("leaves git's info/refs at the permalink to its route without a lookup", async () => {
		const { res, resolve } = run(`/${repoDid}/info/refs`, undefined, "/[handle]/info/refs");
		expect(await redirected(res)).toBeNull();
		expect(resolve).toHaveBeenCalled();
		expect(repos.resolveRepoByRepoDid).not.toHaveBeenCalled();
	});

	it("doesn't look up a handle", async () => {
		const { res } = run("/tangled.org/core");
		await res;
		expect(repos.resolveRepoByRepoDid).not.toHaveBeenCalled();
	});
});

describe("handle: sitemaps", () => {
	it("prefers the worker binding", async () => {
		config.sitemapUrl = "https://sitemap.test";
		const binding = vi.fn(async () => new Response("bound"));
		const network = vi.fn();
		vi.stubGlobal("fetch", network);
		const { res } = run("/sitemap.xml", binding);
		await expect((await res).text()).resolves.toBe("bound");
		expect(network).not.toHaveBeenCalled();
	});

	it.each(["/sitemap.xml", "/sitemaps/repos-3.xml?v=2"])(
		"fetches %s from SITEMAP_URL without a binding, dropping the decoded encoding",
		async (path) => {
			config.sitemapUrl = "https://sitemap.test";
			const network = vi.fn(
				async (_: string) =>
					new Response("<urlset/>", {
						headers: {
							"content-type": "application/xml",
							"content-encoding": "gzip",
							"content-length": "9"
						}
					})
			);
			vi.stubGlobal("fetch", network);
			const { res, resolve } = run(path);
			const out = await res;
			expect(network).toHaveBeenCalledWith(`https://sitemap.test${path}`);
			expect(resolve).not.toHaveBeenCalled();
			await expect(out.text()).resolves.toBe("<urlset/>");
			expect(out.headers.get("content-type")).toBe("application/xml");
			expect(out.headers.get("content-encoding")).toBeNull();
			expect(out.headers.get("content-length")).toBeNull();
		}
	);

	it("falls through to the app with neither", async () => {
		const network = vi.fn();
		vi.stubGlobal("fetch", network);
		const { res, resolve } = run("/sitemap.xml");
		await expect((await res).text()).resolves.toBe("page");
		expect(resolve).toHaveBeenCalledOnce();
		expect(network).not.toHaveBeenCalled();
	});
});

describe("handle: security headers", () => {
	const page = async (response: Response) => {
		const url = new URL("https://next.tangled.org/someone/repo");
		const event = { url, request: new Request(url), platform: undefined };
		const resolve = vi.fn(async () => response);
		return handle({ event, resolve } as unknown as Parameters<typeof handle>[0]);
	};

	it("adds them to a rendered response", async () => {
		const res = await page(new Response("page"));
		expect(Object.fromEntries(res.headers)).toMatchObject({
			"x-content-type-options": "nosniff",
			"referrer-policy": "strict-origin-when-cross-origin",
			"x-frame-options": "DENY",
			"content-security-policy": "frame-ancestors 'none'"
		});
		await expect(res.text()).resolves.toBe("page");
	});

	it("keeps a header the route set itself", async () => {
		const own = "sandbox; default-src 'none'";
		const res = await page(
			new Response("raw", { headers: { "content-security-policy": own } })
		);
		expect(res.headers.get("content-security-policy")).toBe(own);
		expect(res.headers.get("x-frame-options")).toBe("DENY");
	});

	it("copies a response whose headers can't be changed", async () => {
		const res = await page(Response.redirect("https://cdn.example.com/a", 302));
		expect(res.status).toBe(302);
		expect(res.headers.get("location")).toBe("https://cdn.example.com/a");
		expect(res.headers.get("x-content-type-options")).toBe("nosniff");
	});
});

describe("handle: page head", () => {
	it("preconnects to the configured avatar and camo services", async () => {
		const url = new URL("https://next.tangled.org/someone/repo");
		const event = { url, request: new Request(url), platform: undefined };
		const resolve = vi.fn(async (_: unknown, opts: ResolveOptions) => {
			const html = '<link href="%tangled.avatar%" /><link href="%tangled.camo%" />';
			return new Response(await opts.transformPageChunk?.({ html, done: true }));
		});
		const res = await handle({ event, resolve } as unknown as Parameters<typeof handle>[0]);
		await expect(res.text()).resolves.toBe(
			'<link href="https://avatar.test" /><link href="https://camo.test" />'
		);
	});
});

describe("handleFetch", () => {
	beforeEach(() => vi.useFakeTimers());
	afterEach(() => {
		vi.useRealTimers();
		config.upstreamTimeoutMs = 15_000;
	});

	const call = async (request: Request, fetch: (request: Request) => Promise<Response>) =>
		handleFetch({ request, fetch } as unknown as Parameters<typeof handleFetch>[0]);

	const hanging = (request: Request) =>
		new Promise<Response>((_, reject) =>
			request.signal.addEventListener("abort", () => reject(request.signal.reason))
		);

	it("gives up on a get that has no answer in time", async () => {
		const out = call(new Request("https://bobbin.test/xrpc/x"), hanging);
		const failed = expect(out).rejects.toThrow();
		await vi.advanceTimersByTimeAsync(15_000);
		await failed;
		expect(console.warn).toHaveBeenCalledWith(
			"[hooks.server] bobbin.test/xrpc/x gave no answer in 15000 ms"
		);
	});

	it("leaves a body that is still streaming once the headers came", async () => {
		let signal: AbortSignal | undefined;
		const out = await call(new Request("https://bobbin.test/xrpc/x"), async (request) => {
			signal = request.signal;
			return new Response("ok");
		});
		await vi.advanceTimersByTimeAsync(60_000);
		expect(signal?.aborted).toBe(false);
		expect(await out.text()).toBe("ok");
	});

	it("doesn't bound a push or clone post", async () => {
		const out = call(
			new Request("https://knot.test/git-receive-pack", { method: "POST" }),
			hanging
		);
		let settled = false;
		void out.then(
			() => (settled = true),
			() => (settled = true)
		);
		await vi.advanceTimersByTimeAsync(60_000);
		expect(settled).toBe(false);
	});

	it("waits as long as it takes when the timeout is 0", async () => {
		config.upstreamTimeoutMs = 0;
		const out = call(new Request("https://bobbin.test/xrpc/x"), hanging);
		let settled = false;
		void out.then(
			() => (settled = true),
			() => (settled = true)
		);
		await vi.advanceTimersByTimeAsync(60_000);
		expect(settled).toBe(false);
	});
});

// self-signed for 127.0.0.1, only ever served inside this test
const TEST_CERT = `-----BEGIN CERTIFICATE-----
MIIBmzCCAUGgAwIBAgIUfes5w7WyBUNKaPeKwxWOEvao70IwCgYIKoZIzj0EAwIw
FDESMBAGA1UEAwwJbG9jYWxob3N0MCAXDTI2MTAyOTIyMzEzNVoYDzIxMjYxMDA1
MjIzMTM1WjAUMRIwEAYDVQQDDAlsb2NhbGhvc3QwWTATBgcqhkjOPQIBBggqhkjO
PQMBBwNCAASIWcZ5H+k949hDyfpHuWzsHPhx29HL/vLL0G7DMGPIDZ2A00D/Dkl6
1xoFynaNvcMkGTCDSTniIg1R3XIAbSuWo28wbTAdBgNVHQ4EFgQU0cTqOs5nXWSo
m1T+lGfPXCk7HMwwHwYDVR0jBBgwFoAU0cTqOs5nXWSom1T+lGfPXCk7HMwwDwYD
VR0TAQH/BAUwAwEB/zAaBgNVHREEEzARgglsb2NhbGhvc3SHBH8AAAEwCgYIKoZI
zj0EAwIDSAAwRQIgIeV3oKSwakMNd+bG3H3XCAU9cJeX6B/t9yoOJd6pWPYCIQCA
bgl5XnEX5+i6Sa42SS3HW4bY+vz6nFDK2lDu2MLI8w==
-----END CERTIFICATE-----`;
const TEST_KEY = `-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgoxzF7/Ony88kSNqq
Ap1tbQjKUE2/fYeUqDlFVUBzXeuhRANCAASIWcZ5H+k949hDyfpHuWzsHPhx29HL
/vLL0G7DMGPIDZ2A00D/Dkl61xoFynaNvcMkGTCDSTniIg1R3XIAbSuW
-----END PRIVATE KEY-----`;

describe("upstreamPool", () => {
	const ourOrigin = (port: number) => `https://127.0.0.1:${port}`;

	// counts connections and the most requests one connection carried at once
	const h2Server = async () => {
		const sessions = new Map<Http2Session, number>();
		let most = 0;
		const server = createSecureServer({ cert: TEST_CERT, key: TEST_KEY }, (req, res) => {
			const session = req.stream.session!;
			const now = (sessions.get(session) ?? 0) + 1;
			sessions.set(session, now);
			most = Math.max(most, now);
			setTimeout(() => {
				sessions.set(session, sessions.get(session)! - 1);
				res.end("ok");
			}, 50);
		});
		await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
		const port = (server.address() as AddressInfo).port;
		return {
			port,
			stats: () => ({ connections: sessions.size, most }),
			close: () => server.close()
		};
	};

	const burst = async (agent: Agent, url: string, count: number) => {
		const out = await Promise.all(
			Array.from({ length: count }, () =>
				undiciFetch(url, { dispatcher: agent }).then((res) => res.text())
			)
		);
		expect(out.every((body) => body === "ok")).toBe(true);
	};

	const agentFor = (ours: string[]) =>
		new Agent({ factory: upstreamPool(new Set(ours)), connect: { rejectUnauthorized: false } });

	// a cold burst still opens a connection per request, since none knows it got http/2 yet
	it("spreads an http/2 host that isn't ours over streams once it's connected", async () => {
		const server = await h2Server();
		const agent = agentFor([]);
		try {
			await burst(agent, ourOrigin(server.port), 1);
			await burst(agent, ourOrigin(server.port), 50);
			expect(server.stats().connections).toBe(1);
			expect(server.stats().most).toBeGreaterThan(1);
		} finally {
			await agent.close();
			server.close();
		}
	});

	it("keeps our own services to a few http/2 connections", async () => {
		const server = await h2Server();
		const agent = agentFor([ourOrigin(server.port)]);
		try {
			await burst(agent, ourOrigin(server.port), 300);
			expect(server.stats().connections).toBeLessThanOrEqual(8);
		} finally {
			await agent.close();
			server.close();
		}
	});
});