Skip to content

Commit 82c735e

Browse files
committed
Use single option to specify networks and proxy IP addresses. By default trust all loopback IPs
1 parent 4a52338 commit 82c735e

File tree

4 files changed

+13
-17
lines changed

4 files changed

+13
-17
lines changed

custom/conf/app.example.ini

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,10 +551,8 @@ REVERSE_PROXY_AUTHENTICATION_USER = X-WEBAUTH-USER
551551
REVERSE_PROXY_AUTHENTICATION_EMAIL = X-WEBAUTH-EMAIL
552552
; Interpret X-Forwarded-For header or the X-Real-IP header and set this as the remote IP for the request
553553
REVERSE_PROXY_LIMIT = 1
554-
; List of IP addresses seperated by comma of trusted proxy servers. Use `*` to trust all.
555-
REVERSE_PROXY_TRUSTED_PROXIES = 127.0.0.1
556-
; List of network addresses seperated by comma of trusted proxy servers. Example `10.0.0.0/24`.
557-
REVERSE_PROXY_TRUSTED_NETWORKS =
554+
; List of IP addresses and networks seperated by comma of trusted proxy servers. Use `*` to trust all.
555+
REVERSE_PROXY_TRUSTED_PROXIES = 127.0.0.0/8,::1/128
558556
; The minimum password length for new Users
559557
MIN_PASSWORD_LENGTH = 6
560558
; Set to true to allow users to import local server paths

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,7 @@ relation to port exhaustion.
392392
authentication provided email.
393393
- `REVERSE_PROXY_LIMIT`: **1**: Interpret X-Forwarded-For header or the X-Real-IP header and set this as the remote IP for the request.
394394
Number of trusted proxy count. Set to zero to not use these headers.
395-
- `REVERSE_PROXY_TRUSTED_PROXIES`: **127.0.0.1**: List of IP addresses separated by comma of trusted proxy servers. Use `*` to trust all.
396-
- `REVERSE_PROXY_TRUSTED_NETWORKS`: **<empty>**: List of network addresses separated by comma of trusted proxy servers. Example `10.0.0.0/24`.
395+
- `REVERSE_PROXY_TRUSTED_PROXIES`: **127.0.0.0/8,::1/128**: List of IP addresses and networks separated by comma of trusted proxy servers. Use `*` to trust all.
397396
- `DISABLE_GIT_HOOKS`: **true**: Set to `false` to enable users with git hook privilege to create custom git hooks.
398397
WARNING: Custom git hooks can be used to perform arbitrary code execution on the host operating system.
399398
This enables the users to access and modify this config file and the Gitea database and interrupt the Gitea service.

modules/setting/setting.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,7 @@ var (
170170
ReverseProxyAuthUser string
171171
ReverseProxyAuthEmail string
172172
ReverseProxyLimit int
173-
ReverseProxyTrustedIPAddr []string
174-
ReverseProxyTrustedNet []string
173+
ReverseProxyTrustedProxies []string
175174
MinPasswordLength int
176175
ImportLocalPaths bool
177176
DisableGitHooks bool
@@ -827,11 +826,10 @@ func NewContext() {
827826
ReverseProxyAuthEmail = sec.Key("REVERSE_PROXY_AUTHENTICATION_EMAIL").MustString("X-WEBAUTH-EMAIL")
828827

829828
ReverseProxyLimit = sec.Key("REVERSE_PROXY_LIMIT").MustInt(1)
830-
ReverseProxyTrustedIPAddr = sec.Key("REVERSE_PROXY_TRUSTED_PROXIES").Strings(",")
831-
if len(ReverseProxyTrustedIPAddr) == 0 {
832-
ReverseProxyTrustedIPAddr = []string{"127.0.0.1"}
829+
ReverseProxyTrustedProxies = sec.Key("REVERSE_PROXY_TRUSTED_PROXIES").Strings(",")
830+
if len(ReverseProxyTrustedProxies) == 0 {
831+
ReverseProxyTrustedProxies = []string{"127.0.0.0/8", "::1/128"}
833832
}
834-
ReverseProxyTrustedNet = sec.Key("REVERSE_PROXY_TRUSTED_NETWORKS").Strings(",")
835833

836834
MinPasswordLength = sec.Key("MIN_PASSWORD_LENGTH").MustInt(6)
837835
ImportLocalPaths = sec.Key("IMPORT_LOCAL_PATHS").MustBool(false)

routers/routes/web.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ func commonMiddlewares() []func(http.Handler) http.Handler {
7272
opt := proxy.NewForwardedHeadersOptions().
7373
WithForwardLimit(setting.ReverseProxyLimit).
7474
ClearTrustedProxies()
75-
for _, ip := range setting.ReverseProxyTrustedIPAddr {
76-
opt.AddTrustedProxy(ip)
77-
}
78-
for _, n := range setting.ReverseProxyTrustedNet {
79-
opt.AddTrustedNetwork(n)
75+
for _, n := range setting.ReverseProxyTrustedProxies {
76+
if !strings.Contains(n, "/") {
77+
opt.AddTrustedProxy(n)
78+
} else {
79+
opt.AddTrustedNetwork(n)
80+
}
8081
}
8182
handlers = append(handlers, proxy.ForwardedHeaders(opt))
8283
}

0 commit comments

Comments
 (0)