From 6ab12548beceeb8eed1daaeb7ec666d554c25efc Mon Sep 17 00:00:00 2001 From: Dickson Wu <33645481+DicksonWu654@users.noreply.github.com> Date: Mon, 30 Nov 2026 16:06:39 +0000 Subject: [PATCH] fix: show the same forgot-password response while the resend limit is active (#40579) The forgot-password page answers an unknown email with the "reset mail sent" confirmation, which is the right call for not revealing accounts. But for an existing local account, a second request inside the 180 second `MailResendLimit_` window sets `ResendLimited`, and because `IsResetRequest` is already set the template renders the reset form again instead of the confirmation. So an anonymous client can send an address twice and tell from the second response whether it belongs to an account. It needs the mail service enabled, nothing else. This makes the resend-limited branch set the same `IsResetSent` / `ResetPwdCodeLives` data as the normal and unknown-user paths. The resend limit still holds, so no extra mail goes out. It's the same kind of difference #36002 removed from the login errors (#35984), just on the reset page. Tested on a local instance with SQLite and a dummy mailer: on v28.4.0 and on main the repeat request showed the form while an unknown address showed the confirmation; with this change all of them show the confirmation. The `routers/web/auth` package tests pass on main and `release/v28` with the change. It applies to `release/v28` too if you want it backported. I used AI tools to help find this and draft the change; I've gone over the diff and the code path myself. --------- Co-authored-by: wxiaoguang --- models/user/user.go | 37 +++++++++++----- options/locale/locale_en-US.json | 3 +- routers/web/auth/password.go | 51 +++++++++------------- templates/user/auth/forgot_passwd.tmpl | 34 +++++---------- templates/user/auth/forgot_passwd_msg.tmpl | 14 ++++++ 5 files changed, 72 insertions(+), 67 deletions(-) create mode 100644 templates/user/auth/forgot_passwd_msg.tmpl diff --git a/models/user/user.go b/models/user/user.go index 7eba2a359d8..25edbb37087 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -1052,8 +1052,16 @@ func GetInactiveUsers(ctx context.Context, olderThan time.Duration) ([]*User, er // GetUserByID returns the user object by given ID if exists. TODO: for most cases, this function should be avoided. // Use correct GetXxxUser functions instead to handle user types & states correctly. func GetUserByID(ctx context.Context, id int64) (*User, error) { - u := new(User) - has, err := db.GetEngine(ctx).ID(id).Get(u) + return getUserByIDAndTypes(ctx, id) +} + +func getUserByIDAndTypes(ctx context.Context, id int64, types ...UserType) (*User, error) { + u := &User{} + sess := db.GetEngine(ctx).ID(id) + if len(types) > 0 { + sess = sess.In("`type`", types) + } + has, err := sess.Get(u) if err != nil { return nil, err } else if !has { @@ -1392,34 +1400,41 @@ func parseLocalPartToNameID(localPart string) (string, int64) { return name, id } -// GetUserByEmail returns the user object by given e-mail if exists. func GetUserByEmail(ctx context.Context, email string) (*User, error) { - if len(email) == 0 { - return nil, ErrUserNotExist{Name: email} - } + return getUserByEmailAndTypes(ctx, email, UserTypeIndividual, UserTypeBot) +} +func getUserByEmailAndTypes(ctx context.Context, email string, types ...UserType) (*User, error) { + if email == "" { + return nil, ErrUserNotExist{} + } email = strings.ToLower(email) - // Otherwise, check in alternative list for activated email addresses + + // check in activated email addresses emailAddress, has, err := db.Get[EmailAddress](ctx, builder.Eq{"lower_email": email, "is_activated": true}) if err != nil { return nil, err } if has { - return GetUserByID(ctx, emailAddress.UID) + return getUserByIDAndTypes(ctx, emailAddress.UID, types...) } - // Finally, if email address is the protected email address: + // if email address is the protected email address: if localPart, ok := strings.CutSuffix(email, strings.ToLower("@"+setting.Service.NoReplyAddress)); ok { name, id := parseLocalPartToNameID(localPart) if id != 0 { - return GetUserByID(ctx, id) + return getUserByIDAndTypes(ctx, id, types...) } - return GetIndividualUserByName(ctx, name) + return getUserByNameWithTypes(ctx, name, types...) } return nil, ErrUserNotExist{Name: email} } +func GetIndividualUserByEmail(ctx context.Context, email string) (*User, error) { + return getUserByEmailAndTypes(ctx, email, UserTypeIndividual) +} + func GetIndividualUserByPrimaryEmail(ctx context.Context, email string) (*User, error) { email = strings.ToLower(strings.TrimSpace(email)) if len(email) == 0 { diff --git a/options/locale/locale_en-US.json b/options/locale/locale_en-US.json index 9dafaad4f88..d30f441d40a 100644 --- a/options/locale/locale_en-US.json +++ b/options/locale/locale_en-US.json @@ -421,8 +421,7 @@ "auth.oauth.signin.error.access_denied": "The authorization request was denied.", "auth.oauth.signin.error.temporarily_unavailable": "Authorization failed because the authentication server is temporarily unavailable. Please try again later.", "auth.oauth_callback_unable_auto_reg": "Auto Registration is enabled, but OAuth2 Provider %[1]s returned missing fields: %[2]s, unable to create an account automatically. Please create or link to an account, or contact the site administrator.", - "auth.disable_forgot_password_mail": "Account recovery is disabled because no email address is set up. Please contact your site administrator.", - "auth.disable_forgot_password_mail_admin": "Account recovery is only available when an email address is set up.", + "auth.disable_forgot_password_mail": "Account recovery is disabled because no mailer service is configured on this server. Please contact your site administrator.", "auth.email_domain_blacklisted": "You cannot register with your email address.", "auth.authorize_application": "Authorize Application", "auth.authorize_redirect_notice": "You will be redirected to %s if you authorize this application.", diff --git a/routers/web/auth/password.go b/routers/web/auth/password.go index 93025356cc8..ffa5ec52e3a 100644 --- a/routers/web/auth/password.go +++ b/routers/web/auth/password.go @@ -5,6 +5,7 @@ package auth import ( "errors" + "html/template" "net/http" audit_model "gitea.dev/models/audit" @@ -29,65 +30,57 @@ var ( // tplMustChangePassword template for updating a user's password tplMustChangePassword templates.TplName = "user/auth/change_passwd" tplForgotPassword templates.TplName = "user/auth/forgot_passwd" + tplForgotPasswordMsg templates.TplName = "user/auth/forgot_passwd_msg" tplResetPassword templates.TplName = "user/auth/reset_passwd" ) -// ForgotPasswd render the forget password page +func renderForgotPasswordMessage(ctx *context.Context, msg template.HTML) { + ctx.Data["ForgotPasswordMessage"] = msg + ctx.HTML(http.StatusOK, tplForgotPasswordMsg) +} + func ForgotPasswd(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("auth.forgot_password_title") if setting.MailService == nil { - log.Warn("no mail service configured") - ctx.Data["IsResetDisable"] = true - ctx.HTML(http.StatusOK, tplForgotPassword) + msg := ctx.Tr("auth.disable_forgot_password_mail") + renderForgotPasswordMessage(ctx, msg) return } ctx.Data["Email"] = ctx.FormString("email") - - ctx.Data["IsResetRequest"] = true ctx.HTML(http.StatusOK, tplForgotPassword) } -// ForgotPasswdPost response for forget password request func ForgotPasswdPost(ctx *context.Context) { - // TODO: REFACTOR-RENDER-WITH-ERR: form-fetch-action needs navigation for the sent-email and resend-limit views currently rendered by this POST. - ctx.Data["Title"] = ctx.Tr("auth.forgot_password_title") - if setting.MailService == nil { ctx.NotFound() return } - ctx.Data["IsResetRequest"] = true email := ctx.FormString("email") + + ctx.Data["Title"] = ctx.Tr("auth.forgot_password_title") ctx.Data["Email"] = email + msg := ctx.Tr("auth.reset_password_mail_sent_prompt", email, timeutil.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale)) - u, err := user_model.GetUserByEmail(ctx, email) - if err == nil && !u.IsIndividual() { - err = user_model.ErrUserNotExist{} - } - if err != nil { - if user_model.IsErrUserNotExist(err) { - ctx.Data["ResetPwdCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale) - ctx.Data["IsResetSent"] = true - ctx.HTML(http.StatusOK, tplForgotPassword) - return - } - + u, err := user_model.GetIndividualUserByEmail(ctx, email) + if errors.Is(err, util.ErrNotExist) { + renderForgotPasswordMessage(ctx, msg) + return + } else if err != nil { ctx.ServerErrorAuto(err) return } if !u.IsLocal() && !u.IsOAuth2() { - ctx.Data["Err_Email"] = true - ctx.RenderWithErrDeprecated(ctx.Tr("auth.non_local_account"), tplForgotPassword, nil) + ctx.Flash.Error(ctx.Tr("auth.non_local_account"), true) + ctx.HTML(http.StatusOK, tplForgotPassword) return } if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) { - ctx.Data["ResendLimited"] = true - ctx.HTML(http.StatusOK, tplForgotPassword) + renderForgotPasswordMessage(ctx, msg) return } @@ -101,9 +94,7 @@ func ForgotPasswdPost(ctx *context.Context) { log.Error("Set cache(MailResendLimit) fail: %v", err) } - ctx.Data["ResetPwdCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale) - ctx.Data["IsResetSent"] = true - ctx.HTML(http.StatusOK, tplForgotPassword) + renderForgotPasswordMessage(ctx, msg) } func commonResetPassword(ctx *context.Context) (*user_model.User, *auth.TwoFactor) { diff --git a/templates/user/auth/forgot_passwd.tmpl b/templates/user/auth/forgot_passwd.tmpl index 7a710cd4262..f65fa941ba3 100644 --- a/templates/user/auth/forgot_passwd.tmpl +++ b/templates/user/auth/forgot_passwd.tmpl @@ -1,35 +1,21 @@ {{template "base/head" .}} -
+
-
+

{{ctx.Locale.Tr "auth.forgot_password_title"}}

{{template "base/alert" .}} - {{if .IsResetSent}} -

{{ctx.Locale.Tr "auth.reset_password_mail_sent_prompt" .Email .ResetPwdCodeLives}}

- {{else if .IsResetRequest}} -
- - -
-
-
- -
- {{else if .IsResetDisable}} -

- {{if $.IsAdmin}} - {{ctx.Locale.Tr "auth.disable_forgot_password_mail_admin"}} - {{else}} - {{ctx.Locale.Tr "auth.disable_forgot_password_mail"}} - {{end}} -

- {{else if .ResendLimited}} -

{{ctx.Locale.Tr "auth.resent_limit_prompt"}}

- {{end}} +
+ + +
+
+
+ +
diff --git a/templates/user/auth/forgot_passwd_msg.tmpl b/templates/user/auth/forgot_passwd_msg.tmpl new file mode 100644 index 00000000000..ff0ec6a2bcc --- /dev/null +++ b/templates/user/auth/forgot_passwd_msg.tmpl @@ -0,0 +1,14 @@ +{{template "base/head" .}} +
+
+
+

+ {{ctx.Locale.Tr "auth.forgot_password_title"}} +

+
+

{{.ForgotPasswordMessage}}

+
+
+
+
+{{template "base/footer" .}}