claude: with CLAUDE_CONFIG_DIR set, the keychain item read and written is the one Claude Code keeps there (#1842)
What changed: magpie read and wrote Claude Code's own sign-in under the service "Claude Code-credentials" whatever CLAUDE_CONFIG_DIR said. Claude Code 2.1.296 names its item "Claude Code-credentials-<sha256(dir)[:8]>" when CLAUDE_CONFIG_DIR is set (j$ in its secure storage: the suffix is dropped only when CLAUDE_CONFIG_DIR is unset), which is what claudeDirService already computes for saved accounts' directories. Now claudeHomeService gives that name for the read (both the account's item and by service alone) and for saveClaudeCredential. Why now: the previous commit asks the account's item before the file, so with CLAUDE_CONFIG_DIR set the plain "Claude Code-credentials" item — the default directory's sign-in, maybe another account — would have beaten the .credentials.json in CLAUDE_CONFIG_DIR. It was already wrong before that commit (a sign-in was written to an item Claude Code under that directory never reads), only less visible. Run (tip of both commits, sandbox HOME, fake `security` on PATH, private GOCACHE): TestClaudeConfigDirKeychainItem fails with claudeHomeService ignoring CLAUDE_CONFIG_DIR (it read "home-item") and passes with it. gofmt -l clean; go vet ./... on darwin, GOOS=linux -tags nogui and GOOS=windows pass; go test -count=1 ./internal/provider ok (116s); go test -race -count=5 on the keychain tests ok. Not run: no real Keychain, no real Claude Code run under CLAUDE_CONFIG_DIR (the name rule is read from the 2.1.296 binary), no other package's tests (only internal/provider changed).
This commit is contained in:
@@ -489,7 +489,7 @@ type claudeKeychainItem struct {
|
||||
// service alone when the account is "". Either way it is the one under
|
||||
// $USER that a sign-in is written back to.
|
||||
func readClaudeKeychainItem(account string) claudeKeychainItem {
|
||||
args := []string{"find-generic-password", "-s", "Claude Code-credentials", "-w"}
|
||||
args := []string{"find-generic-password", "-s", claudeHomeService(), "-w"}
|
||||
if account != "" {
|
||||
args = append(args, "-a", account)
|
||||
}
|
||||
@@ -508,6 +508,17 @@ func readClaudeKeychainItem(account string) claudeKeychainItem {
|
||||
}
|
||||
}
|
||||
|
||||
// claudeHomeService is the keychain item Claude Code keeps the sign-in
|
||||
// magpie reads as its own in: "Claude Code-credentials", or with
|
||||
// CLAUDE_CONFIG_DIR set, that directory's own (claudeDirService), as
|
||||
// Claude Code names it, beside the .credentials.json there.
|
||||
func claudeHomeService() string {
|
||||
if d := appdir.Getenv("CLAUDE_CONFIG_DIR"); d != "" {
|
||||
return claudeDirService(d)
|
||||
}
|
||||
return "Claude Code-credentials"
|
||||
}
|
||||
|
||||
// claudeKeychainAccount is the account Claude Code keeps its sign-in
|
||||
// under: $USER, else the login name, and claude-code-user for a name it
|
||||
// won't use.
|
||||
@@ -589,7 +600,7 @@ func saveClaudeCredential(loc claudeCredentialLocation, c claudeCredentials) err
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
args := []string{"add-generic-password", "-U", "-s", "Claude Code-credentials"}
|
||||
args := []string{"add-generic-password", "-U", "-s", claudeHomeService()}
|
||||
if loc.account != "" {
|
||||
args = append(args, "-a", loc.account)
|
||||
}
|
||||
|
||||
@@ -187,6 +187,48 @@ func TestClaudeFileSignInWhenNoKeychainItem(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// With CLAUDE_CONFIG_DIR set, Claude Code keeps its sign-in in that
|
||||
// directory's own item, named for it, not in "Claude Code-credentials":
|
||||
// that one is read, and a sign-in is put back there.
|
||||
func TestClaudeConfigDirKeychainItem(t *testing.T) {
|
||||
shellFakes(t)
|
||||
home := claudeHome(t)
|
||||
dir := filepath.Join(home, "elsewhere")
|
||||
t.Setenv("CLAUDE_CONFIG_DIR", dir)
|
||||
t.Setenv("USER", "tester")
|
||||
bin := filepath.Join(home, "bin")
|
||||
os.MkdirAll(bin, 0o755)
|
||||
added := filepath.Join(home, "added.log")
|
||||
testenv.Program(t, filepath.Join(bin, "security"), `#!/bin/sh
|
||||
case "$1" in
|
||||
find-generic-password)
|
||||
case "$*" in
|
||||
*"-s `+claudeDirService(dir)+` "*"-a tester"*) echo '{"claudeAiOauth":{"accessToken":"dir-item"}}' ;;
|
||||
*"-s Claude Code-credentials "*) echo '{"claudeAiOauth":{"accessToken":"home-item"}}' ;;
|
||||
*) exit 1 ;;
|
||||
esac ;;
|
||||
add-generic-password)
|
||||
echo "$*" >> '`+added+`' ;;
|
||||
*) exit 1 ;;
|
||||
esac
|
||||
`)
|
||||
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
|
||||
claudeKeychain = true
|
||||
forgetClaudeCredential()
|
||||
c, loc, ok := readClaudeCredential()
|
||||
if !ok || c.OAuth.AccessToken != "dir-item" || !loc.keychain {
|
||||
t.Fatalf("the config directory's item is not the sign-in: %v %+v %+v", ok, c.OAuth, loc)
|
||||
}
|
||||
if err := putClaudeLogin(savedLogin{Agent: "claude", User: "fresh@example.com", Auth: mustJSONRaw(t, map[string]any{
|
||||
"claudeAiOauth": map[string]any{"accessToken": "fresh-access", "refreshToken": "fresh-refresh"},
|
||||
})}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b, _ := os.ReadFile(added); !strings.Contains(string(b), "-s "+claudeDirService(dir)+" -a tester") || !strings.Contains(string(b), "fresh-access") {
|
||||
t.Fatalf("the sign-in was not put in the config directory's item: %s", b)
|
||||
}
|
||||
}
|
||||
|
||||
// An item magpie wrote over lines comes back from `security -w` as hex,
|
||||
// which Claude Code reads as no item at all (#70): beside a file that holds
|
||||
// a sign-in, the file is the one it goes by, and the item is left alone.
|
||||
|
||||
Reference in New Issue
Block a user