| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "regexp" |
| 8 | "strings" |
| 9 | |
| 10 | "golang.org/x/sys/unix" |
| 11 | ) |
| 12 | |
| 13 | var claudeRuntimeLog = regexp.MustCompile(`^\.cache/claude-cli-nodejs/-workspace/mcp-logs-equipo/[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{3}Z\.jsonl$`) |
| 14 | var claudeNPMLog = regexp.MustCompile(`^\.npm/_logs/[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{3}Z-debug-0\.log$`) |
| 15 | |
| 16 | // 2.1.312 uses Date.now() and Math.random().toString(36).substring(2,8). |
| 17 | // Its session suffix helper is disabled in this binary. |
| 18 | var claudeShellSnapshot = regexp.MustCompile(`^shell-snapshots/snapshot-(bash|zsh|sh)-[0-9]{13}-[a-z0-9]{0,6}\.sh$`) |
| 19 | var claudeSessionDir = regexp.MustCompile(`^session-env/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`) |
| 20 | var claudeSessionFile = regexp.MustCompile(`^session-env/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/(setup|sessionstart|cwdchanged|filechanged)-hook-[0-9]+\.sh$`) |
| 21 | |
| 22 | // Only pinned startup paths are runtime state. Empty support directories |
| 23 | // do not authorize SSH, config, mod or seed files. |
| 24 | func (l *AccountLease) claudeRuntimeEntry(rel string, info os.FileInfo) (bool, error) { |
| 25 | top := strings.SplitN(rel, "/", 2)[0] |
| 26 | if l.source.Token && l.source.Expected.ProviderVersion == ClaudeVersion && (claudeScrubStub[rel] != 0 || top == ".pip") { |
| 27 | return true, claudeScrubEntry(rel, info) |
| 28 | } |
| 29 | category := "" |
| 30 | switch top { |
| 31 | case ".cc-writes": |
| 32 | category = "write_staging" |
| 33 | case ".last-cleanup": |
| 34 | category = "cleanup_marker" |
| 35 | case ".cache": |
| 36 | category = "cache" |
| 37 | case ".config", ".claude": |
| 38 | category = "config" |
| 39 | case ".npm": |
| 40 | category = "npm" |
| 41 | case "dev-mods": |
| 42 | category = "dev_mods" |
| 43 | case "seed-admin": |
| 44 | category = "seed_admin" |
| 45 | case "policy-limits.json", "policy-limits.json.signature.json", "policy-limits.json.signature-iat.json", claudePolicyStamp: |
| 46 | category = "policy_limits" |
| 47 | case "shell-snapshots": |
| 48 | category = "shell_snapshots" |
| 49 | case "session-env": |
| 50 | category = "session_env" |
| 51 | default: |
| 52 | return false, nil |
| 53 | } |
| 54 | if l.source.Expected.ProviderVersion != ClaudeVersion { |
| 55 | return true, accountContainment("runtime_claude_" + category) |
| 56 | } |
| 57 | if rel == ".config/git" || rel == ".config/git/ignore" { |
| 58 | valid := rel == ".config/git" && info.IsDir() && claudeStubDir(info.Mode().Perm()) || rel == ".config/git/ignore" && l.claudeGitIgnore(info) |
| 59 | if !valid { |
| 60 | return true, accountContainment("runtime_claude_git_ignore") |
| 61 | } |
| 62 | return true, nil |
| 63 | } |
| 64 | if category == "write_staging" || rel == ".claude/bridge-spawn" { |
| 65 | if (rel != ".cc-writes" && rel != ".claude/bridge-spawn") || !info.IsDir() || info.Mode().Perm() != 0700 { |
| 66 | return true, accountContainment("runtime_claude_write_staging") |
| 67 | } |
| 68 | entries, err := os.ReadDir(filepath.Join(l.runtime, rel)) |
| 69 | if err != nil || len(entries) != 0 { |
| 70 | return true, accountContainment("runtime_claude_write_staging") |
| 71 | } |
| 72 | return true, nil |
| 73 | } |
| 74 | if category == "cleanup_marker" { |
| 75 | if rel != ".last-cleanup" || !info.Mode().IsRegular() || info.Size() != 24 { |
| 76 | return true, accountContainment("runtime_claude_cleanup_marker") |
| 77 | } |
| 78 | return true, nil |
| 79 | } |
| 80 | directory := false |
| 81 | switch rel { |
| 82 | case ".cache", ".cache/claude-cli-nodejs", ".cache/claude-cli-nodejs/-workspace", ".cache/claude-cli-nodejs/-workspace/mcp-logs-equipo", ".config", ".config/anthropic", ".claude", ".claude/ssh", ".npm", ".npm/_logs", "dev-mods", "seed-admin", "shell-snapshots", "session-env": |
| 83 | directory = true |
| 84 | case "policy-limits.json", "policy-limits.json.signature.json", "policy-limits.json.signature-iat.json": |
| 85 | case claudePolicyStamp: |
| 86 | if info.Size() > claudePolicyStampMax { |
| 87 | return true, accountContainment("runtime_claude_policy_limits") |
| 88 | } |
| 89 | default: |
| 90 | // These scripts are safe only while agent tools cannot access the staged |
| 91 | // home: claude/launcher.go:139-149 builds native and Bash sandbox denies. |
| 92 | directory = claudeSessionDir.MatchString(rel) |
| 93 | if !directory && !claudeShellSnapshot.MatchString(rel) && !claudeSessionFile.MatchString(rel) && rel != ".npm/_update-notifier-last-checked" && !claudeRuntimeLog.MatchString(rel) && !claudeNPMLog.MatchString(rel) { |
| 94 | return true, accountContainment("runtime_claude_" + category) |
| 95 | } |
| 96 | } |
| 97 | if (directory && !info.IsDir()) || (!directory && !info.Mode().IsRegular()) { |
| 98 | return true, accountContainment("runtime_claude_kind") |
| 99 | } |
| 100 | return true, nil |
| 101 | } |
| 102 | |
| 103 | // 2.1.312 in scrub mode (CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=1, set for |
| 104 | // token launches only: LaunchSecretEnv) creates these in its home at |
| 105 | // startup, empty, for its Bash sandbox to cover ("claude-code scrub-mode |
| 106 | // stubs"; observed with a throwaway home 2026-11-21). Empty is all it |
| 107 | // writes; content, another kind or a child is refused. |
| 108 | var claudeScrubStub = map[string]byte{".gitconfig": 'f', ".bash_profile": 'f', ".bashrc": 'f', ".bash_aliases": 'f', ".profile": 'f', ".zshrc": 'f', ".bunfig.toml": 'f', ".netrc": 'f', ".npmrc": 'f', ".yarnrc": 'f', ".yarnrc.yml": 'f', ".pip": 'd', ".config/gh": 'd', ".config/glab-cli": 'd', ".config/pip": 'd'} |
| 109 | |
| 110 | func claudeScrubEntry(rel string, info os.FileInfo) error { |
| 111 | perm, kind := info.Mode().Perm(), claudeScrubStub[rel] |
| 112 | if kind == 'f' && info.Mode().IsRegular() && info.Size() == 0 && (perm == 0600 || perm == 0644 || perm == 0664) || kind == 'd' && info.IsDir() && claudeStubDir(perm) { |
| 113 | return nil |
| 114 | } |
| 115 | return accountContainment("runtime_claude_scrub_stub") |
| 116 | } |
| 117 | |
| 118 | // claudeStubDir is a directory mode the CLI makes under umask 077, 022 or 002. |
| 119 | func claudeStubDir(perm os.FileMode) bool { return perm == 0700 || perm == 0755 || perm == 0775 } |
| 120 | |
| 121 | func (l *AccountLease) claudeGitIgnore(info os.FileInfo) bool { |
| 122 | const observed = "**/.claude/.cc-writes/\n" |
| 123 | if !info.Mode().IsRegular() || (info.Mode().Perm() != 0664 && info.Mode().Perm() != 0600) || info.Size() != int64(len(observed)) { |
| 124 | return false |
| 125 | } |
| 126 | path := filepath.Join(l.runtime, ".config/git/ignore") |
| 127 | fd, err := openNoLinks(path, unix.O_RDONLY|unix.O_CLOEXEC|unix.O_NONBLOCK) |
| 128 | if err != nil { |
| 129 | return false |
| 130 | } |
| 131 | f := os.NewFile(uintptr(fd), path) |
| 132 | defer f.Close() |
| 133 | opened, err := f.Stat() |
| 134 | if err != nil || !os.SameFile(info, opened) || !runtimeEntryMetadata(opened) || !opened.Mode().IsRegular() || opened.Mode().Perm() != info.Mode().Perm() || opened.Size() != info.Size() { |
| 135 | return false |
| 136 | } |
| 137 | raw, err := io.ReadAll(io.LimitReader(f, int64(len(observed)+1))) |
| 138 | return err == nil && string(raw) == observed |
| 139 | } |
| 140 | |
| 141 | // 2.1.312 stamps its policy-limits cache (identity, kind and body digest, |
| 142 | // mode 0600) beside the other policy-limits files. The CLI itself treats a |
| 143 | // stamp over 4096 bytes as unusable, so nothing larger is its own. |
| 144 | const ( |
| 145 | claudePolicyStamp = "policy-limits.json.stamp.json" |
| 146 | claudePolicyStampMax = 4096 |
| 147 | ) |
| 148 | |
| 149 | // remote-settings.json is 2.1.312's cache of the managed settings an |
| 150 | // organization sets for its account. Managed settings outrank the settings |
| 151 | // Equipo mounts, so any setting in it could change policy. The empty |
| 152 | // object sets nothing and so changes no policy: that, and only that, is |
| 153 | // accepted. The CLI writes JSON.stringify(settings, null, 2) with mode 0600, |
| 154 | // "{}" for an organization with none; 64 bytes leaves room only for |
| 155 | // whitespace around it. Its .synced-at sibling holds the millisecond time |
| 156 | // of that sync, never settings (the CLI reads at most 32 bytes of it), and |
| 157 | // is accepted only beside an accepted empty object. |
| 158 | const ( |
| 159 | claudeRemoteSettings = "remote-settings.json" |
| 160 | claudeRemoteSyncedAt = "remote-settings.json.synced-at" |
| 161 | claudeRemoteSettingsMax = 64 |
| 162 | claudeRemoteSyncedAtMax = 32 |
| 163 | ) |
| 164 | |
| 165 | // claudeRemoteSettingsEntry decides either name wherever it appears in the |
| 166 | // home; outside the home's root it is refused. |
| 167 | func (l *AccountLease) claudeRemoteSettingsEntry(rel string) error { |
| 168 | if l.source.Expected.ProviderVersion == ClaudeVersion && l.claudeEmptyRemoteSettings() { |
| 169 | if rel == claudeRemoteSettings { |
| 170 | return nil |
| 171 | } |
| 172 | if rel == claudeRemoteSyncedAt { |
| 173 | if _, ok := l.claudeRemoteFile(claudeRemoteSyncedAt, claudeRemoteSyncedAtMax); ok { |
| 174 | return nil |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | if filepath.Base(rel) == claudeRemoteSyncedAt { |
| 179 | return accountContainment("runtime_claude_remote_settings_synced_at") |
| 180 | } |
| 181 | return accountContainment("runtime_claude_remote_settings") |
| 182 | } |
| 183 | |
| 184 | func (l *AccountLease) claudeEmptyRemoteSettings() bool { |
| 185 | raw, ok := l.claudeRemoteFile(claudeRemoteSettings, claudeRemoteSettingsMax) |
| 186 | return ok && strings.Trim(string(raw), " \t\r\n") == "{}" |
| 187 | } |
| 188 | |
| 189 | // claudeRemoteFile reads a root file of the staged home that is the CLI's |
| 190 | // own: runner uid, one link, no setuid/setgid, regular, 0600, at most limit |
| 191 | // bytes, opened without following links. |
| 192 | func (l *AccountLease) claudeRemoteFile(name string, limit int64) ([]byte, bool) { |
| 193 | path := filepath.Join(l.runtime, name) |
| 194 | private := func(info os.FileInfo) bool { |
| 195 | return runtimeEntryMetadata(info) && info.Mode().IsRegular() && info.Mode().Perm() == 0600 && info.Size() <= limit |
| 196 | } |
| 197 | info, err := os.Lstat(path) |
| 198 | if err != nil || !private(info) { |
| 199 | return nil, false |
| 200 | } |
| 201 | fd, err := openNoLinks(path, unix.O_RDONLY|unix.O_CLOEXEC|unix.O_NONBLOCK) |
| 202 | if err != nil { |
| 203 | return nil, false |
| 204 | } |
| 205 | f := os.NewFile(uintptr(fd), path) |
| 206 | defer f.Close() |
| 207 | opened, err := f.Stat() |
| 208 | if err != nil || !os.SameFile(info, opened) || !private(opened) { |
| 209 | return nil, false |
| 210 | } |
| 211 | raw, err := io.ReadAll(io.LimitReader(f, limit+1)) |
| 212 | return raw, err == nil && int64(len(raw)) <= limit |
| 213 | } |
| 214 | |
| 215 | func runtimePolicyEntry(name string) bool { |
| 216 | name = strings.ToLower(name) |
| 217 | switch name { |
| 218 | case "config.toml", "agents.md", "claude.md", "hooks", "plugins", "skills", "agents", "commands", ".mcp.json", "mcp.json", "mcp-config.json", "shell-snapshots", "session-env": |
| 219 | return true |
| 220 | } |
| 221 | return strings.HasPrefix(name, "settings") && strings.HasSuffix(name, ".json") |
| 222 | } |
| 223 | |