1234import { beforeAll, describe, expect, test } from "bun:test";5import { mkdir, writeFile } from "node:fs/promises";6import { join } from "node:path";7import { openBucket } from "../src/bucket/reader.ts";8import { syncAll } from "../src/bucket/sync.ts";9import { fileAt, filesAt, headId, timeline } from "../src/mirror/queries.ts";10import { openMirror } from "../src/mirror/store.ts";11import { Browser, type Instance, instance } from "./browser.ts";12import { makeRepos } from "./fixture.ts";1314describe("hidden paths", () => {15 let inst: Instance;16 let browser: Browser;17 let repos: Awaited<ReturnType<typeof makeRepos>>;18 let work: string;19 const status = async (path: string) => (await inst.app.request(path)).status;20 const text = async (path: string) => (await inst.app.request(path)).text();21 const hide = (hidden_paths: string) =>22 browser.post("/admin/repos/app", { public: "1", hidden_paths });23 24 const sync = (fail = "\0") => {25 const bucket = openBucket({ kind: "folder", path: repos.bucketDir });26 const { put, list } = bucket;27 const reach = (key: string) =>28 key.startsWith(fail) ? Promise.reject(new Error("bucket unreachable")) : Promise.resolve();29 bucket.put = (key, ...rest) => reach(key).then(() => put(key, ...rest));30 bucket.list = (prefix) => reach(prefix).then(() => list(prefix));31 return syncAll({ bucket, kepr: inst.kepr, mirrorDir: join(inst.dataDir, "mirror") });32 };33 34 const handed = async () => {35 await sync();36 const file = Bun.file(join(repos.bucketDir, ".kepr/hidden/app"));37 return (await file.exists()) ? await file.text() : null;38 };3940 beforeAll(async () => {41 repos = await makeRepos(["app"]);42 work = join(repos.bucketDir, "..", "work", "app");43 await mkdir(join(work, "private"));44 await writeFile(join(work, "private", "notes.md"), "# private\n");45 await writeFile(join(work, "tool.py"), "print(1)\n");46 await writeFile(join(work, "main.ts"), "export const a = 1;\n");47 repos.koh("app", "save", "add sources");48 repos.koh("app", "offer");49 inst = await instance(repos.bucketDir);50 browser = new Browser(inst.app);51 await browser.signIn();52 await hide("");53 });5455 test("refuses unsafe paths and changes nothing", async () => {56 for (const path of ["../x", "/etc", "a//b", "x".repeat(501)]) {57 expect((await hide(path)).status).toBe(400);58 }59 expect(await status("/app/files/tool.py")).toBe(200);60 });6162 test("a hidden file or folder is gone everywhere and handed to Koh", async () => {63 const saved = await hide("tool.py\r\nprivate/\n");64 expect(saved.status).toBe(303);65 for (const path of ["/app/files/tool.py", "/app/raw/tool.py", "/app/raw/private/notes.md"]) {66 expect(`${path} ${await status(path)}`).toBe(`${path} 404`);67 }68 expect(await status("/app/files/main.ts")).toBe(200);69 const page = await text("/app");70 expect(page).not.toContain("tool.py");71 expect(page).not.toContain("notes.md");72 expect(page).not.toContain("Python");73 const head = (await inst.app.request("/app/archive/HEAD.tar.gz")).headers.get("location");74 const tarball = new Uint8Array(await (await inst.app.request(head ?? "")).arrayBuffer());75 const listed = Bun.spawnSync(["tar", "-tzf", "-"], { stdin: tarball }).stdout.toString();76 expect(listed).toContain("main.ts");77 expect(listed).not.toContain("tool.py");78 expect(await handed()).toBe("tool.py\nprivate\n");79 expect(await (await browser.get("/admin/repos?repo=app")).text()).toContain(80 ">tool.py\nprivate</textarea>",81 );82 });8384 test("stays hidden in saves that arrive later", async () => {85 await writeFile(join(work, "tool.py"), "print(2)\n");86 repos.koh("app", "save", "change the tool");87 repos.koh("app", "offer");88 await sync();89 expect(await status("/app/files/tool.py")).toBe(404);90 expect(await text("/app")).toContain("change the tool");91 });9293 test("unhiding brings the files back", async () => {94 expect((await hide("")).status).toBe(303);95 expect(await status("/app/files/tool.py")).toBe(200);96 expect(await text("/app/raw/tool.py")).toBe("print(2)\n");97 expect(await status("/app/raw/private/notes.md")).toBe(200);98 expect(await handed()).toBe("");99 });100101 test("an old kept tarball is never served once a path is hidden, even if cleanup failed", async () => {102 const head = (await inst.app.request("/app/archive/HEAD.tar.gz")).headers.get("location") ?? "";103 await inst.app.request(head);104 const keptDir = join(repos.bucketDir, ".kepr/tarballs/app");105 const old = new Map<string, Uint8Array>();106 for (const path of new Bun.Glob("**/*.tar.gz").scanSync(keptDir)) {107 old.set(path, await Bun.file(join(keptDir, path)).bytes());108 }109 expect(old.size).toBeGreaterThan(0);110 await hide("tool.py");111 for (const [path, bytes] of old) await Bun.write(join(keptDir, path), bytes);112 const tarball = new Uint8Array(await (await inst.app.request(head)).arrayBuffer());113 const listed = Bun.spawnSync(["tar", "-tzf", "-"], { stdin: tarball }).stdout.toString();114 expect(listed).toContain("main.ts");115 expect(listed).not.toContain("tool.py");116 await hide("");117 });118119 test("a list the bucket refused is handed at a later sync", async () => {120 await hide("tool.py");121 expect(await status("/app/files/tool.py")).toBe(404);122 await sync(".kepr/hidden/");123 expect(await handed()).toBe("tool.py\n");124 await hide("");125 await sync();126 });127128 test("a list written before a failed cleanup is not undone by changing back", async () => {129 await hide("tool.py");130 await sync(".kepr/tarballs/");131 await hide("");132 expect(await handed()).toBe("");133 });134135 test("a hidden file's object is neither listed nor served on the Koh routes", async () => {136 const mirror = openMirror(join(inst.dataDir, "mirror"), "app");137 const hash = filesAt(mirror, headId(mirror) as string).find(138 (file) => file.path === "private/notes.md",139 )?.hash as string;140 await hide("private");141 expect(await text("/app/koh/objects")).not.toContain(hash);142 expect(await status(`/app/koh/objects/${hash}`)).toBe(404);143 await hide("");144 expect(await text("/app/koh/objects")).toContain(hash);145 expect(await status(`/app/koh/objects/${hash}`)).toBe(200);146 });147148 test("rebuilding a mirror keeps handles that requests already hold usable", async () => {149 const mirror = openMirror(join(inst.dataDir, "mirror"), "app");150 const count = () =>151 timeline(mirror).filter((save) => fileAt(mirror, save.id, "tool.py") !== null).length;152 await hide("tool.py");153 expect(count()).toBe(0);154 await hide("");155 expect(count()).toBeGreaterThan(0);156 });157});