Code-Enforced Tool Guards for AI Agent Permissions
A developer tutorial presents a reusable tool-call interceptor that checks an AI agent's tool name and arguments before execution, returning ALLOW, DENY, or ASK. The implementation covers command normalization, path-escape detection, outbound-domain allowlists, tainted-context approvals, audit logging, and regression tests, while recommending least-privilege credentials and sandboxes as additional layers.
The tutorial provides a concrete engineering pattern for controlling what AI agents can do without relying solely on prompts, making permissions deterministic and testable at the execution boundary.
本文是《Agent 工程实战》系列的一篇。所有代码可直接运行,不依赖任何付费 API。
昨天 AI 圈有个容易被忽略的细节:OpenAI 宣布暂停 GPT-6.1 Astra 的发布,同时全面收紧工具调用(tool call)监控,新版安全准则把「意图对齐」写进了核心框架。同一天,Anthropic 也更新了使用政策,禁止未经授权的转售与代理。
两条动作指向同一件事:当 Agent 真的能自己调工具、自己改文件、自己发请求,「它被允许做什么」这件事,已经从产品问题变成了工程问题。
我做过一段时间 AI 内容合规与风控。一开始我以为风险主要在「模型说错话」,后来发现真正贵的事故都在「模型做错事」:Agent 顺手把你的内网接口调了一遍、把本地目录 rm -rf 了、把测试环境的 token 带到了生产请求里。
这篇文章讲我怎么把这件事做成一个可复用的工具调用拦截器:不是靠提示词劝模型「不要乱来」,而是在执行前做一次确定性判定,把「不该发生的调用」直接拦掉。