| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "golang.org/x/sys/unix" |
| 13 | ) |
| 14 | |
| 15 | func claudeRuntimeFixture(t *testing.T) *AccountLease { |
| 16 | t.Helper() |
| 17 | b, r := accountFixture(t) |
| 18 | b.Provider = "claude" |
| 19 | b.Expected.ProviderVersion = "2.1.312" |
| 20 | writeMetadata(t, b.Home, b.Expected) |
| 21 | writeFake(t, filepath.Join(b.Home, ".credentials.json"), `{}`) |
| 22 | l := openFixture(t, b, r) |
| 23 | return l |
| 24 | } |
| 25 | func TestAccountClaudeRecordedRuntime(t *testing.T) { |
| 26 | for _, stage := range []string{"before", "disabled"} { |
| 27 | t.Run(stage, func(t *testing.T) { |
| 28 | l := claudeRuntimeFixture(t) |
| 29 | raw, err := os.ReadFile("testdata/claude-empty-home-" + stage + "-2.1.312.json") |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | var tree struct { |
| 34 | Entries []struct { |
| 35 | Path string |
| 36 | Directory bool |
| 37 | Mode string |
| 38 | } |
| 39 | } |
| 40 | if err = json.Unmarshal(raw, &tree); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | for _, e := range tree.Entries { |
| 44 | p := filepath.Join(l.runtime, e.Path) |
| 45 | if e.Directory { |
| 46 | err = os.MkdirAll(p, 0700) |
| 47 | } else { |
| 48 | err = os.WriteFile(p, []byte("fixture"), 0600) |
| 49 | } |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | mode, e2 := strconv.ParseUint(e.Mode, 8, 32) |
| 54 | if e2 != nil { |
| 55 | t.Fatal(e2) |
| 56 | } |
| 57 | if err = os.Chmod(p, os.FileMode(mode)); err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | } |
| 61 | err = l.validateRuntimeEntries() |
| 62 | if stage == "disabled" { |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | } else if err == nil || !strings.Contains(err.Error(), "runtime_claude_policy_entry") { |
| 67 | t.Fatalf("plugin tree not refused: %v", err) |
| 68 | } |
| 69 | }) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestAccountClaudeRuntimeGuards(t *testing.T) { |
| 74 | for _, tc := range []struct{ path, kind, reason string }{ |
| 75 | {".cache/evil", "file", "cache"}, {".config/evil", "file", "config"}, {".claude/ssh/key", "file", "config"}, |
| 76 | {".npm/_logs/evil", "file", "npm"}, {"dev-mods/payload", "file", "dev_mods"}, {"seed-admin/config", "file", "seed_admin"}, |
| 77 | {"plugins", "dir", "policy_entry"}, {"skills", "dir", "policy_entry"}, {"agents", "dir", "policy_entry"}, {"commands", "dir", "policy_entry"}, |
| 78 | {"hooks", "dir", "policy_entry"}, {"settings.extra.json", "file", "policy_entry"}, {"CLAUDE.md", "file", "policy_entry"}, |
| 79 | {".mcp.json", "file", "policy_entry"}, {"mcp.json", "file", "policy_entry"}, {".cache/settings.local.json", "file", "policy_entry"}, |
| 80 | {".cache/agents", "dir", "policy_entry"}, {"shell-snapshots/evil", "dir", "shell_snapshots"}, {"session-env/evil", "dir", "session_env"}, |
| 81 | {"secret-sentinel", "file", "unexpected_entry"}, {".cache", "file", "kind"}, |
| 82 | {".cache", "setgid", "metadata"}, {".cache", "setuid", "metadata"}, |
| 83 | {".cache/claude-cli-nodejs", "link", "special_entry"}, {".cache/pipe", "fifo", "special_entry"}, |
| 84 | {".npm/_update-notifier-last-checked", "hardlink", "metadata"}, |
| 85 | {".cache", "version", "cache"}, {".cache", "provider", "unexpected_entry"}, |
| 86 | } { |
| 87 | t.Run(tc.path+"/"+tc.kind, func(t *testing.T) { |
| 88 | l := claudeRuntimeFixture(t) |
| 89 | path := filepath.Join(l.runtime, tc.path) |
| 90 | must := func(err error) { |
| 91 | t.Helper() |
| 92 | if err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | } |
| 96 | must(os.MkdirAll(filepath.Dir(path), 0700)) |
| 97 | switch tc.kind { |
| 98 | case "dir", "version", "provider", "setuid", "setgid": |
| 99 | must(os.MkdirAll(path, 0700)) |
| 100 | case "link": |
| 101 | must(os.Symlink("/opt/claude", path)) |
| 102 | case "fifo": |
| 103 | must(unix.Mkfifo(path, 0600)) |
| 104 | case "hardlink": |
| 105 | must(os.Link(filepath.Join(l.runtime, ".credentials.json"), path)) |
| 106 | default: |
| 107 | writeFake(t, path, "fixture") |
| 108 | } |
| 109 | if tc.kind == "setuid" { |
| 110 | must(os.Chmod(path, 0700|os.ModeSetuid)) |
| 111 | } |
| 112 | if tc.kind == "setgid" { |
| 113 | must(os.Chmod(path, 0700|os.ModeSetgid)) |
| 114 | } |
| 115 | if tc.kind == "version" { |
| 116 | l.source.Expected.ProviderVersion = "other" |
| 117 | } |
| 118 | if tc.kind == "provider" { |
| 119 | l.source.Provider = "other" |
| 120 | must(os.Remove(filepath.Join(l.runtime, ".credentials.json"))) |
| 121 | } |
| 122 | err := l.validateRuntimeEntries() |
| 123 | want := "runtime_claude_" + tc.reason |
| 124 | if tc.kind == "provider" { |
| 125 | want = "runtime_unexpected_entry" |
| 126 | } |
| 127 | if !errors.Is(err, ErrContainment) || !strings.Contains(err.Error(), want) || strings.Contains(err.Error(), "secret-sentinel") { |
| 128 | t.Fatalf("guard %s: %v", want, err) |
| 129 | } |
| 130 | }) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | func TestAccountClaudeRuntimeNotStaged(t *testing.T) { |
| 135 | l := claudeRuntimeFixture(t) |
| 136 | for _, name := range []string{".cache", ".config", ".claude", ".npm", "dev-mods", "seed-admin"} { |
| 137 | if err := os.Mkdir(filepath.Join(l.source.Home, name), 0700); err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | } |
| 141 | preflightFixture(t, l) |
| 142 | for _, name := range []string{".cache", ".config", ".claude", ".npm", "dev-mods", "seed-admin"} { |
| 143 | if _, err := os.Lstat(filepath.Join(l.runtime, name)); !os.IsNotExist(err) { |
| 144 | t.Fatalf("source state staged: %s: %v", name, err) |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | func TestRuntimeRandomIsolationMarker(t *testing.T) { |
| 150 | for _, family := range []string{"claude", "codex"} { |
| 151 | for _, kind := range []string{"file", "directory", "large", "empty", "symlink", "name"} { |
| 152 | t.Run(family+"/"+kind, func(t *testing.T) { |
| 153 | var l *AccountLease |
| 154 | if family == "claude" { |
| 155 | l = claudeRuntimeFixture(t) |
| 156 | } else { |
| 157 | b, r := accountFixture(t) |
| 158 | l = openFixture(t, b, r) |
| 159 | } |
| 160 | name := "equipo-read-0123456789abcdef0123456789abcdef" |
| 161 | if kind == "name" { |
| 162 | name += "-invalid" |
| 163 | } |
| 164 | path := filepath.Join(l.runtime, name) |
| 165 | var err error |
| 166 | switch kind { |
| 167 | case "directory": |
| 168 | err = os.Mkdir(path, 0700) |
| 169 | case "symlink": |
| 170 | err = os.Symlink("auth.json", path) |
| 171 | default: |
| 172 | raw := []byte("random marker") |
| 173 | if kind == "large" { |
| 174 | raw = make([]byte, 4097) |
| 175 | } |
| 176 | if kind == "empty" { |
| 177 | raw = nil |
| 178 | } |
| 179 | err = os.WriteFile(path, raw, 0600) |
| 180 | } |
| 181 | if err != nil { |
| 182 | t.Fatal(err) |
| 183 | } |
| 184 | if err = l.validateRuntimeEntries(); (err == nil) != (kind == "file") { |
| 185 | t.Fatalf("marker %s: %v", kind, err) |
| 186 | } |
| 187 | }) |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |