feat: 完成 issue #48 DLP 敏感数据拦截(目标 100% 拦截)

This commit is contained in:
2026-08-04 17:15:02 +08:00
parent 4c3a9fdbe6
commit fa5523a37d
6 changed files with 838 additions and 0 deletions
+76
View File
@@ -0,0 +1,76 @@
# iAOP-Core · LLM 网关(LLM Gateway)
对应 PRD 5.4「④ LLM 网关 + RAG」与 EPIC #6(Issue #48 等子任务):
本地 70B(敏感/核心)+ 云端 API(脱敏/通用)**混合**,安全分级路由,
**敏感数据本地闭环,仅脱敏/公开内容可走云端 API**(数据不出厂)。
## 模块结构
```
core/llm-gateway/
├── __init__.py 包入口(导出 DLP 引擎 API)
├── dlp.py DLP 敏感数据拦截引擎(Issue #48)
├── config/
│ └── dlp.template.yaml 模板 DLP 规则资产(ti-cl4 示例,换行业只改它)
└── tests/
├── _bootstrap.py 测试引导(目录含连字符,挂载包名 llm_gateway)
└── test_dlp.py DLP 引擎单元测试
```
## DLP 敏感数据拦截(Issue #48)
出站防线:任何要发往**云端**的内容(用户 query / RAG 检索到的 context /
模型输出)先经 `DlpEngine.check_outbound()` 检查,**命中任一 block 规则
即整包拦截**(fail-closed),并落结构化审计日志(全量)。
```python
from llm_gateway.dlp import DlpEngine
engine = DlpEngine.from_template_config("config/dlp.template.yaml")
result = engine.check_outbound({
"query": user_query, # 用户提问
"context": rag_context, # RAG 命中文档片段
"output": llm_answer, # 模型生成结果
})
if result.blocked:
# 不发往云端:转本地处理 / 转人工确认 / 拒绝(PRD 5.4 异常时转人工)
engine.drain_audit() # 取走审计记录(对接外部审计管道)
```
### 语义(目标 DLP 拦截率 100%)
- **命中即拦截**:`decision="block"`、`reason="hit"`;
- **未配置即拦截**:引擎无任何规则(含内置保底)且 `fail_closed=True` 时
`reason="no_rules"`——未配置 = 不安全,保守拒绝出站;
- **审计不落明文敏感内容**:命中明细只记录规则名 / 类别 / 所属 part /
位置与 `<category>` 脱敏占位。
### 规则来源
1. **内置保底**(`DLP_DEFAULT_RULES`,内核自带):通用 PII 与凭证
(身份证、手机号、邮箱、IPv4、云访问密钥 AK、密钥键值对)+ 示例工艺词;
2. **模板资产**(`config/dlp.template.yaml`):行业敏感资产(工艺参数 /
配方 / 关键设备 / 人员岗位),同名规则覆盖内置实现模板定制。
规则两种匹配方式:`keyword`(大小写不敏感子串)与 `regex`(正则)。
### 验收对照(PRD 5.4 / 父 Issue #6)
| 验收项 | 实现 |
|--------|------|
| DLP 拦截率 100% | fail-closed:命中即 block;未配置规则也 block |
| 敏感数据本地闭环 | 出站(云端通道)必经 check_outbound 检查 |
| 敏感词/DLP 规则为配置点 | `config/dlp.template.yaml`,换行业只改资产 |
| 审计日志全量(NFR 安全-数据) | 每次检查落一条结构化审计,可 drain 对接管道 |
## 运行测试
```bash
cd core/llm-gateway
python -m unittest discover -s tests -v
```
## 后续子任务(EPIC #6 拆分,待扩展)
- 敏感度路由(准确率 ≥ 96.5%)与路由准确率评估脚本(Issue #49);
- Prompt 版本管理与幻觉/事实性校验中间件(Issue #47)。