From ad585ccd6cf3faf7fed8102c707b2c4289ef8959 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 17 Nov 2026 12:17:10 +0800 Subject: [PATCH] claude: settings.json holding a setup-token is readable by its owner alone 2f25728dd switches Claude Code to a setup-token account by writing the token into CLAUDE_CODE_OAUTH_TOKEN in ~/.claude/settings.json. Claude Code writes that file 0644 and edit.SetJSON keeps a file's mode, so a long-lived subscription token sat where every other account on the computer could read it (Linux homes are often 0755), unlike the sign-in Claude Code keeps in .credentials.json (0600) or the keychain. Before the token goes in, the file (a symlink's target) loses its group and other bits; one that isn't there yet is made 0600 first, so the token is never in a file others can read. Run at this head: - mote: go vet ./internal/provider for darwin, windows, linux: ok. - mote: go test ./internal/provider -count=1: ok (143.7s). - magpie-desktop (Linux): the setup-token tests: ok. - Broken by hand: without claudeSettingsPrivate, TestClaudeSetupTokenSettingsPrivate is red in both cases ("644, readable by others"). Not tried: a real Claude Code reading a 0600 settings.json (it reads it as the same user). Not changed: settings.json is still a file people keep in dotfiles repos, and Claude Code passes its env on to the tools it runs; both come with putting the token in its env at all. --- internal/provider/claude_token_env.go | 36 +++++++++++++++++++ internal/provider/claude_token_env_test.go | 41 ++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/internal/provider/claude_token_env.go b/internal/provider/claude_token_env.go index 37fa265ab..2d5f8ea36 100644 --- a/internal/provider/claude_token_env.go +++ b/internal/provider/claude_token_env.go @@ -208,9 +208,45 @@ func setClaudeEnvTokenLocked(token string) error { if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { return err } + if err := claudeSettingsPrivate(path); err != nil { + return err + } return edit.SetJSON(path, edit.KV{Path: "env." + claudeTokenEnvKey, Value: token}) } +// claudeSettingsPrivate makes Claude Code's settings.json readable by its +// owner alone before a token goes in it. Claude Code writes the file 0644, +// and edit.SetJSON keeps a file's mode, so a long-lived subscription token +// would sit where every other account on the computer can read it, unlike +// the sign-in Claude Code keeps in .credentials.json (0600) or the +// keychain. A file that isn't there yet is made 0600 first, so the token +// is never in a file anyone else can read, not even for a moment. +func claudeSettingsPrivate(path string) error { + target, err := edit.Target(path) // a symlink's target holds the token + if err != nil { + return err + } + st, err := os.Stat(target) + if errors.Is(err, os.ErrNotExist) { + f, err := os.OpenFile(target, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600) + if err != nil { + return err + } + if _, err := f.WriteString("{}\n"); err != nil { + f.Close() + return err + } + return f.Close() + } + if err != nil { + return err + } + if perm := st.Mode().Perm(); perm&0o077 != 0 { + return os.Chmod(target, perm&^0o077) + } + return nil +} + // claudeTokenGone takes magpie's token for the removed account l out of // Claude Code's settings.json, so Claude Code goes back to its own // sign-in; another account's, or the user's own, stays. diff --git a/internal/provider/claude_token_env_test.go b/internal/provider/claude_token_env_test.go index a03b9a9c4..9fceb204b 100644 --- a/internal/provider/claude_token_env_test.go +++ b/internal/provider/claude_token_env_test.go @@ -4,6 +4,7 @@ import ( "context" "os" "path/filepath" + "runtime" "strings" "sync/atomic" "testing" @@ -268,3 +269,43 @@ func TestClaudeSetupTokenInUseNotMovedOff(t *testing.T) { t.Fatalf("NextLogin no longer moves the spent sign-in: %q ok=%v", to, ok) } } + +// A setup-token is a long-lived subscription credential: the settings.json +// it goes in is made readable by its owner alone, as Claude Code's own +// .credentials.json is, whether it was 0644 (as Claude Code writes it) or +// not there yet. +func TestClaudeSetupTokenSettingsPrivate(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("no Unix modes") + } + for _, existed := range []bool{true, false} { + t.Run(map[bool]string{true: "0644", false: "missing"}[existed], func(t *testing.T) { + claudeThree(t) + if existed { + writeFile(t, claudeSettingsFile(), map[string]any{"model": "opus"}) + if err := os.Chmod(claudeSettingsFile(), 0o644); err != nil { + t.Fatal(err) + } + } else if err := os.Remove(claudeSettingsFile()); err != nil && !os.IsNotExist(err) { + t.Fatal(err) + } + tok, err := AddClaudeSetupToken(testSetupToken) + if err != nil { + t.Fatal(err) + } + if err := SwitchLogin("claude", tok); err != nil { + t.Fatal(err) + } + if v, _ := envTokenOf(t); v != testSetupToken { + t.Fatalf("settings.json's %s = %q, want the setup-token", claudeTokenEnvKey, v) + } + st, err := os.Stat(claudeSettingsFile()) + if err != nil { + t.Fatal(err) + } + if perm := st.Mode().Perm(); perm&0o077 != 0 { + t.Fatalf("settings.json holding the token is %o, readable by others", perm) + } + }) + } +}