fix(media-use): a manifest record whose path leaves the project is not reused or credited (#8396)

This commit is contained in:
Miguel Ángel
2026-11-21 20:57:38 +00:00
committed by GitHub
parent 22b394c663
commit e3a0797a33
2 changed files with 101 additions and 12 deletions
+15 -11
View File
@@ -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,
+86 -1
View File
@@ -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({