fix(auth): prevent competing remember-me token rotations (#39686) (#39688)

Partially backport #39686
This commit is contained in:
wxiaoguang authored and GitHub committed 2026-10-12 09:48:37 +00:00
1 parent 6064554b8d
commit 33aaf0cfa3
4 files changed
+54 -11

No files matched your search

+10 -4
View File
@@ -77,9 +77,9 @@ func prepareCommonAuthPageData(ctx *context.Context, opt CommonAuthOptions) {
// autoSignIn reads cookie and try to auto-login.
func autoSignIn(ctx *context.Context) (bool, error) {
isSucceed := false
keepRememberCookie := false
defer func() {
if !isSucceed {
if !keepRememberCookie {
ctx.DeleteSiteCookie(setting.CookieRememberName)
}
}()
@@ -112,14 +112,20 @@ func autoSignIn(ctx *context.Context) (bool, error) {
return false, fmt.Errorf("HasTwoFactorOrWebAuthn: %w", err)
}
isSucceed = true
nt, token, err := auth_service.RegenerateAuthToken(ctx, t)
if errors.Is(err, auth_service.ErrAuthTokenRotationConflict) {
keepRememberCookie = true // A late response must not clear the winner's cookie.
return false, nil
}
if errors.Is(err, auth_service.ErrAuthTokenExpired) {
return false, nil
}
if err != nil {
return false, err
}
ctx.SetSiteCookie(setting.CookieRememberName, nt.ID+":"+token, setting.LogInRememberDays*timeutil.Day)
keepRememberCookie = true
if err := regenerateSession(ctx, map[string]any{
session.KeyUID: u.ID,