// The one file a library's media route may name (lane posts-library), over a // temp corpus: only a picture or a video under // channels//trending/posts-media// of a channel carrying the library // marker — never the record, the raw reads, another channel, or a name that // resolves outside the directory. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { link, mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { libraryMediaSrc, resolveLibraryMedia } from "./media.mjs"; const ID = "1900000000000006001"; async function corpus() { const channels = await mkdtemp(path.join(tmpdir(), "umtool-library-media-")); const lib = path.join(channels, "trending-lib"); const cap = path.join(lib, "trending", "posts-media", ID); await mkdir(cap, { recursive: true }); await writeFile(path.join(lib, "config.json"), JSON.stringify({ handling: "transcribe", sourceKind: "social", platform: "twitter", xTrending: {} })); await writeFile(path.join(cap, "shot.png"), "SHOT"); await writeFile(path.join(cap, "capture.json"), "{}"); await writeFile(path.join(cap, "article.md"), "words"); // A media file as the store leaves it: a hardlink to its blob. await mkdir(path.join(lib, "trending", "media-store", "ab"), { recursive: true }); await writeFile(path.join(lib, "trending", "media-store", "ab", "blob.mp4"), "VIDEO-BYTES"); await link(path.join(lib, "trending", "media-store", "ab", "blob.mp4"), path.join(cap, `${ID}_1.mp4`)); // What must never be served. await mkdir(path.join(lib, "trending", "reads", "tr-01JA0000000000000000000000"), { recursive: true }); await writeFile(path.join(lib, "trending", "reads", "tr-01JA0000000000000000000000", "page.png"), "RAW-PAGE"); await writeFile(path.join(lib, "trending", "judgements.json"), "{}"); await writeFile(path.join(channels, "outside.png"), "OUTSIDE"); // An ordinary posts channel with a capture of the same shape. const account = path.join(channels, "kettle-org-x"); await mkdir(path.join(account, "posts-media", ID), { recursive: true }); await mkdir(path.join(account, "trending", "posts-media", ID), { recursive: true }); await writeFile(path.join(account, "config.json"), JSON.stringify({ handling: "transcribe", sourceKind: "social", platform: "twitter", url: "https://x.com/kettle_org" })); await writeFile(path.join(account, "trending", "posts-media", ID, "shot.png"), "NOT-A-LIBRARY"); return { channels, lib, cap }; } test("a picture and a video of a library's capture are found, with their type and size", async () => { const { channels, cap } = await corpus(); try { const shot = await resolveLibraryMedia({ channelsDir: channels, lib: "trending-lib", id: ID, name: "shot.png" }); assert.deepEqual(shot, { ok: true, abs: path.join(cap, "shot.png"), size: 4, type: "image/png" }); const video = await resolveLibraryMedia({ channelsDir: channels, lib: "trending-lib", id: ID, name: `${ID}_1.mp4` }); assert.equal(video.ok && video.type, "video/mp4"); assert.equal(video.ok && video.size, 11); assert.equal(libraryMediaSrc("trending-lib", ID, "shot.png"), `/api/library/media/trending-lib/${ID}/shot.png`); } finally { await rm(channels, { recursive: true, force: true }); } }); test("refused: the record and any non-media name, a traversal, another id shape, a channel that is no library, a missing file", async () => { const { channels } = await corpus(); try { const no = async (lib, id, name) => (await resolveLibraryMedia({ channelsDir: channels, lib, id, name })).ok; for (const name of ["capture.json", "article.md", "shot", ".hidden.png", "..", "../shot.png", "a/b.png", "shot.png/..", "x..png", ""]) { assert.equal(await no("trending-lib", ID, name), false, name); } for (const id of ["..", "../reads", "_avatars", "", "19a", `${ID}/..`]) assert.equal(await no("trending-lib", id, "shot.png"), false, id); for (const lib of ["kettle-org-x", "nope", "..", "trending-lib/..", ""]) assert.equal(await no(lib, ID, "shot.png"), false, lib); assert.equal(await no("trending-lib", ID, "missing.png"), false); // A directory with a media-looking name is not a file. assert.equal(await no("trending-lib", "1900000000000006009", "shot.png"), false); assert.equal((await resolveLibraryMedia({ channelsDir: channels, lib: "trending-lib", id: ID, name: undefined })).ok, false); } finally { await rm(channels, { recursive: true, force: true }); } }); test("a name planted in the capture directory whose real path is OUTSIDE posts-media is refused: the raw read, the corpus, the directory itself", async () => { const { channels, lib, cap } = await corpus(); try { await symlink(path.join(lib, "trending", "reads", "tr-01JA0000000000000000000000", "page.png"), path.join(cap, "raw.png")); await symlink(path.join(channels, "outside.png"), path.join(cap, "outside.png")); await symlink(path.join(lib, "trending", "media-store", "ab", "blob.mp4"), path.join(cap, "viastore.mp4")); await symlink(path.join(lib, "trending", "reads"), path.join(lib, "trending", "posts-media", "1900000000000006002")); for (const name of ["raw.png", "outside.png", "viastore.mp4"]) { assert.deepEqual(await resolveLibraryMedia({ channelsDir: channels, lib: "trending-lib", id: ID, name }), { ok: false, status: 404 }, name); } // An id that is a link to another directory. await writeFile(path.join(lib, "trending", "reads", "x.png"), "RAW"); assert.equal((await resolveLibraryMedia({ channelsDir: channels, lib: "trending-lib", id: "1900000000000006002", name: "x.png" })).ok, false); // A link that stays inside posts-media is the file it names. await symlink(path.join(cap, "shot.png"), path.join(cap, "alias.png")); assert.equal((await resolveLibraryMedia({ channelsDir: channels, lib: "trending-lib", id: ID, name: "alias.png" })).ok, true); } finally { await rm(channels, { recursive: true, force: true }); } });