Repository navigation
fix(test): keep test processes out of the real Claude config directory - #6835
Conversation
#6775) A bare `bun test` pruned the developer's real ~/.claude/agents/ocx-*.md. The preload rewrites HOME after Bun has started, Bun's os.homedir() keeps the start-time home, so claudeConfigDir() resolved the real directory and the management toggles' agent sync deleted the generated files there. - scripts/test.ts pins CLAUDE_CONFIG_DIR and GROK_HOME inside the sandbox and hands the developer's own Claude directory to the guard. - claudeConfigDir() reads HOME (USERPROFILE on Windows) at call time, as Claude Code's Node runtime does; production resolves the same path. - The armed test-home guard refuses Claude agent sync and gateway-cache writes into the real Claude config directory and protects it from removal. - The toggle test passes an explicit agents directory.
…cept writers (#6775) Review found three ways past the first guard: an agents/cache child linked into the real directory, a case alias of a protected root that did not exist when the guard loaded, and the intercept settings.json and served-catalog writers, which never called it. Writers now pass every directory they touch, roots are resolved at check time and compared case-insensitively on macOS and Windows, and both intercept writers call the guard outside their best-effort catches.
…ty (#6775) Second review round: writeFileSync followed a link at gateway-models.json, a child named "..fixture" was read as outside the root, and removal protection still compared the import-time Claude roots case-sensitively. The cache file and each agent definition path are now judged where they resolve, containment rejects only a leading ".." component, and protectedRemovalReason checks the Claude directory with the same current-root, case-folding comparison.
…p checkout content removable (#6775) Third review round: splitting relative() on both separators read a POSIX child named "..\fixture" as outside the root, and the new Claude removal check dropped the checkout-content lift the other protected trees keep. Containment now rejects only ".." or "..<sep>", and content of a checkout that lives inside the Claude directory stays removable while the root and its ancestors do not.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 23 minutes. View limit detailsLimit details: You’ve used all 10 included reviews currently available. Review configuration: ⚙️ Run configuration
📒 Files selected for processing (16)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
✅ Deterministic PR hygiene checks passed. |
* docs(devlog): record the #6777 fixture implementation * docs(devlog): record the #6776 fixture implementation * docs(devlog): close the N3 test-infra unit (#6775, #6777, #6776) Moves devlog/_plan/261009_n3_test_infra to devlog/_fin with an Outcome section recording #6835 (7f642e3), #6833 (6d7e5f4) and #6834 (c4baa30), their exact-head CI, the union check, and the scope left as follow-ups. * docs(devlog): say which N3 CI jobs were skipped
Summary
Fixes #6775. A bare
bun test <file>, whichbunfig.tomldescribes as sandboxed bytests/preload.ts, deleted the developer's real~/.claude/agents/ocx-*.md.The preload rewrites
HOMEafter Bun has started, and Bun'sos.homedir()keeps the home it read at startup.claudeConfigDir()fell back tohomedir()whenCLAUDE_CONFIG_DIRwas unset, so it named the real directory, and the management toggles inclaude-messages-endpoint.test.tsran Claude agent sync there with an empty roster, pruning every generated definition.bun run testwas not affected for unset defaults because its children start withHOMEalready sandboxed, but it passed an inheritedCLAUDE_CONFIG_DIRstraight through.The fix is layered so one gap does not reopen the hole:
scripts/test.ts, shared by the wrapper and the preload): pinsCLAUDE_CONFIG_DIRandGROK_HOMEinside the sandbox root, never inheriting a live override, and hands the developer's own Claude directory to the guard asOCX_REAL_CLAUDE_CONFIG_DIR.src/claude/gateway-cache.ts):claudeConfigDir()readsHOME(USERPROFILEon Windows) at call time, which is what Claude Code's Node runtime does. In production the process environment is the startup environment, so the resolved path is unchanged; only a post-start rewrite now follows.src/lib/test-home-guard.ts):assertNotRealClaudeConfigUnderTestrefuses, in an armed test process only, any write that resolves into the real Claude directory — default and developer override. Writers pass every path they touch, so a link atagents/,cache/,cache/gateway-models.jsonor a definition's temp file is judged by its target. Roots resolve at check time and compare case-insensitively on macOS and Windows; containment rejects only a leading..component. The Claude directory is also a protected removal tree, with the same checkout-content lift the other trees have.settings.jsonwriter.The guard is inert unless
OCX_TEST_HOME_GUARD=1, which only the test preload sets.Other client homes still default to
os.homedir()(Claude Desktop config library, several client-integration writers, Kiro, XDG-based clients). Moving production resolution for those would change where a Windows host with a Git BashHOMEreads its own config, so this PR leaves them; the sandbox and guard cover the observed path. They are follow-up candidates.Verification
Every local run started Bun with a fresh temporary
HOME,USERPROFILEandCLAUDE_CONFIG_DIR; the real~/.claude/agentslisting hash was identical before and after.ocx-probe.mdplanted in a fake home withCLAUDE_CONFIG_DIRunset is pruned on dev byclaude-messages-endpoint.test.ts; with this change it survives (54/54 pass).tests/claude-integration/claude-config-home-isolation.test.ts(8 cases): all fail on dev; the guard case covers direct writes, linkedagents/cache, a linked cache file, a case alias of an override that does not exist yet,..fixtureand POSIX..\\fixturechildren, catalog and settings writers, removal refusal, and an unrefused write to an ordinary fixture.bun run typecheck,bun run structure:check,bun run privacy:scan: pass.structure/ops/test-sandbox-cleanup.mddocuments the pinned client homes and the Claude guard. Other docs mapped forscripts/,src/claude/,src/lib/,tests/were reviewed and describe nothing this changes.Checklist
structure/updated; no user-facing change.)