refactor(plugins): comprehensive code review - ~35 fixes across 14 plugins

Phase 1 - Plugin code review (14/14 plugins):
- Security: 3x token leak in print→logger.debug, Bearer prefix handling
- Bug: bare except→specific exceptions, HorseState type safety, sync→async
- Critical: response_model undefined, route dead code, sync blocking event loop
- Quality: 11x print()→logger, variable name shadowing, consistent logging

Phase 2 - Deep analysis:
- Fix: payout int truncation→max(1, round(amount*odds))
- Fix: room_store get_lock race condition→dict.setdefault()
- Verify: data_manager f-string SQL is safe (uses ? placeholders)

Infrastructure: review reports generated for all plugins.
This commit is contained in:
2026-05-09 23:22:28 +08:00
parent 9a8cb3ad6d
commit c01338f496
43 changed files with 4233 additions and 3645 deletions

View File

@@ -0,0 +1,38 @@
# danding_api 评审报告
## 修复前问题清单 (5项)
| # | 严重度 | 问题 | 文件 |
|---|--------|------|------|
| 1 | **致命** | `addkami`/`createkami`/`addviptime` handler 内误用 `ddonline.finish()` 发送响应,导致:(1) 命令匹配到错误matcher后finish后续matcher仍会执行(2) 对于加卡密/生成卡密/用户加时等敏感操作错误消息可能泄漏给其他matcher | admin.py:36,41,49,58,63,69,78,84,91 |
| 2 | **高** | `session_id` 判断 bug`if session_id is None or "":` — Python中 `or ""` 总是返回右侧空字符串falsy导致该条件**永远为True**每次调用send_mail都触发重新登录 | utils.py:142 |
| 3 | 中 | `requests.post()` 同步阻塞调用在 async 函数中,会阻塞 nonebot 事件循环 | utils.py:20,34,146 |
| 4 | 中 | 硬编码 user `1424473282``post_vcode``get_log` 中 | utils.py:31,50 |
| 5 | 低 | `random.sleep(2,3)` 模拟人工反应(多处) | admin.py |
## 修复后变更清单
### admin.py
-`addkami` handler → 改用 `addkami.finish()`
-`createkami` handler → 改用 `createkami.finish()`
-`addviptime` handler → 改用 `addviptime.finish()`
- ✅ 各 handler 加 `try/except` 错误处理
- ✅ 加 `logger.error` 日志
### utils.py
-`session_id is None or ""``not session_id`
-`requests.post/get``timeout=10`
## 遗留问题(建议后续处理)
- [ ] `requests` 同步阻塞 → 迁移到 `httpx``aiohttp`
- [ ] 硬编码 user `1424473282` → 提取为配置项
- [ ] `login_pmail()` 是同步函数但在模块级调用,应改为异步或在启动时调用
## 验证
- [x] 每个 handler 只调用自身 matcher 的 `.finish()`
- [x] session_id 判断逻辑正确
- [x] API 调用有 timeout
- [x] 敏感操作有 try/except
## 代码质量总结
修复后评级:**B-** (从 D 提升,仍有同步阻塞等架构问题)