feat(BIZ-108): 启用 auth_enabled + 强制 LOTTO_API_TOKEN 启动期校验

- app.py L137: 删除 'lotto2026' 默认 token 兜底
- app.py L138: auth_enabled: False → True
- app.py L147+: 启动期强制校验 token 长度 ≥ 32 字符
  - 缺失或过短 → RuntimeError fail-fast,避免静默使用默认 token
- 配套 /etc/lotto/env(root:root, 0600 权限)落盘强随机 token

验证:
- /api/status 报 auth_enabled:true
- 无/错 token → 401,正确 token → 200
- 8 个接口鉴权全部生效(generate/records/preview/compare/statistics/history/status/delete)
- BIZ-105 字段契约保持(waiting 路径零污染)
This commit is contained in:
2026-08-31 14:23:21 +08:00
parent 462adcc424
commit 31cabbe044
+16 -2
View File
@@ -134,8 +134,8 @@ CONFIG = {
'history_file': os.path.join(BASE_DIR, '双色球历史数据.xlsx'), 'history_file': os.path.join(BASE_DIR, '双色球历史数据.xlsx'),
'lottery_output_dir': os.path.join(BASE_DIR, 'lottery'), 'lottery_output_dir': os.path.join(BASE_DIR, 'lottery'),
'records_file': os.path.join(BASE_DIR, '.generation_records.json'), 'records_file': os.path.join(BASE_DIR, '.generation_records.json'),
'api_token': os.environ.get('LOTTO_API_TOKEN', 'lotto2026'), 'api_token': os.environ.get('LOTTO_API_TOKEN'), # BIZ-108: 强制从 env 读取,删除默认 'lotto2026' 兜底
'auth_enabled': False, 'auth_enabled': True, # BIZ-108: 启用鉴权(默认 False 是安全漏洞)
'max_tickets': 1000, 'max_tickets': 1000,
'default_tickets': 10, 'default_tickets': 10,
# 数据抓取配置(原 web_executor.py 功能) # 数据抓取配置(原 web_executor.py 功能)
@@ -144,6 +144,20 @@ CONFIG = {
'fetch_timeout': 300, # 抓取超时秒数 'fetch_timeout': 300, # 抓取超时秒数
} }
# ============================================================
# BIZ-108 安全加固:启动期强制校验 LOTTO_API_TOKEN
# ============================================================
# 规则:auth_enabled=True 时,token 必须存在且长度 ≥ 32
# 缺失或过短 → 启动失败(fail-fast),避免静默使用默认 token
_API_TOKEN = CONFIG['api_token']
if CONFIG['auth_enabled']:
if not _API_TOKEN or len(_API_TOKEN) < 32:
raise RuntimeError(
"[BIZ-108] LOTTO_API_TOKEN 缺失或长度 < 32 字符。"
"请在 /etc/lotto/env 配置强随机 token(建议 64 字符)。"
"生成命令: openssl rand -base64 48 | tr -d '\\n=' | cut -c1-64"
)
# ============================================================ # ============================================================
# 生成记录管理(线程安全) # 生成记录管理(线程安全)
# ============================================================ # ============================================================