mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-20 15:11:01 +08:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
46751cfb9e | ||
|
|
4a29dbfd6d |
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
+28
-19
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user