Files
xbcr/templates/ring_progress.html
T
zzy f34ed304bc fix: 缺陷审计批次(231 例)+ 复核补钉(①–⑦)
一、审计修复(另一侧完成,我复核通过)
- 认证/限流:_client_ip 归一、限流统一走 rate_limit._SlidingTable(超限只淘汰最旧 key,
  不再整表 clear 自毁)、每用户会话上限 10 + 过期清理
- 邮件验证码:锁内原子占位 → 锁外发信 → 成功落码,失败回滚冷却(并发不再双发)
- 租赁:rentals 部分唯一索引(同 order_id 只允许一行 expired=0,迁移前先查重)、
  开锁幂等落库、Timer 代号机制(取消/重排同临界区)、minutes=null 兜底、
  自动还车 30s×6 → 1m/2m/5m/10m 退避
- 安全:ring 四接口登录 + 任务归属校验(Bearer 或 ?token=,校验 sessions 表)、
  共享还车坐标不再伪造、代开锁 IntegrityError 兜底、token 配置原子写(fsync + 0600)
- 入口鉴权:8 个历史/分析接口未登录 401(HTTP 与 rtn_code 同时给)、
  analysis.html 带 Bearer + 未登录门 + 截断提示、历史查询 LIMIT 60000 并回传 truncated
- UX 四个 P0:计时条出现、真实单走 /api/order/return 而 reminder 走 /api/rental/return、
  定位失败不伪造坐标(三选一弹窗)、地图/配置/脚本失败统一落到可自救失败卡片
- 测试:新增 6 个文件 62 例;既有 test_history_30d 只补登录前置

二、我复核时发现并修掉的 7 处
① email_verify.record_cooldown 的 expires 写成 now,会被 send_code 开头的
   _cleanup_locked 整条删掉 —— 任何人发一次验证码就废掉「未注册邮箱」的冷却,
   找回密码又能枚举邮箱是否注册。改为活过冷却窗口,并补 3 例测试
② timer.py 两处落库(自动续租 INSERT、sync 复活 UPDATE)没防新唯一索引:
   撞索引抛 IntegrityError 会让巡检线程退出,该单从此没有计时器/自动还车(上游还在计费)。
   新增 _active_rental_id_for_order(),捕获后复用存活行
③ (0,0) 当合法坐标会先发给上游:/record/return 不校验范围,宽松后端会把车"还"在海里。
   新增 _coords_usable(),不可用就跳过原始点;候选列表永远丢掉第 0 项
④ boot_smoke.py 按旧契约要求 200,加鉴权后永远失败 → HTTPError 401 视为「未登录,预期」
⑤ 自动还车「不放弃」缺绝对上限:加 AUTO_RETURN_GIVE_UP_SECONDS=6h,到点只告警一次并停手
⑥ 索引谓词 aligned 到查重口径:COALESCE(expired,0)=0,旧定义自动重建
⑦ 访问日志脱敏:?token= / ?ticket= / ?access_token= 的值替换为 ***(werkzeug + root logger)
   否则容器日志里就是明文会话令牌
配套改写 tests/test_rental_dedup.py::test_return_accepts_zero_coordinates →
test_return_treats_zero_coordinates_as_missing(原断言把 (0,0) 当合法值发上游)

三、验证
- python -m pytest -q → 234 passed(基线 231,净增 3)
- node 5 套 → 44 pass / 0 fail
- 14 个 .py ast.parse、node --check dist/app.js → 全部通过
- boot_smoke.py → exit=0;未登录 /api/history/stats、/api/analysis/overview、
  /api/ring_progress/* 实测 401,/api/config 200
- 行为级:传 (0,0) 时上游只收到候选点;合法坐标仍先发上报值;唯一索引拦住重复插入
2026-11-17 09:58:16 +08:00

291 lines
10 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>全车寻铃进度</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
background: #1a1a2e;
color: #eee;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
}
.container {
background: #16213e;
border-radius: 16px;
padding: 32px;
width: 100%;
max-width: 420px;
box-shadow: 0 8px 32px rgba(0,0,0,0.3);
}
.header {
text-align: center;
margin-bottom: 28px;
}
.header h1 {
font-size: 22px;
margin-bottom: 8px;
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
}
.header p {
color: #888;
font-size: 14px;
}
.stats {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 12px;
margin-bottom: 24px;
}
.stat {
background: #0f3460;
border-radius: 12px;
padding: 16px 12px;
text-align: center;
}
.stat-value {
font-size: 28px;
font-weight: 700;
color: #e94560;
}
.stat-value.success { color: #4ade80; }
.stat-value.failed { color: #f87171; }
.stat-label {
font-size: 12px;
color: #888;
margin-top: 4px;
}
.progress-bar {
background: #0f3460;
border-radius: 10px;
height: 20px;
overflow: hidden;
margin-bottom: 20px;
}
.progress-fill {
height: 100%;
background: linear-gradient(90deg, #e94560, #ff6b6b);
border-radius: 10px;
transition: width 0.2s ease;
display: flex;
align-items: center;
justify-content: flex-end;
padding-right: 8px;
font-size: 11px;
font-weight: 600;
min-width: 40px;
}
.progress-fill.done { background: linear-gradient(90deg, #4ade80, #22c55e); }
.actions {
display: flex;
gap: 12px;
}
.btn {
flex: 1;
padding: 14px;
border: none;
border-radius: 10px;
font-size: 15px;
font-weight: 600;
cursor: pointer;
transition: all 0.2s;
}
.btn-cancel {
background: #e94560;
color: white;
}
.btn-cancel:hover { background: #d63850; }
.btn-close {
background: #333;
color: #ccc;
}
.btn-close:hover { background: #444; }
.btn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.log {
margin-top: 20px;
background: #0a0a1a;
border-radius: 10px;
padding: 12px;
max-height: 120px;
overflow-y: auto;
font-size: 12px;
color: #888;
}
.log-entry { margin-bottom: 4px; }
.log-entry.success { color: #4ade80; }
.log-entry.failed { color: #f87171; }
</style>
<link rel="stylesheet" href="/static/css/ui-system.css">
<script defer src="/static/js/ui-motion.js"></script>
</head>
<body>
<div class="container">
<div class="header">
<h1>
<svg viewBox="0 0 24 24" width="24" height="24" fill="none" stroke="currentColor" stroke-width="2"><path d="M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9"/><path d="M13.73 21a2 2 0 0 1-3.46 0"/></svg>
全车寻铃
</h1>
<p id="status-text">正在连接...</p>
</div>
<div class="stats">
<div class="stat">
<div class="stat-value" id="done-count">0</div>
<div class="stat-label">已完成</div>
</div>
<div class="stat">
<div class="stat-value success" id="success-count">0</div>
<div class="stat-label">成功</div>
</div>
<div class="stat">
<div class="stat-value failed" id="failed-count">0</div>
<div class="stat-label">失败</div>
</div>
</div>
<div class="progress-bar">
<div class="progress-fill" id="progress-fill" style="width: 0%">
<span id="progress-text">0%</span>
</div>
</div>
<div class="actions">
<button class="btn btn-cancel" id="cancel-btn" onclick="cancelTask()">取消任务</button>
<button class="btn btn-close" onclick="closeWindow()">关闭</button>
</div>
<div class="log" id="log"></div>
</div>
<script>
const params = new URLSearchParams(window.location.search);
const taskId = params.get('task_id') || '';
const total = parseInt(params.get('total') || '0');
// ===== 登录态(BUG-20)=====
// 批次 C 之后 /api/ring_status | ring_cancel | ring_progress 都要求登录,
// 鉴权接受 Authorization: Bearer 或 ?token=。EventSource 不能自定义 header,
// 只能走查询参数;POST 取消请求两边都带上,任一通道生效即可。
function getAuthToken() {
try { return localStorage.getItem('auth_token') || ''; } catch (e) { return ''; }
}
const authToken = getAuthToken();
const tokenQuery = authToken ? `?token=${encodeURIComponent(authToken)}` : '';
document.getElementById('status-text').textContent = `共 ${total} 辆车`;
let cancelled = false;
let completed = false;
function addLog(msg, type = '') {
const log = document.getElementById('log');
const entry = document.createElement('div');
entry.className = 'log-entry ' + type;
entry.textContent = `[${new Date().toLocaleTimeString()}] ${msg}`;
log.appendChild(entry);
log.scrollTop = log.scrollHeight;
}
function updateUI(data) {
const { status, done, total, success, failed } = data;
document.getElementById('done-count').textContent = done;
document.getElementById('success-count').textContent = success;
document.getElementById('failed-count').textContent = failed;
const pct = total > 0 ? Math.round((done / total) * 100) : 0;
const fill = document.getElementById('progress-fill');
fill.style.width = pct + '%';
document.getElementById('progress-text').textContent = pct + '%';
if (pct >= 100 || status === 'completed' || status === 'cancelled') {
fill.classList.add('done');
document.getElementById('cancel-btn').disabled = true;
completed = true;
}
if (status === 'cancelled') {
document.getElementById('status-text').textContent = '❌ 已取消';
addLog('任务已取消', '');
} else if (status === 'completed') {
document.getElementById('status-text').textContent = '✅ 全部完成';
addLog(`完成!成功:${success} 失败:${failed}`, '');
}
}
function cancelTask() {
if (cancelled || completed) return;
if (!taskId) { addLog('缺少任务号,无法取消', 'failed'); return; }
cancelled = true;
document.getElementById('cancel-btn').disabled = true;
document.getElementById('cancel-btn').textContent = '取消中...';
const headers = {};
if (authToken) headers['Authorization'] = 'Bearer ' + authToken;
fetch(`/api/ring_cancel/${encodeURIComponent(taskId)}${tokenQuery}`, {
method: 'POST',
headers
})
.then(r => r.json())
.then(d => {
addLog(d.message || '取消请求已发送', d.rtn_code === 0 ? '' : 'failed');
})
.catch(() => addLog('取消请求失败', 'failed'));
}
function closeWindow() {
if (!completed && !cancelled) {
if (!confirm('任务还在运行,确定要关闭吗?')) return;
}
uiCloseWindow();
}
// SSE 连接(带 ?token=;没有任务号或未登录时不要连,否则 EventSource 会 401 反复重试)
let es = null;
if (!taskId) {
document.getElementById('status-text').textContent = '缺少任务号';
document.getElementById('cancel-btn').disabled = true;
addLog('缺少 task_id,请从主页面重新发起全车寻铃', 'failed');
} else if (!authToken) {
document.getElementById('status-text').textContent = '请先在主页面登录';
document.getElementById('cancel-btn').disabled = true;
addLog('未登录:请先在主页面登录后再查看寻车进度', 'failed');
} else {
es = new EventSource(`/api/ring_progress/${encodeURIComponent(taskId)}${tokenQuery}`);
es.onmessage = (e) => {
try {
const data = JSON.parse(e.data);
if (data.type === 'progress') {
updateUI(data);
} else if (data.type === 'done') {
es.close();
} else if (data.type === 'error') {
addLog(data.message, 'failed');
es.close();
}
} catch (err) {}
};
es.onerror = () => {
addLog('连接断开', 'failed');
if (es) es.close();
};
addLog('开始监听进度...', '');
}
</script>
</body>
</html>