audit

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: MIT Imports: 13 Imported by: 0 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.

README

audit — Asynchronous, best-effort audit plugin

简体中文 | English

The audit plugin sends the complete context of every MCP request to one or more host-registered sinks: actor, method, tool, arguments, result, latency, headers, and plugin denial metadata. The plugin does not choose the storage backend; a sink may write logs, Kafka, a warehouse, or an external HTTP audit service.

Placement and delivery model

Install audit first so it is the outermost middleware plugin. On the return path it can therefore observe denials from inner plugins and the final result after an inner plugin such as spill rewrites it. It is not outside the runtime token gate: token admission is installed before all plugins.

Delivery is asynchronous and best-effort. The request path only attempts a non-blocking enqueue into a bounded queue; one background worker invokes sinks in order. Audit never blocks the MCP response and never changes its return value. There is intentionally no “wait until persisted” or “reject if audit failed” mode: the tool has already executed when audit runs, so fail-closed at this point cannot prevent side effects and would encourage unsafe retries.

The trade-off is explicit: queue overflow, kill -9, or disk/machine failure can lose events. Drops are counted, rate-limited in logs, and exposed through ReadStats().

Configuration

[audit]
headers          = ["X-Tenant", "User-Agent"]
queue_size       = 4096
flush_timeout    = "3s"
max_args_bytes   = 1024
max_result_bytes = 2048
Field Semantics
headers Optional request headers copied into events. The source is Call.Headers, not *http.Request; the runtime has already removed credential headers. A configured but absent header is recorded as "-". Empty names fail startup. Authorization, Proxy-Authorization, Cookie, and Set-Cookie are rejected at startup.
queue_size Bounded queue length. Default 4096; 0 selects the default; negative values fail startup. There is no unbounded-queue setting.
flush_timeout Positive Go duration used to drain the queue during shutdown. Default 3s. It adds directly to stop/restart time, so do not configure it excessively. Invalid or non-positive values fail startup.
max_args_bytes Argument snapshot limit in bytes. Default 1024; 0 selects the default. Negative values fail. Truncation cannot be disabled.
max_result_bytes Result snapshot limit in bytes. Default 2048; 0 selects the default. Negative values fail. Truncation cannot be disabled.

Configuration errors fail startup rather than silently activating defaults.

Sink API

audit.OnEvent(func(e audit.Event) error {
    // Write to the host's log, queue, warehouse, or audit service.
    return nil
})

Register at least one sink before traffic starts: registration may happen before or after Install, but must happen before Handlers() or Start(). With no sink, startup fails. If all sinks are later removed, events are counted as failed and logged. Multiple sinks run in registration order; an error or panic in one sink does not prevent the remaining sinks from running. Sink errors and panics do not affect the MCP result. Sink code runs on the worker goroutine, so implement its own timeout and retry policy for remote backends.

Event is an owned value snapshot: labels are deep-copied, arguments/results are bounded strings, and metadata is flattened into a new map. It can be retained or passed to another goroutine without retaining large request objects or introducing a data race.

Use the optional redaction hook before truncation:

audit.SetArgsRedactor(func(tool string, args []byte) []byte {
    // Return redacted JSON bytes.
    return args
})

The hook receives a plugin-owned copy, so in-place edits cannot mutate the real request arguments. A nil return means empty arguments. Do not put secrets in tool arguments unless a redactor removes them; the runtime does not understand field semantics.

Event fields and limits

Event contains At, LogID, Method, Tool, Labels, Subject, HasIdentity, Args, ArgsTruncated, Result, ResultTruncated, ResultErr, Cost, IsError, Err, DeniedBy, DenyReason, Headers, and Meta.

  • At is the time the call entered audit. LogID matches the HTTP response log ID and can join audit events with access logs. Method includes initialize, ping, notifications, tools/list, and tools/call; filter in the sink as needed. Tool is empty for non-call methods.
  • Labels and Subject are entry-time snapshots. ActorID() returns Subject.ID, or "(anonymous)" (AnonymousActor) when no trusted identity exists. HasIdentity distinguishes the two cases. The default runtime does not trust client identity headers, so Subject.ID is commonly empty.
  • Args is the original argument JSON after redaction and byte truncation. Truncation removes only an incomplete trailing UTF-8 rune; invalid bytes in the middle are preserved. Sinks targeting PostgreSQL text or strict JSON must normalize or escape invalid UTF-8 themselves.
  • For tools/call, Result is a human/search-oriented JSON fragment and may be incomplete after truncation; do not unmarshal it. For tools/list, it is a valid JSON array of tool names, truncated by item count. ResultErr records serialization errors instead of silently dropping the result.
  • IsError preserves the tool result error state. Err is the protocol-level error returned by the chain. DeniedBy and DenyReason are read from Call.Meta; they are empty when no plugin denied the call.
  • Headers contains only configured headers, with "-" for a missing value.
  • Meta is the generic pass-through of Call.Meta, flattened with fmt.Sprintf("%v"). It is capped at 32 keys and 256 bytes per value; keys are sorted before selecting the first 32 for deterministic cross-process snapshots. This is the only generic channel for other plugins to add audit context.

Coverage limits and security

Token admission is outside the plugin chain. A tools/call rejected by the runtime token gate never reaches audit; the HTTP status remains 200 and no audit event is emitted. Likewise, tools/list filtering happens after this plugin records the response, so Event.Result contains the pre-filter full registry, not necessarily what the token saw. The runtime's rate-limited warning logs ([mcp] warning: 准入拒绝 ... and tools/list 过滤 ...) are currently the only signals for those cases and must be collected for permission auditing.

The runtime strips Authorization, Proxy-Authorization, Cookie, and Set-Cookie before audit; the plugin also rejects configuring them. Nevertheless, arguments may contain application secrets such as passwords. Use SetArgsRedactor or redesign the tool contract before sending events to long-term storage.

Observability

s := audit.ReadStats() // Enqueued, Dropped, Delivered, Failed

ReadStats() returns zero values when the plugin is not installed or has stopped, rather than stale counters.

  • Enqueued: events successfully placed on the queue.
  • Dropped: queue-full or shutdown drops. Persistent growth means the queue or sink throughput is insufficient.
  • Delivered: events delivered successfully to every sink.
  • Failed: at least one sink returned an error or panicked, or no sink remained.

Drop and sink-failure logs are rate-limited per class: the first event is immediate, then at most one per minute, with suppressed and cumulative counts.

Startup logs use the [mcp] audit: prefix and report the effective sink count, queue size, flush timeout, argument/result limits, and headers. Shutdown logs report enqueued, delivered, failed, and dropped. The worker is single-threaded to preserve per-replica delivery order.

Shutdown

OnStop first closes the enqueue gate, then drains the queue within flush_timeout. Events remaining after the budget are counted as dropped. A stuck sink cannot hold shutdown indefinitely; the worker has an additional safety wait and logs if it does not exit in time.

Documentation

Overview

Package audit 是审计插件:把每次 MCP 调用的完整上下文交给使用方注册的落地函数。 基座不参与落地方式,日志、消息队列、数仓都由使用方决定。

安装位置:应最先安装,位于**插件链**最外层——既能记录被内层插件 拒绝的调用,也能看到内层插件(如 spill)改写后的最终结果。

但它不是整条链的最外层:**基座的 token 准入固定装在所有插件之前**,见第 6 条。

使用方必须知道的七条:

  1. **投递是异步的、尽力而为的,永远不阻塞调用返回。** 中间件在返回路径上只把事件 塞进有界队列,落地由后台单 worker 做(见 async.go)。刻意**没有**「阻塞直到落地」 或「没审计就不返回结果」这类档位:审计判定发生在返回路径上、工具此刻已经执行完, fail-closed 挡不住任何副作用,只是把结果藏起来、还得劝调用方不要重试;真正需要 fail-closed 的是执行**前**的门(人白名单、二次确认、配额)。 代价是「每次调用必有记录」降级为尽力而为:队列打满、进程被 kill -9、机器掉盘 都会丢事件。因此丢弃绝不静默——计数 + 限流告警 + ReadStats() 供宿主接监控。
  2. 落地函数要在 Install 之前注册:一个都没注册时启动直接失败。 「审计插件在场但一条也没落」是静默失效,而 required_plugins 只校验插件在场。 接流之后再把 Sink 清空同样会被计成失败并告警(worker 侧兜底)。
  3. 每个 MCP 方法都会产生事件,包括 initialize、ping 与各类 notifications。 远端 Sink 请按 Event.Method 自行过滤。落地慢不再影响调用方,但会挤占队列—— 过滤放在 Sink 里,队列容量按 queue_size 调。
  4. 事件里没有凭据。Call.Headers 已由基座剔除 Authorization/Proxy-Authorization/ Cookie/Set-Cookie,本插件也拒绝把这些头名配进 headers(见 normalize),且不去读 *http.Request 绕过这层剔除。Subject.Token 按基座契约是 token 的**用途名**。
  5. 工具入参不得承载密文。Event.Args 是入参原文,`login(user, password)` 这类工具 的密文会原样进长期审计存储;基座不认识字段语义、代替不了你判断。确有需要时用 SetArgsRedactor 注册脱敏钩子(在截断之前执行)。这条同样适用于其它插件。
  6. **基座 token 准入的拒绝不进审计流**(终审实测,务必知道):那一层固定装在插件链 之前,`tools/call` 被它拒时直接返回,本插件根本不会被调用——越权尝试在审计流里 是 0 条事件、HTTP 状态还是 200。同理 `tools/list` 的按 token 过滤发生在本插件 记录**之后**,所以 Event.Result 里是过滤**前**的全量清单,不等于该 token 实际 看到的那份。两者目前的唯一信号是基座打的 `[mcp] warning: 准入拒绝 …` / `tools/list 过滤 …` 日志(限流,见 runtime/tokenauthz.go)。做「谁看见/试过哪些 工具」这类权限审计时,必须把那两条日志一并采集,不能只看审计流。
  7. Event.Meta 是 Call.Meta 的通用透传(拍平成字符串、键数与值长都有上限)。 它是外置插件把自己的上下文送进审计流的唯一通道——本插件不认识任何具体插件, 所以键名的含义由写入方负责,落地方按需过滤。

Event 是**完全自有的值拷贝**(labels 深拷、入参/结果已截断成 string、Meta 拍平成新 map),交给 worker goroutine 既没有 data race 也不会拖住大对象引用——异步化不需要 额外拷贝,但**新增字段时必须保持这条性质**(不要往 Event 里塞指针或共享 map)。

Index

Constants

View Source
const AnonymousActor = "(anonymous)"

AnonymousActor 是 Subject.ID 为空时 ActorID 返回的占位标识。

基座默认不信任客户端送来的身份头,因此 Subject.ID 常态为空串。落地方若直接把空串 当用户名写进审计,「匿名调用」与「某个 id 为空的人」就再也分不开了,所以这里给一个 一眼能看出不是人名的占位值,并另配 Event.HasIdentity 供程序判定。

View Source
const Name = "audit"

Name 是插件名,与 required_plugins 里的写法一致。

Variables

This section is empty.

Functions

func Install

func Install(r *runtime.Registry) error

Install 安装审计插件。应最先安装,使其位于洋葱最外层。

func OnEvent

func OnEvent(fn Sink)

OnEvent 注册一个审计落地函数,可注册多个(按注册顺序依次调用)。 必须在 Install 之前(或同时)调用,理由见包注释第 1 条。

func SetArgsRedactor

func SetArgsRedactor(fn Redactor)

SetArgsRedactor 注册入参脱敏钩子(可选,传 nil 清除)。未注册时入参原样进事件。

Types

type Config

type Config struct {
	Audit Section `toml:"audit"`
}

Config 对应配置文件的 audit 段。

刻意用具名字段结构体、且字段集与文档一致(基座的配置认领制要求):用 map 兜底解码 会让段内拼错的键被判为「已解码」,于是 `[audit] on_eror = ...` 被完全吞掉、插件按 默认值上线;少声明一个文档承诺的字段,则部署方照文档写全反而启动失败。

type Event

type Event struct {
	// At 是调用进入 audit 的时刻。
	At time.Time
	// LogID 与 HTTP 层回写的同名响应头一致,用于和宿主 access log 对账。
	LogID string
	// Method 是 MCP 方法名,tools/call、tools/list 之外还包括 initialize、ping、
	// notifications/*,落地方可据此过滤。
	Method string
	Tool   string // 非 tools/call 时为空
	// Labels 是该工具的完整 labels(含基座投影的 name/pkg),进入时的快照。
	Labels map[string]string
	// Subject 是调用主体的**进入时快照**:audit 在最外层,返回路径上 Call.Subject
	// (指针)已可能被内层插件改过,直接读会记成被改写后的身份。
	Subject runtime.Subject
	// HasIdentity 区分「有真实身份」与「匿名/未配置身份」,见 AnonymousActor。
	HasIdentity bool
	// Args 是入参 JSON(先脱敏、再按上限截断),ArgsTruncated 标记是否截断。
	//
	// **可能含非法 UTF-8 字节**:它是调用方送来的原始 JSON 字节,审计不替它改写
	// (截断只剥掉尾部那个不完整字符,中段的坏字节原样保留)。落地前请按后端要求
	// 自行转义或替换——Postgres text、部分 JSON 序列化器会对非法 UTF-8 直接报错,
	// 那会让这条事件在你的 Sink 里落地失败(计入 ReadStats().Failed,不影响调用方)。
	Args          string
	ArgsTruncated bool
	// Result 是最终结果的摘要——**限于插件链之内**的最终结果。
	//
	// tools/list 是例外:基座的按 token 过滤在本插件记录之后才发生,所以这里是过滤
	// **前**的全量清单,不是该 token 实际看到的那份(见包注释第 6 条)。
	//
	// tools/call:JSON **片段**——截断后可能不完整(例如 `[{"type":"text",`),
	// 只供人读与全文检索,**不要 Unmarshal**。任意工具的结果形状不可控,
	// 想保证合法 JSON 就只能整份留下,那正是截断要避免的事。
	// tools/list:可见工具名列表,按**个数**裁剪,始终是合法 JSON。
	Result          string
	ResultTruncated bool
	// ResultErr 记录结果序列化失败的原因(不静默丢字段)。
	ResultErr string
	Cost      time.Duration
	// IsError 为真表示结果是错误态(业务级拒绝或工具报错)。
	IsError bool
	// Err 是链上抛出的协议级错误文案,空表示没有。
	Err string
	// DeniedBy / DenyReason 来自 Call.Meta,空表示未被任何插件拒绝。
	DeniedBy   string
	DenyReason string
	// Headers 是按配置采集的请求头,缺失的头值为 "-"。
	Headers map[string]string
	// Meta 是 Call.Meta 在链返回后的快照:任何插件写进 Meta 的内容都会到这里。
	//
	// 刻意做成通用透传而不是逐个插件开字段:audit 不认识别的插件,
	// 加特化字段等于把插件语义搬进 audit,外置插件就再也用不上这条路。
	//
	// 值一律拍平成字符串(按 fmt.Sprintf("%v"))——审计后端要能直接写库,
	// 任意嵌套结构会让落地方在序列化时才爆。单个值按 maxMetaValueBytes 截断;
	// 键数超过 maxMetaKeys 时按键名排序取前 N 个(排序保证同一次调用在不同进程里
	// 截出同一批键,否则对账时会各说各话)。
	Meta map[string]string
}

Event 是一次调用的审计事件。字段在进入链时快照,返回后补齐结果类字段。

func (Event) ActorID

func (e Event) ActorID() string

ActorID 返回可直接写进审计的执行人标识:无身份时返回 AnonymousActor 而不是空串。

type Redactor

type Redactor func(tool string, args []byte) []byte

Redactor 是入参脱敏钩子:入参进事件之前先过它一遍。

契约:返回改写后的切片,返回 nil 视为空入参。 传入的 args 是**本插件拷贝出来的副本**,钩子就地涂改也伤不到真实请求字节—— 这层防御不是可省的:Call.Args 与 MCP SDK 的 CallToolParamsRaw.Arguments 共享同一 底层数组,而工具入参的反序列化发生在链终点(钩子之后),一个写错的钩子否则会让 线上工具收到被涂改的参数。脱敏本就是少数部署才开的开关,这份拷贝不影响默认路径。

type Section

type Section struct {
	// Headers 是需要采集进事件的请求头名;不含凭据类头(配了即启动失败)。
	Headers []string `toml:"headers"`
	// QueueSize 是异步事件队列的容量(条),省略或填 0 取 4096;不能为负。
	//
	// 刻意**没有**「无界队列」的写法:落地方卡住时无界队列就是一条通往 OOM 的路,
	// 而丢事件至少是有计数、有告警、能被监控看见的。
	QueueSize int `toml:"queue_size"`
	// FlushTimeout 是进程退出时排空队列的时间预算(duration 串,如 "3s"),
	// 省略取 3s。它直接加在服务停止耗时上,别配太长。
	FlushTimeout string `toml:"flush_timeout"`
	// MaxArgsBytes 是入参进事件前的截断上限(字节),省略或填 0 取 1024;无法关闭。
	MaxArgsBytes int `toml:"max_args_bytes"`
	// MaxResultBytes 是结果进事件前的截断上限(字节),省略或填 0 取 2048;无法关闭。
	MaxResultBytes int `toml:"max_result_bytes"`
	// contains filtered or unexported fields
}

Section 是 audit 段的字段集,即本插件的对外配置契约。

type Sink

type Sink func(Event) error

Sink 是使用方注册的审计落地函数。

返回 error 而非无返回值:落地方是唯一知道「这条有没有写成功」的一方。 投递是异步的,返回 error 不会影响任何调用结果,但它会被计入 ReadStats().Failed 并触发限流告警——不返回错误,落地失败就只有落地方自己知道。

type Stats

type Stats struct {
	// Enqueued 是成功入队的事件数。
	Enqueued int64
	// Dropped 是被丢弃的事件数:入队时队列已满,或进程已进入停止流程。
	Dropped int64
	// Delivered 是已交给全部 Sink 且无一报错的事件数。
	Delivered int64
	// Failed 是至少有一个 Sink 报错或 panic 的事件数。
	Failed int64
}

Stats 是审计投递的运行期计数,供宿主接监控用。

有了它,「异步会丢事件」才是一件可观测的事而不是一句免责声明: Dropped 持续增长说明队列容量或落地速度不够,Failed 增长说明 Sink 本身有问题。

func ReadStats

func ReadStats() Stats

ReadStats 返回审计投递的运行期计数,供宿主接监控。未安装或已停止时返回零值。

名字带 Read 而不是直接叫 Stats:包里已经有 Stats 这个类型, 而计数类型比访问函数更值得占用这个名字(落地方要在自己的代码里声明它)。

为什么必须给出来:异步投递会丢事件(队列满、进程被强杀),不暴露计数的话 「尽力而为」就只是一句免责声明,运维看不见自己到底丢了多少。

Jump to

Keyboard shortcuts

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