jcode icon indicating copy to clipboard operation
jcode copied to clipboard

macOS: a sandboxed $JCODE_HOME still reads the real user's Keychain, changing onboarding and leaking real credential state into tests

Open ianalitis opened this issue 3 months ago • 3 comments

Summary

A sandboxed jcode (tests, self-dev, onboarding with a throwaway $JCODE_HOME) reads the real user's macOS Keychain when deciding which external logins to offer for import. Every other external credential source is redirected under $JCODE_HOME/external/; the Keychain is not, because it has no path to redirect.

Two consequences:

  1. Onboarding behaves differently on a machine that has Claude Code installed. A sandboxed first run offers the developer's own Claude login as an import candidate and lands on the import-review phase instead of the "Log in to OpenAI?" prompt.
  2. Test runs read real user credential state. A cargo test invocation consults the user's Keychain, which is not something a test run should observe.

Environment

  • Jcode: v0.81.4 (325236119), also present on current master
  • macOS 26.x, arm64
  • A Claude Code-credentials Keychain item present (i.e. Claude Code has been used on the machine)

Root cause

storage::user_home_path sandboxes every file-backed external source:

// crates/jcode-storage/src/lib.rs
pub fn user_home_path(relative: impl AsRef<Path>) -> Result<PathBuf> {
    ...
    if let Ok(path) = std::env::var("JCODE_HOME") {
        return Ok(PathBuf::from(path).join("external").join(relative));
    }
    ...
}

so OpenCode, Pi, OpenClaw, Hermes, Codex, and the Claude JSON file all resolve into the sandbox and correctly appear absent.

native_credentials_present bypasses that entirely:

// crates/jcode-base/src/auth/claude.rs
pub fn native_credentials_present() -> bool {
    if std::env::var(CLAUDE_CODE_OAUTH_TOKEN_ENV)... { return true; }
    claude_code_keychain_item_exists()   // /usr/bin/security find-generic-password
}

pending_external_auth_review_candidates then pushes a ClaudeCodeNative candidate, and onboarding branches on import.is_some().

Reproduction

On a macOS machine that has a Claude Code-credentials Keychain item:

cargo test -p jcode-tui --lib -- --exact --test-threads=1 \
  tui::app::tests::login_openai_phase_is_default_when_no_imports

Actual: fails. The test runs under with_temp_jcode_home, and begin_onboarding_flow_at_login lands on OnboardingPhase::Login { import: Some(ImportReview { candidates: [... "Claude Code (macOS Keychain)", path: "keychain:Claude Code-credentials" ...] }) }.

Expected: a sandboxed home has no importable logins, so onboarding lands on OnboardingPhase::LoginOpenAi { yes_highlighted: true } — which is exactly what the test's own comment says ("Fresh temp home has no importable logins").

The test passes on a machine without Claude Code installed, which is presumably why it has not been caught in CI.

Suggested fix

Skip the Keychain probe when running against a sandboxed home, i.e. when JCODE_HOME is set to anything other than the real ~/.jcode. That matches what user_home_path already does for every path-based source.

CLAUDE_CODE_OAUTH_TOKEN should still be honored: an env var set for this process is a deliberate input to it, not ambient user state.

src/cli/commands/menubar.rs already has the same "am I a sandbox?" concept (running_in_menubar_sandbox) for the same class of problem: a per-machine singleton resource that a throwaway $JCODE_HOME must not touch. This is the credential-store version of it.

Notes

I have a patch on my fork that adds storage::running_with_sandboxed_home (with unit tests for the decision) and gates the probe on it, if a reference is useful. Happy to open a PR, or leave this as a report.

ianalitis avatar Sep 01 '26 18:09 ianalitis

A patch is ready on a fork branch, based directly on master (325236119), one commit:

https://github.com/ianalitis/jcode/tree/fix/sandboxed-home-keychain

Diff: https://github.com/1jehuang/jcode/compare/master...ianalitis:jcode:fix/sandboxed-home-keychain

It adds storage::running_with_sandboxed_home() (JCODE_HOME set to anything other than ~/.jcode), splits the decision into a pure home_is_sandboxed(configured, real) so it is unit-testable without mutating process env, and gates only the Keychain probe on it. CLAUDE_CODE_OAUTH_TOKEN is still honored.

cargo test -p jcode-storage --lib -- sandboxed_home          # 1 passed
cargo test -p jcode-tui --lib -- --test-threads=1 login_openai_phase_is_default_when_no_imports   # passes (fails on master here)

Worth noting the general shape: any credential store without a path is invisible to the user_home_path sandbox. The Keychain is the one I hit; it may be worth a look at whether anything else falls in that category.

PRs are gated on this repo (pull_request_creation_policy: collaborators_only), so I could not open one. Happy for this to be a reference rather than a merge candidate.

ianalitis avatar Sep 01 '26 18:09 ianalitis

Thanks for documenting the cross-boundary leak. I added sandbox-home detection and gated ambient Keychain discovery in 1138; explicit input remains honored. The helper decision tests, reported onboarding regression, and all affected crate checks pass.

--- — Jcode agent (automated triage), on behalf of @1jehuang

1jehuang avatar Sep 01 '26 19:09 1jehuang

Correction: the explicit CLAUDE_CODE_OAUTH_TOKEN input remains honored. The shell used to post my previous comment interpreted the backticks, but the code and PR description are correct.

--- — Jcode agent (automated triage), on behalf of @1jehuang

1jehuang avatar Sep 01 '26 19:09 1jehuang