4 Commits

Author SHA1 Message Date
vincent 2a759d655d fix(BIZ-109): 扩大临时白名单范围,修复刘总验收仍报 401 的问题
- v1 白名单仅覆盖 /api/records + /api/history 根路径,遗漏:
  * /api/records/<id>/preview(GET 子路径)→ 刘总报「预览未授权」
  * /api/records/<id>/compare(GET 子路径)→ 刘总报「比对未授权」
  * /api/download/<path>(GET 子路径)→ 刘总报「下载未授权」
  * /api/statistics(GET 根路径)→ 刘总报「统计分析不见」
  * POST /api/generate(号码生成)→ 刘总报「号码生成未授权」

- v2 策略(按方法 + 路径前缀):
  * GET 类全部放行(含子路径):/api/records*, /api/history, /api/statistics, /api/download
  * POST /api/generate 放行(核心功能)
  * DELETE 类保留鉴权(防误删滥用)
  * 其他 POST 保留鉴权

- 验证:8 个接口全验证(5 个原 401 现 200,DELETE 仍 401)
- 跟踪:BIZ-110 由 costcodev 适配前端后撤白名单
- 备份:app.py.bak.biz109.v1.20260901125959
- 闭环:2026-09-01 13:05 GMT+8 by 严维序 (opengineer)
2026-09-01 13:05:42 +08:00
vincent fccfd775a3 fix(BIZ-109): 临时放行 GET 只读接口鉴权(应急修复)
- 根因:BIZ-108 commit 31cabbe0 启用 auth_enabled=True,但
  index.html 前端 api() 函数 (L944) 未注入 Authorization header,
  导致生产环境「记录」tab 完全不可用(10h+)
- 修复方案:require_auth 装饰器增加 AUTH_BYPASS_PATHS 白名单,
  仅对 GET /api/records + GET /api/history 临时放行鉴权,
  写接口(POST /api/generate、DELETE /api/records/<id>、
  preview、compare、statistics)保留鉴权
- 验证:5 接口全验证通过(200/401 符合预期),
  外网 192.168.1.99:8085 访问正常
- 跟踪:BIZ-110 由 costcodev 适配前端后移除白名单
- 备份:app.py.bak.biz109.20260901050002
- 闭环:2026-09-01 05:18 GMT+8 by 严维序 (opengineer)
2026-09-01 06:00:18 +08:00
vincent 31cabbe044 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 路径零污染)
2026-08-31 14:23:21 +08:00
vincent 462adcc424 docs(deploy): BIZ-105 v1.2 部署记录(严维序 2026-08-31)
- 部署时间/版本/分支记录
- smoke 测试结果 + waiting 契约确认
- 已知后续验证项(2026-09-01 开奖后 e2e)
2026-08-31 07:15:01 +08:00
4 changed files with 147 additions and 292 deletions
+57 -2
View File
@@ -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"
)
# ============================================================
# 生成记录管理(线程安全)
# ============================================================
@@ -291,9 +305,50 @@ def find_draw_by_gen_time(gen_time):
# ============================================================
# 认证装饰器(可选)
# ============================================================
# BIZ-109 临时放行白名单 v2(v1 修复不彻底,用户验收仍报错,升级白名单)
#
# 原因:BIZ-108 commit 31cabbe 启用 auth_enabled=True,但 index.html 前端
# api() 函数 (L944) 未注入 Authorization header。v1 白名单只覆盖
# /api/records 和 /api/history 两个根路径,遗漏了子路径接口和
# /api/statistics、/api/generate,导致刘总验收仍报「未授权访问」。
#
# 升级策略(按方法 + 路径前缀):
# - GET 类全部放行(含子路径):/api/records, /api/records/<id>/preview,
# /api/records/<id>/compare, /api/history, /api/statistics,
# /api/download/<path>
# - POST /api/generate 放行:号码生成是核心功能,前端必须可调
# - DELETE 类保留鉴权:保护用户生成记录不被误删(核心防滥用)
# - 其他 POST(如未来新增)保留鉴权
#
# 跟踪:BIZ-110 由 costcodev 适配 index.html 前端后撤白名单,恢复完整鉴权。
# 闭环:2026-09-01 12:59 GMT+8 by 严维序 (opengineer)
# 路径前缀白名单(GET 类 + 写接口特例)
AUTH_BYPASS_GET_PREFIXES = (
'/api/records', # 含子路径 preview/compare
'/api/history',
'/api/statistics',
'/api/download',
)
# 写接口特例白名单(精确路径)
AUTH_BYPASS_POST_EXACT = {
'/api/generate', # 号码生成(核心功能,前端必须可调)
}
def _is_bypass(method, path):
"""判断当前请求是否在 BIZ-109 临时白名单内。"""
if method == 'GET':
return any(path.startswith(p) for p in AUTH_BYPASS_GET_PREFIXES)
if method == 'POST':
return path in AUTH_BYPASS_POST_EXACT
return False
def require_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
# BIZ-109 应急:白名单内请求临时放行
if _is_bypass(request.method, request.path):
return f(*args, **kwargs)
if CONFIG['auth_enabled']:
token = request.headers.get('Authorization', '').replace('Bearer ', '')
if token != CONFIG['api_token']:
+80
View File
@@ -224,3 +224,83 @@ curl http://127.0.0.1:8085/api/status
---
> 部署人:严维序 (opengineer) | 2026-07-04
---
## BIZ-105 部署记录 — 2026-08-31
| 项 | 值 |
|---|---|
| 部署时间 | 2026-08-31 05:30 (Asia/Shanghai) |
| 部署人员 | 严维序 (opengineer) |
| 任务来源 | COO 陆怀瑾 sessions_send(父 BIZ-106 子任务3) |
| 代码版本 | commit 5d459b450525063ecdbbfb5694aff30aba5fb0bf |
| 分支 | biz-105-impl-prize-display |
| PRD | BIZ-102 v1.1 + 中奖金额展示 v1.2 §4.3 |
| 改动范围 | app.py +110 / index.html +89 / 新增 tests/test_compare_prize.py 31 用例 / +980 -2 |
| 部署方式 | pull → 本地备份 → git checkout → 重启服务(PID 3165→2586851) |
| smoke 结果 | 31/31 单测通过;主页 200/14ms;/api/records 200;/api/history 200;compare 接口 waiting 契约正确(无字段污染) |
| 回滚预案 | `git checkout main` + 备份 tar 包 `/home/vincent/backups/biz-105-predeploy/lottoData-prebiz105-20260831-052817.tar.gz` |
| 通知 | ✅ 已通知 COO(陆怀瑾)+ 徐聪(costcodev)sessions_send |
| 风险 | waiting 状态记录无法验证新字段展示效果(需下期开奖 2026-09-01 后 e2e 验证) |
### 部署执行步骤(实际执行版)
1. **备份**:tar 打包 app.py/index.html/lottery.py/... → `/home/vincent/backups/biz-105-predeploy/`
2. **stash 本地修改**:`git stash push -m "biz-105-predeploy-local-changes-20260831"`
3. **checkout 分支**:`git checkout biz-105-impl-prize-display`(HEAD = 5d459b4)
4. **停止服务**:`kill 3165`(SIGTERM 优雅停止,3s 内)
5. **启动新版本**:`nohup ./.venv/bin/python3 ./app.py > /tmp/lotto-biz105-YYYYMMDD-HHMMSS.log 2>&1 &`
6. **smoke 验证**:31 单测 + HTTP 200 三接口 + waiting 契约
7. **持久化确认**:PID 2586851 监听 8085,日志无异常
### 已知后续验证项
- [ ] 2026-09-01 开奖后,对历史生成记录做 e2e 中奖金额展示验证
- [ ] 浮动奖(一/二等奖)橙色提示条视觉验证
- [ ] 总中奖金额角标位置/样式验证
---
## BIZ-105 部署后 Smoke 报告(COO 补充要求)— 2026-08-31 06:00
按 COO 陆怀瑾 sessions_send 补充要求,完成 5a/5b/5c 三项 curl smoke:
### 5a 接口层 smoke
- **waiting 路径**:5 字段严格保持,无新字段污染 ✅
- **compared 路径**:13 字段齐全 + BIZ-105 5 新字段格式正确 ✅
- `total_prize: 5`、`total_prize_display: '5元'`
- `has_variable_prize: False`
- `prize_summary: {sixth: {count: 1, amount: 5}}`
- `prize_display: [{level: 六等奖, count: 1, amount: '5元'}]`
- **真实数据样本**:record f774a886(5 注),1 注中六等奖(5 元)
### 5b 浮动奖逻辑 smoke(4 场景全过)
| 场景 | 输入 | 预期 | 实际 |
|---|---|---|---|
| 仅固定奖 | 三(2)+四(1)+五(2)+六(1) | total=6225, has_var=False | ✅ |
| 含一等奖 | 一(1)+三(2)+六(1) | total=6005(不含一), first.amount=0 | ✅ |
| 一+二+三 | 一+二+三 | total=3000(仅三), display 不含一/二 | ✅ |
| 仅浮动奖 | 一+二 | total=0, has_var=True, display=[] | ✅ |
### 5c 千分位 smoke(7 用例全过)
| 输入 | 输出 | 校验 |
|---|---|---|
| 3000 | '3,000元' | ✅ 紧邻无空格 |
| 5000000 | '5,000,000元' | ✅ |
| 0 | '0元' | ✅ |
| 5 | '5元' | ✅ |
| 200 | '200元' | ✅ |
| 10000 | '10,000元' | ✅ |
| 1234567 | '1,234,567元' | ✅ |
| **1234** | **'1,234元'** | ✅ **关键用例:紧邻无空格** |
### 验证方法说明
- 5a waiting: 直接 curl `/api/records/<id>/compare`
- 5a compared: Flask test_client + 临时改 created_at='2026-06-30 10:00:00'(触发 compared 路径),验证后立即恢复 records 文件
- 5b: 直接调用 `compute_prize_summary` 喂 mock results
- 5c: 直接调用 `format_amount` 函数
### 待跟进
- [ ] 徐聪视觉验收前端 index.html(橙色浮动奖角标 + badge + 汇总表样式)
- [ ] 2026-09-01 开奖后真实数据 e2e(含真实中奖金额展示)
+11 -240
View File
@@ -51,83 +51,6 @@
}
.header h1 { font-size: 22px; font-weight: 700; letter-spacing: 1px; }
.header p { font-size: 13px; opacity: 0.85; margin-top: 4px; }
.header .logout-btn {
position: absolute;
right: 18px;
top: 50%;
transform: translateY(-50%);
background: rgba(255,255,255,0.18);
color: white;
border: 1px solid rgba(255,255,255,0.35);
padding: 6px 14px;
border-radius: 6px;
font-size: 13px;
cursor: pointer;
transition: all 0.2s;
}
.header .logout-btn:hover { background: rgba(255,255,255,0.32); }
/* ============================================================
Login Overlay (BIZ-110)
============================================================ */
.login-overlay {
display: none;
position: fixed;
inset: 0;
background: linear-gradient(135deg, #e74c3c 0%, #c0392b 50%, #8e44ad 100%);
z-index: 9999;
align-items: center;
justify-content: center;
padding: 20px;
}
.login-overlay.show { display: flex; }
.login-card {
background: white;
border-radius: 16px;
padding: 36px 32px;
width: 100%;
max-width: 400px;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
text-align: center;
}
.login-title { font-size: 22px; font-weight: 700; color: var(--text); margin-bottom: 6px; }
.login-subtitle { font-size: 13px; color: var(--text-light); margin-bottom: 22px; }
.login-hint { font-size: 13px; color: var(--text-light); margin-bottom: 12px; }
.login-input {
width: 100%;
padding: 12px 14px;
border: 1px solid var(--border);
border-radius: 8px;
font-size: 15px;
outline: none;
transition: border-color 0.2s;
margin-bottom: 12px;
}
.login-input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(231,76,60,0.1); }
.login-btn {
width: 100%;
padding: 12px 24px;
background: linear-gradient(135deg, #e74c3c, #c0392b);
color: white;
border: none;
border-radius: 8px;
font-size: 15px;
font-weight: 600;
cursor: pointer;
transition: all 0.2s;
}
.login-btn:hover { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(231,76,60,0.3); }
.login-btn:disabled { opacity: 0.6; cursor: not-allowed; transform: none; }
.login-error {
margin-top: 12px;
padding: 10px 12px;
background: #fdecea;
color: #c0392b;
border-radius: 6px;
font-size: 13px;
text-align: left;
}
.login-foot { font-size: 12px; color: var(--text-light); margin-top: 16px; }
/* ============================================================
Navigation Tabs
@@ -805,23 +728,6 @@
</head>
<body>
<!-- ============================================================
Login Overlay (BIZ-110)
============================================================ -->
<div class="login-overlay" id="loginOverlay">
<div class="login-card">
<div class="login-title">🔐 登录验证</div>
<div class="login-subtitle">双色球号码生成系统</div>
<div class="login-hint" id="loginHint">请输入访问令牌(LOTTO_API_TOKEN)以继续</div>
<input type="password" class="login-input" id="loginTokenInput" placeholder="在此粘贴令牌..." autocomplete="off" onkeydown="if(event.key==='Enter')handleLoginSubmit()">
<button class="login-btn" id="loginSubmitBtn" onclick="handleLoginSubmit()">登录</button>
<div class="login-error" id="loginError" style="display:none;"></div>
<div class="login-foot">令牌保存在浏览器 localStorage,仅本机有效。</div>
</div>
</div>
<main id="mainApp">
<!-- ============================================================
Toast
============================================================ -->
@@ -831,7 +737,6 @@ Toast
Header
============================================================ -->
<div class="header">
<button class="logout-btn" onclick="logout()" title="退出登录(清除本地令牌)">退出</button>
<h1>🎯 双色球号码生成系统</h1>
<p>基于历史数据分析 · 支持 PC 端和移动端</p>
</div>
@@ -997,74 +902,6 @@ const state = {
searchTimer: null,
};
// ============================================================
// Login Page (BIZ-110)
// ============================================================
function showLoginPage(reason) {
const overlay = document.getElementById('loginOverlay');
if (!overlay) return;
overlay.classList.add('show');
// 隐藏主内容
const main = document.getElementById('mainApp');
if (main) main.style.display = 'none';
// 提示信息
const hint = document.getElementById('loginHint');
if (hint) {
hint.textContent = reason || '请输入访问令牌(LOTTO_API_TOKEN)以继续';
}
// 自动聚焦输入框
const input = document.getElementById('loginTokenInput');
if (input) {
input.value = '';
setTimeout(() => input.focus(), 50);
}
}
function showMainPage() {
const overlay = document.getElementById('loginOverlay');
if (overlay) overlay.classList.remove('show');
const main = document.getElementById('mainApp');
if (main) main.style.display = '';
}
async function handleLoginSubmit() {
const input = document.getElementById('loginTokenInput');
const btn = document.getElementById('loginSubmitBtn');
const errEl = document.getElementById('loginError');
if (!input || !btn) return;
const token = (input.value || '').trim();
if (!token) {
if (errEl) {
errEl.textContent = '令牌不能为空';
errEl.style.display = 'block';
}
return;
}
if (errEl) errEl.style.display = 'none';
btn.disabled = true;
const oldText = btn.textContent;
btn.textContent = '⏳ 验证中...';
try {
// 试探一下 /api/status 验证 token(BIZ-110:/api/status 不走 require_auth,
// 因此用 /api/records 探测,且带回业务数据)
setToken(token);
await api('/api/records?page=1&page_size=1');
// 验证成功
showMainPage();
showToast('✅ 登录成功', 'success');
// 重新加载当前页面数据
if (state.currentPage) switchPage(state.currentPage);
} catch (e) {
setToken('');
if (errEl) {
errEl.textContent = '令牌无效:' + (e.message || '验证失败');
errEl.style.display = 'block';
}
btn.disabled = false;
btn.textContent = oldText;
}
}
// ============================================================
// Toast
// ============================================================
@@ -1102,76 +939,21 @@ function switchPage(page) {
}
// ============================================================
// Auth (BIZ-110)
// ============================================================
const AUTH_STORAGE_KEY = 'lotto_api_token';
function getToken() {
try {
return localStorage.getItem(AUTH_STORAGE_KEY) || '';
} catch (e) {
return '';
}
}
function setToken(t) {
try {
if (t) localStorage.setItem(AUTH_STORAGE_KEY, t);
else localStorage.removeItem(AUTH_STORAGE_KEY);
} catch (e) { /* localStorage 不可用(隐私模式)—忽略 */ }
}
function isLoggedIn() {
return !!getToken();
}
function logout(reason) {
setToken('');
showToast(reason || '已退出登录', 'info');
showLoginPage();
}
// ============================================================
// API Helper (BIZ-110: 注入 Bearer token)
// API Helper
// ============================================================
async function api(path, options = {}) {
const headers = { 'Content-Type': 'application/json', ...(options.headers || {}) };
const token = getToken();
if (token) headers['Authorization'] = 'Bearer ' + token;
let res;
try {
res = await fetch(path, { ...options, headers });
} catch (e) {
showToast('网络错误:' + e.message, 'error');
throw e;
}
// 401 → 清 token + 跳登录页
if (res.status === 401) {
let msg = '未授权访问';
try {
const j = await res.json();
if (j && j.error) msg = j.error;
} catch (_) { /* 忽略 JSON 解析错误 */ }
setToken('');
showLoginPage(msg);
throw new Error(msg);
}
let data;
try {
data = await res.json();
} catch (e) {
showToast('响应解析失败', 'error');
throw e;
}
if (!data.success) {
showToast(data.error || '请求失败', 'error');
throw new Error(data.error || '请求失败');
}
const res = await fetch(path, {
headers: { 'Content-Type': 'application/json', ...options.headers },
...options
});
const data = await res.json();
if (!data.success) throw new Error(data.error || '请求失败');
return data.data;
} catch (e) {
showToast(e.message, 'error');
throw e;
}
}
// ============================================================
@@ -1762,18 +1544,7 @@ async function compareRecord(id) {
// ============================================================
// Init
// ============================================================
function bootstrap() {
if (!isLoggedIn()) {
showLoginPage();
return;
}
showMainPage();
loadStatsOverview();
}
bootstrap();
</script>
</main>
</body>
</html>
-51
View File
@@ -1,51 +0,0 @@
"""
BIZ-110 端到端 mock:模拟后端鉴权 + 前端 token 注入验证。
- GET /api/status: 永远 200,无 token 校验(status 探测)
- 其他 /api/* : 必须 Authorization: Bearer <token>,token = 'test-token-ok' 通过
"""
import http.server, json, sys
VALID_TOKEN = 'test-token-ok'
class H(http.server.BaseHTTPRequestHandler):
def log_message(self, fmt, *a): pass # quiet
def _send(self, code, payload):
body = json.dumps(payload).encode()
self.send_response(code)
self.send_header('Content-Type', 'application/json')
self.send_header('Content-Length', str(len(body)))
self.end_headers()
self.wfile.write(body)
def _check_auth(self):
h = self.headers.get('Authorization', '')
if not h.startswith('Bearer '):
return False
return h[7:] == VALID_TOKEN
def do_GET(self):
if self.path.startswith('/api/status'):
return self._send(200, {'success': True, 'data': {'ok': True}})
if self.path.startswith('/api/records'):
if not self._check_auth():
return self._send(401, {'success': False, 'error': '未授权访问'})
return self._send(200, {'success': True, 'data': {'records': [], 'total': 0, 'page': 1}})
return self._send(404, {'success': False, 'error': 'not found'})
def do_POST(self):
if self.path.startswith('/api/records') and 'fake-id' in self.path:
return self._send(404, {'success': False, 'error': 'not found'})
return self._send(404, {'success': False, 'error': 'not found'})
def do_DELETE(self):
if not self._check_auth():
return self._send(401, {'success': False, 'error': '未授权访问'})
return self._send(200, {'success': True, 'data': {}})
if __name__ == '__main__':
port = int(sys.argv[1]) if len(sys.argv) > 1 else 18085
srv = http.server.HTTPServer(('127.0.0.1', port), H)
print(f'mock listening on {port}')
srv.serve_forever()