Files
ARTEX/intercept/persistence.go
T
BaaRai 57d3bf67e7 fix(intercept): fail closed when the rule store cannot be read
A failed rule load left the enabled-tools set nil, so IsToolEnabled
answered false for every tool and the guard returned before any rule
ran; Match, MatchBuiltin and MatchPersistence answered "no match" on
the same error. A database outage therefore let every tool call run
unchecked.

Now, when the rules or the enabled-tools setting cannot be read,
IsToolEnabled answers true so the call reaches the rule path, and the
three matchers return a deny. The error is logged once per outage. A
failed read of the enabled-tools setting counts as a failed load
instead of silently selecting the defaults.

A deny or ask rule whose pattern does not compile is no longer skipped:
it matches every call as a deny until it is fixed or disabled. A broken
allow rule still allows nothing.
2026-11-21 07:34:29 +01:00

78 lines
2.3 KiB
Go

package intercept
import (
"encoding/json"
"strings"
)
// PersistenceTools are the platform tools that store something the agents load or
// run later: a custom tool (its code runs on the host in later calls), an MCP server
// (a stdio entry starts its command), a skill file (agents load it as instructions).
// A prompt-injected page can make an agent call them, so each call needs operator
// approval. The approval comes from the built-in ask rules seeded in db
// ("[built-in] Persistent change: ..."). Nothing an agent can call edits those rules;
// only the console can disable them.
var PersistenceTools = []string{
"create_custom_tool", "update_custom_tool",
"create_mcp", "update_mcp",
"create_skill", "update_skill_file",
}
// IsPersistenceTool reports whether name is one of PersistenceTools.
func IsPersistenceTool(name string) bool {
for _, t := range PersistenceTools {
if t == name {
return true
}
}
return false
}
// MatchPersistence decides a call of a persistence tool from the loaded rules. Any
// enabled deny rule that matches wins. Otherwise the first matching built-in ask rule
// answers, so the approval needs no other rule. User allow rules are not consulted:
// an allow rule cannot skip the approval.
func (i *Interceptor) MatchPersistence(toolName string, input []byte) (Decision, bool) {
if !IsPersistenceTool(toolName) {
return Decision{}, false
}
rules, err := i.rules()
if err != nil {
return storeUnreadableDecision(), true
}
var ask *compiledRule
for idx := range rules {
r := rules[idx]
if !ruleMatches(r, toolName, input) {
continue
}
if r.Action == "deny" {
return persistenceDecision(r), true
}
if ask == nil && r.Action == "ask" && strings.HasPrefix(r.Name, builtinRulePrefix) {
ask = &rules[idx]
}
}
if ask == nil {
return Decision{}, false
}
return persistenceDecision(*ask), true
}
func persistenceDecision(r compiledRule) Decision {
msg := r.Message
if msg == "" {
msg = defaultMessage(r.Action, r.Name)
}
configJSON, _ := json.Marshal(r.InterceptRule)
return Decision{
RuleName: r.Name, ConfigDigest: digestInput(configJSON),
Action: r.Action,
Message: msg,
RuleID: r.ID,
TimeoutEnabled: r.TimeoutEnabled,
TimeoutSeconds: r.TimeoutSeconds,
TimeoutAction: r.TimeoutAction,
}
}