| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "golang.org/x/sys/unix" |
| 12 | ) |
| 13 | |
| 14 | const sessionEnvFixture = "session-env/12345678-1234-4123-8123-123456789abc/" |
| 15 | |
| 16 | func TestAccountClaudeAuthenticatedState(t *testing.T) { |
| 17 | for _, name := range []string{"policy-limits.json", "policy-limits.json.signature.json", "policy-limits.json.signature-iat.json", "policy-limits.json.stamp.json", "shell-snapshots/snapshot-bash-1793880000000-abc123.sh", "shell-snapshots/snapshot-zsh-1793880000000-0.sh", "shell-snapshots/snapshot-sh-1793880000000-.sh", sessionEnvFixture + "setup-hook-0.sh", sessionEnvFixture + "sessionstart-hook-123.sh", sessionEnvFixture + "cwdchanged-hook-1.sh", sessionEnvFixture + "filechanged-hook-9.sh"} { |
| 18 | t.Run(name, func(t *testing.T) { |
| 19 | for _, kind := range []string{"file", "directory", "link", "hardlink", "fifo", "setuid", "setgid", "version", "provider", "source"} { |
| 20 | t.Run(kind, func(t *testing.T) { |
| 21 | fullTurn := kind == "file" || kind == "source" |
| 22 | l := &AccountLease{source: Binding{Provider: "claude", Expected: Identity{ProviderVersion: ClaudeVersion}}, runtime: t.TempDir()} |
| 23 | ctx := context.Background() |
| 24 | if fullTurn { |
| 25 | l = claudeRuntimeFixture(t) |
| 26 | preflightFixture(t, l) |
| 27 | if err := l.BeginTurn(ctx); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | } |
| 31 | root := l.runtime |
| 32 | if kind == "source" { |
| 33 | root = l.source.Home |
| 34 | } |
| 35 | path := filepath.Join(root, name) |
| 36 | if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | var err error |
| 40 | switch kind { |
| 41 | case "directory": |
| 42 | err = os.Mkdir(path, 0700) |
| 43 | case "link": |
| 44 | err = os.Symlink("/opt/claude", path) |
| 45 | case "hardlink": |
| 46 | target := filepath.Join(t.TempDir(), "state") |
| 47 | writeFake(t, target, "fixture") |
| 48 | err = os.Link(target, path) |
| 49 | case "fifo": |
| 50 | err = unix.Mkfifo(path, 0600) |
| 51 | default: |
| 52 | err = os.WriteFile(path, []byte("fixture"), 0600) |
| 53 | } |
| 54 | if err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | if kind == "setuid" { |
| 58 | err = os.Chmod(path, 0600|os.ModeSetuid) |
| 59 | } |
| 60 | if kind == "setgid" { |
| 61 | err = os.Chmod(path, 0600|os.ModeSetgid) |
| 62 | } |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | if fullTurn { |
| 67 | if err = l.EndTurn(ctx); err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | } |
| 71 | if kind == "source" { |
| 72 | preflightFixture(t, l) |
| 73 | if _, err = os.Lstat(filepath.Join(l.runtime, name)); !os.IsNotExist(err) { |
| 74 | t.Fatal("source runtime state staged") |
| 75 | } |
| 76 | } |
| 77 | if kind == "version" { |
| 78 | l.source.Expected.ProviderVersion = "other" |
| 79 | } |
| 80 | if kind == "provider" { |
| 81 | l.source.Provider = "other" |
| 82 | } |
| 83 | if !fullTurn { |
| 84 | err = l.validateRuntimeEntries() |
| 85 | } else { |
| 86 | err = l.BeginTurn(ctx) |
| 87 | } |
| 88 | if kind == "file" || kind == "source" { |
| 89 | if err != nil { |
| 90 | t.Fatalf("second turn: %v", err) |
| 91 | } |
| 92 | } else if !errors.Is(err, ErrContainment) { |
| 93 | t.Fatalf("unsafe %s accepted: %v", kind, err) |
| 94 | } |
| 95 | }) |
| 96 | } |
| 97 | }) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func TestAccountStartupRefusals(t *testing.T) { |
| 102 | for _, tc := range []struct{ family, path, category string }{ |
| 103 | {"claude", "remote-settings.json", "runtime_claude_remote_settings"}, |
| 104 | {"claude", "policy-limits.json.extra", "runtime_claude_unexpected_entry"}, |
| 105 | {"claude", "remote-settings.json.synced-at", "runtime_claude_remote_settings_synced_at"}, |
| 106 | {"claude", ".claude/remote-settings.json", "runtime_claude_remote_settings"}, |
| 107 | {"claude", "shell-snapshots/evil.sh", "runtime_claude_shell_snapshots"}, |
| 108 | {"claude", "shell-snapshots/snapshot-fish-1793880000000-abc123.sh", "runtime_claude_shell_snapshots"}, |
| 109 | {"claude", "shell-snapshots/snapshot-bash-1793880000000-abcdefg.sh", "runtime_claude_shell_snapshots"}, |
| 110 | {"claude", "shell-snapshots/snapshot-bash-1793880000000-ABC123.sh", "runtime_claude_shell_snapshots"}, |
| 111 | {"claude", "shell-snapshots/snapshot-bash-1793880000000-abc123-session.sh", "runtime_claude_shell_snapshots"}, |
| 112 | {"claude", "shell-snapshots/nested/evil.sh", "runtime_claude_shell_snapshots"}, |
| 113 | {"claude", "session-env/not-a-uuid/setup-hook-1.sh", "runtime_claude_session_env"}, |
| 114 | {"claude", sessionEnvFixture + "evil.sh", "runtime_claude_session_env"}, |
| 115 | {"claude", sessionEnvFixture + "setup-hook-x.sh", "runtime_claude_session_env"}, |
| 116 | {"claude", sessionEnvFixture + "stop-hook-1.sh", "runtime_claude_session_env"}, |
| 117 | {"claude", sessionEnvFixture + "nested/setup-hook-1.sh", "runtime_claude_session_env"}, |
| 118 | {"claude", "logs/shell-snapshots/evil.sh", "runtime_claude_policy_entry"}, |
| 119 | {"codex", ".tmp/plugins.sha", "runtime_codex_plugins"}, |
| 120 | {"codex", ".tmp/plugins/cache", "runtime_codex_plugins"}, |
| 121 | {"codex", ".tmp/plugins-other", "runtime_codex_plugins"}, |
| 122 | {"codex", "tmp/config.toml", "runtime_codex_tmp"}, |
| 123 | {"codex", ".tmp/config.toml", "runtime_codex_dot_tmp"}, |
| 124 | {"codex", "skills/.system/evil", "runtime_codex_skills"}, |
| 125 | {"codex", "installation_id/evil", "runtime_codex_installation_id"}, |
| 126 | {"codex", "goals_1.sqlite/evil", "runtime_codex_goals"}, |
| 127 | {"codex", "memories_1.sqlite/evil", "runtime_codex_memories"}, |
| 128 | {"codex", "queue_1.sqlite/evil", "runtime_codex_queue"}, |
| 129 | {"codex", "tmp/arg0-linux/codex-arg0abc123/apply_patch", "runtime_codex_arg0"}, |
| 130 | } { |
| 131 | t.Run(tc.family+"/"+tc.path, func(t *testing.T) { |
| 132 | var l *AccountLease |
| 133 | if tc.family == "claude" { |
| 134 | l = claudeRuntimeFixture(t) |
| 135 | } else { |
| 136 | b, r := accountFixture(t) |
| 137 | l = openFixture(t, b, r) |
| 138 | } |
| 139 | path := filepath.Join(l.runtime, tc.path) |
| 140 | if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { |
| 141 | t.Fatal(err) |
| 142 | } |
| 143 | writeFake(t, path, "secret-sentinel") |
| 144 | err := l.validateRuntimeEntries() |
| 145 | if !errors.Is(err, ErrContainment) || !strings.Contains(err.Error(), tc.category) || strings.Contains(err.Error(), "secret-sentinel") { |
| 146 | t.Fatalf("want %s: %v", tc.category, err) |
| 147 | } |
| 148 | }) |
| 149 | } |
| 150 | } |
| 151 | |