fix(security): use secrets for verification code and atomic token rotation - #279
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
The verification-code change is sound, but the refresh-token rotation still has a concurrency flaw: the old token is checked with a standalone GET before the transaction. Two concurrent requests can both observe it as valid and each execute DELETE old + SET new, so the same refresh token can be redeemed more than once. The rotation needs an atomic compare-and-delete/consume step (for example, a Redis WATCH transaction or a Lua script that validates and rotates only if the old key still exists), with a regression test covering concurrent reuse.
…ation - Replace random.choices with secrets.choice for cryptographically secure verification code generation (prevents PRNG prediction attacks) - Use Redis Lua script for atomic check-delete-store token rotation (eliminates race condition where concurrent requests could both pass the GET check and each successfully rotate the same refresh token) - Add concurrent reuse regression test Closes 1024XEngineer#200
7ffed3e to
791b9fb
Compare
问题
用户认证流程中存在两个安全缺陷(#200):
random.choices使用 Mersenne Twister,攻击者若能观察足够多的输出可以预测后续验证码修复
1. 验证码生成改用
secretssecrets.choice使用操作系统级 CSPRNG,不可预测。2. Token 轮换改为原子操作
Redis pipeline 使用 MULTI/EXEC 保证两个操作在同一事务中执行,消除竞态窗口。
测试
secrets.choice生成的验证码格式不变(6 位数字)refresh_tokens()的返回值语义Closes #200