package provider import ( "context" "errors" "os" "path/filepath" "strings" "testing" ) var scrubFiles = []string{".gitconfig", ".bash_profile", ".bashrc", ".bash_aliases", ".profile", ".zshrc", ".bunfig.toml", ".netrc", ".npmrc", ".yarnrc", ".yarnrc.yml"} // scrubStubs makes what the pinned CLI makes in its home at startup in // scrub mode (a throwaway home, 2026-11-21): empty files and directories. func scrubStubs(t *testing.T, home string, file, dir os.FileMode) { t.Helper() for _, name := range []string{".pip", ".config", ".config/gh", ".config/glab-cli", ".config/git", ".config/pip"} { if err := os.MkdirAll(filepath.Join(home, name), dir); err != nil || os.Chmod(filepath.Join(home, name), dir) != nil { t.Fatal(err) } } for _, name := range scrubFiles { writeFake(t, filepath.Join(home, name), "") if err := os.Chmod(filepath.Join(home, name), file); err != nil { t.Fatal(err) } } } // Production 2026-11-21: a token connection's second turn on one // process was refused, runtime_claude_unexpected_entry. A token launch // sets CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=1, and in that mode the pinned // CLI stubs these out for its Bash sandbox after the copy's first check. func TestTokenCopyAllowsScrubStubs(t *testing.T) { for _, m := range [][2]os.FileMode{{0644, 0755}, {0664, 0775}, {0600, 0700}} { l := openTokenCopy(t, tokenFixture(t)) scrubStubs(t, l.Binding().Home, m[0], m[1]) if err := l.CheckCopy(context.Background()); err != nil { t.Fatalf("modes %o/%o: %v", m[0], m[1], err) } } } // Only those names, empty, of that kind, on a token copy pass; the // refusal names an entry only when it is one the CLI is known to write. func TestScrubStubsOtherwiseRefused(t *testing.T) { for _, tc := range []struct{ name, want string }{ {"content", "runtime_claude_scrub_stub"}, {"directory", "runtime_claude_scrub_stub"}, {"exec", "runtime_claude_scrub_stub"}, {"wide", "runtime_claude_scrub_stub"}, {"link", "runtime_claude_special_entry"}, {"hardlink", "runtime_claude_metadata"}, {"pip-child", "runtime_claude_scrub_stub"}, {"gh-child", "runtime_claude_config"}, {"envrc", "runtime_claude_unexpected_entry:.envrc"}, {"unlisted", "runtime_claude_unexpected_entry:unlisted"}, {"login", "runtime_claude_unexpected_entry:.bash_aliases"}, } { t.Run(tc.name, func(t *testing.T) { var l *AccountLease if tc.name == "login" { l = claudeRuntimeFixture(t) } else { l = openTokenCopy(t, tokenFixture(t)) } home := l.Binding().Home scrubStubs(t, home, 0644, 0755) bashrc := filepath.Join(home, ".bashrc") var err error switch tc.name { case "content": err = os.WriteFile(bashrc, []byte("x"), 0644) case "directory": if err = os.Remove(bashrc); err == nil { err = os.Mkdir(bashrc, 0755) } case "exec": err = os.Chmod(bashrc, 0755) case "wide": err = os.Chmod(bashrc, 0666) case "link": if err = os.Remove(bashrc); err == nil { err = os.Symlink("/dev/null", bashrc) } case "hardlink": if err = os.Remove(filepath.Join(home, ".netrc")); err == nil { err = os.Link(bashrc, filepath.Join(home, ".netrc")) } case "pip-child": err = os.WriteFile(filepath.Join(home, ".pip", "pip.conf"), nil, 0644) case "gh-child": err = os.WriteFile(filepath.Join(home, ".config", "gh", "hosts.yml"), nil, 0644) case "envrc": err = os.WriteFile(filepath.Join(home, ".envrc"), nil, 0644) case "unlisted": err = os.WriteFile(filepath.Join(home, ".secret-sentinel"), nil, 0644) } if err != nil { t.Fatal(err) } err = l.validateRuntimeEntries() if !errors.Is(err, ErrContainment) || ContainmentReason(err) != tc.want || strings.Contains(err.Error(), "secret-sentinel") { t.Fatalf("want %s: %v", tc.want, err) } }) } }