精简短信登录风控:保留单号冷却 + 单设备频控两道 (#109)
按 mentor 要求精简 SMS 登录链路的防刷策略,只保留两道主策略: - 单号 60s 冷却(发码,SMS_SEND_INTERVAL_SEC) - 单设备(device_id + IP)每小时 5 次频控(发码 + 登录,enforce_rate_limit) 删除两道: - 单号每日发送上限(SMS_DAILY_LIMIT_PER_PHONE) - 登录接口纯 IP 20 次/分钟限流(rate_limit 依赖) 验证码安全底线保留(一次性使用 + 输错 SMS_MAX_VERIFY_ATTEMPTS 次即作废), 属验证码正确性、不算限流策略,不在精简范围。 顺带清理:sms.py 移除 _daily_count/_today 及 datetime 导入;config.py 删常量; auth.py 清理变为无用的 Depends/rate_limit 导入;同步 docs/integrations/sms.md 与 docs/后端技术实现.md 的防刷说明(并修正 sms.md 里已过时的 /sms/send rate_limit 描述)。 影响:单号发码上限由 10 条/天放宽为仅受 60s 冷却约束;登录撞库防护改由设备频控 + 验证码错误次数上限兜底。test_auth.py 相应用例已删除/更新,auth 测试全绿。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> --------- Co-authored-by: zzhyyyyy <2685922758@qq.com> Reviewed-on: #109 Co-authored-by: zhuzihao <zhuzihao@wonderable.ai> Co-committed-by: zhuzihao <zhuzihao@wonderable.ai>
This commit was merged in pull request #109.
This commit is contained in:
+2
-22
@@ -17,7 +17,6 @@ def _reset(phone: str) -> None:
|
||||
"""清该号的进程内存状态,隔离 real 模式用例。"""
|
||||
sms._codes.pop(phone, None)
|
||||
sms._last_sent.pop(phone, None)
|
||||
sms._daily_count.pop(phone, None)
|
||||
|
||||
|
||||
class _OkResp:
|
||||
@@ -76,7 +75,7 @@ def test_sms_send_too_frequent(client) -> None:
|
||||
|
||||
def test_sms_send_device_ip_rate_limit(client, monkeypatch) -> None:
|
||||
"""发码防刷:同一设备(device_id) + 同一 IP 每小时最多 N 次发码,超出 429。
|
||||
用不同手机号(绕开单号 60s 冷却)证明限流按设备封顶 —— 堵「换号绕开单号冷却/每日上限」的洞。
|
||||
用不同手机号(绕开单号 60s 冷却)证明限流按设备封顶 —— 堵「换号绕开单号冷却」的洞。
|
||||
conftest 默认关限流;本用例临时打开 + 调小阈值便于测 + 清计数隔离。"""
|
||||
from app.api.v1 import auth
|
||||
from app.core import ratelimit
|
||||
@@ -155,21 +154,6 @@ def test_phone_format_validation(client) -> None:
|
||||
assert r.status_code == 422
|
||||
|
||||
|
||||
def test_sms_daily_limit(monkeypatch) -> None:
|
||||
"""单号每日上限(send 的防刷逻辑 mock/real 都跑;函数级绕开 60s 冷却)。"""
|
||||
phone = "13511135000"
|
||||
_reset(phone)
|
||||
limit = 3
|
||||
monkeypatch.setattr(sms.settings, "SMS_DAILY_LIMIT_PER_PHONE", limit)
|
||||
|
||||
for _ in range(limit):
|
||||
sms.send_code(phone)
|
||||
sms._last_sent.pop(phone, None) # 绕开冷却,只测每日上限
|
||||
|
||||
with pytest.raises(sms.SmsError, match="上限"):
|
||||
sms.send_code(phone)
|
||||
|
||||
|
||||
# ============================ real 路径(不真发)============================
|
||||
|
||||
def test_sms_real_send_calls_jiguang(monkeypatch) -> None:
|
||||
@@ -255,24 +239,20 @@ def test_sms_real_balance_error_keeps_cooldown(monkeypatch) -> None:
|
||||
|
||||
|
||||
def test_sms_gc_purges_stale_only(monkeypatch) -> None:
|
||||
"""GC 清过期码 / 旧冷却 / 隔日计数,但不动今天有效的(阈值设 0 强制每次扫)。"""
|
||||
"""GC 清过期码 / 旧冷却,但不动今天有效的(阈值设 0 强制每次扫)。"""
|
||||
monkeypatch.setattr(sms, "_GC_THRESHOLD", 0)
|
||||
sms._codes.clear()
|
||||
sms._last_sent.clear()
|
||||
sms._daily_count.clear()
|
||||
now = time.time()
|
||||
sms._codes["stale"] = sms._CodeRecord(code="111111", expires_at=now - 1)
|
||||
sms._codes["fresh"] = sms._CodeRecord(code="222222", expires_at=now + 999)
|
||||
sms._last_sent["old"] = now - 99999
|
||||
sms._last_sent["recent"] = now
|
||||
sms._daily_count["yesterday"] = ("2000-01-01", 3)
|
||||
sms._daily_count["today"] = (sms._today(), 1)
|
||||
|
||||
sms._gc(now)
|
||||
|
||||
assert "stale" not in sms._codes and "fresh" in sms._codes
|
||||
assert "old" not in sms._last_sent and "recent" in sms._last_sent
|
||||
assert "yesterday" not in sms._daily_count and "today" in sms._daily_count
|
||||
|
||||
|
||||
# ============================ 用户名 / 默认昵称 ============================
|
||||
|
||||
Reference in New Issue
Block a user