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 <wxiaoguang@gmail.com>
This commit is contained in:
Dickson Wuandwxiaoguang authored and GitHub committed 2026-11-30 16:06:39 +00:00
1 parent 95032ff039
commit 6ab12548be
5 files changed
+72 -67

No files matched your search

+26 -11
View File
@@ -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 {
+1 -2
View File
@@ -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.",
+21 -30
View File
@@ -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) {
+10 -24
View File
@@ -1,35 +1,21 @@
{{template "base/head" .}}
<div role="main" aria-label="{{.Title}}" class="page-content user forgot password">
<div role="main" aria-label="{{.Title}}" class="page-content">
<div class="ui middle very relaxed page grid">
<div class="column">
<div class="column tw-max-w-2xl tw-m-auto">
<form class="ui form ignore-dirty" action="{{.Link}}" method="post">
<h2 class="ui top attached header">
{{ctx.Locale.Tr "auth.forgot_password_title"}}
</h2>
<div class="ui attached segment">
{{template "base/alert" .}}
{{if .IsResetSent}}
<p>{{ctx.Locale.Tr "auth.reset_password_mail_sent_prompt" .Email .ResetPwdCodeLives}}</p>
{{else if .IsResetRequest}}
<div class="required field {{if .Err_Email}}error{{end}}">
<label for="email">{{ctx.Locale.Tr "email"}}</label>
<input id="email" name="email" type="email" value="{{.Email}}" autofocus required>
</div>
<div class="divider"></div>
<div class="inline field">
<button type="submit" class="ui primary button">{{ctx.Locale.Tr "auth.send_reset_mail"}}</button>
</div>
{{else if .IsResetDisable}}
<p class="center">
{{if $.IsAdmin}}
{{ctx.Locale.Tr "auth.disable_forgot_password_mail_admin"}}
{{else}}
{{ctx.Locale.Tr "auth.disable_forgot_password_mail"}}
{{end}}
</p>
{{else if .ResendLimited}}
<p class="center">{{ctx.Locale.Tr "auth.resent_limit_prompt"}}</p>
{{end}}
<div class="required field">
<label>{{ctx.Locale.Tr "email"}}</label>
<input name="email" type="email" value="{{.Email}}" autofocus required>
</div>
<div class="divider"></div>
<div class="inline field">
<button type="submit" class="ui primary button">{{ctx.Locale.Tr "auth.send_reset_mail"}}</button>
</div>
</div>
</form>
</div>
@@ -0,0 +1,14 @@
{{template "base/head" .}}
<div role="main" aria-label="{{.Title}}" class="page-content">
<div class="ui middle very relaxed page grid">
<div class="column tw-max-w-2xl tw-m-auto">
<h2 class="ui top attached header">
{{ctx.Locale.Tr "auth.forgot_password_title"}}
</h2>
<div class="ui attached segment">
<p>{{.ForgotPasswordMessage}}</p>
</div>
</div>
</div>
</div>
{{template "base/footer" .}}