| 1 | package claude |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | func TestHookEventsAndNativeAskPolicy(t *testing.T) { |
| 14 | f := fixtureFor(t, "Engineering") |
| 15 | var m manifest |
| 16 | if err := json.Unmarshal(f.launch.Policy.Manifest, &m); err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | if !slices.Contains(m.Argv, "--include-hook-events") { |
| 20 | t.Error("hook events not requested") |
| 21 | } |
| 22 | var settings struct{ Permissions struct{ Ask, Allow []string } } |
| 23 | if err := json.Unmarshal(m.Settings, &settings); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | for _, tool := range []string{"Read", "Glob", "Grep", "Write", "Edit"} { |
| 27 | rule := tool + "(//workspace/**)" |
| 28 | if !slices.Contains(settings.Permissions.Ask, rule) || slices.Contains(settings.Permissions.Allow, rule) { |
| 29 | t.Errorf("%s can bypass symlink callback", tool) |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestHookEventFlagBoundToPolicy(t *testing.T) { |
| 35 | f := fixtureFor(t, "Engineering") |
| 36 | in := f.inputs |
| 37 | in.Argv = slices.DeleteFunc(slices.Clone(in.Argv), func(arg string) bool { return arg == "--include-hook-events" }) |
| 38 | changed, err := provider.BuildPolicy(f.launch.Binding, in) |
| 39 | if err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | if changed.Hash == f.launch.Policy.Hash { |
| 43 | t.Fatal("hook flag absent from policy hash") |
| 44 | } |
| 45 | if _, err = f.l.validatePolicy(f.launch.Binding, changed); err == nil { |
| 46 | t.Fatal("missing hook-event flag accepted") |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // TestNoShellCommandIsAMarker: the person-approval controls were shell |
| 51 | // commands whose printed text a live-gate harness read as evidence |
| 52 | // (printf equipo-cert-allow, printf equipo-cert-deny). That harness went |
| 53 | // with the certification evidence (bbf490ed); nothing reads the text now, |
| 54 | // and a shell rule could never keep a sandboxed command from printing it. |
| 55 | // No Bash ask rule remains: no command's text means anything to Equipo. |
| 56 | func TestNoShellCommandIsAMarker(t *testing.T) { |
| 57 | f := fixtureFor(t, "Engineering") |
| 58 | var settings struct { |
| 59 | Permissions struct{ Ask []string } |
| 60 | Sandbox map[string]any |
| 61 | } |
| 62 | if err := json.Unmarshal(f.inputs.Settings, &settings); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | for _, ask := range settings.Permissions.Ask { |
| 66 | if strings.HasPrefix(ask, "Bash") { |
| 67 | t.Errorf("shell ask rule %q", ask) |
| 68 | } |
| 69 | } |
| 70 | if settings.Sandbox["autoAllowBashIfSandboxed"] != true || settings.Sandbox["allowUnsandboxedCommands"] != false { |
| 71 | t.Fatal("ordinary sandbox policy changed") |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestWorkspaceFileControlsStayAutomatic(t *testing.T) { |
| 76 | f := fixtureFor(t, "Engineering") |
| 77 | session, events := openFixture(t, f) |
| 78 | f.l.mu.Lock() |
| 79 | state, _ := f.l.state(session) |
| 80 | state.active = true |
| 81 | state.turn = provider.Turn{ID: "turn"} |
| 82 | f.l.mu.Unlock() |
| 83 | for tool, input := range map[string]string{ |
| 84 | "Read": `{"file_path":"/workspace/ordinary.txt"}`, |
| 85 | "Write": `{"file_path":"/workspace/ordinary.txt","content":"ordinary"}`, |
| 86 | "Edit": `{"file_path":"/workspace/ordinary.txt","old_string":"ordinary","new_string":"updated"}`, |
| 87 | "Glob": `{"path":"/workspace","pattern":"*.txt"}`, |
| 88 | "Grep": `{"path":"/workspace","pattern":"ordinary"}`, |
| 89 | } { |
| 90 | t.Run(tool, func(t *testing.T) { |
| 91 | ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 92 | defer cancel() |
| 93 | raw, _ := json.Marshal(permissionRequest{Tool: tool, Input: json.RawMessage(input)}) |
| 94 | reply, err := f.l.Permission(ctx, session, raw) |
| 95 | var answer struct{ Behavior string } |
| 96 | if err != nil || json.Unmarshal(reply, &answer) != nil || answer.Behavior != "allow" { |
| 97 | t.Fatalf("workspace action reached person: %s %v", reply, err) |
| 98 | } |
| 99 | select { |
| 100 | case ev := <-events: |
| 101 | t.Fatalf("workspace approval published: %s", ev.Payload) |
| 102 | default: |
| 103 | } |
| 104 | }) |
| 105 | } |
| 106 | } |
| 107 | |