tobs-code/policy-gate
GitHub: tobs-code/policy-gate
确定性允许列表策略防火墙,专为 LLM 应用与智能体提供可审计的准入与出口管控。
Stars: 1 | Forks: 0
# policy-gate
[](LICENSE)
[](https://www.rust-lang.org)
[](SAFETY_MANUAL.md)
**确定性策略防火墙,专为 LLM 而设计。**
允许列表优先,失败时拒绝,完全可审计。
专为智能体、工具使用流水线以及 SaaS 多租户部署而构建。
```
import { Firewall } from "policy-gate";
const fw = await Firewall.create();
const verdict = await fw.evaluateForTenant("tenant-a", "What is the capital of France?");
if (!verdict.isPass) throw new Error(`Blocked: ${verdict.blockReason}`);
```
**状态:** 实验性 — 请勿用于安全关键型生产环境。
## 为什么不是分类器?
大多数 AI 护栏是概率性的。它们估算风险——并且可能双向出错。
`policy-gate` 采取相反的方法:**只有明确允许的意图才能通过**。所有未知、模糊或存在分歧的内容都会被拒绝。裁决从不依赖 LLM 或概率分数。
## 核心特性
| | |
|---|---|
| **确定性允许列表强制** | 仅已知良好的意图形态通过;未知 = 阻止。 |
| **失败时拒绝投票器(1oo2)** | 两个独立通道必须一致。分歧或故障 → 阻止。 |
| **入口 + 出口防火墙** | 同时验证提示与模型响应(泄漏、PII、框架)。 |
| **多租户策略中心** | 每个租户隔离配置与审计日志。 |
| **影子模式** | 无阻塞评估——适合 rollout 与可观测性。 |
| **代理模式** | 任何 LLM API 前置的即插即用反向代理。零代码变更。 |
### 可选/高级特性
- **流式出口** `[experimental]` — 在 SSE 分块边界上进行 Aho-Corasick 扫描
- **快速语义 2.0** `[optional]` — 稀疏嵌入 + 学习质心语料库,约 31µs,仅作建议
- **会话感知监控** — 多轮升级检测(碎片化、探测、主题漂移)
- **上下文锚点验证(SA-080)** — 出口输出约束源自入口意图
## 工作原理
```
App ──► policy-gate ingress ──────────────────────────────► Upstream LLM
│ │
│ normalize │
│ Channel A: FSM + allowlist ──┐ │
│ Channel B: rule engine ─────┴─► voter ──► PASS │
│ (fault/disagree → BLOCK + audit)
│ │
App ◄── policy-gate egress ◄───────────────────────────────────-─┘
│ Output Channel 1: pattern/PII scan ──┐
│ Output Channel 2: framing / anchor ──┴─► voter ──► PASS / BLOCK
```
**入口通道(A + B)** — 独立技术,设计上彼此不同以防止共同原因故障。
**投票器** — 任何分歧、未知结果或内部故障 → 阻止。
**通道 C** `[advisory]` — 裁决后的启发式评分,**不改变结果**。
**通道 D** `[optional]` — 语义相似性,仅作建议。
## 快速开始
### Node / TypeScript
```
npm install
npm run build:native # builds Rust → native/index.node
npm run build # compiles TypeScript
npm run smoke # basic sanity check
npm run conformance # full corpus
```
### Python
```
python -m venv .venv && .venv\Scripts\activate
pip install maturin
python -m maturin develop --manifest-path crates/firewall-pyo3/Cargo.toml
python scripts/smoke.py
python scripts/conformance.py
```
### 代理(零代码集成)
```
export UPSTREAM_URL="https://api.openai.com/v1/chat/completions"
export UPSTREAM_API_KEY="sk-..."
cargo run --release -p firewall-proxy
# → point your app at http://localhost:8080/v1
```
### Rust 核心
```
cargo test -p firewall-core
cargo clippy -p firewall-core -- -D warnings
```
## 配置
将 [`firewall.example.toml`](./firewall.example.toml) 复制到 `firewall.toml` 并调整。
关键设置:
```
# Only these intents may pass
permitted_intents = ["QuestionFactual", "TaskCodeGeneration"]
# Block any ambiguous intent for high-sensitivity tenants
on_diagnostic_agreement = "fail_closed"
# Optional: explicit tool allowlist for agentic workflows
allowed_tools = ["weather_tool", "calculator_tool"]
# Shadow mode: evaluate but never block (for rollout)
shadow_mode = true
```
## 项目结构
```
policy-gate/
├── crates/
│ ├── firewall-core/ # Rust safety kernel
│ ├── firewall-proxy/ # Standalone reverse proxy (axum)
│ ├── firewall-cli/ # Policy governance CLI
│ ├── firewall-napi/ # Node.js binding (napi-rs)
│ ├── firewall-pyo3/ # Python binding (PyO3 / maturin)
│ ├── firewall-wasm/ # WASM / edge target
│ └── firewall-proxy-wasm/ # Proxy-Wasm / Envoy target
├── docs/ # Extended documentation (see below)
├── policy-hub/ # Pre-built TOML profiles and presets
├── verification/ # Z3 models, corpora, benchmarks, operator tooling
└── firewall.example.toml
```
## 这不是什么
- 不是通用型内容分类器
- 不是越狱检测或提示毒性评分器
- 不是人类策略设计与威胁建模的替代品
- 不是认证级安全系统或 IEC 61508 实现
## 设计灵感
`policy-gate` 借鉴了功能安全工程思想——失败时拒绝、通道多样性、显式故障处理。它**不是** IEC 61508 实现,未经过第三方评估,也不作任何合规声明。详见 [SAFETY_MANUAL.md](./SAFETY_MANUAL.md)。
## 许可证
Apache 2.0 — 参见 [LICENSE](./LICENSE)。
## 进一步阅读
| | |
|---|---|
| [SAFETY_MANUAL.md](./SAFETY_MANUAL.md) | 完整设计、危害分析、通道规范、安全需求 |
| [docs/proxy.md](./docs/proxy.md) | 反向代理、Prometheus 指标、热重载、CLI、Docker、Helm |
| [docs/multi-tenant.md](./docs/multi-tenant.md) | 租户注册、配置文件、投票器严格度 |
| [docs/agents.md](./docs/agents.md) | 工具模式验证、LangGraph 集成 |
| [docs/performance.md](./docs/performance.md) | 基准测试、并行批处理、BERT 语义模式 |
| [docs/verification.md](./docs/verification.md) | Z3 证明、回归数据集、操作员审查工具链 |
标签:AI网关, AI防火墙, fail-closed, JSONLines, MITM代理, Rust实现, SaaS防护, 代理模式, 会话监控, 准入白名单, 可审计策略, 可视化界面, 实验性安全, 影子模式, 流式输出, 确定性策略网关, 策略即服务, 语义嵌入, 输入输出过滤, 逆向工具, 通知系统, 零信任