import { test, type TestContext } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, mkdir, writeFile, readFile, rm, symlink } from "node:fs/promises"; import path from "node:path"; import { tmpdir } from "node:os"; import { openExternalMedia } from "./externalMediaResolver-server"; // Synthetic roots only. The resolver accepts a config and reference so neither // tests nor this leaf need to open operator settings or the live corpus. async function fixture(t: TestContext) { const root = await mkdtemp(path.join(tmpdir(), "external-resolver-")); t.after(() => rm(root, { recursive: true, force: true })); const source = path.join(root, "source"); await mkdir(source); await writeFile(path.join(source, "video.mp4"), "original bytes"); return { root, source, config: { sourceId: "library-1", directory: source }, reference: { version: 1, kind: "external", sourceId: "library-1", relativePath: "video.mp4" } }; } async function linkOrSkip(t: TestContext, target: string, link: string, directory = false) { try { await symlink(target, link, directory ? "junction" : "file"); return true; } catch (error) { if (process.platform === "win32" && (error as NodeJS.ErrnoException).code === "EPERM") { t.skip("Windows symlink privilege unavailable; verify in Linux container"); return false; } throw error; } } test("inspect opens read-only, reports size, and preserves original bytes", async (t) => { const f = await fixture(t); const got = await openExternalMedia({ ...f, purpose: "inspect" }); assert.equal(got.status, "ready"); if (got.status !== "ready") return; try { assert.equal(got.bytes, Buffer.byteLength("original bytes")); assert.equal(await got.handle.readFile("utf8"), "original bytes"); await assert.rejects(got.handle.write(Buffer.from("overwrite"), 0, 9, 0)); } finally { await got.handle.close(); } assert.equal(await readFile(path.join(f.source, "video.mp4"), "utf8"), "original bytes"); }); test("playback requires explicit opt-in; source mismatch is refused", async (t) => { const f = await fixture(t); assert.deepEqual(await openExternalMedia({ ...f, purpose: "playback" }), { status: "refused", reason: "playback-disabled" }); assert.deepEqual(await openExternalMedia({ ...f, reference: { ...f.reference, sourceId: "different" }, purpose: "inspect" }), { status: "refused", reason: "source-mismatch" }); const got = await openExternalMedia({ ...f, config: { ...f.config, playback: true }, purpose: "playback" }); assert.equal(got.status, "ready"); if (got.status === "ready") await got.handle.close(); }); test("absolute and traversal references are refused before file access", async (t) => { const f = await fixture(t); for (const relativePath of ["../video.mp4", "/etc/video.mp4", "a/../../video.mp4", "C:/video.mp4", "a\\video.mp4"]) { assert.deepEqual(await openExternalMedia({ ...f, reference: { ...f.reference, relativePath }, purpose: "inspect" }), { status: "refused", reason: "invalid-reference" }); } }); test("missing configuration and unsupported runtime purposes fail closed", async (t) => { const f = await fixture(t); assert.deepEqual(await openExternalMedia({ ...f, config: null, purpose: "inspect" }), { status: "refused", reason: "invalid-config" }); assert.deepEqual(await openExternalMedia({ ...f, purpose: "other" as "inspect" }), { status: "refused", reason: "playback-disabled" }); }); test("missing root or media is unavailable without changing the reference", async (t) => { const f = await fixture(t); const before = JSON.stringify(f.reference); await rm(path.join(f.source, "video.mp4")); assert.deepEqual(await openExternalMedia({ ...f, purpose: "inspect" }), { status: "refused", reason: "unavailable" }); await rm(f.source, { recursive: true }); assert.deepEqual(await openExternalMedia({ ...f, purpose: "inspect" }), { status: "refused", reason: "unavailable" }); assert.equal(JSON.stringify(f.reference), before); }); test("directory masquerading as MP4 is not a regular file", async (t) => { const f = await fixture(t); await rm(path.join(f.source, "video.mp4")); await mkdir(path.join(f.source, "video.mp4")); assert.deepEqual(await openExternalMedia({ ...f, purpose: "inspect" }), { status: "refused", reason: "not-regular" }); }); test("literal Unicode and percent paths resolve without URL decoding", async (t) => { const f = await fixture(t); const relativePath = "nested/A 'title' $money ⧸ %2e%2e.mp4"; await mkdir(path.join(f.source, "nested")); await writeFile(path.join(f.source, relativePath), "literal"); const got = await openExternalMedia({ ...f, reference: { ...f.reference, relativePath }, purpose: "inspect" }); assert.equal(got.status, "ready"); if (got.status === "ready") { try { assert.equal(await got.handle.readFile("utf8"), "literal"); } finally { await got.handle.close(); } } }); test("final-file symlinks are rejected even when their target is within the library", async (t) => { const f = await fixture(t); if (!await linkOrSkip(t, path.join(f.source, "video.mp4"), path.join(f.source, "alias.mp4"))) return; assert.deepEqual(await openExternalMedia({ ...f, reference: { ...f.reference, relativePath: "alias.mp4" }, purpose: "inspect" }), { status: "refused", reason: "unsafe-path" }); }); test("ancestor symlink escape and sibling-prefix trick are rejected", async (t) => { const f = await fixture(t); const sibling = f.source + "-outside"; await mkdir(sibling); await writeFile(path.join(sibling, "video.mp4"), "outside"); if (!await linkOrSkip(t, sibling, path.join(f.source, "escape"), true)) return; assert.deepEqual(await openExternalMedia({ ...f, reference: { ...f.reference, relativePath: "escape/video.mp4" }, purpose: "inspect" }), { status: "refused", reason: "unsafe-path" }); }); test("operator configured root may itself be a directory link", async (t) => { const f = await fixture(t); const link = path.join(f.root, "configured-root"); if (!await linkOrSkip(t, f.source, link, true)) return; const got = await openExternalMedia({ ...f, config: { ...f.config, directory: link }, purpose: "inspect" }); assert.equal(got.status, "ready"); if (got.status === "ready") await got.handle.close(); });