ARTEX/server/customtool_rules_test.go
BaaRai a0a448ba12 fix(server): guard and audit the custom-tool test endpoint (GHSA-m27c)
The panel's test endpoint dispatched the rendered command, script or URL
without evaluating the intercept rules or writing the audit, so a call a
worker would be blocked from running could be run from the panel. Evaluate
the built-in deny rules and the rule set on the rendered text before the
dispatch, refuse on deny/ask, and record the decision as the agent path
does. The admin-only route and its sandbox behaviour are unchanged.
2026-11-30 23:24:19 +01:00

55 lines
2.2 KiB
Go

package server
import (
"fmt"
"strings"
"testing"
"time"
)
// GHSA-m27c: the panel's test endpoint dispatches outside the agent hook. A rule
// that stops a worker's command, script or request must stop the test too, and
// the decision must be written to the intercept audit.
func TestCustomToolTestEndpointAppliesRules(t *testing.T) {
g := newGateServer(t)
marker := fmt.Sprintf("panel-test-marker-%d", time.Now().UnixNano())
rule, err := g.m.pg.CreateInterceptRule("test-endpoint deny", "tool_input", "string", marker, "deny",
"the test endpoint is forbidden", 50, true, true, 60, "deny")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = g.m.pg.DeleteInterceptRule(rule.ID) })
g.m.interceptor.Invalidate()
calls := []struct{ name, kind, exec string }{
{"command", "command", `{"command":"echo ` + marker + `"}`},
{"script", "script", `{"code":"print('` + marker + `')"}`},
{"http", "http", `{"url":"http://127.0.0.1:1/` + marker + `"}`},
}
for _, c := range calls {
code, out := g.do(t, "POST", "/api/tools/custom/test", fmt.Sprintf(`{"kind":%q,"exec":%s}`, c.kind, c.exec))
if code != 200 || out["is_error"] != true || !strings.Contains(fmt.Sprint(out["output"]), "the test endpoint is forbidden") {
t.Errorf("%s: %d %v, want the rule's refusal", c.name, code, out)
}
}
// The deny is an audit entry, keyed to the rendered command the endpoint
// refused.
var status, source, reason string
err = g.m.pg.QueryRow(`
SELECT status, decision_source, reason FROM intercept_pending
WHERE tool_name='test' AND tool_input->>'command' = $1`, "echo "+marker).Scan(&status, &source, &reason)
if err != nil {
t.Fatalf("the denied test call left no audit row: %v", err)
}
if status != "denied" || source != "rule" || reason != "the test endpoint is forbidden" {
t.Errorf("audit row = %q/%q/%q, want denied/rule/the rule message", status, source, reason)
}
// A call no rule matches still runs: the endpoint is not blocked wholesale.
code, out := g.do(t, "POST", "/api/tools/custom/test",
`{"kind":"command","exec":{"command":"echo allowed-marker"}}`)
if code != 200 || out["is_error"] != false || !strings.Contains(fmt.Sprint(out["output"]), "allowed-marker") {
t.Fatalf("allowed call: %d %v", code, out)
}
}