| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | var scrubFiles = []string{".gitconfig", ".bash_profile", ".bashrc", ".bash_aliases", ".profile", ".zshrc", ".bunfig.toml", ".netrc", ".npmrc", ".yarnrc", ".yarnrc.yml"} |
| 13 | |
| 14 | // scrubStubs makes what the pinned CLI makes in its home at startup in |
| 15 | // scrub mode (a throwaway home, 2026-11-21): empty files and directories. |
| 16 | func scrubStubs(t *testing.T, home string, file, dir os.FileMode) { |
| 17 | t.Helper() |
| 18 | for _, name := range []string{".pip", ".config", ".config/gh", ".config/glab-cli", ".config/git", ".config/pip"} { |
| 19 | if err := os.MkdirAll(filepath.Join(home, name), dir); err != nil || os.Chmod(filepath.Join(home, name), dir) != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | } |
| 23 | for _, name := range scrubFiles { |
| 24 | writeFake(t, filepath.Join(home, name), "") |
| 25 | if err := os.Chmod(filepath.Join(home, name), file); err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | // Production 2026-11-21: a token connection's second turn on one |
| 32 | // process was refused, runtime_claude_unexpected_entry. A token launch |
| 33 | // sets CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=1, and in that mode the pinned |
| 34 | // CLI stubs these out for its Bash sandbox after the copy's first check. |
| 35 | func TestTokenCopyAllowsScrubStubs(t *testing.T) { |
| 36 | for _, m := range [][2]os.FileMode{{0644, 0755}, {0664, 0775}, {0600, 0700}} { |
| 37 | l := openTokenCopy(t, tokenFixture(t)) |
| 38 | scrubStubs(t, l.Binding().Home, m[0], m[1]) |
| 39 | if err := l.CheckCopy(context.Background()); err != nil { |
| 40 | t.Fatalf("modes %o/%o: %v", m[0], m[1], err) |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // Only those names, empty, of that kind, on a token copy pass; the |
| 46 | // refusal names an entry only when it is one the CLI is known to write. |
| 47 | func TestScrubStubsOtherwiseRefused(t *testing.T) { |
| 48 | for _, tc := range []struct{ name, want string }{ |
| 49 | {"content", "runtime_claude_scrub_stub"}, {"directory", "runtime_claude_scrub_stub"}, |
| 50 | {"exec", "runtime_claude_scrub_stub"}, {"wide", "runtime_claude_scrub_stub"}, |
| 51 | {"link", "runtime_claude_special_entry"}, {"hardlink", "runtime_claude_metadata"}, |
| 52 | {"pip-child", "runtime_claude_scrub_stub"}, {"gh-child", "runtime_claude_config"}, |
| 53 | {"envrc", "runtime_claude_unexpected_entry:.envrc"}, {"unlisted", "runtime_claude_unexpected_entry:unlisted"}, |
| 54 | {"login", "runtime_claude_unexpected_entry:.bash_aliases"}, |
| 55 | } { |
| 56 | t.Run(tc.name, func(t *testing.T) { |
| 57 | var l *AccountLease |
| 58 | if tc.name == "login" { |
| 59 | l = claudeRuntimeFixture(t) |
| 60 | } else { |
| 61 | l = openTokenCopy(t, tokenFixture(t)) |
| 62 | } |
| 63 | home := l.Binding().Home |
| 64 | scrubStubs(t, home, 0644, 0755) |
| 65 | bashrc := filepath.Join(home, ".bashrc") |
| 66 | var err error |
| 67 | switch tc.name { |
| 68 | case "content": |
| 69 | err = os.WriteFile(bashrc, []byte("x"), 0644) |
| 70 | case "directory": |
| 71 | if err = os.Remove(bashrc); err == nil { |
| 72 | err = os.Mkdir(bashrc, 0755) |
| 73 | } |
| 74 | case "exec": |
| 75 | err = os.Chmod(bashrc, 0755) |
| 76 | case "wide": |
| 77 | err = os.Chmod(bashrc, 0666) |
| 78 | case "link": |
| 79 | if err = os.Remove(bashrc); err == nil { |
| 80 | err = os.Symlink("/dev/null", bashrc) |
| 81 | } |
| 82 | case "hardlink": |
| 83 | if err = os.Remove(filepath.Join(home, ".netrc")); err == nil { |
| 84 | err = os.Link(bashrc, filepath.Join(home, ".netrc")) |
| 85 | } |
| 86 | case "pip-child": |
| 87 | err = os.WriteFile(filepath.Join(home, ".pip", "pip.conf"), nil, 0644) |
| 88 | case "gh-child": |
| 89 | err = os.WriteFile(filepath.Join(home, ".config", "gh", "hosts.yml"), nil, 0644) |
| 90 | case "envrc": |
| 91 | err = os.WriteFile(filepath.Join(home, ".envrc"), nil, 0644) |
| 92 | case "unlisted": |
| 93 | err = os.WriteFile(filepath.Join(home, ".secret-sentinel"), nil, 0644) |
| 94 | } |
| 95 | if err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | err = l.validateRuntimeEntries() |
| 99 | if !errors.Is(err, ErrContainment) || ContainmentReason(err) != tc.want || strings.Contains(err.Error(), "secret-sentinel") { |
| 100 | t.Fatalf("want %s: %v", tc.want, err) |
| 101 | } |
| 102 | }) |
| 103 | } |
| 104 | } |
| 105 | |