Files
SupplierSystem/docs/design/20260915-audit-redact-api-keys.md
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

55 lines
3.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# TokenHub 密钥明文暴露:处置与审计脱敏修复(P0-4 关联)
日期:2026-09-15
状态:已实现(代码)+ 线上配置已处置(用户当轮指令)
关联:[risk-radar-real-sources](./20260914-risk-radar-real-sources.md)
## 暴露面(实测,只读排查)
同一条 TokenHub 密钥(`LLM_API_KEY`,形如 `sk-HJD8…`)在生产出现于 4 处:
| # | 位置 | 性质 | 处置 |
| --- | --- | --- | --- |
| 1 | `ubuntu` crontab:每 3 分钟一条 `curl … -H 'Authorization: Bearer sk-…'` 探活 TokenHub | 明文写在命令行 → **crontab 文件 + 进程 argv 双重可见**;且 480 次/天白耗额度 | **已删除该 crontab**(原文件备份为 `~/backups/crontab.ubuntu.backup-20260915-093451`,权限 600) |
| 2 | `/opt/ai-fresh-delivery/server/.env` | 应用正常配置(600 权限) | 保留(轮换后需更新) |
| 3 | `/opt/ai-fresh-delivery/server/.env.env.bak-vision-20260910` 等 4 个 `.env` 备份 | 其中 1 个是 `-rw-rw-r--`、3 个是 `-rw-r--r--` → **同机任何可登录账号都能读到密钥** | **已全部 `chmod 600`** |
| 4 | 生产库 `tenant_br002.operation_logs.new_value` 3 行(2026-09-09 18:28 / 18:32 保存 platform/branch-llm-config) | 审计中间件把请求体写库,`apiKeys` **未被脱敏** → 明文入库,且「系统 → 操作日志」对管理员可见 | **代码已修**(见下);历史 3 行未动(改审计日志属生产数据写,待授权) |
补充:本地开发机只在**本地 Postgres 数据文件**里含该串(源于上表第 4 项同步的数据);**git 跟踪文件干净**(`git grep` 无命中)。
## 代码根因与修复
`server/src/middleware/operationAuditMiddleware.ts`:
```ts
// 修复前
const SENSITIVE_KEY = /password|passwd|secret|token|authorization|private_key|access_key/i;
// ↑ 不匹配 apiKeys / api_keys / apiKey / llmApiKey → LLM 密钥被明文写进审计日志
// 修复后(新增 api_?keys? 与 llm_?key)
export const SENSITIVE_KEY =
/password|passwd|secret|token|authorization|private_key|access_key|api_?keys?|llm_?key/i;
```
同时把 `redactValue` 导出供单测使用(行为契约)。
## 验收
- `redactValue({ model, apiKeys: 'sk-…', baseUrl })` → `apiKeys` 变 `***`,其余字段保留;嵌套对象/数组、`Authorization` 头同样脱敏;不误伤 `sku_code`/`pack_factors`/`quantity` 等业务字段。
- 新增 `server/tests/auditRedactApiKeys.test.ts`(7/7 通过)。
- 线上配置侧:`crontab -l` → `no crontab for ubuntu`;cron 目录/进程列表均无该 key;5 个 `.env*` 全部 600(仅 `.env.example` 保持 644,其中无真实密钥)。
## 未做 / 需你决定
1. **轮换该密钥(必须做)**:它已经出现在 crontab/argv/世界可读的 .env 备份/审计日志里,属**已泄漏**,删除副本不能消除风险。
轮换后需要:更新 `/opt/ai-fresh-delivery/server/.env` 的 `LLM_API_KEY` → **重启 `ai-fresh-api`**(生产动作,等你明确下令)。
2. **历史 3 行审计日志**里的明文密钥:要不要脱敏(或随轮换失效即可)——改 `operation_logs` 属生产数据写,需你指令。
3. **H5 机 `119.45.34.106`**:现有 SSH 私钥被拒(`Permission denied (publickey)`),无法检查该机是否有同样的 crontab/.env 副本,请确认或提供访问方式。
4. 同类风险自查建议:`.env` 备份不要保留 world-readable 副本;探活类 cron 一律不要带密钥(需要就用 `curl -K` 读 600 权限的配置文件)。
## 回滚
- 代码:`git revert`(脱敏更严,回滚只会恢复"漏脱敏"的旧行为,不建议)。
- 线上 crontab:`crontab ~/backups/crontab.ubuntu.backup-20260915-093451`(注意:恢复即恢复明文密钥与 480 次/天探活,仅在你明确要恢复探活时才做)。
- 文件权限:`chmod 664/644` 还原(不建议)。