Repository navigation
Fix #23: rate-limit and lock out repeated failed logins - #37
Open
xiaoyao-666-coder wants to merge 2 commits into
Open
xiaoyao-666-coder wants to merge 2 commits into
xiaoyao-666-coder wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
登录接口无任何限流/锁定,可无限爆破:同一账号连续错误密码 40 次仍返回 401,无任何延迟、锁定或 429;换不存在的账号也一样。对一个已知用户名 + 密码字典可无限尝试,失败响应时间恒定,弱口令账号在“无限次 + 恒定成本”下没有保护。(注:issue 描述有两处与代码不符,以代码为准——
auth.py里没有time.sleep(0.1),方法名是verify_credentials不是authenticate;main.py里也不存在 issue 所说的全局限流中间件,只有 CORS 和防缓存中间件,登录之前是完全无上限的。)修复
backend/Dockerfile无--workers),进程内状态即覆盖整个部署;Redis 在本项目是可选的(REDIS_URL可缺省、会话存储会回退内存),把登录安全绑在它上面会在 Redis 宕机或未配置时 fail-open。代价是重启后锁定状态丢失,验收标准允许这一点;将来若改多 worker 部署,需换共享存储。MEDGUIDE_LOGIN_MAX_FAILED_ATTEMPTS、MEDGUIDE_LOGIN_MAX_FAILED_ATTEMPTS_PER_IP、MEDGUIDE_LOGIN_ATTEMPT_WINDOW_SECONDS、MEDGUIDE_LOGIN_LOCKOUT_SECONDS,越界值钳制不报错),未引入新依赖、未改 compose。Retry-After头:锁定期间直接拒绝,文案统一为“登录尝试过于频繁,请稍后再试”,已知/未知账号完全相同,不透露账号是否存在。verify_credentials本来就对未知用户用 dummy salt/hash 跑满 PBKDF2,失败耗时与已知账号一致。_trusted_proxy_networks())时才取X-Forwarded-For首项,否则用直连方地址——部署拓扑是浏览器 → Caddy → UI nginx → API,两级代理都会追加 XFF,首项即真实浏览器 IP;直连客户端伪造 XFF 既逃不掉限流也锁不到别人。验证
MEDGUIDE_MODE=test python -m pytest -q backend/tests534 passed(基线 521 + 13 新)。覆盖:账号锁定与过期(假时钟)、锁定期间正确密码也被拒、成功登录清零计数、未知账号同等锁定、分账号隔离、IP 维度跨账号锁定、失败耗时不泄露账号存在(两者均 ≥5ms、比值 <3、状态与 body 完全相同)、XFF 仅信任可信代理、锁定不延长、key 上限、畸形用户名不崩。MEDGUIDE_MODE=test+ 临时 auth 库,临时账号):注册 201;连续 10 次错密码 →[401, 401, 401, 401, 401, 429, 429, 429, 429, 429];锁定期间正确密码 → 429、{"detail": "登录尝试过于频繁,请稍后再试"}、Retry-After: 900;未知账号 7 次 →[401×5, 429, 429];另一账号注册 201、登录 200——与 round-2 复现的“40 次仍全是 401”对比,爆破已被挡住。服务器已停,临时库已删,未动任何真实账号。已知遗留
Closes #23