From e3a0797a3308b3345cb891cab38b06b51fbf2f14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sat, 21 Nov 2026 20:57:38 +0000 Subject: [PATCH] fix(media-use): a manifest record whose path leaves the project is not reused or credited (#8396) --- packages/cli/src/media-use/resolve.mjs | 26 +++--- packages/cli/src/media-use/resolve.test.mjs | 87 ++++++++++++++++++++- 2 files changed, 101 insertions(+), 12 deletions(-) diff --git a/packages/cli/src/media-use/resolve.mjs b/packages/cli/src/media-use/resolve.mjs index 887a74c138..1e2f777c0e 100644 --- a/packages/cli/src/media-use/resolve.mjs +++ b/packages/cli/src/media-use/resolve.mjs @@ -339,9 +339,21 @@ if (args.provider && !providerMatches(args.type, args.provider)) { process.exit(2); } +// The path of `path` inside the project once links are followed, or null when it is the project itself or leaves it. +function insideProject(projectDir, path) { + const real = (at) => (existsSync(at) ? realpathSync(at) : at); + const inside = relative(real(projectDir), real(path)); + return inside && inside !== ".." && !inside.startsWith(`..${sep}`) && !isAbsolute(inside) + ? inside + : null; +} + function recordAvailable(projectDir, record) { if (!record) return false; - if (record.path) return existsSync(join(projectDir, record.path)); + if (record.path) { + const path = resolve(projectDir, record.path); + return insideProject(projectDir, path) !== null && existsSync(path); + } return record.type === "grade" && record.grading; } @@ -935,16 +947,8 @@ async function ingest(src) { console.error("error: --source records media files; a LUT or grade is ingested without it"); process.exit(2); } - const real = (path) => (existsSync(path) ? realpathSync(path) : path); - const inProject = args.source && !isUrl ? relative(real(projectDir), real(resolve(src))) : null; - if ( - inProject && - inProject !== ".." && - !inProject.startsWith(`..${sep}`) && - !isAbsolute(inProject) - ) { - return recordProjectFile(inProject.split(sep).join("/")); - } + const inProject = args.source && !isUrl ? insideProject(projectDir, resolve(src)) : null; + if (inProject) return recordProjectFile(inProject.split(sep).join("/")); const ext = extname(isUrl ? new URL(src).pathname : src) || defaultExt(type); const { id, localPath, fullPath } = await withReservedFile( projectDir, diff --git a/packages/cli/src/media-use/resolve.test.mjs b/packages/cli/src/media-use/resolve.test.mjs index 76aba7d66a..5bf3f6f2ac 100644 --- a/packages/cli/src/media-use/resolve.test.mjs +++ b/packages/cli/src/media-use/resolve.test.mjs @@ -8,8 +8,9 @@ import { existsSync, readdirSync, chmodSync, + symlinkSync, } from "node:fs"; -import { join } from "node:path"; +import { dirname, join } from "node:path"; import { tmpdir } from "node:os"; import { createServer } from "node:http"; import { execFileSync, spawn, spawnSync } from "node:child_process"; @@ -524,6 +525,90 @@ test("project manifest hit skips providers", () => { cleanup(); }); +// An Openverse sound recorded in the manifest under `path`, its file written at `file`. +function recordedSound(path, file) { + appendRecord( + tmp, + makeRecord({ + id: "sfx_001", + type: "sfx", + path, + provenance: { + provider: "openverse.audio", + license: "by", + credit: "Sam Sound", + source_url: "https://freesound.org/s/1/", + prompt: "whoosh", + }, + }), + ); + mkdirSync(dirname(file), { recursive: true }); + writeFileSync(file, "outside bytes"); +} + +for (const [name, place] of [ + [ + "leaves the project through ..", + () => ["../outside/boom.mp3", join(tmp, "..", "outside", "boom.mp3")], + ], + [ + "is absolute", + () => [join(tmp, "..", "outside", "boom.mp3"), join(tmp, "..", "outside", "boom.mp3")], + ], + [ + "goes through a folder link out of the project", + () => { + mkdirSync(join(tmp, "assets"), { recursive: true }); + mkdirSync(join(tmp, "..", "outside"), { recursive: true }); + symlinkSync(join(tmp, "..", "outside"), join(tmp, "assets", "linked"), "dir"); + return ["assets/linked/boom.mp3", join(tmp, "..", "outside", "boom.mp3")]; + }, + ], +]) { + test(`a manifest record whose path ${name} is neither reused nor credited`, () => { + setup(); + tmp = join(tmp, "proj"); + mkdirSync(tmp); + try { + const [path, file] = place(); + recordedSound(path, file); + const result = spawnResolve( + ["--type", "sfx", "--intent", "whoosh", "--project", tmp, "--json"], + { + env: { HOME: tmp, PATH: tmp }, + nodeArgs: openverseImport([OPENVERSE_BY_WHOOSH]), + }, + ); + assert.equal(result.status, 0, result.stderr); + const parsed = JSON.parse(result.stdout); + assert.notEqual(parsed._source, "cached"); + assert.notEqual(parsed.path, path); + assert.equal(readFileSync(file, "utf8"), "outside bytes"); + assert.ok(!readFileSync(join(tmp, ".media/audio/sfx/CREDITS.md"), "utf8").includes(path)); + } finally { + tmp = dirname(tmp); + cleanup(); + } + }); +} + +test("a manifest record whose path is inside the project is reused and credited", () => { + setup(); + recordedSound("assets/sfx/boom.mp3", join(tmp, "assets", "sfx", "boom.mp3")); + const result = spawnResolve(["--type", "sfx", "--intent", "whoosh", "--project", tmp, "--json"], { + env: { HOME: tmp, PATH: tmp }, + nodeArgs: openverseImport([]), + }); + assert.equal(result.status, 0, result.stderr); + assert.equal(JSON.parse(result.stdout)._source, "cached"); + assert.ok( + readFileSync(join(tmp, ".media/audio/sfx/CREDITS.md"), "utf8").includes( + "`assets/sfx/boom.mp3`: Sam Sound", + ), + ); + cleanup(); +}); + test("a manifest hit prints control characters from the record as visible escapes", () => { setup(); const record = makeRecord({