bucket.test.ts

12.1 KB · raw

1import { beforeAll, describe, expect, test } from "bun:test";2import { cp, writeFile } from "node:fs/promises";3import { join } from "node:path";4import { inflateSync } from "node:zlib";5import { openBucket } from "../src/bucket/reader.ts";6import {7	checkFormat,8	escapePath,9	listEventIds,10	listRepos,11	readEvent,12	readHead,13	readObject,14	readSave,15} from "../src/bucket/repo.ts";16import { type Fixture, makeKohBucket } from "./fixture.ts";1718let fx: Fixture;19beforeAll(async () => {20	fx = await makeKohBucket();21});2223const bucket = () => openBucket({ kind: "folder", path: fx.bucketDir });2425describe("reading a koh3-produced folder remote", () => {26	test("lists the repo", async () => {27		expect(await listRepos(bucket())).toEqual([fx.repo]);28	});2930	test("a folder without a head, or Kepr's own, is not a repo", async () => {31		// As a rename leaves the new name until it copies `head`, last.32		await cp(join(fx.bucketDir, fx.repo, "saves"), join(fx.bucketDir, "half", "saves"), {33			recursive: true,34		});35		await Bun.write(join(fx.bucketDir, ".kepr/hidden", fx.repo), "secret.txt\n");36		expect(await listRepos(bucket())).toEqual([fx.repo]);37	});3839	test("reads format, head, the save record and one verified object end to end", async () => {40		const b = bucket();41		await checkFormat(b, fx.repo);42		const head = await readHead(b, fx.repo);43		expect(head).toBe(fx.saveIds[0] as string);4445		const save = await readSave(b, fx.repo, head as string);46		expect(save.message).toBe("say hello");47		expect(save.is_checkpoint).toBe(false);48		const checkpoint = await readSave(b, fx.repo, save.parent);49		expect(checkpoint.is_checkpoint).toBe(true);50		const first = await readSave(b, fx.repo, checkpoint.parent);51		expect([first.agent, first.session]).toEqual(["test-agent", "fixture"]);5253		const main = save.files.find((file) => file.path === "src/main.ts");54		const { compressed, size } = await readObject(b, fx.repo, main?.hash as string);55		const text = "// Entry point.\nconsole.log('hello');\n";56		expect(new TextDecoder().decode(inflateSync(compressed))).toBe(text);57		expect(size).toBe(text.length);58	});5960	test("a missing key reads as null and a listing is prefix-scoped", async () => {61		const b = bucket();62		expect(await b.get(`${fx.repo}/nothing-here`)).toBeNull();63		const saves = await b.list(`${fx.repo}/saves/`);64		expect(saves.sort()).toEqual(fx.saveIds.map((id) => `${fx.repo}/saves/${id}`).sort());65	});66});6768test("the S3 reader pages through listings with sizes and reads a missing key as null", async () => {69	const pageSize = 2;70	const server = Bun.serve({71		port: 0,72		async fetch(req) {73			const url = new URL(req.url);74			const folder = bucket();75			if (url.searchParams.get("list-type") === "2") {76				const sizes = await folder.sizes(url.searchParams.get("prefix") ?? "");77				const all = [...sizes.keys()];78				const start = Number(url.searchParams.get("continuation-token") ?? 0);79				const page = all.slice(start, start + pageSize);80				const more = start + pageSize < all.length;81				const contents = page82					.map((key) => `<Contents><Key>${key}</Key><Size>${sizes.get(key)}</Size></Contents>`)83					.join("");84				const next = more85					? `<NextContinuationToken>${start + pageSize}</NextContinuationToken>`86					: "";87				return new Response(88					`<ListBucketResult><IsTruncated>${more}</IsTruncated>${contents}${next}</ListBucketResult>`,89				);90			}91			const bytes = await folder.get(url.pathname.replace("/kohbucket/", ""));92			if (bytes) return new Response(bytes);93			return new Response("<Error><Code>NoSuchKey</Code></Error>", { status: 404 });94		},95	});96	try {97		const s3 = openBucket({98			kind: "s3",99			endpoint: `http://localhost:${server.port}`,100			name: "kohbucket",101			region: "fr-par",102			accessKeyId: "read",103			secretAccessKey: "only",104		});105		expect(await s3.list(`${fx.repo}/`)).toEqual(await bucket().list(`${fx.repo}/`));106		const sizes = await s3.sizes(`${fx.repo}/`);107		expect(sizes).toEqual(await bucket().sizes(`${fx.repo}/`));108		expect(sizes.get(`${fx.repo}/head`)).toBe(12);109		expect(await readHead(s3, fx.repo)).toBe(fx.saveIds[0] as string);110		// koh3 writes `<repo>/format` on its first offer (§9.2), so it is present;111		// a key that is not there is what reads as null.112		const format = await s3.get(`${fx.repo}/format`);113		expect(format === null ? null : new TextDecoder().decode(format)).toBe("koh-remote 1");114		expect(await s3.get(`${fx.repo}/no-such-key`)).toBeNull();115		const url = new URL(116			s3.downloadUrl("demo/releases/1.0/android-arm64/a b.apk", "a b.apk", "t/x"),117		);118		expect(url.pathname).toBe("/kohbucket/demo/releases/1.0/android-arm64/a%20b.apk");119		expect(url.searchParams.get("X-Amz-Expires")).toBe("300");120		expect(url.searchParams.get("response-content-disposition")).toBe(121			'attachment; filename="a b.apk"',122		);123		expect(url.searchParams.get("response-content-type")).toBe("t/x");124	} finally {125		server.stop();126	}127});128129describe("the format rule", () => {130	test("accepts koh-remote 1 and refuses anything else", async () => {131		const { bucketDir, repo } = await makeKohBucket("formatted");132		const b = openBucket({ kind: "folder", path: bucketDir });133		await writeFile(join(bucketDir, repo, "format"), "koh-remote 1");134		await checkFormat(b, repo);135		await writeFile(join(bucketDir, repo, "format"), "koh-remote 2");136		await expect(checkFormat(b, repo)).rejects.toThrow('remote format "koh-remote 2"');137	});138});139140describe("object verification", () => {141	test("refuses an object whose content does not match its key", async () => {142		const b = bucket();143		const save = await readSave(b, fx.repo, fx.saveIds[0] as string);144		const [one, two] = save.files;145		const swapped = {146			...b,147			get: (key: string) => b.get(key.replace(one?.hash ?? "", two?.hash ?? "")),148		};149		await expect(readObject(swapped, fx.repo, one?.hash as string)).rejects.toThrow(150			"does not match its hash",151		);152	});153});154155describe("untrusted input", () => {156	// Starts from a genuine koh3 record and corrupts one field, as a tampered bucket would.157	async function readTampered(change: (record: Record<string, unknown>) => void) {158		const id = fx.saveIds[0] as string;159		const real = await bucket().get(`${fx.repo}/saves/${id}`);160		const record = JSON.parse(new TextDecoder().decode(real as Uint8Array));161		change(record);162		const bytes = new TextEncoder().encode(JSON.stringify(record));163		return readSave(164			{165				get: async () => bytes,166				list: async () => [],167				folders: async () => [],168				size: async () => 0,169				sizes: async () => new Map(),170				downloadUrl: () => "",171				viewUrl: () => null,172				put: async () => {},173				remove: async () => {},174				copy: async () => {},175				removeRenamed: async () => {},176			},177			fx.repo,178			id,179		);180	}181	const withPath = (path: string) => (record: Record<string, unknown>) => {182		(record.files as { path: string }[])[0] = { ...(record.files as { path: string }[])[0], path };183	};184185	test("the genuine record is accepted", async () => {186		expect((await readTampered(() => {})).message).toBe("say hello");187	});188189	// §5.5: a path Koh would refuse is quarantined, and the rest of the save still lands.190	test.each([191		"",192		"/etc/passwd",193		"../escape",194		"src/../../escape",195		"./src/main.ts",196		"src//main.ts",197		"src/",198		"src\\main.ts",199		"src/ma\0in.ts",200		"src/ma\nin.ts",201		"Icon\r",202		"a\u0085b",203	])("quarantines the file path %p", async (path) => {204		const save = await readTampered(withPath(path));205		expect(save.files.map((file) => file.path)).not.toContain(path);206		expect(save.files.length).toBeGreaterThan(0);207		expect(save.quarantined).toEqual([escapePath(path)]);208	});209210	test("quarantines bytes outside UTF-8 and unpaired surrogate escapes, and keeps a real U+FFFD", async () => {211		const id = fx.saveIds[0] as string;212		const real = await bucket().get(`${fx.repo}/saves/${id}`);213		const record = JSON.parse(new TextDecoder().decode(real as Uint8Array));214		record.files[0].path = "bad-X-name";215		record.files[1].path = "odd-\ufffd-name";216		record.files[2].path = "lead-\ud800";217		record.files[3].path = "pair-\ud83d\ude00";218		const bytes = Buffer.from(JSON.stringify(record));219		bytes[bytes.indexOf("bad-X-name") + 4] = 0xff;220		const save = await readSave(221			{222				get: async () => bytes,223				list: async () => [],224				folders: async () => [],225				size: async () => 0,226				sizes: async () => new Map(),227				downloadUrl: () => "",228				viewUrl: () => null,229				put: async () => {},230				remove: async () => {},231				copy: async () => {},232				removeRenamed: async () => {},233			},234			fx.repo,235			id,236		);237		expect(save.quarantined.sort()).toEqual(["bad-\\xff-name", "lead-\\xed\\xa0\\x80"]);238		expect(save.files.map((file) => file.path)).toContain("odd-\ufffd-name");239		expect(save.files.map((file) => file.path)).toContain("pair-\ud83d\ude00");240	});241242	test("escapes a name as Koh's escape_path does", () => {243		expect(["Icon\r", "a\\b", "a\u0085", "\x7f", "plain/ok"].map(escapePath)).toEqual([244			"Icon\\x0d",245			"a\\\\b",246			"a\\u0085",247			"\\x7f",248			"plain/ok",249		]);250	});251252	test("refuses duplicate paths in one record", async () => {253		const dupe = (record: Record<string, unknown>) => {254			const files = record.files as { path: string }[];255			files[1] = { ...files[1], path: (files[0] as { path: string }).path } as { path: string };256		};257		await expect(readTampered(dupe)).rejects.toThrow("Koh 3 schema");258	});259260	test("accepts the six-character IDs that Koh 2's early saves keep through migration", async () => {261		expect((await readTampered((record) => (record.parent = "af96c8"))).parent).toBe("af96c8");262	});263264	test.each([265		["parent", "not-an-id"],266		["parent", "af96c"],267		["parent", "af96c8a"],268		["restores", "ABCDEF123456"],269		["replayed_from", "../../../x"],270		["created_at", -1],271		["created_at", 1.5],272		["size_bytes", -10],273		["size_bytes", "12"],274	])("refuses %s = %p", async (field, value) => {275		await expect(readTampered((record) => (record[field] = value))).rejects.toThrow("Koh 3 schema");276	});277278	test("refuses bad repo names and save IDs before building a key", async () => {279		const b = bucket();280		for (const repo of ["..", "a/b", "", "../demo"]) {281			await expect(checkFormat(b, repo)).rejects.toThrow("not a repo name");282			await expect(readHead(b, repo)).rejects.toThrow("not a repo name");283			await expect(readSave(b, repo, fx.saveIds[0] as string)).rejects.toThrow("not a repo name");284			await expect(readObject(b, repo, "0".repeat(64))).rejects.toThrow("not a repo name");285		}286		for (const id of ["../head", "abc", "ABCDEF123456", `${fx.saveIds[0]}0`]) {287			await expect(readSave(b, fx.repo, id)).rejects.toThrow("not a save ID");288		}289	});290});291292describe("event records", () => {293	test("reads a genuine koh3 flag event and refuses a tampered one", async () => {294		const b = bucket();295		const [id] = await listEventIds(b, fx.repo);296		const event = await readEvent(b, fx.repo, id as string);297		expect([event.kind, event.save_id, event.reason]).toEqual([298			"flag",299			fx.saveIds[3] as string,300			"holds a secret",301		]);302		const stripAll = { kind: "strip", save_id: "", path: "secret.txt" };303		const accepted = new TextEncoder().encode(JSON.stringify({ ...event, ...stripAll }));304		const reader = {305			get: async () => accepted,306			list: async () => [],307			folders: async () => [],308			size: async () => 0,309			sizes: async () => new Map(),310			downloadUrl: () => "",311			viewUrl: () => null,312			put: async () => {},313			remove: async () => {},314			copy: async () => {},315			removeRenamed: async () => {},316		};317		expect((await readEvent(reader, fx.repo, event.id)).kind).toBe("strip");318		for (const change of [319			{ kind: "delete" },320			{ path: "../x" },321			{ save_id: "nope" },322			{ kind: "flag", save_id: "" },323			{ kind: "clear", save_id: "" },324			{ kind: "flag", path: "secret.txt" },325			{ kind: "clear", path: "secret.txt" },326			// A strip with an illegal path, "" included, parses; sync ignores it (sync.test.ts).327		]) {328			const bytes = new TextEncoder().encode(JSON.stringify({ ...event, ...change }));329			const tampered = {330				get: async () => bytes,331				list: async () => [],332				folders: async () => [],333				size: async () => 0,334				sizes: async () => new Map(),335				downloadUrl: () => "",336				viewUrl: () => null,337				put: async () => {},338				remove: async () => {},339				copy: async () => {},340				removeRenamed: async () => {},341			};342			await expect(readEvent(tampered, fx.repo, event.id)).rejects.toThrow("Koh 3 schema");343		}344	});345});