Skip to content

Commit b505557

Browse files
committed
Merge remote-tracking branch 'giteaofficial/main'
* giteaofficial/main: [skip ci] Updated translations via Crowdin Display when a release attachment was uploaded (go-gitea#34261) Fix Set Email Preference dropdown and button placement (go-gitea#34255) [skip ci] Updated translations via Crowdin Update compare.tmpl (go-gitea#34251) Make public URL generation configurable (go-gitea#34250) Add API endpoint to request contents of multiple files simultaniously (go-gitea#34139)
2 parents b623b85 + 04fab18 commit b505557

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+681
-492
lines changed

custom/conf/app.example.ini

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,19 @@ RUN_USER = ; git
6363
;PROTOCOL = http
6464
;;
6565
;; Set the domain for the server.
66-
;; Most users should set it to the real website domain of their Gitea instance.
6766
;DOMAIN = localhost
6867
;;
69-
;; The AppURL used by Gitea to generate absolute links, defaults to "{PROTOCOL}://{DOMAIN}:{HTTP_PORT}/".
68+
;; The AppURL is used to generate public URL links, defaults to "{PROTOCOL}://{DOMAIN}:{HTTP_PORT}/".
7069
;; Most users should set it to the real website URL of their Gitea instance when there is a reverse proxy.
71-
;; When it is empty, Gitea will use HTTP "Host" header to generate ROOT_URL, and fall back to the default one if no "Host" header.
7270
;ROOT_URL =
7371
;;
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,
74+
;; 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".
77+
;PUBLIC_URL_DETECTION = legacy
78+
;;
7479
;; For development purpose only. It makes Gitea handle sub-path ("/sub-path/owner/repo/...") directly when debugging without a reverse proxy.
7580
;; DO NOT USE IT IN PRODUCTION!!!
7681
;USE_SUB_URL_PATH = false
@@ -2439,6 +2444,8 @@ LEVEL = Info
24392444
;DEFAULT_GIT_TREES_PER_PAGE = 1000
24402445
;; Default max size of a blob returned by the blobs API (default is 10MiB)
24412446
;DEFAULT_MAX_BLOB_SIZE = 10485760
2447+
;; Default max combined size of all blobs returned by the files API (default is 100MiB)
2448+
;DEFAULT_MAX_RESPONSE_SIZE = 104857600
24422449

24432450
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
24442451
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

modules/git/repo_object.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,3 @@ func (repo *Repository) hashObject(reader io.Reader, save bool) (string, error)
8585
}
8686
return strings.TrimSpace(stdout.String()), nil
8787
}
88-
89-
// GetRefType gets the type of the ref based on the string
90-
func (repo *Repository) GetRefType(ref string) ObjectType {
91-
if repo.IsTagExist(ref) {
92-
return ObjectTag
93-
} else if repo.IsBranchExist(ref) {
94-
return ObjectBranch
95-
} else if repo.IsCommitExist(ref) {
96-
return ObjectCommit
97-
} else if _, err := repo.GetBlob(ref); err == nil {
98-
return ObjectBlob
99-
}
100-
return ObjectType("invalid")
101-
}

modules/httplib/url.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,31 @@ func getRequestScheme(req *http.Request) string {
5353
return ""
5454
}
5555

56-
// GuessCurrentAppURL tries to guess the current full app URL (with sub-path) by http headers. It always has a '/' suffix, exactly the same as setting.AppURL
56+
// GuessCurrentAppURL tries to guess the current full public URL (with sub-path) by http headers. It always has a '/' suffix, exactly the same as setting.AppURL
57+
// TODO: should rename it to GuessCurrentPublicURL in the future
5758
func GuessCurrentAppURL(ctx context.Context) string {
5859
return GuessCurrentHostURL(ctx) + setting.AppSubURL + "/"
5960
}
6061

6162
// GuessCurrentHostURL tries to guess the current full host URL (no sub-path) by http headers, there is no trailing slash.
6263
func GuessCurrentHostURL(ctx context.Context) string {
63-
req, ok := ctx.Value(RequestContextKey).(*http.Request)
64-
if !ok {
65-
return strings.TrimSuffix(setting.AppURL, setting.AppSubURL+"/")
66-
}
67-
// If no scheme provided by reverse proxy, then do not guess the AppURL, use the configured one.
64+
// Try the best guess to get the current host URL (will be used for public URL) by http headers.
6865
// At the moment, if site admin doesn't configure the proxy headers correctly, then Gitea would guess wrong.
6966
// There are some cases:
7067
// 1. The reverse proxy is configured correctly, it passes "X-Forwarded-Proto/Host" headers. Perfect, Gitea can handle it correctly.
7168
// 2. The reverse proxy is not configured correctly, doesn't pass "X-Forwarded-Proto/Host" headers, eg: only one "proxy_pass http://gitea:3000" in Nginx.
7269
// 3. There is no reverse proxy.
7370
// Without more information, Gitea is impossible to distinguish between case 2 and case 3, then case 2 would result in
74-
// wrong guess like guessed AppURL becomes "http://gitea:3000/" behind a "https" reverse proxy, which is not accessible by end users.
75-
// So we introduced "UseHostHeader" option, it could be enabled by setting "ROOT_URL" to empty
71+
// wrong guess like guessed public URL becomes "http://gitea:3000/" behind a "https" reverse proxy, which is not accessible by end users.
72+
// So we introduced "PUBLIC_URL_DETECTION" option, to control the guessing behavior to satisfy different use cases.
73+
req, ok := ctx.Value(RequestContextKey).(*http.Request)
74+
if !ok {
75+
return strings.TrimSuffix(setting.AppURL, setting.AppSubURL+"/")
76+
}
7677
reqScheme := getRequestScheme(req)
7778
if reqScheme == "" {
7879
// if no reverse proxy header, try to use "Host" header for absolute URL
79-
if setting.UseHostHeader && req.Host != "" {
80+
if setting.PublicURLDetection == setting.PublicURLAuto && req.Host != "" {
8081
return util.Iif(req.TLS == nil, "http://", "https://") + req.Host
8182
}
8283
// fall back to default AppURL
@@ -93,8 +94,8 @@ func GuessCurrentHostDomain(ctx context.Context) string {
9394
return util.IfZero(domain, host)
9495
}
9596

96-
// MakeAbsoluteURL tries to make a link to an absolute URL:
97-
// * If link is empty, it returns the current app URL.
97+
// MakeAbsoluteURL tries to make a link to an absolute public URL:
98+
// * If link is empty, it returns the current public URL.
9899
// * If link is absolute, it returns the link.
99100
// * Otherwise, it returns the current host URL + link, the link itself should have correct sub-path (AppSubURL) if needed.
100101
func MakeAbsoluteURL(ctx context.Context, link string) string {

modules/httplib/url_test.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,37 @@ func TestIsRelativeURL(t *testing.T) {
4343
func TestGuessCurrentHostURL(t *testing.T) {
4444
defer test.MockVariableValue(&setting.AppURL, "http://cfg-host/sub/")()
4545
defer test.MockVariableValue(&setting.AppSubURL, "/sub")()
46-
defer test.MockVariableValue(&setting.UseHostHeader, false)()
46+
headersWithProto := http.Header{"X-Forwarded-Proto": {"https"}}
4747

48-
ctx := t.Context()
49-
assert.Equal(t, "http://cfg-host", GuessCurrentHostURL(ctx))
48+
t.Run("Legacy", func(t *testing.T) {
49+
defer test.MockVariableValue(&setting.PublicURLDetection, setting.PublicURLLegacy)()
50+
51+
assert.Equal(t, "http://cfg-host", GuessCurrentHostURL(t.Context()))
52+
53+
// legacy: "Host" is not used when there is no "X-Forwarded-Proto" header
54+
ctx := context.WithValue(t.Context(), RequestContextKey, &http.Request{Host: "req-host:3000"})
55+
assert.Equal(t, "http://cfg-host", GuessCurrentHostURL(ctx))
56+
57+
// if "X-Forwarded-Proto" exists, then use it and "Host" header
58+
ctx = context.WithValue(t.Context(), RequestContextKey, &http.Request{Host: "req-host:3000", Header: headersWithProto})
59+
assert.Equal(t, "https://req-host:3000", GuessCurrentHostURL(ctx))
60+
})
61+
62+
t.Run("Auto", func(t *testing.T) {
63+
defer test.MockVariableValue(&setting.PublicURLDetection, setting.PublicURLAuto)()
5064

51-
ctx = context.WithValue(ctx, RequestContextKey, &http.Request{Host: "localhost:3000"})
52-
assert.Equal(t, "http://cfg-host", GuessCurrentHostURL(ctx))
65+
assert.Equal(t, "http://cfg-host", GuessCurrentHostURL(t.Context()))
5366

54-
defer test.MockVariableValue(&setting.UseHostHeader, true)()
55-
ctx = context.WithValue(ctx, RequestContextKey, &http.Request{Host: "http-host:3000"})
56-
assert.Equal(t, "http://http-host:3000", GuessCurrentHostURL(ctx))
67+
// auto: always use "Host" header, the scheme is determined by "X-Forwarded-Proto" header, or TLS config if no "X-Forwarded-Proto" header
68+
ctx := context.WithValue(t.Context(), RequestContextKey, &http.Request{Host: "req-host:3000"})
69+
assert.Equal(t, "http://req-host:3000", GuessCurrentHostURL(ctx))
5770

58-
ctx = context.WithValue(ctx, RequestContextKey, &http.Request{Host: "http-host", TLS: &tls.ConnectionState{}})
59-
assert.Equal(t, "https://http-host", GuessCurrentHostURL(ctx))
71+
ctx = context.WithValue(t.Context(), RequestContextKey, &http.Request{Host: "req-host", TLS: &tls.ConnectionState{}})
72+
assert.Equal(t, "https://req-host", GuessCurrentHostURL(ctx))
73+
74+
ctx = context.WithValue(t.Context(), RequestContextKey, &http.Request{Host: "req-host:3000", Header: headersWithProto})
75+
assert.Equal(t, "https://req-host:3000", GuessCurrentHostURL(ctx))
76+
})
6077
}
6178

6279
func TestMakeAbsoluteURL(t *testing.T) {

modules/setting/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ var API = struct {
1818
DefaultPagingNum int
1919
DefaultGitTreesPerPage int
2020
DefaultMaxBlobSize int64
21+
DefaultMaxResponseSize int64
2122
}{
2223
EnableSwagger: true,
2324
SwaggerURL: "",
2425
MaxResponseItems: 50,
2526
DefaultPagingNum: 30,
2627
DefaultGitTreesPerPage: 1000,
2728
DefaultMaxBlobSize: 10485760,
29+
DefaultMaxResponseSize: 104857600,
2830
}
2931

3032
func loadAPIFrom(rootCfg ConfigProvider) {

modules/setting/server.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,20 @@ const (
4141
LandingPageLogin LandingPage = "/user/login"
4242
)
4343

44+
const (
45+
PublicURLAuto = "auto"
46+
PublicURLLegacy = "legacy"
47+
)
48+
4449
// Server settings
4550
var (
4651
// AppURL is the Application ROOT_URL. It always has a '/' suffix
4752
// It maps to ini:"ROOT_URL"
4853
AppURL string
4954

55+
// PublicURLDetection controls how to use the HTTP request headers to detect public URL
56+
PublicURLDetection string
57+
5058
// AppSubURL represents the sub-url mounting point for gitea, parsed from "ROOT_URL"
5159
// It is either "" or starts with '/' and ends without '/', such as '/{sub-path}'.
5260
// This value is empty if site does not have sub-url.
@@ -56,9 +64,6 @@ var (
5664
// to make it easier to debug sub-path related problems without a reverse proxy.
5765
UseSubURLPath bool
5866

59-
// UseHostHeader makes Gitea prefer to use the "Host" request header for construction of absolute URLs.
60-
UseHostHeader bool
61-
6267
// AppDataPath is the default path for storing data.
6368
// It maps to ini:"APP_DATA_PATH" in [server] and defaults to AppWorkPath + "/data"
6469
AppDataPath string
@@ -283,10 +288,10 @@ func loadServerFrom(rootCfg ConfigProvider) {
283288
PerWritePerKbTimeout = sec.Key("PER_WRITE_PER_KB_TIMEOUT").MustDuration(PerWritePerKbTimeout)
284289

285290
defaultAppURL := string(Protocol) + "://" + Domain + ":" + HTTPPort
286-
AppURL = sec.Key("ROOT_URL").String()
287-
if AppURL == "" {
288-
UseHostHeader = true
289-
AppURL = defaultAppURL
291+
AppURL = sec.Key("ROOT_URL").MustString(defaultAppURL)
292+
PublicURLDetection = sec.Key("PUBLIC_URL_DETECTION").MustString(PublicURLLegacy)
293+
if PublicURLDetection != PublicURLAuto && PublicURLDetection != PublicURLLegacy {
294+
log.Fatal("Invalid PUBLIC_URL_DETECTION value: %s", PublicURLDetection)
290295
}
291296

292297
// Check validity of AppURL

modules/structs/git_blob.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ package structs
55

66
// GitBlobResponse represents a git blob
77
type GitBlobResponse struct {
8-
Content string `json:"content"`
9-
Encoding string `json:"encoding"`
10-
URL string `json:"url"`
11-
SHA string `json:"sha"`
12-
Size int64 `json:"size"`
8+
Content *string `json:"content"`
9+
Encoding *string `json:"encoding"`
10+
URL string `json:"url"`
11+
SHA string `json:"sha"`
12+
Size int64 `json:"size"`
1313
}

modules/structs/repo_file.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,8 @@ type FileDeleteResponse struct {
176176
Commit *FileCommitResponse `json:"commit"`
177177
Verification *PayloadCommitVerification `json:"verification"`
178178
}
179+
180+
// GetFilesOptions options for retrieving metadate and content of multiple files
181+
type GetFilesOptions struct {
182+
Files []string `json:"files" binding:"Required"`
183+
}

modules/structs/settings.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type GeneralAPISettings struct {
2626
DefaultPagingNum int `json:"default_paging_num"`
2727
DefaultGitTreesPerPage int `json:"default_git_trees_per_page"`
2828
DefaultMaxBlobSize int64 `json:"default_max_blob_size"`
29+
DefaultMaxResponseSize int64 `json:"default_max_response_size"`
2930
}
3031

3132
// GeneralAttachmentSettings contains global Attachment settings exposed by API

options/locale/locale_cs-CZ.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1652,7 +1652,6 @@ issues.pin_comment=připnuto %s
16521652
issues.unpin_comment=odepnul/a tento %s
16531653
issues.lock=Uzamknout konverzaci
16541654
issues.unlock=Odemknout konverzaci
1655-
issues.lock.unknown_reason=Úkol nelze z neznámého důvodu uzamknout.
16561655
issues.lock_duplicate=Úkol nemůže být uzamčený dvakrát.
16571656
issues.unlock_error=Nelze odemknout úkol, který je uzamčený.
16581657
issues.lock_with_reason=uzamkl/a jako <strong>%s</strong> a omezil/a konverzaci na spolupracovníky %s

0 commit comments

Comments
 (0)