mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
Mechanical modernization of the traffic, intercept, guard and egress packages with the gopls/go fix modernizers: range over int, strings.SplitSeq, strings.Cut / CutPrefix, slices.Contains / Sort / Backward, maps.Copy, min / max, sync.WaitGroup.Go and t.Context in tests. No behaviour change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
73 lines
2.2 KiB
Go
73 lines
2.2 KiB
Go
package intercept
|
|
|
|
import (
|
|
"encoding/json"
|
|
"slices"
|
|
"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 {
|
|
return slices.Contains(PersistenceTools, name)
|
|
}
|
|
|
|
// 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,
|
|
}
|
|
}
|