|
| 1 | +package mcpsvc |
| 2 | + |
| 3 | +// The new rules-leverage payoff (docs/adr/0047 §5.4): a guardrail rule |
| 4 | +// authored through the ordinary Configure > Guardrail CRUD -- the exact |
| 5 | +// same guardrailsvc.GuardrailService.CreateRule a human uses from the |
| 6 | +// Review "Rules" audit view -- now governs a gated MCP write, because |
| 7 | +// gateWrite's evaluateWriteVerdict judges it through EvaluateAction, the |
| 8 | +// same rule-evaluation core the workflow execution gate and |
| 9 | +// RequestGuardedAction already share (guardrailservice_request.go). |
| 10 | + |
| 11 | +import ( |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/alicoding/mill/internal/domain/composition" |
| 15 | + "github.com/alicoding/mill/internal/domain/guardrail" |
| 16 | + "github.com/alicoding/mill/internal/services/compositionsvc" |
| 17 | + "github.com/alicoding/mill/internal/services/configuresvc" |
| 18 | + "github.com/alicoding/mill/internal/services/guardrailsvc" |
| 19 | + "github.com/alicoding/mill/internal/services/servicetest" |
| 20 | +) |
| 21 | + |
| 22 | +// newGuardrailWiredHarness builds a MillMCPService with a real, |
| 23 | +// store-backed GuardrailService wired via SetGuardrailService -- the |
| 24 | +// main.go wiring this test proves matters, unlike every other harness in |
| 25 | +// this package which leaves m.guard nil deliberately. |
| 26 | +func newGuardrailWiredHarness(t *testing.T) (*MillMCPService, *guardrailsvc.GuardrailService, *compositionsvc.CompositionService) { |
| 27 | + t.Helper() |
| 28 | + store := servicetest.NewFakeStore() |
| 29 | + comp := compositionsvc.NewCompositionService(store) |
| 30 | + cfg := configuresvc.NewConfigureService(store, comp, servicetest.FakeCredentialStore{}) |
| 31 | + guard := guardrailsvc.NewGuardrailService(store, comp) |
| 32 | + |
| 33 | + m := NewMillMCPService("0.0.0-test", comp, cfg, store, nil) |
| 34 | + m.SetGuardrailService(guard) |
| 35 | + if err := store.Set(MCPWriteEnabledKey, "true"); err != nil { |
| 36 | + t.Fatalf("set write key: %v", err) |
| 37 | + } |
| 38 | + // approval key left unset: required is the default -- a rule must be |
| 39 | + // what changes the outcome here, not a relaxed toggle. |
| 40 | + return m, guard, comp |
| 41 | +} |
| 42 | + |
| 43 | +// TestGateWrite_DenyRuleFromNormalCRUD_BlocksTheWriteWithNoPark proves a |
| 44 | +// guardrail rule created through CreateRule (not a bespoke MCP-only |
| 45 | +// concept) denies a gated write outright -- no park, no Activity/audit |
| 46 | +// ceremony a plain policy deny never gets, and nothing written. |
| 47 | +func TestGateWrite_DenyRuleFromNormalCRUD_BlocksTheWriteWithNoPark(t *testing.T) { |
| 48 | + m, guard, comp := newGuardrailWiredHarness(t) |
| 49 | + before := len(comp.Workflows()) |
| 50 | + |
| 51 | + if _, err := guard.CreateRule(guardrail.Rule{ |
| 52 | + Label: "Block all MCP writes", Effect: guardrail.EffectDeny, NodeTypeID: mcpWriteGuardrailKind, |
| 53 | + }); err != nil { |
| 54 | + t.Fatalf("CreateRule: %v", err) |
| 55 | + } |
| 56 | + |
| 57 | + res, err := m.gateWrite("import_workflow", "denied by rule", "{}") |
| 58 | + if err == nil { |
| 59 | + t.Fatalf("gateWrite() with a deny rule in place: want an error, got a result (res=%+v)", res) |
| 60 | + } |
| 61 | + if got := len(comp.Workflows()); got != before { |
| 62 | + t.Errorf("workflow count = %d, want %d -- a denied write must write nothing", got, before) |
| 63 | + } |
| 64 | + if pending := m.PendingMCPWrites(); len(pending) != 0 { |
| 65 | + t.Errorf("PendingMCPWrites() = %+v, want empty -- a deny-rule verdict must never park", pending) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// TestGateWrite_AllowRuleFromNormalCRUD_SkipsTheParkAndExecutes proves |
| 70 | +// the allow side of the same leverage: a matching allow rule executes |
| 71 | +// the write immediately even though MCPWriteApprovalKey defaults to |
| 72 | +// required, since the rule now short-circuits the ask-every-time |
| 73 | +// default that governed every MCP write before this rebase. |
| 74 | +func TestGateWrite_AllowRuleFromNormalCRUD_SkipsTheParkAndExecutes(t *testing.T) { |
| 75 | + m, guard, comp := newGuardrailWiredHarness(t) |
| 76 | + |
| 77 | + wf, err := comp.CreateWorkflow("Allow-rule source workflow", "", |
| 78 | + []composition.Node{{ID: "t", NodeTypeID: "trigger-manual"}, {ID: "c", NodeTypeID: "capture-clipboard-html"}}, |
| 79 | + []composition.Edge{{ID: "e1", Source: "t", Target: "c"}}) |
| 80 | + if err != nil { |
| 81 | + t.Fatalf("CreateWorkflow: %v", err) |
| 82 | + } |
| 83 | + exported, err := comp.ExportWorkflow(wf.ID) |
| 84 | + if err != nil { |
| 85 | + t.Fatalf("ExportWorkflow: %v", err) |
| 86 | + } |
| 87 | + before := len(comp.Workflows()) |
| 88 | + |
| 89 | + if _, err := guard.CreateRule(guardrail.Rule{ |
| 90 | + Label: "Allow all MCP writes", Effect: guardrail.EffectAllow, NodeTypeID: mcpWriteGuardrailKind, |
| 91 | + }); err != nil { |
| 92 | + t.Fatalf("CreateRule: %v", err) |
| 93 | + } |
| 94 | + |
| 95 | + argsJSON, err := marshalArgs(importToolArgs{JSON: stripJSONIDField(t, exported)}) |
| 96 | + if err != nil { |
| 97 | + t.Fatalf("marshalArgs: %v", err) |
| 98 | + } |
| 99 | + res, err := m.gateWrite("import_workflow", "allowed by rule", argsJSON) |
| 100 | + if err != nil { |
| 101 | + t.Fatalf("gateWrite() with an allow rule in place: %v", err) |
| 102 | + } |
| 103 | + if res == nil || res.IsError { |
| 104 | + t.Fatalf("gateWrite() result = %+v, want a successful (non-error) result", res) |
| 105 | + } |
| 106 | + if got := len(comp.Workflows()); got != before+1 { |
| 107 | + t.Errorf("workflow count = %d, want %d -- an allow-rule verdict must execute immediately, no park needed", got, before+1) |
| 108 | + } |
| 109 | + if pending := m.PendingMCPWrites(); len(pending) != 0 { |
| 110 | + t.Errorf("PendingMCPWrites() = %+v, want empty -- an allow-rule verdict must never park", pending) |
| 111 | + } |
| 112 | +} |
0 commit comments