|
| 1 | +package policy |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/spec" |
| 8 | +) |
| 9 | + |
| 10 | +func shellSafeGraph() *spec.ProjectGraph { |
| 11 | + return &spec.ProjectGraph{ |
| 12 | + Tools: map[string]*spec.ToolResource{ |
| 13 | + "shell": { |
| 14 | + Metadata: spec.Metadata{Name: "shell"}, |
| 15 | + Spec: spec.ToolSpec{ |
| 16 | + Type: "native", |
| 17 | + Safety: &spec.ToolSafety{ |
| 18 | + SideEffects: spec.BoolPtr(true), |
| 19 | + }, |
| 20 | + }, |
| 21 | + }, |
| 22 | + }, |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func TestCheckToolCall_shellSafe_allowsLs(t *testing.T) { |
| 27 | + pol, err := spec.BuildPreset(spec.PresetShellSafe) |
| 28 | + if err != nil { |
| 29 | + t.Fatal(err) |
| 30 | + } |
| 31 | + ev := NewEvaluator(shellSafeGraph(), &pol) |
| 32 | + err = ev.CheckToolCall(context.Background(), ToolCallContext{ |
| 33 | + Uses: "tool.shell.command.run", |
| 34 | + With: map[string]any{"command": "ls -la"}, |
| 35 | + }) |
| 36 | + if err != nil { |
| 37 | + t.Fatalf("ls should be allowed: %v", err) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestCheckToolCall_shellSafe_gatesRm(t *testing.T) { |
| 42 | + pol, err := spec.BuildPreset(spec.PresetShellSafe) |
| 43 | + if err != nil { |
| 44 | + t.Fatal(err) |
| 45 | + } |
| 46 | + ev := NewEvaluator(shellSafeGraph(), &pol) |
| 47 | + err = ev.CheckToolCall(context.Background(), ToolCallContext{ |
| 48 | + Uses: "tool.shell.command.run", |
| 49 | + With: map[string]any{"command": "rm -rf /tmp/x"}, |
| 50 | + }) |
| 51 | + if err == nil { |
| 52 | + t.Fatal("expected rm to require approval") |
| 53 | + } |
| 54 | + d, ok := AsDenied(err) |
| 55 | + if !ok || d.Reason != ReasonApprovalRequired { |
| 56 | + t.Fatalf("got %v", err) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestCheckToolCall_shellSafe_unknownTokenGated(t *testing.T) { |
| 61 | + pol, err := spec.BuildPreset(spec.PresetShellSafe) |
| 62 | + if err != nil { |
| 63 | + t.Fatal(err) |
| 64 | + } |
| 65 | + ev := NewEvaluator(shellSafeGraph(), &pol) |
| 66 | + err = ev.CheckToolCall(context.Background(), ToolCallContext{ |
| 67 | + Uses: "tool.shell.command.run", |
| 68 | + With: map[string]any{"command": "totally-unknown"}, |
| 69 | + }) |
| 70 | + if err == nil { |
| 71 | + t.Fatal("expected unknown token to gate") |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestCheckToolCall_strict_gatesAllTools(t *testing.T) { |
| 76 | + g := testGraphWithTools("helper") |
| 77 | + g.Tools["helper"].Spec.Safety = &spec.ToolSafety{SideEffects: spec.BoolPtr(false)} |
| 78 | + pol, err := spec.BuildPreset(spec.PresetStrict) |
| 79 | + if err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + ev := NewEvaluator(g, &pol) |
| 83 | + err = ev.CheckToolCall(context.Background(), ToolCallContext{ |
| 84 | + Uses: "tool.helper.echo", |
| 85 | + }) |
| 86 | + if err == nil { |
| 87 | + t.Fatal("strict should gate all tools") |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestCheckToolCall_permissive_allowsMutatingTool(t *testing.T) { |
| 92 | + g := testGraphWithTools("slack") |
| 93 | + g.Tools["slack"].Spec.Safety = &spec.ToolSafety{SideEffects: spec.BoolPtr(true)} |
| 94 | + pol, err := spec.BuildPreset(spec.PresetPermissive) |
| 95 | + if err != nil { |
| 96 | + t.Fatal(err) |
| 97 | + } |
| 98 | + ev := NewEvaluator(g, &pol) |
| 99 | + err = ev.CheckToolCall(context.Background(), ToolCallContext{ |
| 100 | + Uses: "tool.slack.message.send", |
| 101 | + }) |
| 102 | + if err != nil { |
| 103 | + t.Fatalf("permissive should allow: %v", err) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestExpandPresetsInGraph_materializesDefault(t *testing.T) { |
| 108 | + g := &spec.ProjectGraph{ |
| 109 | + Spec: spec.ProjectSpec{ |
| 110 | + Defaults: &spec.ProjectDefaults{Policy: spec.PresetShellSafe}, |
| 111 | + }, |
| 112 | + } |
| 113 | + spec.ExpandPresetsInGraph(g) |
| 114 | + pr, ok := g.Policies[spec.PresetShellSafe] |
| 115 | + if !ok || pr == nil { |
| 116 | + t.Fatal("expected injected shell_safe policy") |
| 117 | + } |
| 118 | + if pr.Spec.ResolvedPreset != spec.PresetShellSafe { |
| 119 | + t.Fatalf("ResolvedPreset = %q", pr.Spec.ResolvedPreset) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func TestExpandPresetsInGraph_userPolicyOverridesBuiltin(t *testing.T) { |
| 124 | + g := &spec.ProjectGraph{ |
| 125 | + Spec: spec.ProjectSpec{ |
| 126 | + Defaults: &spec.ProjectDefaults{Policy: spec.PresetStrict}, |
| 127 | + }, |
| 128 | + Policies: map[string]*spec.PolicyResource{ |
| 129 | + spec.PresetStrict: { |
| 130 | + Metadata: spec.Metadata{Name: spec.PresetStrict}, |
| 131 | + Spec: spec.PolicySpec{ |
| 132 | + Execution: &spec.PolicyExecution{MaxWallClockSeconds: 99}, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + } |
| 137 | + spec.ExpandPresetsInGraph(g) |
| 138 | + if g.Policies[spec.PresetStrict].Spec.Execution.MaxWallClockSeconds != 99 { |
| 139 | + t.Fatal("user policy should not be replaced by builtin") |
| 140 | + } |
| 141 | +} |
0 commit comments