Overview
Concrete tool for regex-based pre-execution tool-call blocking provided by the internal/toolguard package.
Description
The Manager holds an atomic pointer to an immutable Policy snapshot. NewManager compiles a Config (up to 100 rules) into a Policy and stores it atomically. Manager.Check loads the current snapshot and delegates to Policy.Check, which collects candidate texts from the tool name and the (recursively walked, percent-decoded up to 3 rounds) arguments, matches each against ordered enabled rules, and returns the first *Match with a templated message. A nil policy or disabled config returns nil (allow). The default config ships a government-domain protection rule.
Usage
- Deployment / HITL / Federation: the composition root calls
toolguard.NewManager(cfg.EffectiveToolGuard()) and the result is consulted before every tool execution.
- Live config update:
Manager.Update(config) atomically swaps the policy after validating the new rules.
- First engagement pre-check:
Manager.Check(toolName, args) is called before RBAC authorization to reject forbidden targets early.
Code Reference
Source Location
- Repository: CyberStrikeAI
- File: internal/toolguard/toolguard.go
- Lines: Config L28-39, Match L41-46, Policy.Check L118-145, Manager L147-185
Signature
type Config struct {
Enabled bool `json:"enabled" yaml:"enabled"`
Rules []Rule `json:"rules" yaml:"rules"`
}
type Rule struct {
ID, Name, Pattern, Message string
Enabled bool
}
type Match struct {
RuleID, RuleName, MatchedText, Message string
}
func DefaultConfig() Config
func Compile(config Config) (*Policy, error)
func (p *Policy) Check(toolName string, args map[string]interface{}) *Match
type Manager struct{ policy atomic.Pointer[Policy] }
func NewManager(config Config) (*Manager, error)
func (m *Manager) Update(config Config) error
func (m *Manager) Config() Config
func (m *Manager) Check(toolName string, args map[string]interface{}) *Match
Import
import "cyberstrike-ai/internal/toolguard"
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| config |
toolguard.Config |
Yes (NewManager) |
Enabled flag + up to 100 rules
|
| toolName |
string |
Yes (Check) |
Tool being called
|
| args |
map[string]interface{} |
Yes (Check) |
JSON-compatible args, walked to depth 10001
|
Outputs
| Name |
Type |
Description
|
| NewManager returns |
*Manager, error |
Manager, or compile error
|
| Check returns |
*Match |
Nil = allow; non-nil = block with RuleID/Message
|
| Match.Message |
string |
Templated with {match}/{tool}/{rule}
|
Usage Examples
Default Government-Domain Protection
mgr, err := toolguard.NewManager(toolguard.DefaultConfig())
if err != nil { log.Fatal(err) }
m := mgr.Check("nmap", map[string]interface{}{
"target": "https://www.example.gov.cn",
})
if m != nil {
// m.Message: "识别到 www.example.gov.cn,禁止攻击政府网站..."
return fmt.Errorf("blocked: %s", m.Message)
}
Live Policy Update
err := mgr.Update(toolguard.Config{
Enabled: true,
Rules: []toolguard.Rule{{
ID: "block-internal", Name: "Internal ranges",
Enabled: true, Pattern: `(?i)10\.\d+\.\d+\.\d+`,
Message: "{match} is internal; not authorized.",
}},
})
Related Pages
Implements Principle
Uses Heuristic
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.