Skip to content

Commit ccc11c1

Browse files
zeripathlafriks
andauthored
Prevent NPE when cache service is disabled (#19703) (#19783)
Backport #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. Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: Lauris BH <[email protected]>
1 parent 336e1ac commit ccc11c1

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

routers/web/auth/auth.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ func SignUp(ctx *context.Context) {
417417
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
418418
ctx.Data["PageIsSignUp"] = true
419419

420-
//Show Disabled Registration message if DisableRegistration or AllowOnlyExternalRegistration options are true
420+
// Show Disabled Registration message if DisableRegistration or AllowOnlyExternalRegistration options are true
421421
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration || setting.Service.AllowOnlyExternalRegistration
422422

423423
ctx.HTML(http.StatusOK, tplSignUp)
@@ -438,7 +438,7 @@ func SignUpPost(ctx *context.Context) {
438438
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
439439
ctx.Data["PageIsSignUp"] = true
440440

441-
//Permission denied if DisableRegistration or AllowOnlyExternalRegistration options are true
441+
// Permission denied if DisableRegistration or AllowOnlyExternalRegistration options are true
442442
if setting.Service.DisableRegistration || setting.Service.AllowOnlyExternalRegistration {
443443
ctx.Error(http.StatusForbidden)
444444
return
@@ -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,15 @@ func Activate(ctx *context.Context) {
653655
}
654656
// Resend confirmation email.
655657
if setting.Service.RegisterEmailConfirm {
656-
if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) {
658+
if setting.CacheService.Enabled && ctx.Cache.IsExist("MailResendLimit_"+ctx.User.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.User)
661-
662-
if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
663-
log.Error("Set cache(MailResendLimit) fail: %v", err)
663+
if setting.CacheService.Enabled {
664+
if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
665+
log.Error("Set cache(MailResendLimit) fail: %v", err)
666+
}
664667
}
665668
}
666669
} else {
@@ -789,7 +792,7 @@ func ActivateEmail(ctx *context.Context) {
789792

790793
if u, err := user_model.GetUserByID(email.UID); err != nil {
791794
log.Warn("GetUserByID: %d", email.UID)
792-
} else {
795+
} else if setting.CacheService.Enabled {
793796
// Allow user to validate more emails
794797
_ = ctx.Cache.Delete("MailResendLimit_" + u.LowerName)
795798
}

routers/web/auth/password.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,18 @@ func ForgotPasswdPost(ctx *context.Context) {
8080
return
8181
}
8282

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

8989
mailer.SendResetPasswordMail(u)
9090

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

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

routers/web/user/setting/account.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func EmailPost(ctx *context.Context) {
106106
// Send activation Email
107107
if ctx.FormString("_method") == "SENDACTIVATION" {
108108
var address string
109-
if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) {
109+
if setting.CacheService.Enabled && ctx.Cache.IsExist("MailResendLimit_"+ctx.User.LowerName) {
110110
log.Error("Send activation: activation still pending")
111111
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
112112
return
@@ -142,8 +142,10 @@ func EmailPost(ctx *context.Context) {
142142
}
143143
address = email.Email
144144

145-
if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
146-
log.Error("Set cache(MailResendLimit) fail: %v", err)
145+
if setting.CacheService.Enabled {
146+
if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
147+
log.Error("Set cache(MailResendLimit) fail: %v", err)
148+
}
147149
}
148150
ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", address, timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())))
149151
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
@@ -202,8 +204,10 @@ func EmailPost(ctx *context.Context) {
202204
// Send confirmation email
203205
if setting.Service.RegisterEmailConfirm {
204206
mailer.SendActivateEmailMail(ctx.User, email)
205-
if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
206-
log.Error("Set cache(MailResendLimit) fail: %v", err)
207+
if setting.CacheService.Enabled {
208+
if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
209+
log.Error("Set cache(MailResendLimit) fail: %v", err)
210+
}
207211
}
208212
ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", email.Email, timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())))
209213
} else {
@@ -271,7 +275,7 @@ func loadAccountData(ctx *context.Context) {
271275
user_model.EmailAddress
272276
CanBePrimary bool
273277
}
274-
pendingActivation := ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName)
278+
pendingActivation := setting.CacheService.Enabled && ctx.Cache.IsExist("MailResendLimit_"+ctx.User.LowerName)
275279
emails := make([]*UserEmail, len(emlist))
276280
for i, em := range emlist {
277281
var email UserEmail

0 commit comments

Comments
 (0)