Documentation
¶
Overview ¶
Package policy is the per-tool policy layer (issue #314, audit S14, OWASP MCP02 / MCP07): which upstream tools a client may call, which ones need a human's confirmation first, and which ones are refused.
A policy is the `policy:` config block:
policy:
default: allow # allow | deny
unknown_tools: deny # deny | allow
rules: # first match wins; globs on "server.tool"
- match: "postgres.pg_execute"
action: confirm # allow | deny | confirm
- match: "github.delete_*"
action: deny
- match: "*"
annotations: { destructiveHint: true }
action: confirm
A call is decided in this order, and the first step that decides wins:
- unknown_tools: a tool the server does not list in its tools/list is refused (deny, the default) or evaluated like any other tool (allow), without annotations;
- rules, top to bottom: the first rule whose match glob AND annotations (all of them) match decides;
- default: allow (the default) or deny.
There is no "most specific rule" logic: order the rules from the most specific to the most general.
Config holds the YAML, Compile turns it into an Engine whose globs are precompiled; Engine.Evaluate is safe for concurrent use and does not allocate.
Index ¶
- Constants
- func RuleLabel(i int, match string) string
- type Action
- type Config
- type Decision
- type Engine
- func (e *Engine) ConfirmTimeout() time.Duration
- func (e *Engine) Default() Action
- func (e *Engine) Describe(i int) string
- func (e *Engine) Evaluate(server, tool string, h Hints) Decision
- func (e *Engine) EvaluateID(id string, h Hints) Decision
- func (e *Engine) Explain(server, tool string, h Hints, listed bool) Explanation
- func (e *Engine) MayDeny() bool
- func (e *Engine) Rules() []Rule
- func (e *Engine) Summary() string
- func (e *Engine) Trivial() bool
- func (e *Engine) UnknownDecision() Decision
- func (e *Engine) UnknownTools() Action
- type Explanation
- type Hints
- type Injection
- type InjectionOverride
- type Rule
- type Step
Constants ¶
const ( HintReadOnly = "readOnlyHint" HintDestructive = "destructiveHint" HintIdempotent = "idempotentHint" HintOpenWorld = "openWorldHint" )
Annotation hint names accepted in Rule.Annotations.
const ( // RuleDefault marks a decision taken by policy.default. RuleDefault = -1 // RuleUnknownTools marks a decision taken by policy.unknown_tools. RuleUnknownTools = -2 )
Rule indexes of the decisions that do not come from policy.rules.
const DefaultConfirmTimeout = 5 * time.Minute
DefaultConfirmTimeout bounds how long a confirmation waits for the user.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Action ¶
type Action string
Action is what a policy decides for a tool call.
const ( // ActionAllow lets the call through. ActionAllow Action = "allow" // ActionDeny refuses the call; the upstream is never called. ActionDeny Action = "deny" // ActionConfirm asks the user (MCP elicitation) before the call; a // client that cannot be asked gets the call refused. ActionConfirm Action = "confirm" )
type Config ¶
type Config struct {
// Default is the action when no rule matches: allow (default) or deny.
Default Action `yaml:"default,omitempty"`
// UnknownTools is what happens to a call to a tool that is not in the
// server's current tools/list: deny (default) or allow.
UnknownTools Action `yaml:"unknown_tools,omitempty"`
// Rules are evaluated top to bottom; the first match wins.
Rules []Rule `yaml:"rules,omitempty"`
// ConfirmTimeout bounds how long a confirm rule waits for the user's
// answer (Go duration, default 5m). No answer in time refuses the call.
ConfirmTimeout string `yaml:"confirm_timeout,omitempty"`
}
Config is the `policy:` block. A nil *Config means the defaults: every advertised tool is allowed, tools a server does not advertise are refused.
func (*Config) EffectiveConfirmTimeout ¶
EffectiveConfirmTimeout returns confirm_timeout, or its default. An invalid value (rejected by Validate) also yields the default.
func (*Config) EffectiveDefault ¶
EffectiveDefault returns default, or allow when unset.
func (*Config) EffectiveUnknownTools ¶
EffectiveUnknownTools returns unknown_tools, or deny when unset.
type Decision ¶
type Decision struct {
Action Action
// Rule is the index in policy.rules of the deciding rule, or
// RuleDefault / RuleUnknownTools.
Rule int
// Label names what decided, for errors and logs: `rules[1] (match
// "github.delete_*")`, "policy.default" or "policy.unknown_tools".
Label string
// Injection is the deciding rule's injection override (nil: none).
Injection *Injection
}
Decision is the outcome of evaluating one tool call.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine is a compiled policy. It is immutable and safe for concurrent use.
func Compile ¶
Compile validates cfg and precompiles its globs and injection overrides. A nil cfg yields the default policy.
func MustCompile ¶
MustCompile is Compile for configs known to be valid (tests).
func (*Engine) ConfirmTimeout ¶
ConfirmTimeout returns how long a confirmation waits for the user.
func (*Engine) Evaluate ¶
Evaluate decides a call to tool on server (an advertised tool, or an unknown one under unknown_tools: allow, with zero Hints).
func (*Engine) EvaluateID ¶
EvaluateID is Evaluate for an already joined "server.tool" identity. It does not allocate.
func (*Engine) Explain ¶
func (e *Engine) Explain(server, tool string, h Hints, listed bool) Explanation
Explain evaluates a call like the middleware does, recording why: listed is whether the server advertises the tool.
func (*Engine) MayDeny ¶
MayDeny reports whether some advertised tool can be denied (a deny rule or default deny): discovery only needs filtering then.
func (*Engine) Summary ¶
Summary is the one-line description of the policy for startup logs and `doctor security`.
func (*Engine) Trivial ¶
Trivial reports whether every advertised tool is allowed (no rules, default allow).
func (*Engine) UnknownDecision ¶
UnknownDecision is the decision for a tool the server does not list, when unknown_tools is deny.
func (*Engine) UnknownTools ¶
UnknownTools returns policy.unknown_tools.
type Explanation ¶
type Explanation struct {
Decision Decision
// Listed is whether the tool is in the server's tools/list (false
// also when that is not known).
Listed bool
Steps []Step
}
Explanation is the decision for a call and how every rule compared with it, top to bottom, up to the deciding one.
type Hints ¶
type Hints struct {
ReadOnly, Destructive, Idempotent, OpenWorld *bool
}
Hints are the behavior annotations a tool declares (nil: not declared).
type Injection ¶
type Injection struct {
Requests *injection.Dispatcher
Responses *injection.Dispatcher
}
Injection is a rule's compiled injection override: a nil dispatcher keeps the guard's global one.
type InjectionOverride ¶
type InjectionOverride struct {
// RequestPolicies replace injection.request_policies for the call's
// arguments (actions: block, quarantine, redact, log).
RequestPolicies []injection.Rule `yaml:"request_policies,omitempty"`
// ResponsePolicies replace injection.response_policies for the tool's
// result (actions: annotate, redact, block, log).
ResponsePolicies []injection.Rule `yaml:"response_policies,omitempty"`
}
InjectionOverride replaces the injection guard's risk bands for the tools of one rule. A list that is not set keeps the global one.
type Rule ¶
type Rule struct {
// Match is a glob on "server.tool": `*` matches any run of characters
// (dots included), `?` exactly one character; everything else is
// literal and case-sensitive.
Match string `yaml:"match"`
// Annotations, when set, also require the tool to declare each of
// these hints (readOnlyHint, destructiveHint, idempotentHint,
// openWorldHint) with exactly this value. A hint the tool does not
// declare never matches.
Annotations map[string]bool `yaml:"annotations,omitempty"`
// Action is allow, deny or confirm.
Action Action `yaml:"action"`
// Injection overrides the prompt-injection guard's policies for the
// calls this rule matches (and lets through).
Injection *InjectionOverride `yaml:"injection,omitempty"`
}
Rule is one entry of policy.rules.