| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "io/fs" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "regexp" |
| 8 | "slices" |
| 9 | "strings" |
| 10 | "syscall" |
| 11 | ) |
| 12 | |
| 13 | func (l *AccountLease) validateRuntimeEntries() error { |
| 14 | reason := func(category string) error { |
| 15 | if l.source.Provider == "claude" { |
| 16 | return accountContainment("runtime_claude_" + category) |
| 17 | } |
| 18 | return accountContainment("runtime_" + category) |
| 19 | } |
| 20 | allowed := map[string]bool{IdentityFile: true, credentialName(l.source.Provider): true, ".claude.json": true, "history.jsonl": true, "sessions": true, "archived_sessions": true, "session_index.jsonl": true, "shell_snapshots": true, "log": true, "logs": true, "debug": true, "projects": true, "todos": true, "tasks": true, "plans": true, "backups": true, "file-history": true, "telemetry": true, "statsig": true, "paste-cache": true, "downloads": true, "stats-cache.json": true, "models_cache.json": true, "version.json": true, ".last_migration": true, ".codex-global-state.json": true, "equipo-read-sentinel": true} |
| 21 | return filepath.WalkDir(l.runtime, func(path string, d fs.DirEntry, err error) error { |
| 22 | if err != nil { |
| 23 | return reason("walk") |
| 24 | } |
| 25 | if path == l.runtime { |
| 26 | return nil |
| 27 | } |
| 28 | name := d.Name() |
| 29 | rel, err := filepath.Rel(l.runtime, path) |
| 30 | if err != nil { |
| 31 | return reason("walk") |
| 32 | } |
| 33 | entryReason := reason |
| 34 | if l.source.Provider == "codex" { |
| 35 | entryReason = func(category string) error { |
| 36 | if known := codexRuntimeCategory(rel); known != "" { |
| 37 | return accountContainment("runtime_codex_" + known) |
| 38 | } |
| 39 | return reason(category) |
| 40 | } |
| 41 | if strings.HasPrefix(rel, ".tmp/plugins") { |
| 42 | return entryReason("policy_entry") |
| 43 | } |
| 44 | if codexArg0Link.MatchString(rel) && d.Type()&os.ModeSymlink == 0 { |
| 45 | return entryReason("special_entry") |
| 46 | } |
| 47 | } |
| 48 | if l.source.Provider == "claude" && (name == claudeRemoteSettings || name == claudeRemoteSyncedAt) { |
| 49 | return l.claudeRemoteSettingsEntry(rel) |
| 50 | } |
| 51 | if d.Type()&os.ModeSymlink != 0 { |
| 52 | info, statErr := d.Info() |
| 53 | if statErr == nil && l.codexRuntimeLink(path, rel, info) { |
| 54 | return nil |
| 55 | } |
| 56 | return entryReason("special_entry") |
| 57 | } |
| 58 | if d.Type()&(os.ModeSocket|os.ModeDevice|os.ModeNamedPipe) != 0 { |
| 59 | return entryReason("special_entry") |
| 60 | } |
| 61 | claudeShell := l.source.Provider == "claude" && l.source.Expected.ProviderVersion == ClaudeVersion && (rel == "shell-snapshots" || rel == "session-env") |
| 62 | if runtimePolicyEntry(name) && !claudeShell { |
| 63 | return entryReason("policy_entry") |
| 64 | } |
| 65 | if filepath.Dir(path) == l.runtime && ProviderHomeTarget("selected-home", "/home/runner/"+name) { |
| 66 | info, err := d.Info() |
| 67 | if err != nil || !runtimeEntryMetadata(info) || !info.Mode().IsRegular() || info.Mode().Perm() != 0600 || info.Size() < 1 || info.Size() > 4096 { |
| 68 | return reason("marker_metadata") |
| 69 | } |
| 70 | return nil |
| 71 | } |
| 72 | top := strings.SplitN(rel, string(filepath.Separator), 2)[0] |
| 73 | if l.source.Provider == "claude" { |
| 74 | info, e := d.Info() |
| 75 | if e != nil || !runtimeEntryMetadata(info) { |
| 76 | return reason("metadata") |
| 77 | } |
| 78 | if handled, e := l.claudeRuntimeEntry(rel, info); handled { |
| 79 | return e |
| 80 | } |
| 81 | } |
| 82 | if l.source.Provider == "codex" && l.source.Expected.ProviderVersion == CodexVersion { |
| 83 | if top == ".sandbox_migration" || top == "thread-writer-locks" || top == ".sqlite-maintenance.lock" { |
| 84 | info, err := d.Info() |
| 85 | if err != nil { |
| 86 | return entryReason("metadata") |
| 87 | } |
| 88 | return codexTurnEntry(rel, info) |
| 89 | } |
| 90 | scratch := top == ".tmp" || top == "tmp" |
| 91 | if scratch || codexRuntimeState(top) { |
| 92 | info, err := d.Info() |
| 93 | if err != nil || !runtimeEntryMetadata(info) { |
| 94 | return entryReason("metadata") |
| 95 | } |
| 96 | if (scratch && rel == top && !info.IsDir()) || (!scratch && !info.Mode().IsRegular()) { |
| 97 | return entryReason("kind") |
| 98 | } |
| 99 | return nil |
| 100 | } |
| 101 | } |
| 102 | if filepath.Dir(path) == l.runtime && !allowed[name] && !strings.HasPrefix(name, "state_") && !strings.HasPrefix(name, "logs_") { |
| 103 | return entryReason("unexpected_entry:" + knownEntry(name)) |
| 104 | } |
| 105 | return nil |
| 106 | }) |
| 107 | } |
| 108 | |
| 109 | // knownEntries are names the pinned CLIs are known to write in a home |
| 110 | // (2.1.312's own list and its sandbox's protected dotfiles). A refusal |
| 111 | // names only these: any other name could be text the session chose. |
| 112 | var knownEntries = strings.Fields(`.claude.json.backup .session_ingress_token ide hfi-auth.json daemon jobs teams usage-data shares state uploads feedback feedback-bundles cache mcp-discovery-cache mcp-needs-auth-cache.json gh-pr-status-cache.json local antproto.json ccr bridge-spawn active-time.json loop.md server-sessions.json image-cache file-transfers mcp-skill-archives computer-use.lock server.lock api-dumps chrome local-settings project-settings remote remote-control scratch storage-v2 systemd agent-memory cowork_plugins dump-prompts traces startup-perf keybindings.json .gitmodules .zprofile .zshenv .zlogin .zlogout .bash_login .bash_logout .envrc .ripgreprc .git-credentials .pypirc .pgpass .bash_history .zsh_history .python_history .node_repl_history`) |
| 113 | |
| 114 | func knownEntry(name string) string { |
| 115 | if slices.Contains(knownEntries, name) || claudeScrubStub[name] != 0 { |
| 116 | return name |
| 117 | } |
| 118 | return "unlisted" |
| 119 | } |
| 120 | |
| 121 | // These are startup state in the staged home, never source-home inputs. |
| 122 | func codexRuntimeState(name string) bool { |
| 123 | switch name { |
| 124 | case "installation_id", "goals_1.sqlite", "goals_1.sqlite-shm", "goals_1.sqlite-wal", |
| 125 | "memories_1.sqlite", "memories_1.sqlite-shm", "memories_1.sqlite-wal", |
| 126 | "queue_1.sqlite", "queue_1.sqlite-shm", "queue_1.sqlite-wal", |
| 127 | "thread_history_1.sqlite", "thread_history_1.sqlite-shm", "thread_history_1.sqlite-wal": |
| 128 | return true |
| 129 | } |
| 130 | return false |
| 131 | } |
| 132 | |
| 133 | func runtimeEntryMetadata(info os.FileInfo) bool { |
| 134 | stat, ok := info.Sys().(*syscall.Stat_t) |
| 135 | return ok && stat.Uid == uint32(os.Getuid()) && info.Mode()&(os.ModeSetuid|os.ModeSetgid) == 0 && |
| 136 | (info.IsDir() || (info.Mode().IsRegular() && stat.Nlink == 1)) |
| 137 | } |
| 138 | |
| 139 | // The CLI points these launch shims at its pinned mount inside the namespace. |
| 140 | // Readlink compares the text only; the host must never resolve /opt/codex. |
| 141 | var codexArg0Link = regexp.MustCompile(`^tmp/arg0-linux/codex-arg0[A-Za-z0-9]{6}/(apply_patch|applypatch|codex-linux-sandbox)$`) |
| 142 | |
| 143 | func (l *AccountLease) codexRuntimeLink(path, rel string, info os.FileInfo) bool { |
| 144 | if l.source.Provider != "codex" || l.source.Expected.ProviderVersion != CodexVersion || !codexArg0Link.MatchString(rel) { |
| 145 | return false |
| 146 | } |
| 147 | stat, ok := info.Sys().(*syscall.Stat_t) |
| 148 | if !ok || stat.Uid != uint32(os.Getuid()) || info.Mode()&os.ModeSymlink == 0 || info.Mode()&(os.ModeSetuid|os.ModeSetgid) != 0 { |
| 149 | return false |
| 150 | } |
| 151 | target, err := os.Readlink(path) |
| 152 | return err == nil && target == "/opt/codex" |
| 153 | } |
| 154 | |
| 155 | func codexRuntimeCategory(rel string) string { |
| 156 | if strings.HasPrefix(rel, ".tmp/plugins") { |
| 157 | return "plugins" |
| 158 | } |
| 159 | if strings.HasPrefix(rel, "tmp/arg0-linux") { |
| 160 | return "arg0" |
| 161 | } |
| 162 | top := strings.SplitN(rel, "/", 2)[0] |
| 163 | switch top { |
| 164 | case ".tmp": |
| 165 | return "dot_tmp" |
| 166 | case "tmp": |
| 167 | return "tmp" |
| 168 | case "skills", "plugins", "installation_id": |
| 169 | return top |
| 170 | } |
| 171 | for _, state := range []string{"goals", "memories", "queue"} { |
| 172 | if strings.HasPrefix(top, state+"_") { |
| 173 | return state |
| 174 | } |
| 175 | } |
| 176 | return "" |
| 177 | } |
| 178 | |