Repository navigation
fix(media-use): write local generator output into private per-run folders - #8030
Conversation
b70bd5f to
0cde213
Compare
…ders The codex image provider ran `codex exec` with the shared temp folder as its project root and accepted any non-empty file at a guessable name there (media-use-codex-<pid>-<ms>.png). On a host where the temp folder is shared (/tmp on Linux), another account could plant AGENTS.md (and a .git marker) for Codex to follow, or create the output file first and have it taken as the generated image. Codex now works in a fresh mode-0700 folder under the per-user HyperFrames cache, with project_root_markers=[] so current Codex reads instructions only from that folder; the folder is removed once the image is copied out. The mflux, ltx and local Kokoro providers, which used the same guessable names, now write into a fresh mkdtemp folder too.
…r resolve Once resolve has copied a generated file into the project, the private folder it was written in is no longer needed. Only folders privateOutputPath made are removed; files a provider does not own, such as a bundled SFX library sound, are left alone.
…pty ones Codex now works in the same mkdtemp folder under the system temp folder as the other generators, with project_root_markers=[]. Every Codex release with the image tool honours that setting for AGENTS.md (the image tool landed months after it), so a per-user cache folder is not needed, and the cache path no longer has to be built outside hyperframesCacheDir. A provider that returns nothing now removes the folder it made. The helper test no longer writes a fixed-name file in the temp folder.
0cde213 to
9c67ffd
Compare
eddy-heygen
left a comment
There was a problem hiding this comment.
(Eddy's agent) Reviewed by Eddy's Claude agent at commit 9c67ffd.
Verdict: approved, good to ship.
The branch was rebased since the reviewer's pass; I compared the PR's own patch before and after and it is unchanged.
Summary. Four local media generators (the coding-agent image generator, the local FLUX image runner, the local video runner and the local voice generator) wrote their output at a guessable name directly in the system temp folder, and the coding-agent CLI was run with that temp folder as its working folder. On a host where the temp folder is shared between accounts, another account could pre-create the output file and have it taken as the generated media, or plant an instruction file that the agent CLI would follow. The PR moves every one of these outputs into a fresh mkdtemp folder (random name, owner-only), runs the agent CLI inside that folder with its project-root markers emptied, and removes the folder after the file is copied into the project or when generation fails. I found no remaining guessable-name write in the media-use folder and no new weakness; the remainders are version-dependent behaviour of the external agent CLI and some cleanup robustness points.
I read the core myself. Each local generator now writes into a fresh folder made with mkdtemp (random name, owner-only), the coding-agent tool runs inside that folder with its arguments as an array and the prompt on standard input, and the folder is removed after the file is copied out or when generation fails. The cleanup only touches folders directly under the temp folder that match the helper's own naming.
One thing I could not confirm by reading: that the external agent tool honours the setting that empties its project-root markers. If a version ignores it, the private folder alone still keeps a planted file out of its working folder, but not out of parent folders. Worth one manual check with the installed version.
Blockers
None.
Non-blockers
packages/cli/src/media-use/lib/codex-provider.mjs:135: Protection against planted instruction files depends on the external agent CLI honouring a config key that is passed unconditionally and never checked. The private working folder alone does not stop the agent CLI from walking up to a project marker planted in the shared temp folder; only-c project_root_markers=[]does. The provider still accepts older CLI builds (the older feature-flag name at lines 66-67). If such a build ignores the key, a marker plus instruction file planted in the shared temp folder would still be read on a shared-temp host. This is the gap the default branch already has in full; the PR narrows it to old CLI versions. The author's claim that every CLI with the image tool honours the key is not verifiable from the repository. To resolve: Owner to confirm against the oldest supported CLI version, or gate on a minimum CLI version, or place the working folder under a per-user location that has no other-account-writable ancestor.packages/cli/src/media-use/lib/private-output.mjs:18: Folder cleanup is not best-effort and recognises its folders by name pattern. discardPrivateOutput calls rmSync without a try/catch. In resolve.mjs:576-579 it runs in a finally after the file has been copied into the project, so a removal error (for example a file still locked on Windows) would turn a successful resolve into a failure with the file copied but not registered; in the mflux and ltx providers it sits outside the try that their own comment says must never mask the real failure. Separately, ownership is inferred from the path: the parent must equal the raw os.tmpdir() string, so a non-normalised temp setting (for example one ending in two slashes) never matches and every folder is left behind, and the two other providers that hand resolve a mkdtemp file (heygen-video-provider.mjs:115, logo-provider.mjs:237) are not matched and still leave their folders. None of this reopens the weakness: leftovers stay owner-only. To resolve: Wrap the removal in try/catch, and track created folders in a module-level set (or compare resolved paths) instead of matching names.packages/cli/src/media-use/resolve.test.mjs:1706: Tests leave folders behind and one new test is vacuous on Windows. The new resolve test redirects the temp folder with TMPDIR only, which os.tmpdir() ignores on Windows, so its 'no media-use-* folder left' assertion passes there even without the cleanup. Existing success-path unit tests for the video, image and voice providers (for example ltx-video-provider.test.mjs:93) now create real mkdtemp folders in the machine temp folder and never remove them. To resolve: Also set TEMP/TMP (or skip on Windows) in the resolve test, and remove the returned folder in the success-path provider tests.
What the change needed to do
- Generator output is no longer written at a guessable name directly in the shared temp folder; the folder is created exclusively with an unpredictable name and owner-only access: met (packages/cli/src/media-use/lib/private-output.mjs:10 uses mkdtempSync(join(tmpdir(),
media-use-<provider>-)) and returns output inside it; mkdtemp creates the folder exclusively with mode 0700 on POSIX (Windows and macOS temp folders are per-account).) - Every local generator uses the private folder; no sibling still writes a pid/timestamp name in the temp folder: met (codex-provider.mjs:114, mflux-provider.mjs:72, ltx-video-provider.mjs:54, tts-local-provider.mjs:43 all call privateOutputPath; a grep of packages/cli/src/media-use for tmpdir()/process.pid finds only mkdtemp users (heygen-video-provider.mjs:115, logo-provider.mjs:237) and unrelated lock/prefs temp names outside the temp folder. No copy of these providers exists under skills/media-use/scripts.)
- A file planted by another account cannot be accepted as the generated output: met (codex-provider.mjs:150 and mflux-provider.mjs:111 / ltx-video-provider.mjs:79 / tts-local-provider.mjs:89 check existence of outPath, which now lives inside the owner-only folder, so another account cannot create it.)
- The agent CLI does not load instruction files from the shared temp folder: partly met (codex-provider.mjs:133-135 passes --cd and -c project_root_markers=[] as separate array arguments (execFileSync, no shell). Whether every CLI version this provider supports honours that key is a property of the external tool and was not verified; the provider still supports older CLI builds via the older feature-flag name (codex-provider.mjs:66-67).)
- The private folder is removed on success and on failure, after the file is copied out, without deleting anything else: met (resolve.mjs:564-579 copies via freezeLocalFile (copyFileSync, lib/freeze.mjs:50) then discards in finally; each provider discards on every null-return path (codex-provider.mjs:116, mflux-provider.mjs:51/89/112, ltx-video-provider.mjs:35/80, tts-local-provider.mjs:61/71/86/90). private-output.mjs:18-19 only removes a folder directly under the temp folder whose name matches the helper's pattern and whose file is output.. Not removed on interruption or process.exit (folder stays, still owner-only).)
- Regression tests that fail on the old code: partly met (codex-provider.test.mjs:57-86 (arguments and planted-file case), ltx-video-provider.test.mjs:297, private-output.test.mjs, resolve.test.mjs:1681 exist; the agent CLI is a shell stand-in that only records arguments, so the tests do not show the real CLI ignores a planted instruction file, and the POSIX-only cases are skipped on Windows.)
Open outside this PR
- Behaviour of the external agent CLI is not verified: whether all supported versions honour the project-root-markers override given with -c, whether an unknown key is ignored or rejected by old versions, and whether any other project-level config is discovered from parent folders. Owner should check against the oldest supported CLI version.
- The agent CLI still loads the user's own global config and home-level instruction file and inherits the environment; those belong to the same account and are outside this weakness.
- Interruption (signal, timeout kill of the parent, or the process.exit paths in resolve) leaves the private folder behind; it remains owner-only.
- Untouched code: the hosted-video and logo providers already use mkdtemp but their folders are never removed after resolve; the local voice command receives the user's intent as a positional argument that could be read as an option if it starts with a dash (same on the default branch).
- The branch conflicts with the base branch and must be rebased; the resolve.mjs hunk around the freeze step should be re-read after the rebase, and CI re-checked since many jobs were skipped in the latest run.
CI: every check that ran passes at this commit, including the Windows test lanes.
How this was reviewed: a code-review pass and a security-review pass, by a reviewer agent in an isolated checkout of this commit; I then read the core of the fix and the cited lines myself. Nothing was built or run.
Edit accuracy did not run: 0 of 15 shards reported results; see the shard jobs.Nothing was compared with the base branch. |
What
codex.image_gen,mflux.local,ltx.localand the local Kokoro voice write their output into a freshmkdtempfolder (owner-only) under the system temp folder, instead of atmedia-use-<provider>-<pid>-<ms>.<ext>directly in it.--cd) instead of the temp folder itself, and with-c project_root_markers=[], so it reads project instructions (AGENTS.md) only from that folder.resolvecopies a generated file into the project, it removes the private folder the file was written in. Only folders the helper made are removed; files a provider does not own, such as a bundled SFX library sound, are left alone.lib/private-output.mjs, holds the folder rules.Why
Each generator wrote to a fixed-pattern name directly in the shared temp folder, and Codex ran with that folder as its working folder. Giving each run its own fresh folder keeps every run's output and working folder separate, and lets the run remove exactly what it made. Codex reads only the project instructions of its own folder.
Related work
No overlapping PR.
How
Codex looks for
AGENTS.mdfrom its working folder up to the nearest folder with a project marker (.gitby default), so a private working folder alone would still let a.githigher up make that folder the project root.project_root_markers=[]stops that walk. Codex has honoured the setting forAGENTS.mdsince openai/codex#12639 (February 2026); its image tool, which this provider requires, arrived later (openai/codex#24723, May 2026), so every Codex this provider can run honours it.Each
mkdtempsits directly under the system temp folder, so the folders stay short on Windows; the media-use engine runs unbundled and imports only Node built-ins.Test plan
private-output.test.mjs: each call makes a new folder, owner-only, under the temp folder; discarding removes such a folder and leaves a library file, a lookalike folder elsewhere and another tool's temp folder alone.resolve.test.mjscase: a local Kokoro voiceover, with a stand-inhyperframes tts, lands in the project and leaves nomedia-use-*folder in the temp folder. Fails without the cleanup inresolve.mjs.codex-provider.test.mjscases with a stand-incodexonPATH: Codex is started with--cd <private temp folder> -c project_root_markers=[], and the folder is gone when no image comes back; a file already sitting at the old temp name is not returned as the image.ltx-video-provider.test.mjs: when every tier fails, no attempt's folder is left.bun run test:skills: 737 pass, 0 fail.check-media-use-copy-parity, core'scacheDirandnoBareMkdtempguards and the comment ratchet pass. oxlint and oxfmt clean.