Quarry 对话安全规则扫描工具
Quarry 是 Cisco 开源的 AI 对话安全检测工具,支持 Python 3.11 至 3.13。它通过规则文件对文本、正则、结构化字段和工具调用进行检查,并提供可选的模型评分与分类能力。
# 项目采石 Project Quarry 是用于检测代理对话中的威胁和行为模式的 Python 框架。您可以在本地检查不足时,从规则中调用配置的模型适配器 。 采石解析并验证加载时的规则,然后将每个规则编译为象征性的有限状态自动马通。扫描会将编译的程序运行到对话中。在加载时无法编译失败的规则。 Quarry为α,其DSL语法可能在v1.0.0之前以需要对现有规则进行更新的方式改变. ## 特征 - 文本、正则表达式( RE2) 结构化字段和工具使用检查 - 多回合流规则、捕获、计数和输入的政策参数 - 可选配置的模型评分、分类、完成、符号计数和跨度 - 证人证据,解释产出,并按辅助规则输入收藏品 - 同步和ASync Python API,加上人,JSON,和CSV CLI输出 - 明确输入、编译、时间和模式调用限制 ## 安装 Quarry通过3.13支持Python 3.11. **设置 :** ```bash git clone https://github.com/cisco-ai-defense/project-quarry.git cd project-quarry uv sync --frozen ``` 运行 `uv sync --frozen --extra inference` 如果您的应用程序使用内置的模型提供者。 ## 快速启动 从寄存器根扫描一个实例对话 : ```bash uv run --frozen quarry scan --config examples/config.yaml --target examples/data/benign.json ``` `examples/config.yaml` 选择14个没有示范证书运行的本地规则。 四个语义规则通过一个明确的配置选择来使用决定性关键词根;这些根用于示例和测试,而不是生产决定. 要看到匹配, 扫描 `examples/data/tool_calls.json` 配置。您也可以瞄准目标 `examples/data/` 以扫描每个对话文件; 成功扫描匹配的退出 `1`. 规则收集的全部实例由 `uv run --frozen python scripts/e2e_signature_test.py`它提供决定性模式和进行中的执行。 一个 YAML 配置文件会载入您自己的规则 : ```yaml config_version: 2 rules: - rules/ ``` 例如,将此规则保存为 `rules/credential-request.rule`: ```text rule "Credential request" strings: $terms = ["api key", "access token"] nocase condition: any msg in user_messages where msg.content contains_any $terms ``` 以 JSON 方式通过 OpenAI 形状的对话 `messages` 阵列。 CLI 退出 `0` 为了彻底的扫描, `1` 完全匹配,和 `2` 当扫描无效或不完整时。 ## 规则结构 A `.rule` 文件包含一条命名规则。在扫描器加载时,查询解析、验证、链接和编译规则。 [生成语言引用](<https://github.com/cisco-ai-defense/project-quarry/blob/main/quarry/language/reference.md>) 列表当前函数、字段、运算符和可运行示例。 ```text rule "Suspicious request" metadata: description = "Match requests to reveal a secret or override policy" strings: $blocked = ["reveal a secret", "ignore the policy"] nocase variables: has_blocked_text = any msg in user_messages where msg.content contains_any $blocked condition: has_blocked_text ``` 可选块按此顺序出现 : `metadata`, `strings`, `variables`, `parameters`。每个规则都需要 `condition:`。模型跨度规则也可以有: `collect:` 块后 `condition:`使用时 `//` 用于行注释; `#name` 是发生数运算符。 使用( U) `=` 中定义值 `metadata:`, `strings:`,以及 `variables:`使用时 `==` 以比较 `condition:` 表达式,包括内部的上游 `matches { ... }`. 其他比较仍然存在 `!=`, `<`, `<=`, `>`,以及 `>=`. ```text rule "Shell call" strings: $needle = "sudo" condition: any call in tool_calls where call.name == "Bash" and $needle in call.arguments.* ``` ## 检测规则包 那个... [核心软件包](<https://github.com/cisco-ai-defense/project-quarry/blob/main/rules/rule-packs/core>) 由采石队负责维护。 [社区包](<https://github.com/cisco-ai-defense/project-quarry/blob/main/rules/rule-packs/community>) 由捐款者维持 由项目维护者审查 。 每条规则都包含一个 README 描述 报告所涵盖的威胁及其限度。 [规则包概览](<https://github.com/cisco-ai-defense/project-quarry/blob/main/rules/README.md>). 采石场不鼓励、支持或建议使用或分享主动攻击 有效载荷、性特征或NSFW内容、儿童性虐待材料(CSAM), 或禁止的其他材料,甚至用于试验或研究。 每一份贡献,包括编码或嵌入的内容、附件和 链接用于共享被禁止的材料。 将被拒绝,捐款者可能被禁止参加该项目。 [规则贡献准则](<https://github.com/cisco-ai-defense/project-quarry/blob/main/rules/rules-contribution-guidelines.md>) 联 合 国 完全政策和允许的数据集参考。 ## Python API (英语). ```python from quarry import QuarryGuard guard = QuarryGuard("config.yaml") matches = guard.scan({"messages": [{"role": "user", "content": "Plan a picnic."}]}) for match in matches: print(match["rule"], match["metadata"]) ``` `QuarryGuard` 在不完整扫描上加价。 `DSLScanner` 返回 a `ScanResult` 这样您就可以检查完成状态、证据和输入输出 。 `compile_policy` 编写一条可重复使用的参数化规则。 ## 文档 - [开始](<https://github.com/cisco-ai-defense/project-quarry/blob/main/docs/getting-started.md>) - [采石作业方式](<https://github.com/cisco-ai-defense/project-quarry/blob/main/docs/how-quarry-works.md>) - [DSL 指南](<https://github.com/cisco-ai-defense/project-quarry/blob/main/docs/dsl.md>) 财务报告和财务报告 [生成语言引用](<https://github.com/cisco-ai-defense/project-quarry/blob/main/quarry/language/reference.md>) - [规则编写](<https://github.com/cisco-ai-defense/project-quarry/blob/main/docs/rule-writing.md>) 财务报告和财务报告 [字符串匹配](<https://github.com/cisco-ai-defense/project-quarry/blob/main/docs/string-matching.md>) - [配置](<https://github.com/cisco-ai-defense/project-quarry/blob/main/docs/configuration.md>) 财务报告和财务报告 [消息输入](<https://github.com/cisco-ai-defense/project-quarry/blob/main/docs/messages.md>) - [Python API (英语).](<https://github.com/cisco-ai-defense/project-quarry/blob/main/docs/python-api.md>) 财务报告和财务报告 [参数](<https://github.com/cisco-ai-defense/project-quarry/blob/main/docs/parameters.md>) - [已宣布投入](<https://github.com/cisco-ai-defense/project-quarry/blob/main/docs/inputs.md>) 用于对话消息旁的数据 - [模型推论](<https://github.com/cisco-ai-defense/project-quarry/blob/main/docs/model-inference.md>) 财务报告和财务报告 [应用程序集成](<https://github.com/cisco-ai-defense/project-quarry/blob/main/docs/integration.md>) - [可选的正式规则核查](<https://github.com/cisco-ai-defense/project-quarry/blob/main/docs/formal-verification.md>) - [代理插件](<https://github.com/cisco-ai-defense/project-quarry/blob/main/docs/PLUGIN.md>) 用于 Codex, Claude 代码和 MCP 主机 - [建筑](<https://github.com/cisco-ai-defense/project-quarry/blob/main/docs/architecture.md>) 财务报告和财务报告 [测试](<https://github.com/cisco-ai-defense/project-quarry/blob/main/docs/testing.md>) - [实例](<https://github.com/cisco-ai-defense/project-quarry/blob/main/examples/README.md>) 见 [CONTRIBUTING . md (英语).](<https://github.com/cisco-ai-defense/project-quarry/blob/main/CONTRIBUTING.md>) 报告安全问题。 [SECURITY . md (英语).](<https://github.com/cisco-ai-defense/project-quarry/blob/main/SECURITY.md>)将对话数据和全权证书排除在公共问题之外。 ## 许可证 分布于 `Apache 2.0` 驾照,看 [LICENSE](<https://github.com/cisco-ai-defense/project-quarry/blob/main/LICENSE>) 为了更多 资料。
查看原始来源