Skip to content

Harden --prompt-file against paths outside the working directory#289

Open
lohengrin332 wants to merge 1 commit intoopenai:mainfrom
lohengrin332:harden-prompt-file-containment
Open

Harden --prompt-file against paths outside the working directory#289
lohengrin332 wants to merge 1 commit intoopenai:mainfrom
lohengrin332:harden-prompt-file-containment

Conversation

@lohengrin332
Copy link
Copy Markdown

@lohengrin332 lohengrin332 commented May 2, 2026

This is a defense-in-depth hardening PR, not a CVE. There is no remotely exploitable bug here in normal use of the plugin.

The behavior I'm changing: codex-companion.mjs task --prompt-file <path> currently does fs.readFileSync(path.resolve(cwd, value), "utf8") with no containment check. The file's contents are then forwarded to OpenAI as the Codex task prompt.

Why I think it's worth tightening: under prompt-injection-adjacent threat models — where Claude itself might be steered into composing slash command arguments — a path like --prompt-file ~/.ssh/id_rsa or a symlinked sibling file would exfiltrate user secrets to OpenAI. Constraining the resolved path (after symlink resolution) to live under cwd removes that capability without affecting any documented or supported usage.

Changes:

  • Extract readTaskPrompt to lib/task-prompt.mjs so it can be unit-tested directly. (Open to inlining instead if you'd prefer fewer files.)
  • Resolve symlinks via fs.realpathSync before the containment check.
  • Throw a clear error when the resolved path lies outside realpath(cwd).
  • Add tests/task-prompt.test.mjs with coverage for: happy paths (relative + absolute inside cwd), relative escape, absolute outside, symlink-out-of-cwd, and the no-prompt-file positional fallback.

No changes to documented behavior or any pre-existing test. Happy to adjust the error message format, the file-vs-inline factoring, or the exact set of cases covered.

Previously, `--prompt-file <path>` accepted any user-readable file
on the filesystem, including symlinks pointing outside cwd. The file
contents are forwarded to OpenAI as a Codex task prompt, so a
mistakenly or maliciously composed `--prompt-file ~/.ssh/id_rsa`
would exfiltrate that file. This is not exploitable in normal
single-user CLI use, but it is a defense-in-depth gap under
prompt-injection-adjacent threat models where Claude itself might
compose hostile arguments.

Resolve symlinks via realpathSync and reject any --prompt-file that
does not live under the realpath of cwd. Add coverage for relative
escapes, absolute paths outside cwd, and symlinks that point outside.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@lohengrin332 lohengrin332 requested a review from a team May 2, 2026 20:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant