diff --git a/guard/target_gate_test.go b/guard/target_gate_test.go index f22b5d9..d9b935e 100644 --- a/guard/target_gate_test.go +++ b/guard/target_gate_test.go @@ -530,7 +530,9 @@ func TestGateOffLeavesEveryBranchUnchanged(t *testing.T) { for name, ctx := range off { before := len(g.rows()) // 1. A tool outside the enabled set: passes, nothing is written. - for _, c := range []struct{ tool, input string }{{"Monitor", curl(deny)}, {"mcp__scan__run", `{"target":"203.0.113.7","note":"` + deny + `"}`}} { + // Monitor and ExecuteExtraTool are in the default set now, so use read-only + // tools and an MCP name (inner names are not in the set). + for _, c := range []struct{ tool, input string }{{"list_assets", curl(deny)}, {"mcp__scan__run", `{"target":"203.0.113.7","note":"` + deny + `"}`}} { if block, msg := pre(guard, ctx, c.tool, c.input); block { t.Fatalf("%s: %s blocked: %s", name, c.tool, msg) } diff --git a/intercept/default_tools_test.go b/intercept/default_tools_test.go new file mode 100644 index 0000000..4a8d8ef --- /dev/null +++ b/intercept/default_tools_test.go @@ -0,0 +1,48 @@ +package intercept + +import "testing" + +// TestDefaultEnabledToolsCoverExecutors pins the tools that must be gated when no +// intercept_enabled_tools setting has been saved. ExecuteExtraTool and Monitor +// dispatch MCP / deferred tools and background commands, so leaving them out let +// those calls bypass every rule. +func TestDefaultEnabledToolsCoverExecutors(t *testing.T) { + got := map[string]bool{} + for _, name := range defaultEnabledTools { + got[name] = true + } + for _, want := range []string{"Bash", "Write", "Edit", "MultiEdit", "ExecuteExtraTool", "Monitor"} { + if !got[want] { + t.Errorf("defaultEnabledTools must include %q", want) + } + } + for _, readOnly := range []string{"list_assets", "traffic_search", "Read"} { + if got[readOnly] { + t.Errorf("defaultEnabledTools must not gate the read-only tool %q by default", readOnly) + } + } +} + +// TestEnabledToolsFromSettingNeverDisablesTheGate: an unset or malformed setting +// must keep the defaults, not turn the rule system off. +func TestEnabledToolsFromSettingNeverDisablesTheGate(t *testing.T) { + for _, tc := range []struct { + name string + val string + ok bool + }{ + {"unset", "", false}, + {"malformed", "{not json", true}, + {"wrong shape", `"Bash"`, true}, + } { + got := enabledToolsFromSetting(tc.val, tc.ok) + if !got["Bash"] || !got["ExecuteExtraTool"] || !got["Monitor"] { + t.Errorf("%s: enabled tools %v, want the defaults", tc.name, got) + } + } + // A valid explicit list replaces the defaults. + got := enabledToolsFromSetting(`["Bash"]`, true) + if !got["Bash"] || got["WebFetch"] { + t.Fatalf("explicit list: %v, want only Bash", got) + } +} diff --git a/intercept/intercept.go b/intercept/intercept.go index a41a383..c9078e9 100644 --- a/intercept/intercept.go +++ b/intercept/intercept.go @@ -266,6 +266,10 @@ var defaultEnabledTools = []string{ "Bash", "WebFetch", "web_search", "shell_open", "shell_send", "Write", "Edit", "MultiEdit", + // ExecuteExtraTool dispatches MCP and deferred tools, and Monitor starts a + // background command; both execute third-party or shell commands, so they + // belong in the default gate with the rest. + "ExecuteExtraTool", "Monitor", } // New creates an Interceptor backed by d. The rule cache is lazy-loaded on @@ -305,27 +309,32 @@ func (i *Interceptor) loadLocked() error { // Load enabled-tools set from settings, falling back to hard-coded defaults. val, ok, _ := i.db.GetSetting("intercept_enabled_tools") - if !ok { - m := make(map[string]bool, len(defaultEnabledTools)) - for _, n := range defaultEnabledTools { - m[n] = true - } - i.enabledTools = m - } else { - var names []string - if json.Unmarshal([]byte(val), &names) != nil { - i.enabledTools = map[string]bool{} - } else { - m := make(map[string]bool, len(names)) - for _, n := range names { - m[n] = true - } - i.enabledTools = m - } - } + i.enabledTools = enabledToolsFromSetting(val, ok) return nil } +// enabledToolsFromSetting maps the stored intercept_enabled_tools value to the +// gate's set. A malformed value must not silently disable the gate, so it falls +// back to the same defaults as an unset setting. +func enabledToolsFromSetting(val string, ok bool) map[string]bool { + m := make(map[string]bool, len(defaultEnabledTools)) + for _, n := range defaultEnabledTools { + m[n] = true + } + if !ok { + return m + } + var names []string + if json.Unmarshal([]byte(val), &names) != nil { + return m + } + m = make(map[string]bool, len(names)) + for _, n := range names { + m[n] = true + } + return m +} + func (i *Interceptor) rules() ([]compiledRule, error) { i.mu.RLock() if i.cached != nil { @@ -381,7 +390,7 @@ func (i *Interceptor) GetEnabledTools() ([]string, error) { } var names []string if err := json.Unmarshal([]byte(val), &names); err != nil { - return []string{}, nil + return nil, fmt.Errorf("intercept_enabled_tools is not valid JSON: %w", err) } return names, nil }