一、审计修复(另一侧完成,我复核通过) - 认证/限流:_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) 时上游只收到候选点;合法坐标仍先发上报值;唯一索引拦住重复插入
144 lines
6.3 KiB
JavaScript
144 lines
6.3 KiB
JavaScript
#!/usr/bin/env node
|
|
/* 验证 4:模板里的内联事件函数名,必须全部能在 bundle 里找到 window.X = X。
|
|
*
|
|
* node src-ts/tools/verify-globals.mjs
|
|
*
|
|
* 做法:
|
|
* 1) 从 templates/map.html 抓所有 on*="..." 内联事件,抽出被调用的标识符
|
|
* 2) 从 static/dist/app.js 抓 `window.X = X` 的赋值集合
|
|
* 3) 从**仍然单独加载**的静态脚本里抓它们的 window 赋值(ui-motion.js 等),
|
|
* 这些不是 bundle 的责任,单独列出来
|
|
* 4) 差集必须为空;同时反查 bundle 里多挂的全局(仅供参考)
|
|
*
|
|
* 退出码:差集非空 → 1
|
|
*/
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const HERE = path.dirname(fileURLToPath(import.meta.url));
|
|
const ROOT = path.resolve(HERE, "..", "..");
|
|
|
|
const TEMPLATE = path.join(ROOT, "templates", "map.html");
|
|
const BUNDLE = path.join(ROOT, "static", "dist", "app.js");
|
|
const ORIGIN_SCRIPTS = [
|
|
"static/js/map.js",
|
|
"static/js/navigation.js",
|
|
"static/js/share.js",
|
|
"static/js/subscribe.js",
|
|
];
|
|
/** 切换到 bundle 之后仍然保留的独立脚本(它们的全局不算 bundle 的缺口) */
|
|
const STILL_LOADED = [
|
|
"static/js/ui-motion.js",
|
|
"static/js/coordtransform.js",
|
|
"static/js/settings-pages.js",
|
|
"static/js/bike-clusters.js",
|
|
"static/js/qrcode.min.js",
|
|
"static/js/src/core/geo.js",
|
|
];
|
|
|
|
const EVENT_ATTR = /on(?:click|input|change|submit|keydown|keyup|keypress|blur|focus|dblclick|contextmenu|mouseover|mouseout|mouseenter|mouseleave|mousedown|mouseup|touchstart|touchmove|touchend|scroll|load|error|reset|select)\s*=\s*"([^"]*)"/g;
|
|
const JS_KEYWORDS = new Set([
|
|
"if", "else", "for", "while", "do", "switch", "case", "break", "continue", "return",
|
|
"new", "delete", "typeof", "instanceof", "in", "of", "var", "let", "const", "function",
|
|
"true", "false", "null", "undefined", "this", "void", "try", "catch", "finally", "throw",
|
|
"await", "async", "yield", "class", "super", "default", "export", "import", "static",
|
|
]);
|
|
const DOM_GLOBALS = new Set([
|
|
"window", "document", "event", "this", "navigator", "location", "console", "JSON", "Math",
|
|
"Date", "Number", "String", "Boolean", "Array", "Object", "Map", "Set", "Promise", "Error",
|
|
"parseInt", "parseFloat", "isNaN", "setTimeout", "clearTimeout", "setInterval",
|
|
"clearInterval", "fetch", "alert", "confirm", "prompt", "localStorage", "sessionStorage",
|
|
"encodeURIComponent", "decodeURIComponent", "history", "BMapGL", "QRCode", "SettingsPages",
|
|
"BikeClusters", "coordtransform", "Geo",
|
|
]);
|
|
|
|
const read = (rel) => fs.readFileSync(path.join(ROOT, rel), "utf8");
|
|
|
|
/* 1) 模板里的内联事件名 */
|
|
const html = read("templates/map.html");
|
|
const handlerCalls = new Set();
|
|
const handlerAny = new Set();
|
|
let handlerCount = 0;
|
|
{
|
|
let m;
|
|
EVENT_ATTR.lastIndex = 0;
|
|
while ((m = EVENT_ATTR.exec(html))) {
|
|
handlerCount++;
|
|
const code = m[1];
|
|
for (const c of code.matchAll(/(?:^|[^\w$.'"-])([A-Za-z_$][\w$]*)\s*\(/g)) handlerCalls.add(c[1]);
|
|
for (const c of code.matchAll(/(?:^|[^\w$.'"-])([A-Za-z_$][\w$]*)(?![\w$(])/g)) handlerAny.add(c[1]);
|
|
}
|
|
}
|
|
|
|
/* 2) bundle 里的 window 赋值 */
|
|
const bundle = read("static/dist/app.js");
|
|
const bundleGlobals = new Set();
|
|
for (const m of bundle.matchAll(/window\.([A-Za-z_$][\w$]*)\s*=/g)) bundleGlobals.add(m[1]);
|
|
|
|
/* 3) 仍单独加载的脚本提供的全局 */
|
|
const externalGlobals = new Map();
|
|
for (const rel of STILL_LOADED) {
|
|
let src;
|
|
try { src = read(rel); } catch { continue; }
|
|
for (const m of src.matchAll(/(?:window|root|global)\.([A-Za-z_$][\w$]*)\s*=/g)) {
|
|
if (!externalGlobals.has(m[1])) externalGlobals.set(m[1], rel);
|
|
}
|
|
for (const m of src.matchAll(/^(?:function\s+|const\s+|var\s+|let\s+)([A-Za-z_$][\w$]*)/gm)) {
|
|
if (!externalGlobals.has(m[1])) externalGlobals.set(m[1], rel);
|
|
}
|
|
}
|
|
|
|
/* 4) 原 4 个经典脚本里声明过的顶层函数名(判断某个内联名到底该谁提供)
|
|
* 注意:迁到 bundle 之后 static/js/{map,navigation,share,subscribe}.js 已删除,
|
|
* 这里改成「读得到就收,读不到就跳过」——旧版直接 read() 会 ENOENT 崩掉,
|
|
* 这个自检工具也就废了。 */
|
|
const originNames = new Set();
|
|
for (const rel of ORIGIN_SCRIPTS) {
|
|
let src;
|
|
try { src = read(rel); } catch { continue; }
|
|
for (const m of src.matchAll(/^\s{0,6}(?:async\s+)?function\s+([A-Za-z_$][\w$]*)/gm)) originNames.add(m[1]);
|
|
}
|
|
|
|
const relevant = (n) => !JS_KEYWORDS.has(n) && !DOM_GLOBALS.has(n);
|
|
const wanted = new Set([...handlerCalls, ...handlerAny].filter(relevant));
|
|
|
|
const missing = [];
|
|
const fromExternal = [];
|
|
const unknown = [];
|
|
for (const n of [...wanted].sort()) {
|
|
if (bundleGlobals.has(n)) continue;
|
|
if (externalGlobals.has(n)) { fromExternal.push(`${n} (<- ${externalGlobals.get(n)})`); continue; }
|
|
if (originNames.has(n)) { missing.push(n); continue; }
|
|
unknown.push(n);
|
|
}
|
|
|
|
const extras = [...bundleGlobals].filter((n) => !wanted.has(n)).sort();
|
|
|
|
console.log("=== 全局函数核对 ===");
|
|
console.log(`模板内联事件处数 : ${handlerCount}`);
|
|
console.log(`其中被调用的不同标识符 : ${handlerCalls.size}`);
|
|
console.log(`连裸标识符一起算 : ${handlerAny.size}`);
|
|
console.log(`筛选后需要全局的候选名 : ${wanted.size}`);
|
|
console.log(`bundle 里 window.X = X : ${bundleGlobals.size}`);
|
|
console.log("");
|
|
console.log(`由 bundle 提供的 : ${[...wanted].filter((n) => bundleGlobals.has(n)).length}`);
|
|
console.log(`由仍加载的静态脚本提供 : ${fromExternal.length}`);
|
|
fromExternal.forEach((n) => console.log(" " + n));
|
|
console.log("");
|
|
console.log(`★ 差集(模板要、bundle 没挂):${missing.length}`);
|
|
missing.forEach((n) => console.log(" MISSING " + n));
|
|
console.log("");
|
|
console.log(`模板里既不在原脚本、也不在静态脚本里的名字(人工确认,多为 DOM/事件变量):${unknown.length}`);
|
|
unknown.forEach((n) => console.log(" ? " + n));
|
|
console.log("");
|
|
console.log(`bundle 多挂的全局(原实现也是全局,属正常):${extras.length}`);
|
|
console.log(" " + extras.join(", "));
|
|
|
|
if (missing.length) {
|
|
console.error("\n失败:有内联事件函数名没有挂到 window 上");
|
|
process.exitCode = 1;
|
|
} else {
|
|
console.log("\n通过:模板内联事件所需函数名,bundle + 仍加载脚本合起来无缝覆盖(差集为空)");
|
|
}
|