From 2a243573389f4410794689d2db526dbf24ba2720 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Sun, 22 Nov 2026 02:13:04 +0000 Subject: [PATCH] fix(media-use): a manifest path naming a share or another root is refused without looking at it (#8437) --- .../cli/src/media-use/lib/record-path.mjs | 50 ++++++++++++ .../src/media-use/lib/record-path.test.mjs | 81 +++++++++++++++++++ packages/cli/src/media-use/resolve.mjs | 23 +----- packages/cli/src/media-use/resolve.test.mjs | 56 +++++++++++++ 4 files changed, 190 insertions(+), 20 deletions(-) create mode 100644 packages/cli/src/media-use/lib/record-path.mjs create mode 100644 packages/cli/src/media-use/lib/record-path.test.mjs diff --git a/packages/cli/src/media-use/lib/record-path.mjs b/packages/cli/src/media-use/lib/record-path.mjs new file mode 100644 index 0000000000..6fee74e792 --- /dev/null +++ b/packages/cli/src/media-use/lib/record-path.mjs @@ -0,0 +1,50 @@ +import * as nodeFs from "node:fs"; +import * as nodePath from "node:path"; + +// A leading slash or backslash (a root, a UNC share, a device path) or a drive letter. +const ROOTED = /^(?:[\\/]|[A-Za-z]:)/; + +/** + * The path a manifest record names, judged from its text alone: null when the text is rooted + * (absolute, a UNC share, a device path, a drive) or climbs out of the project. The manifest is + * written by whoever made the project, so a path it names outside the project is never looked at: + * on Windows, merely asking whether `\\host\share\x` exists signs in to that host. + */ +export function recordPathInProject(projectDir, recordPath, path = nodePath) { + if (typeof recordPath !== "string" || !recordPath || ROOTED.test(recordPath)) return null; + const root = path.resolve(projectDir); + const full = path.resolve(root, recordPath); + const inside = path.relative(root, full); + return inside && + inside !== ".." && + !inside.startsWith(`..${path.sep}`) && + !path.isAbsolute(inside) + ? full + : null; +} + +/** The path of `path` inside the project once links are followed, or null when it is the project itself or leaves it. */ +export function insideProject(projectDir, path, { fs = nodeFs, pathApi = nodePath } = {}) { + const real = (at) => (fs.existsSync(at) ? fs.realpathSync(at) : at); + const inside = pathApi.relative(real(projectDir), real(path)); + return inside && + inside !== ".." && + !inside.startsWith(`..${pathApi.sep}`) && + !pathApi.isAbsolute(inside) + ? inside + : null; +} + +/** Whether a manifest record can be handed back as is: its file is in the project, or it is an inline grade. */ +export function recordAvailable(projectDir, record, { fs = nodeFs, pathApi = nodePath } = {}) { + if (!record) return false; + if (record.path) { + const path = recordPathInProject(projectDir, record.path, pathApi); + return ( + path !== null && + insideProject(projectDir, path, { fs, pathApi }) !== null && + fs.existsSync(path) + ); + } + return record.type === "grade" && record.grading; +} diff --git a/packages/cli/src/media-use/lib/record-path.test.mjs b/packages/cli/src/media-use/lib/record-path.test.mjs new file mode 100644 index 0000000000..26a5c66285 --- /dev/null +++ b/packages/cli/src/media-use/lib/record-path.test.mjs @@ -0,0 +1,81 @@ +import { strict as assert } from "node:assert"; +import { win32 } from "node:path"; +import { test } from "node:test"; +import { recordAvailable, recordPathInProject } from "./record-path.mjs"; + +const PROJECT = "C:\\work\\proj"; + +// A file system that answers from a set of present paths and records every path it is asked about. +function spyFs(present = []) { + const asked = []; + const has = new Set(present.map((p) => p.toLowerCase())); + return { + asked, + existsSync: (p) => (asked.push(p), has.has(String(p).toLowerCase())), + realpathSync: (p) => (asked.push(p), p), + }; +} + +const OUTSIDE = [ + "\\\\evil-host\\share\\boom.mp3", + "//evil-host/share/boom.mp3", + "\\\\?\\UNC\\evil-host\\share\\boom.mp3", + "//?/UNC/evil-host/share/boom.mp3", + "\\\\.\\C:\\work\\proj\\boom.mp3", + "//./C:/work/proj/boom.mp3", + "\\\\?\\C:\\work\\proj\\boom.mp3", + "D:\\boom.mp3", + "D:boom.mp3", + "C:\\work\\proj\\boom.mp3", + "\\work\\proj\\boom.mp3", + "..\\outside\\boom.mp3", + "../outside/boom.mp3", + "assets\\..\\..\\outside\\boom.mp3", + "..", +]; + +for (const path of OUTSIDE) { + test(`a Windows manifest path ${JSON.stringify(path)} is refused from its text, with no file-system call`, () => { + const fs = spyFs([PROJECT, path]); + assert.equal(recordPathInProject(PROJECT, path, win32), null); + assert.equal( + recordAvailable(PROJECT, { id: "sfx_001", type: "sfx", path }, { fs, pathApi: win32 }), + false, + ); + assert.deepEqual(fs.asked, []); + }); +} + +for (const path of [ + "assets\\sfx\\boom.mp3", + "assets/sfx/boom.mp3", + ".media/audio/sfx/sfx_001.mp3", +]) { + test(`a Windows manifest path ${JSON.stringify(path)} inside the project is checked on disk`, () => { + const file = win32.resolve(PROJECT, path); + const fs = spyFs([PROJECT, file]); + assert.equal(recordPathInProject(PROJECT, path, win32), file); + assert.equal( + recordAvailable(PROJECT, { id: "sfx_001", type: "sfx", path }, { fs, pathApi: win32 }), + true, + ); + assert.ok(fs.asked.length > 0); + assert.ok( + fs.asked.every((asked) => + win32.resolve(asked).toLowerCase().startsWith(PROJECT.toLowerCase()), + ), + ); + }); +} + +test("an inline grade with no path is available without touching the file system", () => { + const fs = spyFs(); + assert.ok( + recordAvailable( + PROJECT, + { type: "grade", grading: { preset: "warm" } }, + { fs, pathApi: win32 }, + ), + ); + assert.deepEqual(fs.asked, []); +}); diff --git a/packages/cli/src/media-use/resolve.mjs b/packages/cli/src/media-use/resolve.mjs index 34181e0135..da16c23d9c 100644 --- a/packages/cli/src/media-use/resolve.mjs +++ b/packages/cli/src/media-use/resolve.mjs @@ -1,8 +1,8 @@ #!/usr/bin/env node import { spawnSync } from "node:child_process"; -import { existsSync, statSync, renameSync, rmSync, realpathSync } from "node:fs"; -import { resolve, join, extname, basename, relative, isAbsolute, sep } from "node:path"; +import { existsSync, statSync, renameSync, rmSync } from "node:fs"; +import { resolve, join, extname, basename, sep } from "node:path"; import { parseArgs } from "node:util"; import { AGENT_SOURCES, @@ -19,6 +19,7 @@ import { withReservedFileSync, } from "./lib/manifest.mjs"; import { regenerateIndex } from "./lib/index-gen.mjs"; +import { insideProject, recordAvailable } from "./lib/record-path.mjs"; import { cacheGet, cacheGetByEntity, importFromCache, cachePut } from "./lib/cache.mjs"; import { runCapability, @@ -339,24 +340,6 @@ 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) { - const path = resolve(projectDir, record.path); - return insideProject(projectDir, path) !== null && existsSync(path); - } - return record.type === "grade" && record.grading; -} - // Sparse `{ authMethod }` for a heygen-family provider name (e.g. "heygen.tts"), // else `{}` — keeps auth_method telemetry absent for every non-heygen resolve // instead of implying an auth method that doesn't apply. diff --git a/packages/cli/src/media-use/resolve.test.mjs b/packages/cli/src/media-use/resolve.test.mjs index b9898f2bb0..480d2dd6de 100644 --- a/packages/cli/src/media-use/resolve.test.mjs +++ b/packages/cli/src/media-use/resolve.test.mjs @@ -592,6 +592,62 @@ for (const [name, place] of [ }); } +// Node args that log, into `log`, every path naming evil-host that resolve asks the file system about. +function fsProbeLog(log) { + const spy = `import fs from "node:fs"; +import { syncBuiltinESMExports } from "node:module"; +const append = fs.appendFileSync; +for (const name of ["existsSync", "statSync", "lstatSync", "realpathSync", "openSync", "accessSync", "readFileSync"]) { + const real = fs[name]; + const spied = function (path, ...rest) { + if (String(path).includes("evil-host")) append(${JSON.stringify(log)}, String(path) + "\\n"); + return real.call(this, path, ...rest); + }; + Object.assign(spied, real); + fs[name] = spied; +} +syncBuiltinESMExports();`; + return [`--import=data:text/javascript,${encodeURIComponent(spy)}`]; +} + +// fails without the fix: resolve asked the file system whether //evil-host/share/boom.mp3 exists, which on +// Windows signs in to that host. +for (const path of [ + "//evil-host/share/boom.mp3", + "\\\\evil-host\\share\\boom.mp3", + "//?/UNC/evil-host/share/boom.mp3", + "\\\\?\\UNC\\evil-host\\share\\boom.mp3", +]) { + test(`a manifest record naming the share ${JSON.stringify(path)} is refused without looking at it`, () => { + setup(); + try { + const log = join(tmp, "probes.log"); + writeFileSync(log, ""); + appendRecord( + tmp, + makeRecord({ id: "sfx_001", type: "sfx", path, provenance: { prompt: "whoosh" } }), + ); + const result = spawnResolve( + ["--type", "sfx", "--intent", "whoosh", "--project", tmp, "--json"], + { + env: { HOME: tmp, PATH: tmp }, + nodeArgs: [...fsProbeLog(log), ...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(log, "utf8"), ""); + assert.ok( + !readFileSync(join(tmp, ".media/audio/sfx/CREDITS.md"), "utf8").includes("evil-host"), + ); + } finally { + cleanup(); + } + }); +} + // fails without the fix: the manifest's own provider claim wrote "Sam Sound" into the credits file. test("a manifest record inside the project is reused, but its provider claim writes no credit", () => { setup();