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" + ) + # ============================================================ # 生成记录管理(线程安全) # ============================================================