diff --git a/packages/cli/src/media-use/lib/codex-provider.mjs b/packages/cli/src/media-use/lib/codex-provider.mjs index c4e96b7501..5ed772597a 100644 --- a/packages/cli/src/media-use/lib/codex-provider.mjs +++ b/packages/cli/src/media-use/lib/codex-provider.mjs @@ -1,7 +1,8 @@ import { execFileSync } from "node:child_process"; -import { copyFileSync, existsSync, readdirSync, statSync, unlinkSync } from "node:fs"; -import { homedir, tmpdir } from "node:os"; -import { join } from "node:path"; +import { copyFileSync, existsSync, readdirSync, statSync } from "node:fs"; +import { homedir } from "node:os"; +import { dirname, join } from "node:path"; +import { discardPrivateOutput, privateOutputPath } from "./private-output.mjs"; // Image generation via the OpenAI Codex CLI's built-in image tool (gpt-image-2) // on the user's ChatGPT subscription: the codex CLI owns auth, media-use holds @@ -107,17 +108,21 @@ export async function codexImageGenerate(intent) { console.error(`media-use: codex image upsell unavailable: ${reason}`); return null; } - const outPath = join(tmpdir(), `media-use-codex-${process.pid}-${Date.now()}.png`); + // Codex works and saves in a fresh private folder. `project_root_markers=[]` keeps it from + // reading AGENTS.md in the folders above (the shared temp folder on Linux); every Codex with + // the image tool honours it. + const outPath = privateOutputPath("codex", ".png"); + const result = generateIn(outPath, intent, flag); + if (!result) discardPrivateOutput(outPath); + return result; +} + +function generateIn(outPath, intent, flag) { const prompt = `${intent}\n\n` + `Use your built-in image generation tool to render this, then save the image ` + `to ${outPath} (overwrite if it exists). Do not ask for confirmation. ` + `If you have no built-in image tool, do nothing (no PIL/matplotlib/SVG substitute).`; - try { - unlinkSync(outPath); // clear any prior file so verify-first can't accept a stale render - } catch { - /* no prior file */ - } const started = Date.now(); try { execFileSync( @@ -125,7 +130,9 @@ export async function codexImageGenerate(intent) { [ "exec", "--cd", - tmpdir(), + dirname(outPath), + "-c", + "project_root_markers=[]", "-s", "workspace-write", "--skip-git-repo-check", diff --git a/packages/cli/src/media-use/lib/codex-provider.test.mjs b/packages/cli/src/media-use/lib/codex-provider.test.mjs index e3a54815b5..36cbec5edf 100644 --- a/packages/cli/src/media-use/lib/codex-provider.test.mjs +++ b/packages/cli/src/media-use/lib/codex-provider.test.mjs @@ -1,6 +1,89 @@ import test from "node:test"; import assert from "node:assert"; -import { imageGenFlag } from "./codex-provider.mjs"; +import { + chmodSync, + existsSync, + mkdirSync, + mkdtempSync, + readFileSync, + rmSync, + writeFileSync, +} from "node:fs"; +import { tmpdir } from "node:os"; +import { basename, delimiter, dirname, join } from "node:path"; +import { codexImageGenerate, imageGenFlag } from "./codex-provider.mjs"; + +// A stand-in `codex` on PATH: `features list` offers the image tool; `exec` records its +// arguments, renders nothing, and plants a file where an attacker could (EVIL_PATH). +function fakeCodex(t) { + const root = mkdtempSync(join(tmpdir(), "fake-codex-")); + const bin = join(root, "bin"); + mkdirSync(bin); + writeFileSync( + join(bin, "codex"), + [ + "#!/bin/sh", + 'case "$1" in', + ' features) echo "image_generation stable true" ;;', + ' exec) printf "%s\\n" "$@" > "$CODEX_LOG"; cat > /dev/null;', + ' [ -n "$EVIL_PATH" ] && printf EVIL > "$EVIL_PATH" ;;', + "esac", + "", + ].join("\n"), + ); + chmodSync(join(bin, "codex"), 0o755); + const codexHome = join(root, "codex-home"); + mkdirSync(codexHome); + writeFileSync(join(codexHome, "auth.json"), "{}"); + const saved = { PATH: process.env.PATH, CODEX_HOME: process.env.CODEX_HOME }; + process.env.PATH = `${bin}${delimiter}${process.env.PATH}`; + process.env.CODEX_HOME = codexHome; + process.env.CODEX_LOG = join(root, "exec.log"); + t.after(() => { + process.env.PATH = saved.PATH; + if (saved.CODEX_HOME === undefined) delete process.env.CODEX_HOME; + else process.env.CODEX_HOME = saved.CODEX_HOME; + if (process.env.EVIL_PATH) rmSync(process.env.EVIL_PATH, { force: true }); + delete process.env.CODEX_LOG; + delete process.env.EVIL_PATH; + rmSync(root, { recursive: true, force: true }); + }); + return () => readFileSync(process.env.CODEX_LOG, "utf8").trim().split("\n"); +} + +const posixOnly = { skip: process.platform === "win32" }; + +test( + "codex works in a fresh private temp folder and is told not to look above it", + posixOnly, + async (t) => { + const execLog = fakeCodex(t); + assert.equal(await codexImageGenerate("a red cube"), null); + const args = execLog(); + const cwd = args[args.indexOf("--cd") + 1]; + assert.equal(dirname(cwd), tmpdir()); + assert.match(basename(cwd), /^media-use-codex-/); + assert.deepEqual(args.slice(args.indexOf("--cd"), args.indexOf("--cd") + 4), [ + "--cd", + cwd, + "-c", + "project_root_markers=[]", + ]); + assert.equal(existsSync(cwd), false, "a folder with no image is removed"); + }, +); + +test( + "a file planted at the old guessable temp path is not taken as the image", + posixOnly, + async (t) => { + fakeCodex(t); + const now = 1_700_000_000_000; + t.mock.method(Date, "now", () => now); + process.env.EVIL_PATH = join(tmpdir(), `media-use-codex-${process.pid}-${now}.png`); + assert.equal(await codexImageGenerate("a red cube"), null); + }, +); // Real `codex features list` output is three space-padded columns: // diff --git a/packages/cli/src/media-use/lib/ltx-video-provider.mjs b/packages/cli/src/media-use/lib/ltx-video-provider.mjs index 497a345099..f800a754b9 100644 --- a/packages/cli/src/media-use/lib/ltx-video-provider.mjs +++ b/packages/cli/src/media-use/lib/ltx-video-provider.mjs @@ -1,9 +1,9 @@ import { execFileSync } from "node:child_process"; import { existsSync, unlinkSync } from "node:fs"; -import { homedir, tmpdir } from "node:os"; -import { join } from "node:path"; +import { homedir } from "node:os"; import { describeDownload, probeSpecs } from "./specs.mjs"; import { buildArgv, selectModel, selectModelLadder } from "./local-models.mjs"; +import { discardPrivateOutput, privateOutputPath } from "./private-output.mjs"; export async function ltxVideoGenerate( intent, @@ -22,7 +22,7 @@ export async function ltxVideoGenerate( return null; } - // Each attempt mints its own timestamped output path, so a partial artifact + // Each attempt gets its own private output folder, so a partial artifact // from a failed tier is orphaned rather than overwritten - and a lower tier // then succeeding hides it. Discard it before demoting. Best-effort: a // partial we cannot remove must never mask the real failure. @@ -32,6 +32,7 @@ export async function ltxVideoGenerate( } catch { // nothing actionable: the generate failure below is the real story } + discardPrivateOutput(path); }; // Walk the whole ladder, best tier first. A tier that cannot run on this @@ -50,7 +51,7 @@ export async function ltxVideoGenerate( continue; } - const outPath = join(tmpdir(), `media-use-ltx-${process.pid}-${Date.now()}.mp4`); + const outPath = privateOutputPath("ltx", ".mp4"); const argv = buildArgv(model.invoke, { prompt: intent, w: ctx?.width || 512, @@ -76,6 +77,7 @@ export async function ltxVideoGenerate( continue; } if (!pathExists(outPath)) { + discardPartial(outPath); console.error( `media-use: local video gen (${model.id}) exited cleanly but wrote no output file`, ); diff --git a/packages/cli/src/media-use/lib/ltx-video-provider.test.mjs b/packages/cli/src/media-use/lib/ltx-video-provider.test.mjs index 0c922c3370..5c0ca907a3 100644 --- a/packages/cli/src/media-use/lib/ltx-video-provider.test.mjs +++ b/packages/cli/src/media-use/lib/ltx-video-provider.test.mjs @@ -1,5 +1,6 @@ import { test } from "node:test"; import assert from "node:assert/strict"; +import { existsSync } from "node:fs"; import { dirname } from "node:path"; import { homedir, tmpdir } from "node:os"; import { ltxVideoGenerate } from "./ltx-video-provider.mjs"; @@ -89,8 +90,8 @@ test("successful generation returns the generated MP4 result", async () => { assert.ok(result); assert.equal(calls.length, 2); - assert.equal(dirname(result.localPath), tmpdir()); - assert.match(result.localPath, /media-use-ltx-\d+-\d+\.mp4$/); + assert.equal(dirname(dirname(result.localPath)), tmpdir()); + assert.match(result.localPath, /media-use-ltx-[^/\\]+[/\\]output\.mp4$/); assert.deepEqual(result, { localPath: result.localPath, ext: ".mp4", @@ -293,6 +294,7 @@ test("every tier failing discards every partial, one per attempt", async (t) => assert.equal(result, null); assert.equal(attempted.length, 2, "both tiers attempted"); assert.deepEqual(unlinked, attempted, "nothing is left behind on the all-fail path"); + for (const path of attempted) assert.equal(existsSync(dirname(path)), false, path); }); test("a successful generation is never discarded", async () => { diff --git a/packages/cli/src/media-use/lib/mflux-provider.mjs b/packages/cli/src/media-use/lib/mflux-provider.mjs index af5511ad85..d965dcb822 100644 --- a/packages/cli/src/media-use/lib/mflux-provider.mjs +++ b/packages/cli/src/media-use/lib/mflux-provider.mjs @@ -1,9 +1,8 @@ import { execFileSync } from "node:child_process"; import { existsSync, unlinkSync } from "node:fs"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; import { describeDownload, probeSpecs } from "./specs.mjs"; import { buildArgv, selectModelLadder } from "./local-models.mjs"; +import { discardPrivateOutput, privateOutputPath } from "./private-output.mjs"; // Local image generation via mflux (FLUX-on-MLX), the Mac-native runner. // Spec-gated: selectModelLadder("imagegen", specs) returns every FLUX-class @@ -39,7 +38,7 @@ export async function mfluxImageGenerate( const ladder = selectModelLadder("imagegen", specs, { preferTier: ctx?.preferTier }); if (!ladder.length) return null; // no local model fits -> codex upsell/fallback - // Each attempt mints its own timestamped output path, so a partial artifact + // Each attempt gets its own private output folder, so a partial artifact // from a failed tier is orphaned rather than overwritten - and a lower tier // then succeeding hides it. Discard it before demoting. Best-effort: a // partial we cannot remove must never mask the real failure. @@ -49,6 +48,7 @@ export async function mfluxImageGenerate( } catch { // nothing actionable: the generate failure below is the real story } + discardPrivateOutput(path); }; // Best tier first, demoting past any tier that cannot run here (runner off @@ -69,7 +69,7 @@ export async function mfluxImageGenerate( continue; } - const outPath = join(tmpdir(), `media-use-mflux-${process.pid}-${Date.now()}.png`); + const outPath = privateOutputPath("mflux", ".png"); const vars = { prompt: intent, w: ctx?.width || 512, @@ -86,6 +86,7 @@ export async function mfluxImageGenerate( why = `hf download failed: ${err.stderr?.toString().trim().slice(-200) || err.message}`; } if (!snap) { + discardPartial(outPath); console.error(`media-use: local image gen (${model.id}): ${why}`); continue; } @@ -108,6 +109,7 @@ export async function mfluxImageGenerate( continue; } if (!pathExists(outPath)) { + discardPartial(outPath); console.error( `media-use: local image gen (${model.id}) exited cleanly but wrote no output file`, ); diff --git a/packages/cli/src/media-use/lib/private-output.mjs b/packages/cli/src/media-use/lib/private-output.mjs new file mode 100644 index 0000000000..9ee736b7e1 --- /dev/null +++ b/packages/cli/src/media-use/lib/private-output.mjs @@ -0,0 +1,21 @@ +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { basename, dirname, join } from "node:path"; + +// Where a local generator writes its file: inside a fresh folder that only this account can +// open (mkdtemp: an unguessable name, created exclusively, mode 0700). A guessable name in the +// shared temp folder, which is /tmp on Linux, could be created first by another account and +// then taken as the generated file. +export function privateOutputPath(provider, ext) { + return join(mkdtempSync(join(tmpdir(), `media-use-${provider}-`)), `output${ext}`); +} + +// Remove the folder privateOutputPath made for `path`, once its file is copied into the project. +// Any other path is left alone: providers also return files they do not own, such as a sound in +// the bundled SFX library. +export function discardPrivateOutput(path) { + const dir = dirname(path); + if (dirname(dir) !== tmpdir() || !/^media-use-[a-z]+-[A-Za-z0-9]{6}$/.test(basename(dir))) return; + if (!/^output\.[a-z0-9]+$/.test(basename(path))) return; + rmSync(dir, { recursive: true, force: true }); +} diff --git a/packages/cli/src/media-use/lib/private-output.test.mjs b/packages/cli/src/media-use/lib/private-output.test.mjs new file mode 100644 index 0000000000..60e9e2342f --- /dev/null +++ b/packages/cli/src/media-use/lib/private-output.test.mjs @@ -0,0 +1,56 @@ +import test from "node:test"; +import assert from "node:assert"; +import { existsSync, mkdirSync, mkdtempSync, rmSync, statSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { basename, dirname, join } from "node:path"; +import { discardPrivateOutput, privateOutputPath } from "./private-output.mjs"; + +const ownerOnly = (path) => (statSync(path).mode & 0o777).toString(8); +// Output folders land in the real temp folder; remove them with the test. +const removeAfter = (t, path) => { + t.after(() => rmSync(path, { recursive: true, force: true })); + return path; +}; + +test("each output goes in its own new folder, not straight in the shared temp folder", (t) => { + const first = privateOutputPath("mflux", ".png"); + const second = privateOutputPath("mflux", ".png"); + removeAfter(t, dirname(first)); + removeAfter(t, dirname(second)); + assert.equal(dirname(dirname(first)), tmpdir()); + assert.match(basename(dirname(first)), /^media-use-mflux-/); + assert.equal(basename(first), "output.png"); + assert.notEqual(dirname(first), dirname(second)); +}); + +test("only this account can open an output folder", { skip: process.platform === "win32" }, (t) => { + const dir = removeAfter(t, dirname(privateOutputPath("ltx", ".mp4"))); + assert.equal(ownerOnly(dir), "700"); +}); + +test("discarding a generator's output removes the folder it was made in", (t) => { + const out = privateOutputPath("kokoro", ".wav"); + removeAfter(t, dirname(out)); + writeFileSync(out, "voice"); + discardPrivateOutput(out); + assert.equal(existsSync(dirname(out)), false); +}); + +test("discarding leaves any file a generator does not own", (t) => { + const elsewhere = removeAfter(t, mkdtempSync(join(tmpdir(), "bundled-sfx-"))); + const library = join(elsewhere, "sfx", "whoosh.wav"); + mkdirSync(dirname(library), { recursive: true }); + writeFileSync(library, "whoosh"); + const lookalikeParent = removeAfter(t, mkdtempSync(join(tmpdir(), "not-temp-root-"))); + const lookalike = join(lookalikeParent, "media-use-codex-abcdef", "output.png"); + mkdirSync(dirname(lookalike), { recursive: true }); + writeFileSync(lookalike, "x"); + const otherTool = join(removeAfter(t, mkdtempSync(join(tmpdir(), "other-tool-"))), "output.png"); + writeFileSync(otherTool, "x"); + + for (const path of [library, lookalike, otherTool]) discardPrivateOutput(path); + + assert.equal(existsSync(library), true); + assert.equal(existsSync(lookalike), true); + assert.equal(existsSync(otherTool), true); +}); diff --git a/packages/cli/src/media-use/lib/tts-local-provider.mjs b/packages/cli/src/media-use/lib/tts-local-provider.mjs index d0a2dcda9f..d346276dfb 100644 --- a/packages/cli/src/media-use/lib/tts-local-provider.mjs +++ b/packages/cli/src/media-use/lib/tts-local-provider.mjs @@ -1,8 +1,7 @@ import { execFileSync } from "node:child_process"; import { existsSync, statSync } from "node:fs"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; import { ffprobeBinary } from "./ff-binaries.mjs"; +import { discardPrivateOutput, privateOutputPath } from "./private-output.mjs"; import { resolveSpawnCommand } from "../../audio/scripts/lib/tts.mjs"; // Local voiceover via the packaged Kokoro-82M TTS (the `hyperframes tts` CLI), @@ -41,7 +40,7 @@ export async function localTtsGenerate( pathExists = existsSync, ) { const ffprobe = ffprobeBinary(); - const outPath = join(tmpdir(), `media-use-kokoro-${process.pid}-${Date.now()}.wav`); + const outPath = privateOutputPath("kokoro", ".wav"); const argv = ["hyperframes", "tts", intent, "--output", outPath]; if (ctx?.voice) argv.push("--voice", ctx.voice); if (ctx?.lang && ctx.lang !== "en") argv.push("--lang", ctx.lang); @@ -59,6 +58,7 @@ export async function localTtsGenerate( ); } catch (err) { console.error(`media-use: local voice not enabled (kokoro). ${err.message}`); + discardPrivateOutput(outPath); return null; } if (!resolved) { @@ -68,6 +68,7 @@ export async function localTtsGenerate( "media-use: local voice not enabled (kokoro). Cannot run npx on Windows: " + "npm's npx-cli.js was not found (install npm with Node, or run via npx/npm run so npm_execpath is set).", ); + discardPrivateOutput(outPath); return null; } try { @@ -82,9 +83,13 @@ export async function localTtsGenerate( ? "install for free on-device voice: pip install kokoro-onnx soundfile (or set HYPERFRAMES_PYTHON to a venv that has it)" : out.slice(-200) || err.message; console.error(`media-use: local voice not enabled (kokoro). ${hint}`); + discardPrivateOutput(outPath); + return null; + } + if (!existsSync(outPath) || statSync(outPath).size === 0) { + discardPrivateOutput(outPath); return null; } - if (!existsSync(outPath) || statSync(outPath).size === 0) return null; return { localPath: outPath, ext: ".wav", diff --git a/packages/cli/src/media-use/resolve.mjs b/packages/cli/src/media-use/resolve.mjs index fe79c9356f..989f478704 100644 --- a/packages/cli/src/media-use/resolve.mjs +++ b/packages/cli/src/media-use/resolve.mjs @@ -70,6 +70,7 @@ import { } from "./lib/sfx-provider.mjs"; import { writeSfxCredit } from "./lib/openverse.mjs"; import { terminalText } from "./lib/terminal-text.mjs"; +import { discardPrivateOutput } from "./lib/private-output.mjs"; import { mediaVectorRows, rankMediaRowsWithVectors } from "./lib/local-media-search.mjs"; const INGEST_TYPES = listTypes(); @@ -564,11 +565,9 @@ async function run() { // 5. freeze and register atomically. const ext = searchResult.ext || extFromUrl(searchResult.url || "") || defaultExt(type); - const { id, localPath, fullPath } = await withReservedFile( - projectDir, - type, - ext, - async (reservation) => { + let reserved; + try { + reserved = await withReservedFile(projectDir, type, ext, async (reservation) => { if (searchResult.localPath) { freezeLocalFile(searchResult.localPath, reservation.fullPath); } else if (searchResult.url) { @@ -577,8 +576,12 @@ async function run() { throw new Error("provider returned no url or localPath"); } return reservation; - }, - ); + }); + } finally { + // A local generator's private output folder is not needed once the file is in the project. + if (searchResult.localPath) discardPrivateOutput(searchResult.localPath); + } + const { id, localPath, fullPath } = reserved; const record = { id, diff --git a/packages/cli/src/media-use/resolve.test.mjs b/packages/cli/src/media-use/resolve.test.mjs index 0ed1c36437..f71f734646 100644 --- a/packages/cli/src/media-use/resolve.test.mjs +++ b/packages/cli/src/media-use/resolve.test.mjs @@ -1738,3 +1738,39 @@ globalThis.fetch = async (url, init) => { rmSync(otherProject, { recursive: true, force: true }); cleanup(); }); + +test("a local generator's private output folder is gone once the file is in the project", () => { + setup(); + const privateTmp = join(tmp, "tmp"); + mkdirSync(privateTmp); + // A stand-in for `hyperframes tts`: write a few bytes to the --output path it is given. + const entry = join(tmp, "fake-cli.mjs"); + writeFileSync( + entry, + 'import { writeFileSync } from "node:fs";\n' + + 'const at = process.argv.indexOf("--output");\n' + + 'writeFileSync(process.argv[at + 1], "RIFF-voice");\n', + ); + const result = spawnResolve( + [ + "--type", + "voice", + "--intent", + "hello there", + "--project", + tmp, + "--provider", + "kokoro.local", + "--json", + ], + { env: { HOME: tmp, TMPDIR: privateTmp, HYPERFRAMES_CLI_ENTRY: entry } }, + ); + assert.equal(result.status, 0, result.stderr); + const parsed = JSON.parse(result.stdout); + assert.equal(readFileSync(join(tmp, parsed.path), "utf8"), "RIFF-voice"); + assert.deepEqual( + readdirSync(privateTmp).filter((name) => name.startsWith("media-use-")), + [], + ); + cleanup(); +});