Skip to content

Commit 468387e

Browse files
authored
Prevent NPE when cache service is disabled (#19703)
The cache service can be disabled - at which point ctx.Cache will be nil and the use of it will cause an NPE. The main part of this PR is that the cache is used for restricting resending of activation mails and without this we cache we cannot restrict this. Whilst this code could be re-considered to use the db and probably should be, I think we can simply disable this code in the case that the cache is disabled. There are also several bug fixes in the /nodeinfo API endpoint. Signed-off-by: Andrew Thornton <[email protected]>
1 parent bc4764f commit 468387e

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

routers/web/auth/auth.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,10 @@ func handleUserCreated(ctx *context.Context, u *user_model.User, gothUser *goth.
632632
ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
633633
ctx.HTML(http.StatusOK, TplActivate)
634634

635-
if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
636-
log.Error("Set cache(MailResendLimit) fail: %v", err)
635+
if setting.CacheService.Enabled {
636+
if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
637+
log.Error("Set cache(MailResendLimit) fail: %v", err)
638+
}
637639
}
638640
return
639641
}
@@ -653,14 +655,16 @@ func Activate(ctx *context.Context) {
653655
}
654656
// Resend confirmation email.
655657
if setting.Service.RegisterEmailConfirm {
656-
if ctx.Cache.IsExist("MailResendLimit_" + ctx.Doer.LowerName) {
658+
if setting.CacheService.Enabled && ctx.Cache.IsExist("MailResendLimit_"+ctx.Doer.LowerName) {
657659
ctx.Data["ResendLimited"] = true
658660
} else {
659661
ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
660662
mailer.SendActivateAccountMail(ctx.Locale, ctx.Doer)
661663

662-
if err := ctx.Cache.Put("MailResendLimit_"+ctx.Doer.LowerName, ctx.Doer.LowerName, 180); err != nil {
663-
log.Error("Set cache(MailResendLimit) fail: %v", err)
664+
if setting.CacheService.Enabled {
665+
if err := ctx.Cache.Put("MailResendLimit_"+ctx.Doer.LowerName, ctx.Doer.LowerName, 180); err != nil {
666+
log.Error("Set cache(MailResendLimit) fail: %v", err)
667+
}
664668
}
665669
}
666670
} else {
@@ -789,7 +793,7 @@ func ActivateEmail(ctx *context.Context) {
789793

790794
if u, err := user_model.GetUserByID(email.UID); err != nil {
791795
log.Warn("GetUserByID: %d", email.UID)
792-
} else {
796+
} else if setting.CacheService.Enabled {
793797
// Allow user to validate more emails
794798
_ = ctx.Cache.Delete("MailResendLimit_" + u.LowerName)
795799
}

routers/web/auth/password.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,18 @@ func ForgotPasswdPost(ctx *context.Context) {
7979
return
8080
}
8181

82-
if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) {
82+
if setting.CacheService.Enabled && ctx.Cache.IsExist("MailResendLimit_"+u.LowerName) {
8383
ctx.Data["ResendLimited"] = true
8484
ctx.HTML(http.StatusOK, tplForgotPassword)
8585
return
8686
}
8787

8888
mailer.SendResetPasswordMail(u)
8989

90-
if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
91-
log.Error("Set cache(MailResendLimit) fail: %v", err)
90+
if setting.CacheService.Enabled {
91+
if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
92+
log.Error("Set cache(MailResendLimit) fail: %v", err)
93+
}
9294
}
9395

9496
ctx.Data["ResetPwdCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale.Language())

routers/web/user/setting/account.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func EmailPost(ctx *context.Context) {
105105
// Send activation Email
106106
if ctx.FormString("_method") == "SENDACTIVATION" {
107107
var address string
108-
if ctx.Cache.IsExist("MailResendLimit_" + ctx.Doer.LowerName) {
108+
if setting.CacheService.Enabled && ctx.Cache.IsExist("MailResendLimit_"+ctx.Doer.LowerName) {
109109
log.Error("Send activation: activation still pending")
110110
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
111111
return
@@ -141,8 +141,10 @@ func EmailPost(ctx *context.Context) {
141141
}
142142
address = email.Email
143143

144-
if err := ctx.Cache.Put("MailResendLimit_"+ctx.Doer.LowerName, ctx.Doer.LowerName, 180); err != nil {
145-
log.Error("Set cache(MailResendLimit) fail: %v", err)
144+
if setting.CacheService.Enabled {
145+
if err := ctx.Cache.Put("MailResendLimit_"+ctx.Doer.LowerName, ctx.Doer.LowerName, 180); err != nil {
146+
log.Error("Set cache(MailResendLimit) fail: %v", err)
147+
}
146148
}
147149
ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", address, timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())))
148150
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
@@ -201,8 +203,10 @@ func EmailPost(ctx *context.Context) {
201203
// Send confirmation email
202204
if setting.Service.RegisterEmailConfirm {
203205
mailer.SendActivateEmailMail(ctx.Doer, email)
204-
if err := ctx.Cache.Put("MailResendLimit_"+ctx.Doer.LowerName, ctx.Doer.LowerName, 180); err != nil {
205-
log.Error("Set cache(MailResendLimit) fail: %v", err)
206+
if setting.CacheService.Enabled {
207+
if err := ctx.Cache.Put("MailResendLimit_"+ctx.Doer.LowerName, ctx.Doer.LowerName, 180); err != nil {
208+
log.Error("Set cache(MailResendLimit) fail: %v", err)
209+
}
206210
}
207211
ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", email.Email, timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())))
208212
} else {
@@ -273,7 +277,7 @@ func loadAccountData(ctx *context.Context) {
273277
user_model.EmailAddress
274278
CanBePrimary bool
275279
}
276-
pendingActivation := ctx.Cache.IsExist("MailResendLimit_" + ctx.Doer.LowerName)
280+
pendingActivation := setting.CacheService.Enabled && ctx.Cache.IsExist("MailResendLimit_"+ctx.Doer.LowerName)
277281
emails := make([]*UserEmail, len(emlist))
278282
for i, em := range emlist {
279283
var email UserEmail

0 commit comments

Comments
 (0)