bucket.test.ts

9.9 KB · raw

1import { beforeAll, describe, expect, test } from "bun:test";2import { writeFile } from "node:fs/promises";3import { join } from "node:path";4import { openBucket } from "../src/bucket/reader.ts";5import {6	checkFormat,7	escapePath,8	listEventIds,9	listRepos,10	readEvent,11	readHead,12	readObject,13	readSave,14} from "../src/bucket/repo.ts";15import { type Fixture, makeKohBucket } from "./fixture.ts";1617let fx: Fixture;18beforeAll(async () => {19	fx = await makeKohBucket();20});2122const bucket = () => openBucket({ kind: "folder", path: fx.bucketDir });2324describe("reading a koh3-produced folder remote", () => {25	test("lists the repo", async () => {26		expect(await listRepos(bucket())).toEqual([fx.repo]);27	});2829	test("reads format, head, the save record and one verified object end to end", async () => {30		const b = bucket();31		await checkFormat(b, fx.repo);32		const head = await readHead(b, fx.repo);33		expect(head).toBe(fx.saveIds[0] as string);3435		const save = await readSave(b, fx.repo, head as string);36		expect(save.message).toBe("say hello");37		expect(save.is_checkpoint).toBe(false);38		const checkpoint = await readSave(b, fx.repo, save.parent);39		expect(checkpoint.is_checkpoint).toBe(true);40		const first = await readSave(b, fx.repo, checkpoint.parent);41		expect([first.agent, first.session]).toEqual(["test-agent", "fixture"]);4243		const main = save.files.find((file) => file.path === "src/main.ts");44		const { bytes } = await readObject(b, fx.repo, main?.hash as string);45		expect(new TextDecoder().decode(bytes)).toBe("// Entry point.\nconsole.log('hello');\n");46	});4748	test("a missing key reads as null and a listing is prefix-scoped", async () => {49		const b = bucket();50		expect(await b.get(`${fx.repo}/nothing-here`)).toBeNull();51		const saves = await b.list(`${fx.repo}/saves/`);52		expect(saves.sort()).toEqual(fx.saveIds.map((id) => `${fx.repo}/saves/${id}`).sort());53	});54});5556test("the S3 reader pages through listings and reads a missing key as null", async () => {57	const pageSize = 2;58	const server = Bun.serve({59		port: 0,60		async fetch(req) {61			const url = new URL(req.url);62			const folder = bucket();63			if (url.searchParams.get("list-type") === "2") {64				const all = await folder.list(url.searchParams.get("prefix") ?? "");65				const start = Number(url.searchParams.get("continuation-token") ?? 0);66				const page = all.slice(start, start + pageSize);67				const more = start + pageSize < all.length;68				const contents = page.map((key) => `<Contents><Key>${key}</Key></Contents>`).join("");69				const next = more70					? `<NextContinuationToken>${start + pageSize}</NextContinuationToken>`71					: "";72				return new Response(73					`<ListBucketResult><IsTruncated>${more}</IsTruncated>${contents}${next}</ListBucketResult>`,74				);75			}76			const bytes = await folder.get(url.pathname.replace("/kohbucket/", ""));77			if (bytes) return new Response(bytes);78			return new Response("<Error><Code>NoSuchKey</Code></Error>", { status: 404 });79		},80	});81	try {82		const s3 = openBucket({83			kind: "s3",84			endpoint: `http://localhost:${server.port}`,85			name: "kohbucket",86			region: "fr-par",87			accessKeyId: "read",88			secretAccessKey: "only",89		});90		expect(await s3.list(`${fx.repo}/`)).toEqual(await bucket().list(`${fx.repo}/`));91		expect(await readHead(s3, fx.repo)).toBe(fx.saveIds[0] as string);92		// koh3 writes `<repo>/format` on its first offer (§9.2), so it is present;93		// a key that is not there is what reads as null.94		const format = await s3.get(`${fx.repo}/format`);95		expect(format === null ? null : new TextDecoder().decode(format)).toBe("koh-remote 1");96		expect(await s3.get(`${fx.repo}/no-such-key`)).toBeNull();97		const url = new URL(98			s3.downloadUrl("demo/releases/1.0/android-arm64/a b.apk", "a b.apk", "t/x"),99		);100		expect(url.pathname).toBe("/kohbucket/demo/releases/1.0/android-arm64/a%20b.apk");101		expect(url.searchParams.get("X-Amz-Expires")).toBe("300");102		expect(url.searchParams.get("response-content-disposition")).toBe(103			'attachment; filename="a b.apk"',104		);105		expect(url.searchParams.get("response-content-type")).toBe("t/x");106	} finally {107		server.stop();108	}109});110111describe("the format rule", () => {112	test("accepts koh-remote 1 and refuses anything else", async () => {113		const { bucketDir, repo } = await makeKohBucket("formatted");114		const b = openBucket({ kind: "folder", path: bucketDir });115		await writeFile(join(bucketDir, repo, "format"), "koh-remote 1");116		await checkFormat(b, repo);117		await writeFile(join(bucketDir, repo, "format"), "koh-remote 2");118		await expect(checkFormat(b, repo)).rejects.toThrow('remote format "koh-remote 2"');119	});120});121122describe("object verification", () => {123	test("refuses an object whose content does not match its key", async () => {124		const b = bucket();125		const save = await readSave(b, fx.repo, fx.saveIds[0] as string);126		const [one, two] = save.files;127		const swapped = {128			...b,129			get: (key: string) => b.get(key.replace(one?.hash ?? "", two?.hash ?? "")),130		};131		await expect(readObject(swapped, fx.repo, one?.hash as string)).rejects.toThrow(132			"does not match its hash",133		);134	});135});136137describe("untrusted input", () => {138	// Starts from a genuine koh3 record and corrupts one field, as a tampered bucket would.139	async function readTampered(change: (record: Record<string, unknown>) => void) {140		const id = fx.saveIds[0] as string;141		const real = await bucket().get(`${fx.repo}/saves/${id}`);142		const record = JSON.parse(new TextDecoder().decode(real as Uint8Array));143		change(record);144		const bytes = new TextEncoder().encode(JSON.stringify(record));145		return readSave(146			{ get: async () => bytes, list: async () => [], downloadUrl: () => "" },147			fx.repo,148			id,149		);150	}151	const withPath = (path: string) => (record: Record<string, unknown>) => {152		(record.files as { path: string }[])[0] = { ...(record.files as { path: string }[])[0], path };153	};154155	test("the genuine record is accepted", async () => {156		expect((await readTampered(() => {})).message).toBe("say hello");157	});158159	// §5.5: a path Koh would refuse is quarantined, and the rest of the save still lands.160	test.each([161		"",162		"/etc/passwd",163		"../escape",164		"src/../../escape",165		"./src/main.ts",166		"src//main.ts",167		"src/",168		"src\\main.ts",169		"src/ma\0in.ts",170		"src/ma\nin.ts",171		"Icon\r",172		"a\u0085b",173		"a\ud800b",174	])("quarantines the file path %p", async (path) => {175		const save = await readTampered(withPath(path));176		expect(save.files.map((file) => file.path)).not.toContain(path);177		expect(save.files.length).toBeGreaterThan(0);178		expect(save.quarantined).toEqual([escapePath(path)]);179	});180181	test("quarantines a path that is not valid UTF-8, and keeps a real U+FFFD in a valid record", async () => {182		const id = fx.saveIds[0] as string;183		const real = await bucket().get(`${fx.repo}/saves/${id}`);184		const record = JSON.parse(new TextDecoder().decode(real as Uint8Array));185		record.files[0].path = "bad-X-name";186		const bytes = Buffer.from(JSON.stringify(record));187		bytes[bytes.indexOf("bad-X-name") + 4] = 0xff;188		const save = await readSave(189			{ get: async () => bytes, list: async () => [], downloadUrl: () => "" },190			fx.repo,191			id,192		);193		expect(save.quarantined).toEqual(["bad-\ufffd-name"]);194		expect((await readTampered(withPath("odd-\ufffd-name"))).quarantined).toEqual([]);195	});196197	test("escapes a name as Koh's escape_path does", () => {198		expect(["Icon\r", "a\\b", "a\u0085", "\x7f", "plain/ok"].map(escapePath)).toEqual([199			"Icon\\x0d",200			"a\\\\b",201			"a\\u0085",202			"\\x7f",203			"plain/ok",204		]);205	});206207	test("refuses duplicate paths in one record", async () => {208		const dupe = (record: Record<string, unknown>) => {209			const files = record.files as { path: string }[];210			files[1] = { ...files[1], path: (files[0] as { path: string }).path } as { path: string };211		};212		await expect(readTampered(dupe)).rejects.toThrow("Koh 3 schema");213	});214215	test.each([216		["parent", "not-an-id"],217		["restores", "ABCDEF123456"],218		["replayed_from", "../../../x"],219		["created_at", -1],220		["created_at", 1.5],221		["size_bytes", -10],222		["size_bytes", "12"],223	])("refuses %s = %p", async (field, value) => {224		await expect(readTampered((record) => (record[field] = value))).rejects.toThrow("Koh 3 schema");225	});226227	test("refuses bad repo names and save IDs before building a key", async () => {228		const b = bucket();229		for (const repo of ["..", "a/b", "", "../demo"]) {230			await expect(checkFormat(b, repo)).rejects.toThrow("not a repo name");231			await expect(readHead(b, repo)).rejects.toThrow("not a repo name");232			await expect(readSave(b, repo, fx.saveIds[0] as string)).rejects.toThrow("not a repo name");233			await expect(readObject(b, repo, "0".repeat(64))).rejects.toThrow("not a repo name");234		}235		for (const id of ["../head", "abc", "ABCDEF123456", `${fx.saveIds[0]}0`]) {236			await expect(readSave(b, fx.repo, id)).rejects.toThrow("not a save ID");237		}238	});239});240241describe("event records", () => {242	test("reads a genuine koh3 flag event and refuses a tampered one", async () => {243		const b = bucket();244		const [id] = await listEventIds(b, fx.repo);245		const event = await readEvent(b, fx.repo, id as string);246		expect([event.kind, event.save_id, event.reason]).toEqual([247			"flag",248			fx.saveIds[3] as string,249			"holds a secret",250		]);251		const stripAll = { kind: "strip", save_id: "", path: "secret.txt" };252		const accepted = new TextEncoder().encode(JSON.stringify({ ...event, ...stripAll }));253		const reader = { get: async () => accepted, list: async () => [], downloadUrl: () => "" };254		expect((await readEvent(reader, fx.repo, event.id)).kind).toBe("strip");255		for (const change of [256			{ kind: "delete" },257			{ path: "../x" },258			{ save_id: "nope" },259			{ kind: "flag", save_id: "" },260			{ kind: "clear", save_id: "" },261			{ kind: "flag", path: "secret.txt" },262			{ kind: "clear", path: "secret.txt" },263			{ kind: "strip", path: "" },264			{ kind: "strip", save_id: "", path: "" },265		]) {266			const bytes = new TextEncoder().encode(JSON.stringify({ ...event, ...change }));267			const tampered = { get: async () => bytes, list: async () => [], downloadUrl: () => "" };268			await expect(readEvent(tampered, fx.repo, event.id)).rejects.toThrow("Koh 3 schema");269		}270	});271});