| 1 | package claude |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "crypto/sha256" |
| 7 | "encoding/hex" |
| 8 | "encoding/json" |
| 9 | "os" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 15 | ) |
| 16 | |
| 17 | func TestClaudePermissionResponse(t *testing.T) { |
| 18 | f := fixtureFor(t, "Engineering") |
| 19 | s, events := openFixture(t, f) |
| 20 | f.l.mu.Lock() |
| 21 | st, _ := f.l.state(s) |
| 22 | st.active = true |
| 23 | st.turn = provider.Turn{ID: "turn"} |
| 24 | f.l.mu.Unlock() |
| 25 | ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) |
| 26 | defer cancel() |
| 27 | raw := json.RawMessage(`{"tool_name":"AskUserQuestion","input":{"questions":[{"question":"Which?","options":[{"label":"blue"}]}]}}`) |
| 28 | result := make(chan json.RawMessage, 1) |
| 29 | errs := make(chan error, 1) |
| 30 | go func() { v, e := f.l.Permission(ctx, s, raw); result <- v; errs <- e }() |
| 31 | var ev provider.Event |
| 32 | select { |
| 33 | case ev = <-events: |
| 34 | case <-ctx.Done(): |
| 35 | t.Fatal("permission not relayed") |
| 36 | } |
| 37 | if ev.Kind != "request" || ev.RequestID == "" { |
| 38 | t.Fatal(ev) |
| 39 | } |
| 40 | for _, input := range []string{`{"answers":{"Which?":"blue"},"command":"evil"}`, `{"answers":{"Other?":"blue"}}`} { |
| 41 | if f.l.Respond(ctx, s, provider.Answer{TurnID: "turn", RequestID: ev.RequestID, Behavior: "allow", Input: json.RawMessage(input)}) == nil { |
| 42 | t.Fatal("widened answer accepted", input) |
| 43 | } |
| 44 | } |
| 45 | f.capture.mu.Lock() |
| 46 | before := f.capture.inputs |
| 47 | f.capture.mu.Unlock() |
| 48 | if e := f.l.Respond(ctx, s, provider.Answer{TurnID: "turn", RequestID: ev.RequestID, Behavior: "allow", Input: json.RawMessage(`{"answers":{"Which?":"blue"}}`)}); e != nil { |
| 49 | t.Fatal(e) |
| 50 | } |
| 51 | reply := <-result |
| 52 | if e := <-errs; e != nil { |
| 53 | t.Fatal(e) |
| 54 | } |
| 55 | var v struct { |
| 56 | Behavior string |
| 57 | UpdatedInput map[string]json.RawMessage |
| 58 | } |
| 59 | if json.Unmarshal(reply, &v) != nil || v.Behavior != "allow" || !strings.Contains(string(v.UpdatedInput["questions"]), "options") || !strings.Contains(string(v.UpdatedInput["answers"]), "blue") { |
| 60 | t.Fatal(string(reply)) |
| 61 | } |
| 62 | f.capture.mu.Lock() |
| 63 | after := f.capture.inputs |
| 64 | f.capture.mu.Unlock() |
| 65 | if before != after { |
| 66 | t.Fatal("permission reply sent an ordinary prompt") |
| 67 | } |
| 68 | if f.l.Respond(ctx, s, provider.Answer{TurnID: "turn", RequestID: ev.RequestID, Behavior: "deny"}) == nil { |
| 69 | t.Fatal("replayed answer") |
| 70 | } |
| 71 | for _, name := range f.inputs.Tools { |
| 72 | if name == "AskUserQuestion" { |
| 73 | t.Fatal("built-in question added to specialist tools") |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func TestClaudePermissionResponseNativePolicy(t *testing.T) { |
| 79 | f := fixtureFor(t, "Engineering") |
| 80 | original := permissionRequest{Tool: "Write", Input: json.RawMessage(`{"file_path":"/workspace/hello.txt","content":"hi"}`)} |
| 81 | reply, e := permissionResult(original, provider.Answer{Behavior: "allow"}, f.launch.Policy) |
| 82 | if e != nil || !strings.Contains(string(reply), `"content":"hi"`) { |
| 83 | t.Fatal("original input lost", string(reply), e) |
| 84 | } |
| 85 | for _, raw := range []string{`{"file_path":"/tool/equipo.sock","content":"hi"}`, `{"file_path":"/workspace/a","content":"hi","policyAmendment":true}`, `{"file_path":"/workspace/a","content":"hi","dangerouslyDisableSandbox":true}`} { |
| 86 | bad := original |
| 87 | bad.Input = json.RawMessage(raw) |
| 88 | if _, e := permissionResult(bad, provider.Answer{Behavior: "allow"}, f.launch.Policy); e == nil { |
| 89 | t.Fatal("native policy widened", raw) |
| 90 | } |
| 91 | } |
| 92 | if _, e := permissionResult(original, provider.Answer{Behavior: "allow", Input: json.RawMessage(`{"file_path":"/workspace/replaced"}`)}, f.launch.Policy); e == nil { |
| 93 | t.Fatal("native tool input replaced") |
| 94 | } |
| 95 | deny, e := permissionResult(original, provider.Answer{Behavior: "deny"}, f.launch.Policy) |
| 96 | var denied struct{ Behavior, Message string } |
| 97 | if e != nil || json.Unmarshal(deny, &denied) != nil || denied.Behavior != "deny" || strings.TrimSpace(denied.Message) == "" { |
| 98 | t.Fatal(string(deny), e) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func TestClaudePermissionResponseScopedMCPPolicy(t *testing.T) { |
| 103 | f := fixtureFor(t, "Engineering") |
| 104 | in := f.inputs |
| 105 | in.Tools = []string{"mcp__equipo__ask_person", "mcp__equipo__submit_candidate"} |
| 106 | got, e := PolicyInputs(f.lease.Binding(), in, "Engineering", "claude-haiku-5-5") |
| 107 | if e != nil { |
| 108 | t.Fatal(e) |
| 109 | } |
| 110 | var settings struct{ Permissions struct{ Allow []string } } |
| 111 | if json.Unmarshal(got.Settings, &settings) != nil || !strings.HasPrefix(strings.Join(settings.Permissions.Allow, ","), strings.Join(in.Tools, ",")) { |
| 112 | t.Fatal("scoped MCP would fall through native permission hook", string(got.Settings)) |
| 113 | } |
| 114 | in.Tools = []string{"mcp__equipo__publish"} |
| 115 | if _, e = PolicyInputs(f.lease.Binding(), in, "Engineering", "claude-haiku-5-5"); e == nil { |
| 116 | t.Fatal("unscoped MCP permission") |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func TestClaudePermissionResponsePinnedWire(t *testing.T) { |
| 121 | raw, e := os.ReadFile("testdata/permission-prompt-2.1.312.json") |
| 122 | if e != nil { |
| 123 | t.Fatal(e) |
| 124 | } |
| 125 | var fixture struct{ Request json.RawMessage } |
| 126 | if e = json.Unmarshal(raw, &fixture); e != nil { |
| 127 | t.Fatal(e) |
| 128 | } |
| 129 | f := fixtureFor(t, "Engineering") |
| 130 | s, events := openFixture(t, f) |
| 131 | f.l.mu.Lock() |
| 132 | st, _ := f.l.state(s) |
| 133 | st.active = true |
| 134 | st.turn = provider.Turn{ID: "turn"} |
| 135 | f.l.mu.Unlock() |
| 136 | ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 137 | defer cancel() |
| 138 | type response struct { |
| 139 | raw json.RawMessage |
| 140 | err error |
| 141 | } |
| 142 | // A workspace action inside the profile is answered by Equipo, not relayed. |
| 143 | allowed, e := f.l.Permission(ctx, s, fixture.Request) |
| 144 | var allow struct{ Behavior string } |
| 145 | if e != nil || json.Unmarshal(allowed, &allow) != nil || allow.Behavior != "allow" { |
| 146 | t.Fatalf("workspace action not allowed in profile: %s %v", allowed, e) |
| 147 | } |
| 148 | select { |
| 149 | case ev := <-events: |
| 150 | t.Fatalf("workspace action relayed to a person: %s", ev.Payload) |
| 151 | default: |
| 152 | } |
| 153 | // Outside the workspace the pinned request still reaches the person. |
| 154 | outside := bytes.Replace(fixture.Request, []byte(`/workspace/hello.txt`), []byte(`/srv/hello.txt`), 1) |
| 155 | result := make(chan response, 1) |
| 156 | go func() { raw, err := f.l.Permission(ctx, s, outside); result <- response{raw, err} }() |
| 157 | select { |
| 158 | case ev := <-events: |
| 159 | if e = f.l.Respond(ctx, s, provider.Answer{TurnID: "turn", RequestID: ev.RequestID, Behavior: "deny"}); e != nil { |
| 160 | t.Fatal(e) |
| 161 | } |
| 162 | case got := <-result: |
| 163 | t.Fatalf("pinned permission input rejected: %v", got.err) |
| 164 | case <-ctx.Done(): |
| 165 | t.Fatal("permission not relayed") |
| 166 | } |
| 167 | got := <-result |
| 168 | var deny struct{ Behavior, Message string } |
| 169 | if got.err != nil || json.Unmarshal(got.raw, &deny) != nil || deny.Behavior != "deny" || strings.TrimSpace(deny.Message) == "" { |
| 170 | t.Fatalf("pinned denial needs message: %s %v", got.raw, got.err) |
| 171 | } |
| 172 | // Only the three pinned input fields are accepted. |
| 173 | if _, e = f.l.Permission(ctx, s, json.RawMessage(`{"tool_name":"Write","input":{},"tool_use_id":"fixture","permissions":{}}`)); e == nil { |
| 174 | t.Fatal("unknown field accepted") |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // TestStepPermission: with nobody to ask, an Engineering step may use its |
| 179 | // file tools inside /workspace under the same checks, and its shell, which |
| 180 | // the policy confines to the CLI's sandbox; nothing else. |
| 181 | func TestStepPermission(t *testing.T) { |
| 182 | f := fixtureFor(t, "Engineering") |
| 183 | allowed := func(raw string) bool { |
| 184 | var v struct{ Behavior string } |
| 185 | out := StepPermission(json.RawMessage(raw), f.launch.Policy) |
| 186 | if json.Unmarshal(out, &v) != nil { |
| 187 | t.Fatalf("not JSON: %s", out) |
| 188 | } |
| 189 | return v.Behavior == "allow" |
| 190 | } |
| 191 | for _, raw := range []string{ |
| 192 | `{"tool_name":"Edit","input":{"file_path":"/workspace/README.md","old_string":"teh","new_string":"the"}}`, |
| 193 | `{"tool_name":"Write","input":{"file_path":"/workspace/panes/new.go","content":"package panes"}}`, |
| 194 | `{"tool_name":"Read","input":{"file_path":"/workspace/README.md"}}`, |
| 195 | `{"tool_name":"Grep","input":{"pattern":"swipe","path":"/workspace"}}`, |
| 196 | `{"tool_name":"Bash","input":{"command":"curl example.com"}}`, |
| 197 | } { |
| 198 | if !allowed(raw) { |
| 199 | t.Fatalf("refused %s", raw) |
| 200 | } |
| 201 | } |
| 202 | for _, raw := range []string{ |
| 203 | `{"tool_name":"Edit","input":{"file_path":"/etc/passwd","old_string":"a","new_string":"b"}}`, |
| 204 | `{"tool_name":"Read","input":{"file_path":"/workspace/../home/runner/.credentials.json"}}`, |
| 205 | `{"tool_name":"Write","input":{"file_path":"/tool/equipo.sock","content":"x"}}`, |
| 206 | `{"tool_name":"Write","input":{"file_path":"/workspace/a","content":"x","dangerouslyDisableSandbox":true}}`, |
| 207 | `{"tool_name":"Bash","input":{"command":"ls","dangerouslyDisableSandbox":true}}`, |
| 208 | `{"tool_name":"mcp__equipo__submit_candidate","input":{}}`, |
| 209 | `{"tool_name":"Read","input":{"file_path":"README.md"}}`, |
| 210 | `{"tool_name":"Edit","input":{"file_path":"/workspace/a"},"extra":1}`, |
| 211 | `not json`, |
| 212 | } { |
| 213 | if allowed(raw) { |
| 214 | t.Fatalf("allowed %s", raw) |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // TestStepPermissionReadsResearch: Engineering and Review may read the |
| 220 | // kept Research images at /research (Read, Glob, Grep only); nothing |
| 221 | // writes there, and nothing reaches past it. |
| 222 | func TestStepPermissionReadsResearch(t *testing.T) { |
| 223 | f := fixtureFor(t, "Engineering") |
| 224 | allowed := func(check func(json.RawMessage, provider.Policy) json.RawMessage, raw string) bool { |
| 225 | var v struct{ Behavior string } |
| 226 | if json.Unmarshal(check(json.RawMessage(raw), f.launch.Policy), &v) != nil { |
| 227 | t.Fatalf("not JSON for %s", raw) |
| 228 | } |
| 229 | return v.Behavior == "allow" |
| 230 | } |
| 231 | for _, check := range []func(json.RawMessage, provider.Policy) json.RawMessage{StepPermission, ReadPermission} { |
| 232 | for _, raw := range []string{ |
| 233 | `{"tool_name":"Read","input":{"file_path":"/research/img1-logo.png"}}`, |
| 234 | `{"tool_name":"Glob","input":{"pattern":"*.png","path":"/research"}}`, |
| 235 | `{"tool_name":"Grep","input":{"pattern":"x","path":"/research/"}}`, |
| 236 | } { |
| 237 | if !allowed(check, raw) { |
| 238 | t.Fatalf("refused %s", raw) |
| 239 | } |
| 240 | } |
| 241 | for _, raw := range []string{ |
| 242 | `{"tool_name":"Write","input":{"file_path":"/research/img9.png","content":"x"}}`, |
| 243 | `{"tool_name":"Edit","input":{"file_path":"/research/img1-logo.png","old_string":"a","new_string":"b"}}`, |
| 244 | `{"tool_name":"Read","input":{"file_path":"/research/../home/runner/.credentials.json"}}`, |
| 245 | `{"tool_name":"Read","input":{"file_path":"/research/../etc/passwd"}}`, |
| 246 | `{"tool_name":"Read","input":{"file_path":"/researchers/a"}}`, |
| 247 | `{"tool_name":"Read","input":{"file_path":"research/img1-logo.png"}}`, |
| 248 | `{"tool_name":"Glob","input":{"pattern":"../home/*","path":"/research"}}`, |
| 249 | } { |
| 250 | if allowed(check, raw) { |
| 251 | t.Fatalf("allowed %s", raw) |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | // Engineering's shell runs in the sandbox, which binds /research |
| 256 | // read-only; a read-only step runs no shell through its callback. |
| 257 | if allowed(ReadPermission, `{"tool_name":"Bash","input":{"command":"cat /research/img1-logo.png"}}`) { |
| 258 | t.Fatal("read-only step allowed a shell read") |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | // TestStepPermissionRunsSandboxedShell: production 2026-11-22, a token |
| 263 | // connection (claude-2). Token launches set CLAUDE_CODE_SUBPROCESS_ENV_SCRUB, |
| 264 | // and in that mode the pinned CLI turns sandbox auto-approval off, so its |
| 265 | // ordinary shell commands reached this callback and every one was refused. |
| 266 | // The policy forces every command into the CLI's sandbox, so the step |
| 267 | // allows them exactly as the CLI's auto-approval would. No command's text |
| 268 | // is special; a policy that reserves shell approval for a person (any |
| 269 | // Bash ask rule) refuses every shell request in a step instead. |
| 270 | func TestStepPermissionRunsSandboxedShell(t *testing.T) { |
| 271 | f := fixtureFor(t, "Engineering") |
| 272 | // The commands the CLI escalated in that step (its reason codes: |
| 273 | // cd-compound-read, too-complex, no-rule-match, outside the working |
| 274 | // directory). |
| 275 | for _, command := range []string{ |
| 276 | `cd internal/home; grep -ln "operator/confirm" *_test.go`, |
| 277 | `cd /workspace/internal/home; ls *operator*`, |
| 278 | `export PATH=/opt/go/bin:$PATH GOMODCACHE=/opt/go-mod GOTOOLCHAIN=local; go test ./internal/home/`, |
| 279 | `GOTOOLCHAIN=local CGO_ENABLED=0 /opt/go/bin/go test -count=1 ./internal/home/ 2>&1 | tail -30`, |
| 280 | `/opt/go/bin/go version`, |
| 281 | `ls /opt/go/bin`, |
| 282 | `which go gofmt; ls /opt`, |
| 283 | `printf equipo-cert-deny`, `printf "equipo-cert-allow"`, `env printf equipo-cert-deny`, |
| 284 | } { |
| 285 | input, _ := json.Marshal(map[string]any{"command": command, "description": "d", "timeout": 600000}) |
| 286 | raw, _ := json.Marshal(map[string]any{"tool_name": "Bash", "tool_use_id": "toolu_x", "input": json.RawMessage(input)}) |
| 287 | var v struct { |
| 288 | Behavior string |
| 289 | UpdatedInput json.RawMessage |
| 290 | } |
| 291 | if json.Unmarshal(StepPermission(raw, f.launch.Policy), &v) != nil || v.Behavior != "allow" || !sameJSON(v.UpdatedInput, input) { |
| 292 | t.Fatalf("sandboxed command refused or changed: %s -> %+v", command, v) |
| 293 | } |
| 294 | } |
| 295 | for _, raw := range []string{ |
| 296 | `{"tool_name":"Bash","input":{"command":"ls","dangerouslyDisableSandbox":false}}`, |
| 297 | `{"tool_name":"Bash","input":{"command":"ls","sandbox":false}}`, |
| 298 | `{"tool_name":"Bash","input":{"command":7}}`, |
| 299 | `{"tool_name":"Bash","input":{}}`, |
| 300 | } { |
| 301 | if stepBehavior(t, StepPermission, raw, f.launch.Policy) == "allow" { |
| 302 | t.Fatalf("allowed %s", raw) |
| 303 | } |
| 304 | } |
| 305 | // Only a policy that keeps every command in the sandbox lets the step |
| 306 | // approve shell commands itself. |
| 307 | for name, mutate := range map[string]func(map[string]any){ |
| 308 | "sandbox off": func(s map[string]any) { s["enabled"] = false }, |
| 309 | "unsandboxed allowed": func(s map[string]any) { s["allowUnsandboxedCommands"] = true }, |
| 310 | "may run unconfined": func(s map[string]any) { s["failIfUnavailable"] = false }, |
| 311 | "excluded commands": func(s map[string]any) { s["excludedCommands"] = []string{"go"} }, |
| 312 | "weaker nested": func(s map[string]any) { s["enableWeakerNestedSandbox"] = true }, |
| 313 | "no sandbox key": nil, |
| 314 | } { |
| 315 | p := withSandboxSettings(t, f.launch.Policy, mutate) |
| 316 | if stepBehavior(t, StepPermission, `{"tool_name":"Bash","input":{"command":"ls"}}`, p) == "allow" { |
| 317 | t.Fatalf("%s: shell allowed", name) |
| 318 | } |
| 319 | } |
| 320 | reserved := withSettings(t, f.launch.Policy, func(s map[string]any) { |
| 321 | s["permissions"].(map[string]any)["ask"] = []string{"Bash(git push:*)"} |
| 322 | }) |
| 323 | if stepBehavior(t, StepPermission, `{"tool_name":"Bash","input":{"command":"ls"}}`, reserved) == "allow" { |
| 324 | t.Fatal("a policy reserving shell approval for a person let a step approve") |
| 325 | } |
| 326 | // Review keeps its read-only callback: no shell through it. |
| 327 | r := fixtureFor(t, "Review") |
| 328 | if stepBehavior(t, ReadPermission, `{"tool_name":"Bash","input":{"command":"ls"}}`, r.launch.Policy) == "allow" { |
| 329 | t.Fatal("read-only step allowed a shell command") |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | // TestStepPermissionNamesTheRule: a refusal tells the model which rule |
| 334 | // refused it and what to do instead, in fixed words that never repeat |
| 335 | // the request. |
| 336 | func TestStepPermissionNamesTheRule(t *testing.T) { |
| 337 | f := fixtureFor(t, "Engineering") |
| 338 | secret := "sk-ant-oat01-zzzz" |
| 339 | for _, tc := range []struct{ raw, want string }{ |
| 340 | {`{"tool_name":"Glob","input":{"pattern":"internal/home/*` + secret + `*"}}`, "absolute path"}, |
| 341 | {`{"tool_name":"Read","input":{"file_path":"README-` + secret + `.md"}}`, "absolute path"}, |
| 342 | {`{"tool_name":"Read","input":{"file_path":"~/` + secret + `"}}`, "absolute path"}, |
| 343 | {`{"tool_name":"Read","input":{"file_path":"/etc/` + secret + `"}}`, "/workspace"}, |
| 344 | {`{"tool_name":"Write","input":{"file_path":"/tool/equipo.sock","content":"` + secret + `"}}`, "protected"}, |
| 345 | {`{"tool_name":"Write","input":{"file_path":"/research/a","content":"` + secret + `"}}`, "read"}, |
| 346 | {`{"tool_name":"Bash","input":{"command":"ls ` + secret + `","dangerouslyDisableSandbox":true}}`, "sandbox"}, |
| 347 | {`{"tool_name":"WebFetch","input":{"url":"https://` + secret + `"}}`, "not available"}, |
| 348 | {`{"tool_name":"Edit","input":{"file_path":"/workspace/a","old_string":"` + secret + `","new_string":"b","extra":1}}`, "field"}, |
| 349 | } { |
| 350 | out := StepPermission(json.RawMessage(tc.raw), f.launch.Policy) |
| 351 | var v struct{ Behavior, Message string } |
| 352 | if json.Unmarshal(out, &v) != nil || v.Behavior != "deny" { |
| 353 | t.Fatalf("not denied: %s -> %s", tc.raw, out) |
| 354 | } |
| 355 | if !strings.HasPrefix(v.Message, provider.ErrPolicy.Error()+": ") || !strings.Contains(v.Message, tc.want) || strings.Contains(v.Message, secret) { |
| 356 | t.Fatalf("%s: message %q, want the rule naming %q and no request text", tc.raw, v.Message, tc.want) |
| 357 | } |
| 358 | } |
| 359 | r := fixtureFor(t, "Review") |
| 360 | var v struct{ Behavior, Message string } |
| 361 | if json.Unmarshal(ReadPermission(json.RawMessage(`{"tool_name":"Bash","input":{"command":"ls"}}`), r.launch.Policy), &v) != nil || v.Behavior != "deny" || !strings.Contains(v.Message, "Read, Glob and Grep") { |
| 362 | t.Fatalf("read-only refusal does not name its rule: %+v", v) |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | // TestPermissionLinuxUnchanged: the Mac port (task 7 of slice F) leaves |
| 367 | // every Linux launch exactly as certified. The digests are of the |
| 368 | // settings, argv, tools and denied paths PolicyInputs renders for each |
| 369 | // Linux launch shape, taken at 098407b0 before the port; the existing |
| 370 | // answer tests above keep passing beside it. |
| 371 | func TestPermissionLinuxUnchanged(t *testing.T) { |
| 372 | for _, c := range linuxGoldenCases() { |
| 373 | if got := linuxSettingsDigest(t, c.action, c.role); got != c.digest { |
| 374 | t.Errorf("Linux launch %s/%s changed: %s", c.action, c.role, got) |
| 375 | } |
| 376 | } |
| 377 | // A Linux policy reads as Linux: /workspace and /research, and a |
| 378 | // refusal's words are the fixed ones. |
| 379 | f := fixtureFor(t, "Engineering") |
| 380 | if w, r := Roots(f.launch.Policy); LinuxWorkspaceRoot != "/workspace" || LinuxResearchRoot != "/research" || w != LinuxWorkspaceRoot || r != LinuxResearchRoot { |
| 381 | t.Fatalf("Linux roots %q %q", w, r) |
| 382 | } |
| 383 | var v struct{ Behavior, Message string } |
| 384 | if json.Unmarshal(StepPermission(json.RawMessage(`{"tool_name":"Read","input":{"file_path":"/etc/passwd"}}`), f.launch.Policy), &v) != nil || v.Message != provider.ErrPolicy.Error()+": "+refuseOutside { |
| 385 | t.Fatalf("Linux refusal changed: %+v", v) |
| 386 | } |
| 387 | // SandboxNetworkAccess is refused on Linux as before: not a step tool. |
| 388 | if json.Unmarshal(StepPermission(json.RawMessage(`{"tool_name":"SandboxNetworkAccess","input":{"host":"example.com"}}`), f.launch.Policy), &v) != nil || v.Behavior != "deny" || v.Message != provider.ErrPolicy.Error()+": "+refuseTool { |
| 389 | t.Fatalf("Linux network answer changed: %+v", v) |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | type linuxGolden struct{ action, role, digest string } |
| 394 | |
| 395 | func linuxGoldenCases() []linuxGolden { |
| 396 | return []linuxGolden{ |
| 397 | {"", "Engineering", "c6e60b449d74ee60e3c9aaf1eac5ca25ce11935352630317483243abebec73a9"}, |
| 398 | {"", "Copy", "5a0a805f511b3fe0279fa14559de79b485710c9b7edfe7b4f2b7ccf5a857509c"}, |
| 399 | {"", "Review", "0dadd06fc34ce8574b36610916c9afbdb2e9f89a33887c7338b8e7b0d6ac705f"}, |
| 400 | {"", "Reviewer", "75816fb0ac63443d22e1af355dd024d2ae4f32c10806625d648d460c7a4b1b65"}, |
| 401 | {"", "Answerer", "5fc13350c7f125299b9379f7fc56305e6d5c7ed4bdbfbae2d828261bb2346faa"}, |
| 402 | {"", "Coordinator", "b07b942edf4adc989ecb0e94bb6ef42739f99cf1b0872f69ab676ad1bab62cb7"}, |
| 403 | {"implement", "", "7bff2dd969a1a40de62495b1feb4b42c6140ee54f1fb2ecff53602a968963d48"}, |
| 404 | {"copy", "", "9c331b861ab54acc34395eebdfe49f97fd1a44088f15d832018c05d3e56f3f16"}, |
| 405 | {"design", "", "bb15be7302f9751339a4383d7c3769a4d444045759aa6fc32de9a93fc1508c90"}, |
| 406 | {"review", "", "25b61566499af8c549dade811ff947a9b2df86f6e278833b226842450eae11d6"}, |
| 407 | {"inspect", "", "d560cd170556bd85493ce037540d95222818c931d12ef9991f4064803cd42326"}, |
| 408 | {"answer", "", "1ca67f173bd6b222c69e024ef1064a0a1ca69f7901d82e97fa343ab6c617ca51"}, |
| 409 | {"scope", "", "b0f8f413cc6147912376d23fa298000ff2219d997dddb8cda9f1fd4c77dde6aa"}, |
| 410 | {"research", "", "709577e8d5973f8914a27f4b86aca35d5c204ec43c6da68abad63934f9945e9e"}, |
| 411 | {"reproduce", "", "abfc371fef557a74133b246295d9c1c46534934c2913f06576676c6367a125f7"}, |
| 412 | {"coordinate", "", "f75f6b326f242d5f2c4ed09f3d937afa35acd4a7edc5deee52e67e6d0933f120"}, |
| 413 | {"converse", "", "453f436806e4f24a18fb08f24f09d43afe33d8913ea055218854b176509408ec"}, |
| 414 | {"assist", "", "f04e6fe8fb11e7aa8bfe5d926fc68624590d667f60c8b569a5316a8e460d4743"}, |
| 415 | {provider.ResearchStepAction, "", "353b55fcff5dd3a4f6829a7aec71e1a94c2332d476066c3d61fe577e7c4ac8bb"}, |
| 416 | {provider.GlanceAction, "", "4c2a746684372478a8f262c6755bcb33055ff05d18a7a7d0baea2b0b33250a89"}, |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | // linuxSettingsDigest renders one Linux launch's inputs on a fixed binding |
| 421 | // and returns the digest of what the certified policy is made from. |
| 422 | func linuxSettingsDigest(t *testing.T, action, role string) string { |
| 423 | t.Helper() |
| 424 | b := provider.Binding{Provider: "claude", CredentialHome: "/private/account", Home: "/private/run", RuntimeRoot: "/private/runtime"} |
| 425 | in := provider.PolicyInputs{Action: action, DeniedPaths: []string{"/private/state", "/alias/login"}} |
| 426 | if action != "" { |
| 427 | p, err := provider.Profile(action) |
| 428 | if err != nil { |
| 429 | t.Fatal(err) |
| 430 | } |
| 431 | role, in.Tools = p.Specialist, p.MCPTools() |
| 432 | } |
| 433 | got, err := PolicyInputs(b, in, role, "claude-haiku-5-5") |
| 434 | if err != nil { |
| 435 | t.Fatalf("%s/%s: %v", action, role, err) |
| 436 | } |
| 437 | raw, _ := json.Marshal([]any{got.Settings, got.MCP, got.ThreadPolicy, got.TurnPolicy, got.Argv, got.Tools, got.DeniedPaths, got.Env}) |
| 438 | sum := sha256.Sum256(raw) |
| 439 | return hex.EncodeToString(sum[:]) |
| 440 | } |
| 441 | |
| 442 | func stepBehavior(t *testing.T, check func(json.RawMessage, provider.Policy) json.RawMessage, raw string, p provider.Policy) string { |
| 443 | t.Helper() |
| 444 | var v struct{ Behavior string } |
| 445 | if out := check(json.RawMessage(raw), p); json.Unmarshal(out, &v) != nil { |
| 446 | t.Fatalf("not JSON: %s", out) |
| 447 | } |
| 448 | return v.Behavior |
| 449 | } |
| 450 | |
| 451 | // withSandboxSettings returns p with its manifest's sandbox settings |
| 452 | // changed by mutate (nil removes the sandbox key). |
| 453 | func withSandboxSettings(t *testing.T, p provider.Policy, mutate func(map[string]any)) provider.Policy { |
| 454 | return withSettings(t, p, func(s map[string]any) { |
| 455 | if mutate == nil { |
| 456 | delete(s, "sandbox") |
| 457 | } else { |
| 458 | mutate(s["sandbox"].(map[string]any)) |
| 459 | } |
| 460 | }) |
| 461 | } |
| 462 | |
| 463 | // withSettings returns p with its manifest's settings changed by mutate. |
| 464 | func withSettings(t *testing.T, p provider.Policy, mutate func(map[string]any)) provider.Policy { |
| 465 | t.Helper() |
| 466 | var m map[string]json.RawMessage |
| 467 | var settings map[string]any |
| 468 | if json.Unmarshal(p.Manifest, &m) != nil || json.Unmarshal(m["Settings"], &settings) != nil { |
| 469 | t.Fatal("manifest settings unreadable") |
| 470 | } |
| 471 | mutate(settings) |
| 472 | m["Settings"], _ = json.Marshal(settings) |
| 473 | p.Manifest, _ = json.Marshal(m) |
| 474 | return p |
| 475 | } |
| 476 | |