Skip to content

Commit 63c2b69

Browse files
authored
Make PUBLIC_URL_DETECTION default to "auto" (#36955)
Related issues including: #36939 , #35619, #34950 , #34253 , #32554 For users who use reverse-proxy, we have documented the requirements clearly since long time ago : https://docs.gitea.com/administration/reverse-proxies
1 parent 86401fd commit 63c2b69

3 files changed

Lines changed: 22 additions & 12 deletions

File tree

custom/conf/app.example.ini

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,12 @@ RUN_USER = ; git
6969
;; Most users should set it to the real website URL of their Gitea instance when there is a reverse proxy.
7070
;ROOT_URL =
7171
;;
72-
;; Controls how to detect the public URL.
73-
;; Although it defaults to "legacy" (to avoid breaking existing users), most instances should use the "auto" behavior,
72+
;; Controls how to detect the public URL. Most instances should use the "auto" behavior,
7473
;; especially when the Gitea instance needs to be accessed in a container network.
75-
;; * legacy: detect the public URL from "Host" header if "X-Forwarded-Proto" header exists, otherwise use "ROOT_URL".
76-
;; * auto: always use "Host" header, and also use "X-Forwarded-Proto" header if it exists. If no "Host" header, use "ROOT_URL".
74+
;; * legacy: (default <= 1.25) detect the public URL from "Host" header if "X-Forwarded-Proto" header exists, otherwise use "ROOT_URL".
75+
;; * auto: (default >= 1.26) always use "Host" header, and also use "X-Forwarded-Proto" header if it exists. If no "Host" header, use "ROOT_URL".
7776
;; * never: always use "ROOT_URL", never detect from request headers.
78-
;PUBLIC_URL_DETECTION = legacy
77+
;PUBLIC_URL_DETECTION = auto
7978
;;
8079
;; For development purpose only. It makes Gitea handle sub-path ("/sub-path/owner/repo/...") directly when debugging without a reverse proxy.
8180
;; DO NOT USE IT IN PRODUCTION!!!

modules/setting/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func loadServerFrom(rootCfg ConfigProvider) {
286286

287287
defaultAppURL := string(Protocol) + "://" + Domain + ":" + HTTPPort
288288
AppURL = sec.Key("ROOT_URL").MustString(defaultAppURL)
289-
PublicURLDetection = sec.Key("PUBLIC_URL_DETECTION").MustString(PublicURLLegacy)
289+
PublicURLDetection = sec.Key("PUBLIC_URL_DETECTION").MustString(PublicURLAuto)
290290
if PublicURLDetection != PublicURLAuto && PublicURLDetection != PublicURLLegacy && PublicURLDetection != PublicURLNever {
291291
log.Fatal("Invalid PUBLIC_URL_DETECTION value: %s", PublicURLDetection)
292292
}

routers/web/admin/admin_test.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"code.gitea.io/gitea/services/contexttest"
1414

1515
"github.com/stretchr/testify/assert"
16+
"github.com/stretchr/testify/require"
1617
)
1718

1819
func TestShadowPassword(t *testing.T) {
@@ -74,19 +75,29 @@ func TestShadowPassword(t *testing.T) {
7475
}
7576

7677
func TestSelfCheckPost(t *testing.T) {
78+
defer test.MockVariableValue(&setting.PublicURLDetection)()
7779
defer test.MockVariableValue(&setting.AppURL, "http://config/sub/")()
7880
defer test.MockVariableValue(&setting.AppSubURL, "/sub")()
7981

80-
ctx, resp := contexttest.MockContext(t, "GET http://host/sub/admin/self_check?location_origin=http://frontend")
81-
SelfCheckPost(ctx)
82-
assert.Equal(t, http.StatusOK, resp.Code)
83-
8482
data := struct {
8583
Problems []string `json:"problems"`
8684
}{}
87-
err := json.Unmarshal(resp.Body.Bytes(), &data)
88-
assert.NoError(t, err)
85+
86+
setting.PublicURLDetection = setting.PublicURLLegacy
87+
ctx, resp := contexttest.MockContext(t, "GET http://host/sub/admin/self_check?location_origin=http://frontend")
88+
SelfCheckPost(ctx)
89+
assert.Equal(t, http.StatusOK, resp.Code)
90+
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &data))
8991
assert.Equal(t, []string{
9092
ctx.Locale.TrString("admin.self_check.location_origin_mismatch", "http://frontend/sub/", "http://config/sub/"),
9193
}, data.Problems)
94+
95+
setting.PublicURLDetection = setting.PublicURLAuto
96+
ctx, resp = contexttest.MockContext(t, "GET http://host/sub/admin/self_check?location_origin=http://frontend")
97+
SelfCheckPost(ctx)
98+
assert.Equal(t, http.StatusOK, resp.Code)
99+
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &data))
100+
assert.Equal(t, []string{
101+
ctx.Locale.TrString("admin.self_check.location_origin_mismatch", "http://frontend/sub/", "http://host/sub/"),
102+
}, data.Problems)
92103
}

0 commit comments

Comments
 (0)