a3bb78e06b
Security Quality Gate / npm audit, Bandit, pip-audit, detect-secrets (pull_request) Failing after 16m35s
2.0 KiB
2.0 KiB
AI 合同平台 V2 - 审计完整性功能闭环记录
日期:2026-06-26
背景
对照《AI 合同平台 V2 重构版开发方案》Phase 7 安全、性能与上线准备,本轮实现审计完整性功能,确保关键操作 100% 留痕。
已完成内容
Hash Chain 防篡改
新增字段:
event_hash: 当前事件 hash(32 字符 SHA256)previous_event_hash: 前一个事件 hash
新增函数:
compute_audit_event_hash(event, previous_hash): 计算审计事件 hashcompute_audit_event_hash_from_model(model, previous_hash): 从模型计算 hash
Hash 计算逻辑:
data = f"{id}|{previous_hash}|{event_type}|{action}|{actor_name}|{created_at}"
hash = sha256(data)[:32]
审计事件记录改进
record_audit_event 方法更新:
- 获取该合同的最后一个审计事件 hash
- 新事件包含 previous_event_hash
- 自动计算并设置 event_hash
- 支持租户过滤
审计完整性校验接口
新增 API:
GET /api/v1/contracts/{id}/audit-integrity: 校验审计完整性
返回结构:
{
"contract_id": "...",
"valid": true,
"errors": [],
"event_count": 5
}
校验逻辑:
- 检查 previous_event_hash 链接正确性
- 检查 event_hash 计算正确性
- 返回错误列表
新增测试
test_audit_integrity.py:
test_audit_hash_chain_computation: hash 计算测试test_audit_events_recorded_on_contract_create: 创建时记录审计事件test_audit_integrity_verification: 完整性校验测试
验证结果
python -m pytest yuqei-contract-platform/services/contract-api/tests -q # 31 passed
Phase 7 进度
| 功能 | 状态 |
|---|---|
| 租户隔离 | ✅ |
| 权限矩阵 | 🔄 |
| 审计完整性 | ✅(本轮) |
| 数据备份和恢复 | 🔄 |
| 错误监控 | 🔄 |
| 性能压测 | 🔄 |
| 安全扫描 | 🔄 |
| 部署文档 | 🔄 |
后续建议
下一步继续 Phase 7 的数据备份和恢复功能,实现每日备份、WAL 归档和恢复演练。