Claude Code's macOS credential store is a composite named keychain-with-plaintext-fallback: it reads the Keychain first and falls back to <config dir>/.credentials.json, and a failed Keychain write migrates the credential into that file. ccswitch looked only at the Keychain, under a hardcoded service name, and passed the token on argv. Three consequences, all fixed here. Headless and SSH machines. Over SSH the login Keychain cannot be unlocked without a GUI prompt (security exits 36, errSecInteractionNotAllowed), so Claude Code lives entirely in the plaintext file. read() saw nothing, so save reported "no active credential" on a signed-in machine and sync_current silently dropped rotated tokens; write() could not land the incoming credential, leaving ~/.claude.json naming the new account while the live token still belonged to the old one, which Claude Code reports as not logged in. KeychainStore now mirrors the same arbitration and keeps exactly one authoritative copy: a successful Keychain write deletes the plaintext file, a failed one deletes the Keychain item, so a stale copy cannot shadow the switch from the other kind of session. $CCSWITCH_CREDENTIALS =file forces the plaintext path outright. $CLAUDE_CONFIG_DIR. It moves .claude.json and the credential fallback, and it renames the Keychain item: Claude Code namespaces the service with the first eight hex characters of sha256($CLAUDE_CONFIG_DIR). With the variable set, ccswitch was reading and writing a different account's credential. This affected ordinary use, because ccswitch isolate launches claude with CLAUDE_CONFIG_DIR set to the isolate directory. claude_env.rs owns the derivation, including the related $CLAUDE_SECURESTORAGE_CONFIG_DIR and $CLAUDE_CODE_CUSTOM_OAUTH_URL. Token exposure. add-generic-password -w put the OAuth token in argv, which ps shows to every user on the machine. It is now hex-encoded and piped to security -i, with the same argv fallback Claude Code uses past the stdin limit. The Keychain port collapses to one run(args, stdin) so that choice is a tested decision in creds.rs rather than shim detail. Also fixed: account_attr parsed stderr, but find-generic-password -g prints attributes on stdout and the password on stderr, so it always returned None and every profile stored an empty keychain_account. Writes fell through to $USER, which is what Claude Code looks items up by, so this was invisible. Account attributes are now sanitized the same way Claude Code sanitizes them. sha256.rs is hand-rolled against the FIPS vectors rather than pulling sha2 and six transitive crates for one eight-character string, per the dependency policy in AGENTS.md. Claude Code NFC-normalizes the directory before hashing and we hash the bytes as given; that agrees for every ASCII path, and the limitation is documented with its workaround. Behavior change: profile and isolate storage now default to accounts/ and profiles/ inside the resolved config directory rather than always ~/.claude. Identical with $CLAUDE_CONFIG_DIR unset; with it set, each config directory gets its own profiles. Set $CCSWITCH_HOME to a fixed path to share one set across all of them. legacy-fish is synced with the same three fixes. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
44 lines
1.6 KiB
Rust
44 lines
1.6 KiB
Rust
//! Smoke test: the crate's public pure helpers behave as documented.
|
|
|
|
use ccswitch::cli;
|
|
use ccswitch::ClaudeEnv;
|
|
use std::path::Path;
|
|
|
|
#[test]
|
|
fn help_text_and_reserved_words_are_public() {
|
|
assert!(cli::help_text().contains("ccswitch <name>"));
|
|
assert!(cli::is_reserved("list"));
|
|
assert!(!cli::is_reserved("dev"));
|
|
}
|
|
|
|
#[test]
|
|
fn default_paths_resolve_under_home() {
|
|
let home = Path::new("/home/example");
|
|
let env = ClaudeEnv::default();
|
|
let claude = env.config_dir(home);
|
|
assert_eq!(claude, home.join(".claude"));
|
|
assert_eq!(cli::ccswitch_home(None, &claude), claude.join("accounts"));
|
|
assert_eq!(cli::isolate_home(None, &claude), claude.join("profiles"));
|
|
assert_eq!(env.config_path(home), home.join(".claude.json"));
|
|
assert_eq!(env.keychain_service(), "Claude Code-credentials");
|
|
}
|
|
|
|
#[test]
|
|
fn claude_config_dir_moves_the_whole_account_including_the_keychain_item() {
|
|
let home = Path::new("/home/example");
|
|
let env = ClaudeEnv::new(Some("/opt/claude-work".to_string()), None, None);
|
|
let claude = env.config_dir(home);
|
|
assert_eq!(claude, Path::new("/opt/claude-work"));
|
|
assert_eq!(env.config_path(home), claude.join(".claude.json"));
|
|
assert_eq!(env.credentials_dir(home), claude);
|
|
assert_eq!(cli::ccswitch_home(None, &claude), claude.join("accounts"));
|
|
// Crucially a *different* Keychain item from the default account's.
|
|
assert_ne!(
|
|
env.keychain_service(),
|
|
ClaudeEnv::default().keychain_service()
|
|
);
|
|
assert!(env
|
|
.keychain_service()
|
|
.starts_with("Claude Code-credentials-"));
|
|
}
|