Repository navigation
harden(codex-home): warn when $CODEX_HOME escapes $HOME (#27) - #66
Merged
Merged
Conversation
`resolve_codex_dir_from()` returned whatever `$CODEX_HOME` env var
contained without sanity-checking it. A malicious or accidentally-set
env var (`CODEX_HOME=/etc`, `CODEX_HOME=/tmp/x`) would redirect
`contextcrawler init -g --codex` / `uninstall -g --codex` writes and
deletes there.
## Fix
Cheapest paranoid check: emit a one-line stderr WARNING when the
resolved `$CODEX_HOME` does not start with `$HOME`. Don't bail —
upstream OpenAI Codex CLI follows the same convention and legit
non-home setups exist (e.g. NixOS, sandboxed pipelines, CI runners).
The warning makes accidental misconfiguration visible BEFORE the
binary writes a config file or deletes one.
New pure helper `codex_home_path_escapes_home(codex_home, home)`:
- Tries `fs::canonicalize` on both paths to resolve symlinks / `..`
- Falls back to lexical comparison if canonicalize fails (first-run
init writes to a not-yet-existing dir)
- Returns `true` iff the resolved codex path does not start with the
resolved home path
## Pinning tests
- `test_codex_home_path_escapes_home`: lexical fallback for paths
that don't exist yet — `/etc`, `/tmp/x`, `/var/folders` flagged;
`/Users/test/.codex`, `/Users/test/custom-codex` not flagged.
- `test_resolve_codex_dir_returns_path_even_when_outside_home`: pins
the behaviour contract — function STILL returns the escaping path
(defence-in-depth WARNS, doesn't BAIL); matches upstream Codex CLI.
## Verified
- cargo build clean: 0 warnings
- cargo test --bin contextcrawler: 2119 pass / 0 fail (+2 new pins)
- All 9 integration suites green
- Empirical:
- `CODEX_HOME=/etc/x contextcrawler init -g --codex --dry-run`
-> WARNING fires + continues
- `CODEX_HOME=$HOME/test contextcrawler init -g --codex --dry-run`
-> silent (path inside home)
- `unset CODEX_HOME; contextcrawler init -g --codex --dry-run`
-> silent (default ~/.codex)
- `init --codex` (no `-g`) unaffected — local mode never consulted
`$CODEX_HOME` anyway.
Closes #27.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #27 (defence-in-depth).
resolve_codex_dir_fromtrusted$CODEX_HOMEverbatim. A misconfigured env var (CODEX_HOME=/etc,CODEX_HOME=/tmp/x) would redirectcontextcrawler init -g --codexwrites anduninstall -g --codexdeletes there.Fix
Emit a one-line stderr WARNING when the resolved
$CODEX_HOMEdoes not start with$HOME. Don't bail — upstream Codex CLI accepts the same convention and legit non-home setups exist.New pure helper
codex_home_path_escapes_home(codex_home, home):fs::canonicalizeon both paths (resolves symlinks +..)trueiff resolved codex doesn't start with resolved homeEmpirical
CODEX_HOME=/etc/x init -g --codex --dry-runCODEX_HOME=$HOME/x init -g --codex --dry-rununset CODEX_HOME; init -g --codex --dry-runinit --codex(no-g)Tests
2 new pinning tests:
test_codex_home_path_escapes_home— lexical fallback for non-existing pathstest_resolve_codex_dir_returns_path_even_when_outside_home— behaviour contract (WARN not BAIL)Verified
cargo buildclean, 0 warningscargo test --bin contextcrawler— 2119 pass / 0 failCloses #27.