Skip to content

Commit 0821c09

Browse files
committed
Use ctx.RemoteAddr() to get the real ip instead of getting it from the http header
Signed-off-by: ByLCY <[email protected]>
1 parent 59186ab commit 0821c09

File tree

6 files changed

+6
-11
lines changed

6 files changed

+6
-11
lines changed

custom/conf/app.example.ini

-1
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,6 @@ ROUTER = console
790790
;; Go to https://dash.cloudflare.com/?to=/:account/turnstile to sign up for a key
791791
;CF_TURNSTILE_SITEKEY =
792792
;CF_TURNSTILE_SECRET =
793-
;CF_REVERSE_PROXY_HEADER =
794793
;;
795794
;; Default value for KeepEmailPrivate
796795
;; Each new user will get the value of this setting copied into their profile

docs/content/doc/advanced/config-cheat-sheet.en-us.md

-1
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,6 @@ Certain queues have defaults that override the defaults set in `[queue]` (this o
655655
- `MCAPTCHA_URL` **https://demo.mcaptcha.org/**: Set the mCaptcha URL.
656656
- `CF_TURNSTILE_SECRET` **""**: Go to https://dash.cloudflare.com/?to=/:account/turnstile to get a secret for cloudflare turnstile.
657657
- `CF_TURNSTILE_SITEKEY` **""**: Go to https://dash.cloudflare.com/?to=/:account/turnstile to get a sitekey for cloudflare turnstile.
658-
- `CF_REVERSE_PROXY_HEADER` **""**: The http header where the user's real ip is located. Otherwise it should be `""`.
659658
- `DEFAULT_KEEP_EMAIL_PRIVATE`: **false**: By default set users to keep their email address private.
660659
- `DEFAULT_ALLOW_CREATE_ORGANIZATION`: **true**: Allow new users to create organizations by default.
661660
- `DEFAULT_USER_IS_RESTRICTED`: **false**: Give new users restricted permissions by default

docs/content/doc/advanced/config-cheat-sheet.zh-cn.md

-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ menu:
158158
- `MCAPTCHA_URL` **https://demo.mcaptcha.org/**: 设置 remCaptchacaptcha 的 url 。
159159
- `CF_TURNSTILE_SECRET` **""**: cloudlfare turnstile 服务的密钥,可在 https://dash.cloudflare.com/?to=/:account/turnstile 获取。
160160
- `CF_TURNSTILE_SITEKEY` **""**: cloudlfare turnstile 服务的网站密钥 ,可在 https://www.google.com/recaptcha/admin 获取。
161-
- `CF_REVERSE_PROXY_HEADER` **""**: http 的 header 字段,用于获取客户端的 ip 供 cloudflare turnstile 验证时使用。如果没有反向代理设置这里应设置为 `""`
162161

163162
### Service - Expore (`service.explore`)
164163

modules/context/captcha.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package context
55

66
import (
77
"fmt"
8+
"net"
89
"sync"
910

1011
"code.gitea.io/gitea/modules/base"
@@ -78,10 +79,9 @@ func VerifyCaptcha(ctx *Context, tpl base.TplName, form interface{}) {
7879
valid, err = mcaptcha.Verify(ctx, ctx.Req.Form.Get(mCaptchaResponseField))
7980
case setting.CfTurnstile:
8081
var ip string
81-
if setting.Service.CfReverseProxyHeader == "" {
82-
ip = ctx.RemoteAddr()
83-
} else {
84-
ip = ctx.Req.Header.Get(setting.Service.CfReverseProxyHeader)
82+
ip, _, err = net.SplitHostPort(ctx.RemoteAddr())
83+
if err != nil {
84+
break
8585
}
8686
valid, err = turnstile.Verify(ctx, ctx.Req.Form.Get(cfTurnstileResponseField), ip)
8787
default:

modules/setting/service.go

-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ var Service = struct {
4848
RecaptchaURL string
4949
CfTurnstileSecret string
5050
CfTurnstileSitekey string
51-
CfReverseProxyHeader string
5251
HcaptchaSecret string
5352
HcaptchaSitekey string
5453
McaptchaSecret string
@@ -142,7 +141,6 @@ func newService() {
142141
Service.RecaptchaURL = sec.Key("RECAPTCHA_URL").MustString("https://www.google.com/recaptcha/")
143142
Service.CfTurnstileSecret = sec.Key("CF_TURNSTILE_SECRET").MustString("")
144143
Service.CfTurnstileSitekey = sec.Key("CF_TURNSTILE_SITEKEY").MustString("")
145-
Service.CfReverseProxyHeader = sec.Key("CF_REVERSE_PROXY_HEADER").MustString("")
146144
Service.HcaptchaSecret = sec.Key("HCAPTCHA_SECRET").MustString("")
147145
Service.HcaptchaSitekey = sec.Key("HCAPTCHA_SITEKEY").MustString("")
148146
Service.McaptchaURL = sec.Key("MCAPTCHA_URL").MustString("https://demo.mcaptcha.org/")

modules/turnstile/turnstile.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ func Verify(ctx context.Context, response, ip string) (bool, error) {
5252
}
5353

5454
var jsonResponse Response
55-
err = json.Unmarshal(body, &jsonResponse)
56-
if err != nil {
55+
if err := json.Unmarshal(body, &jsonResponse); err != nil {
5756
return false, fmt.Errorf("Failed to parse CAPTCHA response: %s", err)
5857
}
58+
5959
var respErr error
6060
if len(jsonResponse.ErrorCodes) > 0 {
6161
respErr = jsonResponse.ErrorCodes[0]

0 commit comments

Comments
 (0)