policy

package
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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:

  1. 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;
  2. rules, top to bottom: the first rule whose match glob AND annotations (all of them) match decides;
  3. 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

View Source
const (
	HintReadOnly    = "readOnlyHint"
	HintDestructive = "destructiveHint"
	HintIdempotent  = "idempotentHint"
	HintOpenWorld   = "openWorldHint"
)

Annotation hint names accepted in Rule.Annotations.

View Source
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.

View Source
const DefaultConfirmTimeout = 5 * time.Minute

DefaultConfirmTimeout bounds how long a confirmation waits for the user.

Variables

This section is empty.

Functions

func RuleLabel

func RuleLabel(i int, match string) string

RuleLabel is the label of policy.rules[i].

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

func (c *Config) EffectiveConfirmTimeout() time.Duration

EffectiveConfirmTimeout returns confirm_timeout, or its default. An invalid value (rejected by Validate) also yields the default.

func (*Config) EffectiveDefault

func (c *Config) EffectiveDefault() Action

EffectiveDefault returns default, or allow when unset.

func (*Config) EffectiveUnknownTools

func (c *Config) EffectiveUnknownTools() Action

EffectiveUnknownTools returns unknown_tools, or deny when unset.

func (*Config) Validate

func (c *Config) Validate() error

Validate checks every action, glob, annotation name and injection override. A nil receiver is valid.

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

func Compile(cfg *Config) (*Engine, error)

Compile validates cfg and precompiles its globs and injection overrides. A nil cfg yields the default policy.

func MustCompile

func MustCompile(cfg *Config) *Engine

MustCompile is Compile for configs known to be valid (tests).

func (*Engine) ConfirmTimeout

func (e *Engine) ConfirmTimeout() time.Duration

ConfirmTimeout returns how long a confirmation waits for the user.

func (*Engine) Default

func (e *Engine) Default() Action

Default returns policy.default.

func (*Engine) Describe

func (e *Engine) Describe(i int) string

Describe renders rule i as it is configured (for `doctor security`).

func (*Engine) Evaluate

func (e *Engine) Evaluate(server, tool string, h Hints) Decision

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

func (e *Engine) EvaluateID(id string, h Hints) Decision

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

func (e *Engine) MayDeny() bool

MayDeny reports whether some advertised tool can be denied (a deny rule or default deny): discovery only needs filtering then.

func (*Engine) Rules

func (e *Engine) Rules() []Rule

Rules returns a copy of the configured rules.

func (*Engine) Summary

func (e *Engine) Summary() string

Summary is the one-line description of the policy for startup logs and `doctor security`.

func (*Engine) Trivial

func (e *Engine) Trivial() bool

Trivial reports whether every advertised tool is allowed (no rules, default allow).

func (*Engine) UnknownDecision

func (e *Engine) UnknownDecision() Decision

UnknownDecision is the decision for a tool the server does not list, when unknown_tools is deny.

func (*Engine) UnknownTools

func (e *Engine) UnknownTools() Action

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.

type Step

type Step struct {
	Label string
	// Glob and Hints report whether the match glob and the annotations
	// matched (Hints is true when the rule requires none).
	Glob, Hints bool
	Action      Action
}

Step is one line of an explanation: how one rule (or the default) compared with the call.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL