Skip to content

harden(codex-home): warn when $CODEX_HOME escapes $HOME (#27) - #66

Merged
thehoff merged 1 commit into
developfrom
harden/codex-home-resolve-27
May 18, 2026
Merged

thehoff merged 1 commit into
developfrom
harden/codex-home-resolve-27

Conversation

@thehoff

@thehoff thehoff commented May 18, 2026

Copy link
Copy Markdown
Owner

Summary

Closes #27 (defence-in-depth). resolve_codex_dir_from trusted $CODEX_HOME verbatim. A misconfigured env var (CODEX_HOME=/etc, CODEX_HOME=/tmp/x) would redirect contextcrawler init -g --codex writes and uninstall -g --codex deletes there.

Fix

Emit a one-line stderr WARNING when the resolved $CODEX_HOME does 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):

  • Tries fs::canonicalize on both paths (resolves symlinks + ..)
  • Falls back to lexical comparison if canonicalize fails (first-run init writes to non-existing dirs)
  • Returns true iff resolved codex doesn't start with resolved home

Empirical

Invocation Behaviour
CODEX_HOME=/etc/x init -g --codex --dry-run WARNING fires, continues
CODEX_HOME=$HOME/x init -g --codex --dry-run silent
unset CODEX_HOME; init -g --codex --dry-run silent (default ~/.codex)
init --codex (no -g) unaffected (local mode never consults $CODEX_HOME)

Tests

2 new pinning tests:

  • test_codex_home_path_escapes_home — lexical fallback for non-existing paths
  • test_resolve_codex_dir_returns_path_even_when_outside_home — behaviour contract (WARN not BAIL)

Verified

  • cargo build clean, 0 warnings
  • cargo test --bin contextcrawler — 2119 pass / 0 fail
  • All 9 integration suites green

Closes #27.

`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.
@thehoff
thehoff merged commit 9f5d944 into develop May 18, 2026
@thehoff
thehoff deleted the harden/codex-home-resolve-27 branch May 18, 2026 13:38
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.

defence-in-depth: $CODEX_HOME from env is trusted verbatim — no canonicalization or outside-$HOME check

1 participant