Repository navigation
fix(auth): consume refresh tokens atomically during rotation - #8637
Merged
Wei-Shaw merged 1 commit intoNov 10, 2026
Merged
Conversation
RefreshTokenPair read the token and deleted it in two Redis calls. Two concurrent requests with the same refresh token both passed the read and validation, so both could mint a new token pair, keeping a stolen token alive across rotations. Add an atomic ConsumeRefreshToken (Lua GET+DEL) and require rotation to win that consume. Losers get ErrRefreshTokenInvalid; Redis errors now fail closed instead of issuing a new pair without invalidating the old token.
Contributor
Author
|
I have read the CLA Document and I hereby sign the CLA |
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
问题
RefreshTokenPair现在用GetRefreshToken+DeleteRefreshToken两步完成轮转,Redis 操作不是原子的。两个并发请求可以都读到旧 token、都通过校验,然后各签发一套新 token(同一 family),同一个 refresh token 就被兑换了两次。修复
RefreshTokenCache新增ConsumeRefreshToken;GET+DEL;RefreshTokenPair必须成功消费旧 token 才继续签发新 token;ErrRefreshTokenInvalid;Redis 错误 →ErrServiceUnavailable(fail closed,不再在旧 token 未失效时发新 token)。测试
repository/refresh_token_cache_test.go:16 个 goroutine 并发消费同一个 token,只成功 1 次;service/auth_service_refresh_race_test.go:用屏障让 8 个请求都先读到旧 token,再同时进入消费阶段;旧实现 8 个全部成功(测试失败),新实现 1 个成功 + 7 个 invalid。本地验证:
go test -tags=unit ./internal/service/通过-race通过Closes #8638