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) 时上游只收到候选点;合法坐标仍先发上报值;唯一索引拦住重复插入
This commit is contained in:
@@ -22,6 +22,21 @@ POI_MAX_MATCH_M = 3000
|
||||
# 停车点默认统计半径(米):订阅时判断"这个点附近有没有车"
|
||||
DEFAULT_PARKING_RADIUS_M = 150
|
||||
|
||||
# 校园中心(GCJ02):缺定位时的最后兜底点。全站只有这一份数据源,
|
||||
# routes/map_api.py 的 /api/config 也从这里取,别再各写一份魔数。
|
||||
CAMPUS_CENTER_LAT = 36.531820
|
||||
CAMPUS_CENTER_LNG = 116.780180
|
||||
|
||||
|
||||
def campus_center():
|
||||
"""校园中心坐标 (lat, lng),GCJ02。没配置就返回 None —— 绝不编造坐标。"""
|
||||
if CAMPUS_CENTER_LAT is None or CAMPUS_CENTER_LNG is None:
|
||||
return None
|
||||
try:
|
||||
return float(CAMPUS_CENTER_LAT), float(CAMPUS_CENTER_LNG)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
# ========== 坐标转换(与 static/js/coordtransform.js 同算法) ==========
|
||||
|
||||
@@ -242,37 +257,75 @@ def _num(value, default=0.0):
|
||||
return default
|
||||
|
||||
|
||||
def bikes_near_parking(points, loc):
|
||||
"""统计某个停车点附近的车辆(订阅用)。
|
||||
def parking_area_ctx(loc):
|
||||
"""停车区判定上下文:中心点 + 半径 + 历史多边形。
|
||||
|
||||
points: [{bike_code, latitude, longitude, state, status, electric}, ...]
|
||||
(调用方保证是每辆车最近一条记录)
|
||||
loc: campus_locations 行(kind='parking',按坐标 + 半径判断)
|
||||
返回: {"available": n, "low": n, "total": n, "codes": [可用车牌...]}
|
||||
「候选统计」(parking_candidates,自动开锁挑车)与「状态统计」
|
||||
(bikes_near_parking,订阅邮件里的可用/低电/总数)**必须**共用这一套
|
||||
判定条件 —— 之前一处认 polygon、一处只认半径,会出现「邮件说附近有 8 辆、
|
||||
候选 0 辆、自动开锁永不触发」(BUG-16)。
|
||||
|
||||
返回 None 表示这个停车点既没有合法中心点也没有多边形,无法判定范围。
|
||||
"""
|
||||
available, low, total, codes = 0, 0, 0, []
|
||||
if not loc:
|
||||
return None
|
||||
poly = parse_polygon(loc.get("polygon"))
|
||||
lat0 = lng0 = None
|
||||
try:
|
||||
lat0, lng0 = float(loc.get("latitude")), float(loc.get("longitude"))
|
||||
if loc.get("latitude") is not None and loc.get("longitude") is not None:
|
||||
lat0, lng0 = float(loc["latitude"]), float(loc["longitude"])
|
||||
except (TypeError, ValueError):
|
||||
return {"available": 0, "low": 0, "total": 0, "codes": []}
|
||||
lat0 = lng0 = None
|
||||
if lat0 is None or lng0 is None:
|
||||
lat0 = lng0 = None
|
||||
if len(poly) < 3:
|
||||
return None
|
||||
radius = loc.get("radius")
|
||||
try:
|
||||
radius = float(radius) if radius else DEFAULT_PARKING_RADIUS_M
|
||||
except (TypeError, ValueError):
|
||||
radius = DEFAULT_PARKING_RADIUS_M
|
||||
# 兼容历史多边形停车区:点在多边形内也算
|
||||
poly = parse_polygon(loc.get("polygon"))
|
||||
return {"lat": lat0, "lng": lng0, "radius": radius, "polygon": poly}
|
||||
|
||||
|
||||
def in_parking_area(ctx, lat, lng):
|
||||
"""点是否落在停车区内(圆心+半径 或 历史多边形)。
|
||||
|
||||
返回 (hit, distance_m);只有多边形时可判定命中,距离取到最近顶点。
|
||||
"""
|
||||
if ctx is None:
|
||||
return False, None
|
||||
hit = False
|
||||
dist = None
|
||||
if ctx["lat"] is not None:
|
||||
dist = haversine_m(lat, lng, ctx["lat"], ctx["lng"])
|
||||
hit = dist <= ctx["radius"]
|
||||
if not hit and len(ctx["polygon"]) >= 3:
|
||||
hit = point_in_polygon(lat, lng, ctx["polygon"])
|
||||
if dist is None and hit:
|
||||
dist = min(haversine_m(lat, lng, p[0], p[1]) for p in ctx["polygon"])
|
||||
return hit, dist
|
||||
|
||||
|
||||
def bikes_near_parking(points, loc):
|
||||
"""统计某个停车点附近的车辆(订阅用)。
|
||||
|
||||
points: [{bike_code, latitude, longitude, state, status, electric}, ...]
|
||||
(调用方保证是每辆车最近一条记录)
|
||||
loc: campus_locations 行(kind='parking',按坐标 + 半径 / 历史多边形判断)
|
||||
返回: {"available": n, "low": n, "total": n, "codes": [可用车牌...]}
|
||||
"""
|
||||
available, low, total, codes = 0, 0, 0, []
|
||||
ctx = parking_area_ctx(loc)
|
||||
if ctx is None:
|
||||
return {"available": 0, "low": 0, "total": 0, "codes": []}
|
||||
|
||||
for p in points:
|
||||
try:
|
||||
plat, plng = float(p.get("latitude")), float(p.get("longitude"))
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
hit = False
|
||||
if lat0 is not None and lng0 is not None:
|
||||
hit = haversine_m(plat, plng, lat0, lng0) <= radius
|
||||
if not hit and len(poly) >= 3:
|
||||
hit = point_in_polygon(plat, plng, poly)
|
||||
hit, _dist = in_parking_area(ctx, plat, plng)
|
||||
if not hit:
|
||||
continue
|
||||
total += 1
|
||||
@@ -314,17 +367,12 @@ def parking_candidates(points, loc, kinds=None, min_electric=0, max_electric=100
|
||||
points: 每辆车最近一条采集点(GCJ02)
|
||||
kinds: 允许的车型集合(None/空 = 不限制)
|
||||
返回: [{"bike_code","kind","kind_label","electric","distance"}, ...]
|
||||
范围判定与 bikes_near_parking 共用 parking_area_ctx / in_parking_area,
|
||||
不再出现「候选 0 辆但状态统计有车」的口径分裂(BUG-16)。
|
||||
"""
|
||||
if loc is None or loc.get("latitude") is None or loc.get("longitude") is None:
|
||||
ctx = parking_area_ctx(loc)
|
||||
if ctx is None:
|
||||
return []
|
||||
try:
|
||||
lat0, lng0 = float(loc["latitude"]), float(loc["longitude"])
|
||||
except (TypeError, ValueError):
|
||||
return []
|
||||
try:
|
||||
radius = float(loc.get("radius") or DEFAULT_PARKING_RADIUS_M)
|
||||
except (TypeError, ValueError):
|
||||
radius = DEFAULT_PARKING_RADIUS_M
|
||||
kind_filter = set(kinds or []) or {"small", "big", "red"}
|
||||
try:
|
||||
min_e = float(min_electric if min_electric is not None else 0)
|
||||
@@ -338,12 +386,12 @@ def parking_candidates(points, loc, kinds=None, min_electric=0, max_electric=100
|
||||
plat, plng = float(p.get("latitude")), float(p.get("longitude"))
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
hit, dist = in_parking_area(ctx, plat, plng)
|
||||
if not hit:
|
||||
continue
|
||||
electric = _num(p.get("electric"), 0.0)
|
||||
state = int(_num(p.get("state"), 1))
|
||||
status = int(_num(p.get("status"), 1))
|
||||
dist = haversine_m(plat, plng, lat0, lng0)
|
||||
if dist > radius:
|
||||
continue
|
||||
# 只挑「空闲可用」:state=1 且 status!=1;电量落在设定区间
|
||||
if not (state == 1 and status != 1):
|
||||
continue
|
||||
@@ -357,7 +405,7 @@ def parking_candidates(points, loc, kinds=None, min_electric=0, max_electric=100
|
||||
"kind": k,
|
||||
"kind_label": BIKE_KIND_LABELS.get(k, k),
|
||||
"electric": round(electric, 1),
|
||||
"distance": round(dist),
|
||||
"distance": round(dist or 0),
|
||||
})
|
||||
|
||||
if sort_by == "electric_asc":
|
||||
|
||||
Reference in New Issue
Block a user