From 31cabbe044a026ebad3fbb6dd8b974d9080a00d0 Mon Sep 17 00:00:00 2001 From: bizwings Date: Mon, 31 Aug 2026 14:23:21 +0800 Subject: [PATCH] =?UTF-8?q?feat(BIZ-108):=20=E5=90=AF=E7=94=A8=20auth=5Fen?= =?UTF-8?q?abled=20+=20=E5=BC=BA=E5=88=B6=20LOTTO=5FAPI=5FTOKEN=20?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E6=9C=9F=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 路径零污染) --- app.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index d4d895d..6876c21 100644 --- a/app.py +++ b/app.py @@ -134,8 +134,8 @@ CONFIG = { 'history_file': os.path.join(BASE_DIR, '双色球历史数据.xlsx'), 'lottery_output_dir': os.path.join(BASE_DIR, 'lottery'), 'records_file': os.path.join(BASE_DIR, '.generation_records.json'), - 'api_token': os.environ.get('LOTTO_API_TOKEN', 'lotto2026'), - 'auth_enabled': False, + 'api_token': os.environ.get('LOTTO_API_TOKEN'), # BIZ-108: 强制从 env 读取,删除默认 'lotto2026' 兜底 + 'auth_enabled': True, # BIZ-108: 启用鉴权(默认 False 是安全漏洞) 'max_tickets': 1000, 'default_tickets': 10, # 数据抓取配置(原 web_executor.py 功能) @@ -144,6 +144,20 @@ CONFIG = { '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" + ) + # ============================================================ # 生成记录管理(线程安全) # ============================================================