package provider import ( "io" "os" "path/filepath" "regexp" "strings" "golang.org/x/sys/unix" ) 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$`) 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$`) // 2.1.312 uses Date.now() and Math.random().toString(36).substring(2,8). // Its session suffix helper is disabled in this binary. var claudeShellSnapshot = regexp.MustCompile(`^shell-snapshots/snapshot-(bash|zsh|sh)-[0-9]{13}-[a-z0-9]{0,6}\.sh$`) 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}$`) 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$`) // Only pinned startup paths are runtime state. Empty support directories // do not authorize SSH, config, mod or seed files. func (l *AccountLease) claudeRuntimeEntry(rel string, info os.FileInfo) (bool, error) { top := strings.SplitN(rel, "/", 2)[0] if l.source.Token && l.source.Expected.ProviderVersion == ClaudeVersion && (claudeScrubStub[rel] != 0 || top == ".pip") { return true, claudeScrubEntry(rel, info) } category := "" switch top { case ".cc-writes": category = "write_staging" case ".last-cleanup": category = "cleanup_marker" case ".cache": category = "cache" case ".config", ".claude": category = "config" case ".npm": category = "npm" case "dev-mods": category = "dev_mods" case "seed-admin": category = "seed_admin" case "policy-limits.json", "policy-limits.json.signature.json", "policy-limits.json.signature-iat.json", claudePolicyStamp: category = "policy_limits" case "shell-snapshots": category = "shell_snapshots" case "session-env": category = "session_env" default: return false, nil } if l.source.Expected.ProviderVersion != ClaudeVersion { return true, accountContainment("runtime_claude_" + category) } if rel == ".config/git" || rel == ".config/git/ignore" { valid := rel == ".config/git" && info.IsDir() && claudeStubDir(info.Mode().Perm()) || rel == ".config/git/ignore" && l.claudeGitIgnore(info) if !valid { return true, accountContainment("runtime_claude_git_ignore") } return true, nil } if category == "write_staging" || rel == ".claude/bridge-spawn" { if (rel != ".cc-writes" && rel != ".claude/bridge-spawn") || !info.IsDir() || info.Mode().Perm() != 0700 { return true, accountContainment("runtime_claude_write_staging") } entries, err := os.ReadDir(filepath.Join(l.runtime, rel)) if err != nil || len(entries) != 0 { return true, accountContainment("runtime_claude_write_staging") } return true, nil } if category == "cleanup_marker" { if rel != ".last-cleanup" || !info.Mode().IsRegular() || info.Size() != 24 { return true, accountContainment("runtime_claude_cleanup_marker") } return true, nil } directory := false switch rel { 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": directory = true case "policy-limits.json", "policy-limits.json.signature.json", "policy-limits.json.signature-iat.json": case claudePolicyStamp: if info.Size() > claudePolicyStampMax { return true, accountContainment("runtime_claude_policy_limits") } default: // These scripts are safe only while agent tools cannot access the staged // home: claude/launcher.go:139-149 builds native and Bash sandbox denies. directory = claudeSessionDir.MatchString(rel) if !directory && !claudeShellSnapshot.MatchString(rel) && !claudeSessionFile.MatchString(rel) && rel != ".npm/_update-notifier-last-checked" && !claudeRuntimeLog.MatchString(rel) && !claudeNPMLog.MatchString(rel) { return true, accountContainment("runtime_claude_" + category) } } if (directory && !info.IsDir()) || (!directory && !info.Mode().IsRegular()) { return true, accountContainment("runtime_claude_kind") } return true, nil } // 2.1.312 in scrub mode (CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=1, set for // token launches only: LaunchSecretEnv) creates these in its home at // startup, empty, for its Bash sandbox to cover ("claude-code scrub-mode // stubs"; observed with a throwaway home 2026-11-21). Empty is all it // writes; content, another kind or a child is refused. 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'} func claudeScrubEntry(rel string, info os.FileInfo) error { perm, kind := info.Mode().Perm(), claudeScrubStub[rel] if kind == 'f' && info.Mode().IsRegular() && info.Size() == 0 && (perm == 0600 || perm == 0644 || perm == 0664) || kind == 'd' && info.IsDir() && claudeStubDir(perm) { return nil } return accountContainment("runtime_claude_scrub_stub") } // claudeStubDir is a directory mode the CLI makes under umask 077, 022 or 002. func claudeStubDir(perm os.FileMode) bool { return perm == 0700 || perm == 0755 || perm == 0775 } func (l *AccountLease) claudeGitIgnore(info os.FileInfo) bool { const observed = "**/.claude/.cc-writes/\n" if !info.Mode().IsRegular() || (info.Mode().Perm() != 0664 && info.Mode().Perm() != 0600) || info.Size() != int64(len(observed)) { return false } path := filepath.Join(l.runtime, ".config/git/ignore") fd, err := openNoLinks(path, unix.O_RDONLY|unix.O_CLOEXEC|unix.O_NONBLOCK) if err != nil { return false } f := os.NewFile(uintptr(fd), path) defer f.Close() opened, err := f.Stat() if err != nil || !os.SameFile(info, opened) || !runtimeEntryMetadata(opened) || !opened.Mode().IsRegular() || opened.Mode().Perm() != info.Mode().Perm() || opened.Size() != info.Size() { return false } raw, err := io.ReadAll(io.LimitReader(f, int64(len(observed)+1))) return err == nil && string(raw) == observed } // 2.1.312 stamps its policy-limits cache (identity, kind and body digest, // mode 0600) beside the other policy-limits files. The CLI itself treats a // stamp over 4096 bytes as unusable, so nothing larger is its own. const ( claudePolicyStamp = "policy-limits.json.stamp.json" claudePolicyStampMax = 4096 ) // remote-settings.json is 2.1.312's cache of the managed settings an // organization sets for its account. Managed settings outrank the settings // Equipo mounts, so any setting in it could change policy. The empty // object sets nothing and so changes no policy: that, and only that, is // accepted. The CLI writes JSON.stringify(settings, null, 2) with mode 0600, // "{}" for an organization with none; 64 bytes leaves room only for // whitespace around it. Its .synced-at sibling holds the millisecond time // of that sync, never settings (the CLI reads at most 32 bytes of it), and // is accepted only beside an accepted empty object. const ( claudeRemoteSettings = "remote-settings.json" claudeRemoteSyncedAt = "remote-settings.json.synced-at" claudeRemoteSettingsMax = 64 claudeRemoteSyncedAtMax = 32 ) // claudeRemoteSettingsEntry decides either name wherever it appears in the // home; outside the home's root it is refused. func (l *AccountLease) claudeRemoteSettingsEntry(rel string) error { if l.source.Expected.ProviderVersion == ClaudeVersion && l.claudeEmptyRemoteSettings() { if rel == claudeRemoteSettings { return nil } if rel == claudeRemoteSyncedAt { if _, ok := l.claudeRemoteFile(claudeRemoteSyncedAt, claudeRemoteSyncedAtMax); ok { return nil } } } if filepath.Base(rel) == claudeRemoteSyncedAt { return accountContainment("runtime_claude_remote_settings_synced_at") } return accountContainment("runtime_claude_remote_settings") } func (l *AccountLease) claudeEmptyRemoteSettings() bool { raw, ok := l.claudeRemoteFile(claudeRemoteSettings, claudeRemoteSettingsMax) return ok && strings.Trim(string(raw), " \t\r\n") == "{}" } // claudeRemoteFile reads a root file of the staged home that is the CLI's // own: runner uid, one link, no setuid/setgid, regular, 0600, at most limit // bytes, opened without following links. func (l *AccountLease) claudeRemoteFile(name string, limit int64) ([]byte, bool) { path := filepath.Join(l.runtime, name) private := func(info os.FileInfo) bool { return runtimeEntryMetadata(info) && info.Mode().IsRegular() && info.Mode().Perm() == 0600 && info.Size() <= limit } info, err := os.Lstat(path) if err != nil || !private(info) { return nil, false } fd, err := openNoLinks(path, unix.O_RDONLY|unix.O_CLOEXEC|unix.O_NONBLOCK) if err != nil { return nil, false } f := os.NewFile(uintptr(fd), path) defer f.Close() opened, err := f.Stat() if err != nil || !os.SameFile(info, opened) || !private(opened) { return nil, false } raw, err := io.ReadAll(io.LimitReader(f, limit+1)) return raw, err == nil && int64(len(raw)) <= limit } func runtimePolicyEntry(name string) bool { name = strings.ToLower(name) switch name { case "config.toml", "agents.md", "claude.md", "hooks", "plugins", "skills", "agents", "commands", ".mcp.json", "mcp.json", "mcp-config.json", "shell-snapshots", "session-env": return true } return strings.HasPrefix(name, "settings") && strings.HasSuffix(name, ".json") }