Skip to content

Commit 41c0776

Browse files
lunny6543lafriks
authored
Fix captcha (#14488)
Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lauris BH <lauris@nix.lv>
1 parent 669ff8e commit 41c0776

File tree

9 files changed

+30
-49
lines changed

9 files changed

+30
-49
lines changed

modules/cache/cache.go

+1-19
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,6 @@ func newCache(cacheConfig setting.Cache) (mc.Cache, error) {
2727
})
2828
}
2929

30-
// Cache is the interface that operates the cache data.
31-
type Cache interface {
32-
// Put puts value into cache with key and expire time.
33-
Put(key string, val interface{}, timeout int64) error
34-
// Get gets cached value by given key.
35-
Get(key string) interface{}
36-
// Delete deletes cached value by given key.
37-
Delete(key string) error
38-
// Incr increases cached int-type value by given key as a counter.
39-
Incr(key string) error
40-
// Decr decreases cached int-type value by given key as a counter.
41-
Decr(key string) error
42-
// IsExist returns true if cached value exists.
43-
IsExist(key string) bool
44-
// Flush deletes all cached data.
45-
Flush() error
46-
}
47-
4830
// NewContext start cache service
4931
func NewContext() error {
5032
var err error
@@ -59,7 +41,7 @@ func NewContext() error {
5941
}
6042

6143
// GetCache returns the currently configured cache
62-
func GetCache() Cache {
44+
func GetCache() mc.Cache {
6345
return conn
6446
}
6547

modules/context/captcha.go

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package context
77
import (
88
"sync"
99

10+
"code.gitea.io/gitea/modules/cache"
1011
"code.gitea.io/gitea/modules/setting"
1112

1213
"gitea.com/go-chi/captcha"
@@ -21,6 +22,7 @@ func GetImageCaptcha() *captcha.Captcha {
2122
cpt = captcha.NewCaptcha(captcha.Options{
2223
SubURL: setting.AppSubURL,
2324
})
25+
cpt.Store = cache.GetCache()
2426
})
2527
return cpt
2628
}

modules/context/context.go

+11-27
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"code.gitea.io/gitea/models"
2424
"code.gitea.io/gitea/modules/auth/sso"
2525
"code.gitea.io/gitea/modules/base"
26+
mc "code.gitea.io/gitea/modules/cache"
2627
"code.gitea.io/gitea/modules/log"
2728
"code.gitea.io/gitea/modules/middlewares"
2829
"code.gitea.io/gitea/modules/setting"
@@ -499,23 +500,8 @@ func getCsrfOpts() CsrfOptions {
499500

500501
// Contexter initializes a classic context for a request.
501502
func Contexter() func(next http.Handler) http.Handler {
502-
rnd := templates.HTMLRenderer()
503-
504-
var c cache.Cache
505-
var err error
506-
if setting.CacheService.Enabled {
507-
c, err = cache.NewCacher(cache.Options{
508-
Adapter: setting.CacheService.Adapter,
509-
AdapterConfig: setting.CacheService.Conn,
510-
Interval: setting.CacheService.Interval,
511-
})
512-
if err != nil {
513-
panic(err)
514-
}
515-
}
516-
503+
var rnd = templates.HTMLRenderer()
517504
var csrfOpts = getCsrfOpts()
518-
//var flashEncryptionKey, _ = NewSecret()
519505

520506
return func(next http.Handler) http.Handler {
521507
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
@@ -524,7 +510,7 @@ func Contexter() func(next http.Handler) http.Handler {
524510
var link = setting.AppSubURL + strings.TrimSuffix(req.URL.EscapedPath(), "/")
525511
var ctx = Context{
526512
Resp: NewResponse(resp),
527-
Cache: c,
513+
Cache: mc.GetCache(),
528514
Locale: locale,
529515
Link: link,
530516
Render: rnd,
@@ -571,16 +557,14 @@ func Contexter() func(next http.Handler) http.Handler {
571557
}
572558
ctx.Resp.Before(func(resp ResponseWriter) {
573559
if flash := f.Encode(); len(flash) > 0 {
574-
if err == nil {
575-
middlewares.SetCookie(resp, "macaron_flash", flash, 0,
576-
setting.SessionConfig.CookiePath,
577-
middlewares.Domain(setting.SessionConfig.Domain),
578-
middlewares.HTTPOnly(true),
579-
middlewares.Secure(setting.SessionConfig.Secure),
580-
//middlewares.SameSite(opt.SameSite), FIXME: we need a samesite config
581-
)
582-
return
583-
}
560+
middlewares.SetCookie(resp, "macaron_flash", flash, 0,
561+
setting.SessionConfig.CookiePath,
562+
middlewares.Domain(setting.SessionConfig.Domain),
563+
middlewares.HTTPOnly(true),
564+
middlewares.Secure(setting.SessionConfig.Secure),
565+
//middlewares.SameSite(opt.SameSite), FIXME: we need a samesite config
566+
)
567+
return
584568
}
585569

586570
ctx.SetCookie("macaron_flash", "", -1,

modules/setting/cache.go

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ func newCacheService() {
6868

6969
if CacheService.Enabled {
7070
log.Info("Cache Service Enabled")
71+
} else {
72+
log.Warn("Cache Service Disabled so that captcha disabled too")
73+
// captcha depends on cache service
74+
Service.EnableCaptcha = false
7175
}
7276

7377
sec = Cfg.Section("cache.last_commit")

routers/routes/web.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ func WebRoutes() *web.Route {
161161

162162
mailer.InitMailRender(templates.Mailer())
163163

164-
r.Use(captcha.Captchaer(context.GetImageCaptcha()))
164+
if setting.Service.EnableCaptcha {
165+
r.Use(captcha.Captchaer(context.GetImageCaptcha()))
166+
}
165167
// Removed: toolbox.Toolboxer middleware will provide debug informations which seems unnecessary
166168
r.Use(context.Contexter())
167169
// Removed: SetAutoHead allow a get request redirect to head if get method is not exist

routers/user/auth.go

+5
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,7 @@ func LinkAccount(ctx *context.Context) {
747747
ctx.Data["Title"] = ctx.Tr("link_account")
748748
ctx.Data["LinkAccountMode"] = true
749749
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha && setting.Service.RequireExternalRegistrationCaptcha
750+
ctx.Data["Captcha"] = context.GetImageCaptcha()
750751
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
751752
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
752753
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
@@ -800,6 +801,7 @@ func LinkAccountPostSignIn(ctx *context.Context) {
800801
ctx.Data["LinkAccountModeSignIn"] = true
801802
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha && setting.Service.RequireExternalRegistrationCaptcha
802803
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
804+
ctx.Data["Captcha"] = context.GetImageCaptcha()
803805
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
804806
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
805807
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
@@ -885,6 +887,7 @@ func LinkAccountPostRegister(ctx *context.Context) {
885887
ctx.Data["LinkAccountModeRegister"] = true
886888
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha && setting.Service.RequireExternalRegistrationCaptcha
887889
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
890+
ctx.Data["Captcha"] = context.GetImageCaptcha()
888891
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
889892
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
890893
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
@@ -1063,6 +1066,7 @@ func SignUp(ctx *context.Context) {
10631066

10641067
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
10651068
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
1069+
ctx.Data["Captcha"] = context.GetImageCaptcha()
10661070
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
10671071
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
10681072
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
@@ -1083,6 +1087,7 @@ func SignUpPost(ctx *context.Context) {
10831087

10841088
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
10851089
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
1090+
ctx.Data["Captcha"] = context.GetImageCaptcha()
10861091
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
10871092
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
10881093
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey

routers/user/auth_openid.go

+2
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ func RegisterOpenID(ctx *context.Context) {
329329
ctx.Data["PageIsOpenIDRegister"] = true
330330
ctx.Data["EnableOpenIDSignUp"] = setting.Service.EnableOpenIDSignUp
331331
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
332+
ctx.Data["Captcha"] = context.GetImageCaptcha()
332333
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
333334
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
334335
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
@@ -360,6 +361,7 @@ func RegisterOpenIDPost(ctx *context.Context) {
360361
ctx.Data["EnableOpenIDSignUp"] = setting.Service.EnableOpenIDSignUp
361362
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
362363
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
364+
ctx.Data["Captcha"] = context.GetImageCaptcha()
363365
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
364366
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
365367
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey

templates/user/auth/signup_inner.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
{{if and .EnableCaptcha (eq .CaptchaType "image")}}
3838
<div class="inline field">
3939
<label></label>
40-
{{.Captcha.CreateHtml}}
40+
{{.Captcha.CreateHTML}}
4141
</div>
4242
<div class="required inline field {{if .Err_Captcha}}error{{end}}">
4343
<label for="captcha">{{.i18n.Tr "captcha"}}</label>

templates/user/auth/signup_openid_register.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
{{if and .EnableCaptcha (eq .CaptchaType "image")}}
2424
<div class="inline field">
2525
<label></label>
26-
{{.Captcha.CreateHtml}}
26+
{{.Captcha.CreateHTML}}
2727
</div>
2828
<div class="required inline field {{if .Err_Captcha}}error{{end}}">
2929
<label for="captcha">{{.i18n.Tr "captcha"}}</label>

0 commit comments

Comments
 (0)