lzf_0626
e4cd336afa
修复权限拒绝变 500、访客令牌无限流,并让两处测试跟上代码
## 1. 权限不足从 500 修回 403(14 个测试失败里的 11 个)
`app/service/authorization_service.py` 的 `_deny` 构造 `InteractionAudit(...)` 时
**漏了 `created_at`**(该列 NOT NULL 且无默认值),而 `raise ForbiddenAgentError`
写在 `session.commit()` **之后** —— commit 必抛 IntegrityError
(1048: Column 'created_at' cannot be null),于是**永远走不到 raise**:
预期 403,实际 500:Internal Server Error
**⇒ 任何「权限不足」的请求都变成 500**,破坏 docs/05 §3.6 的错误码契约。
全仓 100+ 处 `InteractionAudit(...)` 都跟着 `created_at=now`,只有这一处没有
(由 2026-09-14 的 `857c106`「投顾工作台:三接口支持按客户出方案」引入)。
修两处:
- 补 `created_at`;
- **并把审计写入失败与 403 解耦**(try/except + `logger.warning(exc_info=True)`):
安全判定不该依赖审计表是否可写 —— 拒绝就是拒绝。但也绝不静默,审计缺失是合规问题。
## 2. 访客令牌端点补限流(P0-4)
`app/api/controllers/visitor_tokens.py` 此前**零认证、零限流**,可以不限量铸造
有效 JWT;每个都能调 `/api/v1/agent-runs` 触发 LLM 调用,而 agent-runs 的限流
按 `user_id` 计、访客 `sub` 每次都是新随机值 ⇒ **限流被天然绕过**。
把 `rate_limit.py` 的登录闸门抽成通用的 `_enforce_ip_rate_limit(...)`,
新增 `enforce_visitor_token_rate_limit` 挂到该端点的 router 上。
⚠️ 刻意**用独立计数器前缀**(`visitor-token` vs `login`)而不是直接复用登录闸门:
共用会让两者互相挤占配额 —— 正常访客刷几次页面就把别人挡在登录外。
阈值 30 次/分钟(比登录的 10 次宽松,因为访客进站/刷新会正常签发)。
## 3. 测试跟上代码(2 个)
- `test_product_recommendation_service.py`:monkeypatch 打在 `current_for_agent` 上,
而 `generate()` 现在走 `current_for_customer(customer_id, context)` —— 补丁不生效,
真实方法被执行并命中鉴权抛 403。改为按新签名打补丁。
- `test_authorization_service.py` 等 3 个:随第 1 项一起恢复。
## 实测
- `pytest tests/unit tests/contract` -> **1427 passed, 2 skipped, 2 failed**
(修复前 **14 failed** / 1426 passed)
- `_deny`:权限不足恢复 **403**(原为 500)
- 访客限流:连发 40 次 -> `{201: 30, 429: 10}`,首次被拒返回
`RATE_LIMITED「访客令牌签发过于频繁:每 60 秒最多 30 次」`
(修复前:连发 15 次全部 201、无限流)
- `ruff check` -> All checks passed
## 剩余 2 个失败(需产品决策,未擅自处理)
1. `test_advisor_workspace_registers_documented_operation_endpoints` ——
`employee-advisor/dashboard/index.html` 已被**整个替换**为一个自包含静态页
(`data-page-node-id` 属性、内联全部 CSS/JS、硬编码 `API="http://127.0.0.1:8000"`、
自带「离线本地引擎」),**不引用 `api-client.js` / `app-shell.js`、不调 `mountShell`**。
测试断言的是旧页面措辞("组合分析"),新页面写的是"生成推荐方案"。
是接受替换后的页面(改测试断言),还是恢复挂平台壳的版本,属产品决策。
2. `test_docs_endpoint_ids` —— `docs/05` §19 表里被插入了分组标题行
(`**场外基金**`、`**账户与交易**`),而检查工具要求首列是端点编号。
2026-09-14 20:14:50 +08:00
..
2026-09-14 18:13:10 +08:00
2026-09-09 21:55:37 +08:00
2026-09-13 22:08:04 +08:00
2026-09-11 20:28:21 +08:00
2026-09-14 18:13:10 +08:00
2026-09-11 22:31:51 +08:00
2026-09-11 15:08:37 +08:00
2026-09-11 14:12:56 +08:00
2026-09-14 18:22:56 +08:00
2026-09-11 20:50:41 +08:00
2026-09-14 20:14:50 +08:00
2026-09-11 17:32:47 +08:00
2026-09-11 16:57:47 +08:00
2026-09-12 13:14:57 +08:00
2026-09-11 14:46:40 +08:00
2026-09-11 16:11:30 +08:00
2026-09-11 16:11:30 +08:00
2026-09-11 16:11:30 +08:00
2026-09-11 14:46:40 +08:00
2026-09-11 14:44:29 +08:00
2026-09-14 18:13:10 +08:00
2026-09-11 16:57:47 +08:00
2026-09-11 16:12:38 +08:00
2026-09-10 21:43:39 +08:00
2026-09-12 12:50:14 +08:00
2026-09-09 21:55:37 +08:00
2026-09-11 16:57:47 +08:00
2026-09-14 18:22:56 +08:00
2026-09-10 18:42:44 +08:00
2026-09-11 22:31:51 +08:00
2026-09-11 14:46:40 +08:00
2026-09-11 14:46:40 +08:00
2026-09-10 22:08:15 +08:00
2026-09-12 11:15:24 +08:00
2026-09-11 18:42:42 +08:00
2026-09-11 16:57:47 +08:00
2026-09-10 20:22:42 +08:00
2026-09-14 00:30:06 +08:00
2026-09-13 20:39:36 +08:00
2026-09-12 14:11:42 +08:00
2026-09-11 16:57:47 +08:00
2026-09-11 16:57:47 +08:00
2026-09-11 22:31:51 +08:00
2026-09-11 16:57:47 +08:00
2026-09-12 12:50:14 +08:00
2026-09-09 21:55:37 +08:00
2026-09-14 01:07:43 +08:00
2026-09-14 01:07:43 +08:00
2026-09-14 18:13:10 +08:00
2026-09-11 16:57:47 +08:00
2026-09-11 16:57:47 +08:00
2026-09-10 09:23:22 +08:00
2026-09-09 21:55:37 +08:00
2026-09-14 18:22:56 +08:00
2026-09-11 19:20:32 +08:00
2026-09-11 12:47:01 +08:00
2026-09-12 12:10:10 +08:00
2026-09-11 12:47:01 +08:00
2026-09-14 18:22:56 +08:00
2026-09-10 21:58:42 +08:00
2026-09-12 10:45:40 +08:00
2026-09-11 16:42:31 +08:00
2026-09-10 21:55:03 +08:00
2026-09-10 21:58:42 +08:00
2026-09-09 21:55:37 +08:00
2026-09-11 16:57:47 +08:00
2026-09-14 18:13:10 +08:00
2026-09-14 18:13:10 +08:00
2026-09-14 18:13:10 +08:00
2026-09-11 16:57:47 +08:00
2026-09-14 18:13:10 +08:00
2026-09-14 18:13:10 +08:00
2026-09-14 18:13:10 +08:00
2026-09-12 15:37:36 +08:00
2026-09-14 00:30:06 +08:00
2026-09-11 21:22:56 +08:00
2026-09-11 14:14:50 +08:00
2026-09-10 21:03:44 +08:00
2026-09-10 21:03:44 +08:00
2026-09-13 19:17:56 +08:00
2026-09-12 15:56:50 +08:00
2026-09-11 14:16:07 +08:00
2026-09-11 11:42:53 +08:00
2026-09-11 14:24:45 +08:00
2026-09-10 21:03:44 +08:00
2026-09-12 15:56:50 +08:00
2026-09-13 23:51:45 +08:00
2026-09-14 01:41:02 +08:00
2026-09-11 09:49:21 +08:00
2026-09-12 16:30:56 +08:00
2026-09-12 15:56:50 +08:00
2026-09-11 19:37:26 +08:00
2026-09-14 18:13:10 +08:00
2026-09-11 16:57:47 +08:00
2026-09-11 21:03:58 +08:00
2026-09-11 16:57:47 +08:00
2026-09-13 19:30:59 +08:00