Skip to content

Commit 0e5b895

Browse files
authored
Merge branch 'main' into fix-20894-gpg-key-to-mediumtext
2 parents 386d7c7 + 36dfe54 commit 0e5b895

File tree

23 files changed

+852
-94
lines changed

23 files changed

+852
-94
lines changed

cmd/web.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func runHTTPRedirector() {
7676
http.Redirect(w, r, target, http.StatusTemporaryRedirect)
7777
})
7878

79-
err := runHTTP("tcp", source, "HTTP Redirector", handler)
79+
err := runHTTP("tcp", source, "HTTP Redirector", handler, setting.RedirectorUseProxyProtocol)
8080
if err != nil {
8181
log.Fatal("Failed to start port redirection: %v", err)
8282
}
@@ -231,40 +231,38 @@ func listen(m http.Handler, handleRedirector bool) error {
231231
if handleRedirector {
232232
NoHTTPRedirector()
233233
}
234-
err = runHTTP("tcp", listenAddr, "Web", m)
234+
err = runHTTP("tcp", listenAddr, "Web", m, setting.UseProxyProtocol)
235235
case setting.HTTPS:
236236
if setting.EnableAcme {
237237
err = runACME(listenAddr, m)
238238
break
239-
} else {
240-
if handleRedirector {
241-
if setting.RedirectOtherPort {
242-
go runHTTPRedirector()
243-
} else {
244-
NoHTTPRedirector()
245-
}
239+
}
240+
if handleRedirector {
241+
if setting.RedirectOtherPort {
242+
go runHTTPRedirector()
243+
} else {
244+
NoHTTPRedirector()
246245
}
247-
err = runHTTPS("tcp", listenAddr, "Web", setting.CertFile, setting.KeyFile, m)
248246
}
247+
err = runHTTPS("tcp", listenAddr, "Web", setting.CertFile, setting.KeyFile, m, setting.UseProxyProtocol, setting.ProxyProtocolTLSBridging)
249248
case setting.FCGI:
250249
if handleRedirector {
251250
NoHTTPRedirector()
252251
}
253-
err = runFCGI("tcp", listenAddr, "FCGI Web", m)
252+
err = runFCGI("tcp", listenAddr, "FCGI Web", m, setting.UseProxyProtocol)
254253
case setting.HTTPUnix:
255254
if handleRedirector {
256255
NoHTTPRedirector()
257256
}
258-
err = runHTTP("unix", listenAddr, "Web", m)
257+
err = runHTTP("unix", listenAddr, "Web", m, setting.UseProxyProtocol)
259258
case setting.FCGIUnix:
260259
if handleRedirector {
261260
NoHTTPRedirector()
262261
}
263-
err = runFCGI("unix", listenAddr, "Web", m)
262+
err = runFCGI("unix", listenAddr, "Web", m, setting.UseProxyProtocol)
264263
default:
265264
log.Fatal("Invalid protocol: %s", setting.Protocol)
266265
}
267-
268266
if err != nil {
269267
log.Critical("Failed to start server: %v", err)
270268
}

cmd/web_acme.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,14 @@ func runACME(listenAddr string, m http.Handler) error {
113113

114114
log.Info("Running Let's Encrypt handler on %s", setting.HTTPAddr+":"+setting.PortToRedirect)
115115
// all traffic coming into HTTP will be redirect to HTTPS automatically (LE HTTP-01 validation happens here)
116-
err := runHTTP("tcp", setting.HTTPAddr+":"+setting.PortToRedirect, "Let's Encrypt HTTP Challenge", myACME.HTTPChallengeHandler(http.HandlerFunc(runLetsEncryptFallbackHandler)))
116+
err := runHTTP("tcp", setting.HTTPAddr+":"+setting.PortToRedirect, "Let's Encrypt HTTP Challenge", myACME.HTTPChallengeHandler(http.HandlerFunc(runLetsEncryptFallbackHandler)), setting.RedirectorUseProxyProtocol)
117117
if err != nil {
118118
log.Fatal("Failed to start the Let's Encrypt handler on port %s: %v", setting.PortToRedirect, err)
119119
}
120120
}()
121121
}
122122

123-
return runHTTPSWithTLSConfig("tcp", listenAddr, "Web", tlsConfig, m)
123+
return runHTTPSWithTLSConfig("tcp", listenAddr, "Web", tlsConfig, m, setting.UseProxyProtocol, setting.ProxyProtocolTLSBridging)
124124
}
125125

126126
func runLetsEncryptFallbackHandler(w http.ResponseWriter, r *http.Request) {

cmd/web_graceful.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515
"code.gitea.io/gitea/modules/setting"
1616
)
1717

18-
func runHTTP(network, listenAddr, name string, m http.Handler) error {
19-
return graceful.HTTPListenAndServe(network, listenAddr, name, m)
18+
func runHTTP(network, listenAddr, name string, m http.Handler, useProxyProtocol bool) error {
19+
return graceful.HTTPListenAndServe(network, listenAddr, name, m, useProxyProtocol)
2020
}
2121

2222
// NoHTTPRedirector tells our cleanup routine that we will not be using a fallback http redirector
@@ -36,7 +36,7 @@ func NoInstallListener() {
3636
graceful.GetManager().InformCleanup()
3737
}
3838

39-
func runFCGI(network, listenAddr, name string, m http.Handler) error {
39+
func runFCGI(network, listenAddr, name string, m http.Handler, useProxyProtocol bool) error {
4040
// This needs to handle stdin as fcgi point
4141
fcgiServer := graceful.NewServer(network, listenAddr, name)
4242

@@ -47,7 +47,7 @@ func runFCGI(network, listenAddr, name string, m http.Handler) error {
4747
}
4848
m.ServeHTTP(resp, req)
4949
}))
50-
})
50+
}, useProxyProtocol)
5151
if err != nil {
5252
log.Fatal("Failed to start FCGI main server: %v", err)
5353
}

cmd/web_https.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,14 @@ var (
129129
defaultCiphersChaChaFirst = append(defaultCiphersChaCha, defaultCiphersAES...)
130130
)
131131

132-
// runHTTPs listens on the provided network address and then calls
132+
// runHTTPS listens on the provided network address and then calls
133133
// Serve to handle requests on incoming TLS connections.
134134
//
135135
// Filenames containing a certificate and matching private key for the server must
136136
// be provided. If the certificate is signed by a certificate authority, the
137137
// certFile should be the concatenation of the server's certificate followed by the
138138
// CA's certificate.
139-
func runHTTPS(network, listenAddr, name, certFile, keyFile string, m http.Handler) error {
139+
func runHTTPS(network, listenAddr, name, certFile, keyFile string, m http.Handler, useProxyProtocol, proxyProtocolTLSBridging bool) error {
140140
tlsConfig := &tls.Config{}
141141
if tlsConfig.NextProtos == nil {
142142
tlsConfig.NextProtos = []string{"h2", "http/1.1"}
@@ -184,9 +184,9 @@ func runHTTPS(network, listenAddr, name, certFile, keyFile string, m http.Handle
184184
return err
185185
}
186186

187-
return graceful.HTTPListenAndServeTLSConfig(network, listenAddr, name, tlsConfig, m)
187+
return graceful.HTTPListenAndServeTLSConfig(network, listenAddr, name, tlsConfig, m, useProxyProtocol, proxyProtocolTLSBridging)
188188
}
189189

190-
func runHTTPSWithTLSConfig(network, listenAddr, name string, tlsConfig *tls.Config, m http.Handler) error {
191-
return graceful.HTTPListenAndServeTLSConfig(network, listenAddr, name, tlsConfig, m)
190+
func runHTTPSWithTLSConfig(network, listenAddr, name string, tlsConfig *tls.Config, m http.Handler, useProxyProtocol, proxyProtocolTLSBridging bool) error {
191+
return graceful.HTTPListenAndServeTLSConfig(network, listenAddr, name, tlsConfig, m, useProxyProtocol, proxyProtocolTLSBridging)
192192
}

custom/conf/app.example.ini

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ RUN_MODE = ; prod
2929
;; The protocol the server listens on. One of 'http', 'https', 'unix' or 'fcgi'. Defaults to 'http'
3030
;PROTOCOL = http
3131
;;
32+
;; Expect PROXY protocol headers on connections
33+
;USE_PROXY_PROTOCOL = false
34+
;;
35+
;; Use PROXY protocol in TLS Bridging mode
36+
;PROXY_PROTOCOL_TLS_BRIDGING = false
37+
;;
38+
; Timeout to wait for PROXY protocol header (set to 0 to have no timeout)
39+
;PROXY_PROTOCOL_HEADER_TIMEOUT=5s
40+
;;
41+
; Accept PROXY protocol headers with UNKNOWN type
42+
;PROXY_PROTOCOL_ACCEPT_UNKNOWN=false
43+
;;
3244
;; Set the domain for the server
3345
;DOMAIN = localhost
3446
;;
@@ -51,6 +63,8 @@ RUN_MODE = ; prod
5163
;REDIRECT_OTHER_PORT = false
5264
;PORT_TO_REDIRECT = 80
5365
;;
66+
;; expect PROXY protocol header on connections to https redirector.
67+
;REDIRECTOR_USE_PROXY_PROTOCOL = %(USE_PROXY_PROTOCOL)
5468
;; Minimum and maximum supported TLS versions
5569
;SSL_MIN_VERSION=TLSv1.2
5670
;SSL_MAX_VERSION=
@@ -76,13 +90,19 @@ RUN_MODE = ; prod
7690
;; Do not set this variable if PROTOCOL is set to 'unix'.
7791
;LOCAL_ROOT_URL = %(PROTOCOL)s://%(HTTP_ADDR)s:%(HTTP_PORT)s/
7892
;;
93+
;; When making local connections pass the PROXY protocol header.
94+
;LOCAL_USE_PROXY_PROTOCOL = %(USE_PROXY_PROTOCOL)
95+
;;
7996
;; Disable SSH feature when not available
8097
;DISABLE_SSH = false
8198
;;
8299
;; Whether to use the builtin SSH server or not.
83100
;START_SSH_SERVER = false
84101
;;
85-
;; Username to use for the builtin SSH server.
102+
;; Expect PROXY protocol header on connections to the built-in SSH server
103+
;SSH_SERVER_USE_PROXY_PROTOCOL = false
104+
;;
105+
;; Username to use for the builtin SSH server. If blank, then it is the value of RUN_USER.
86106
;BUILTIN_SSH_SERVER_USER = %(RUN_USER)s
87107
;;
88108
;; Domain name to be exposed in clone URL

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ The following configuration set `Content-Type: application/vnd.android.package-a
238238
## Server (`server`)
239239

240240
- `PROTOCOL`: **http**: \[http, https, fcgi, http+unix, fcgi+unix\]
241+
- `USE_PROXY_PROTOCOL`: **false**: Expect PROXY protocol headers on connections
242+
- `PROXY_PROTOCOL_TLS_BRIDGING`: **false**: When protocol is https, expect PROXY protocol headers after TLS negotiation.
243+
- `PROXY_PROTOCOL_HEADER_TIMEOUT`: **5s**: Timeout to wait for PROXY protocol header (set to 0 to have no timeout)
244+
- `PROXY_PROTOCOL_ACCEPT_UNKNOWN`: **false**: Accept PROXY protocol headers with Unknown type.
241245
- `DOMAIN`: **localhost**: Domain name of this server.
242246
- `ROOT_URL`: **%(PROTOCOL)s://%(DOMAIN)s:%(HTTP\_PORT)s/**:
243247
Overwrite the automatically generated public URL.
@@ -262,12 +266,15 @@ The following configuration set `Content-Type: application/vnd.android.package-a
262266
most cases you do not need to change the default value. Alter it only if
263267
your SSH server node is not the same as HTTP node. Do not set this variable
264268
if `PROTOCOL` is set to `http+unix`.
269+
- `LOCAL_USE_PROXY_PROTOCOL`: **%(USE_PROXY_PROTOCOL)**: When making local connections pass the PROXY protocol header.
270+
This should be set to false if the local connection will go through the proxy.
265271
- `PER_WRITE_TIMEOUT`: **30s**: Timeout for any write to the connection. (Set to -1 to
266272
disable all timeouts.)
267273
- `PER_WRITE_PER_KB_TIMEOUT`: **10s**: Timeout per Kb written to connections.
268274

269275
- `DISABLE_SSH`: **false**: Disable SSH feature when it's not available.
270276
- `START_SSH_SERVER`: **false**: When enabled, use the built-in SSH server.
277+
- `SSH_SERVER_USE_PROXY_PROTOCOL`: **false**: Expect PROXY protocol header on connections to the built-in SSH Server.
271278
- `BUILTIN_SSH_SERVER_USER`: **%(RUN_USER)s**: Username to use for the built-in SSH Server.
272279
- `SSH_USER`: **%(BUILTIN_SSH_SERVER_USER)**: SSH username displayed in clone URLs. This is only for people who configure the SSH server themselves; in most cases, you want to leave this blank and modify the `BUILTIN_SSH_SERVER_USER`.
273280
- `SSH_DOMAIN`: **%(DOMAIN)s**: Domain name of this server, used for displayed clone URL.
@@ -313,6 +320,7 @@ The following configuration set `Content-Type: application/vnd.android.package-a
313320
- `LFS_LOCKS_PAGING_NUM`: **50**: Maximum number of LFS Locks returned per page.
314321

315322
- `REDIRECT_OTHER_PORT`: **false**: If true and `PROTOCOL` is https, allows redirecting http requests on `PORT_TO_REDIRECT` to the https port Gitea listens on.
323+
- `REDIRECTOR_USE_PROXY_PROTOCOL`: **%(USE_PROXY_PROTOCOL)**: expect PROXY protocol header on connections to https redirector.
316324
- `PORT_TO_REDIRECT`: **80**: Port for the http redirection service to listen on. Used when `REDIRECT_OTHER_PORT` is true.
317325
- `SSL_MIN_VERSION`: **TLSv1.2**: Set the minimum version of ssl support.
318326
- `SSL_MAX_VERSION`: **\<empty\>**: Set the maximum version of ssl support.

integrations/api_team_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func TestAPITeamSearch(t *testing.T) {
223223
defer prepareTestEnv(t)()
224224

225225
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
226-
org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
226+
org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 17})
227227

228228
var results TeamSearchResults
229229

integrations/api_user_orgs_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,19 @@ func TestUserOrgs(t *testing.T) {
2626
orgs := getUserOrgs(t, adminUsername, normalUsername)
2727

2828
user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user3"})
29+
user17 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user17"})
2930

3031
assert.Equal(t, []*api.Organization{
32+
{
33+
ID: 17,
34+
UserName: user17.Name,
35+
FullName: user17.FullName,
36+
AvatarURL: user17.AvatarLink(),
37+
Description: "",
38+
Website: "",
39+
Location: "",
40+
Visibility: "public",
41+
},
3142
{
3243
ID: 3,
3344
UserName: user3.Name,
@@ -82,8 +93,19 @@ func TestMyOrgs(t *testing.T) {
8293
var orgs []*api.Organization
8394
DecodeJSON(t, resp, &orgs)
8495
user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user3"})
96+
user17 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user17"})
8597

8698
assert.Equal(t, []*api.Organization{
99+
{
100+
ID: 17,
101+
UserName: user17.Name,
102+
FullName: user17.FullName,
103+
AvatarURL: user17.AvatarLink(),
104+
Description: "",
105+
Website: "",
106+
Location: "",
107+
Visibility: "public",
108+
},
87109
{
88110
ID: 3,
89111
UserName: user3.Name,

integrations/org_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ func TestOrgRestrictedUser(t *testing.T) {
197197
func TestTeamSearch(t *testing.T) {
198198
defer prepareTestEnv(t)()
199199

200-
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
201-
org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
200+
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 15})
201+
org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 17})
202202

203203
var results TeamSearchResults
204204

@@ -209,8 +209,9 @@ func TestTeamSearch(t *testing.T) {
209209
resp := session.MakeRequest(t, req, http.StatusOK)
210210
DecodeJSON(t, resp, &results)
211211
assert.NotEmpty(t, results.Data)
212-
assert.Len(t, results.Data, 1)
213-
assert.Equal(t, "test_team", results.Data[0].Name)
212+
assert.Len(t, results.Data, 2)
213+
assert.Equal(t, "review_team", results.Data[0].Name)
214+
assert.Equal(t, "test_team", results.Data[1].Name)
214215

215216
// no access if not organization member
216217
user5 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})

models/fixtures/org_user.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,9 @@
6363
uid: 29
6464
org_id: 17
6565
is_public: true
66+
67+
-
68+
id: 12
69+
uid: 2
70+
org_id: 17
71+
is_public: true

models/fixtures/user.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@
309309
avatar_email: [email protected]
310310
num_repos: 2
311311
is_active: true
312-
num_members: 3
312+
num_members: 4
313313
num_teams: 3
314314

315315
-

models/organization/team.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,7 @@ type SearchTeamOptions struct {
9696
IncludeDesc bool
9797
}
9898

99-
// SearchTeam search for teams. Caller is responsible to check permissions.
100-
func SearchTeam(opts *SearchTeamOptions) ([]*Team, int64, error) {
101-
if opts.Page <= 0 {
102-
opts.Page = 1
103-
}
104-
if opts.PageSize == 0 {
105-
// Default limit
106-
opts.PageSize = 10
107-
}
108-
99+
func (opts *SearchTeamOptions) toCond() builder.Cond {
109100
cond := builder.NewCond()
110101

111102
if len(opts.Keyword) > 0 {
@@ -117,18 +108,39 @@ func SearchTeam(opts *SearchTeamOptions) ([]*Team, int64, error) {
117108
cond = cond.And(keywordCond)
118109
}
119110

120-
cond = cond.And(builder.Eq{"org_id": opts.OrgID})
111+
if opts.OrgID > 0 {
112+
cond = cond.And(builder.Eq{"`team`.org_id": opts.OrgID})
113+
}
114+
115+
if opts.UserID > 0 {
116+
cond = cond.And(builder.Eq{"team_user.uid": opts.UserID})
117+
}
118+
119+
return cond
120+
}
121121

122+
// SearchTeam search for teams. Caller is responsible to check permissions.
123+
func SearchTeam(opts *SearchTeamOptions) ([]*Team, int64, error) {
122124
sess := db.GetEngine(db.DefaultContext)
123125

126+
opts.SetDefaultValues()
127+
cond := opts.toCond()
128+
129+
if opts.UserID > 0 {
130+
sess = sess.Join("INNER", "team_user", "team_user.team_id = team.id")
131+
}
132+
124133
count, err := sess.
125134
Where(cond).
126135
Count(new(Team))
127136
if err != nil {
128137
return nil, 0, err
129138
}
130139

131-
sess = sess.Where(cond)
140+
if opts.UserID > 0 {
141+
sess = sess.Join("INNER", "team_user", "team_user.team_id = team.id")
142+
}
143+
132144
if opts.PageSize == -1 {
133145
opts.PageSize = int(count)
134146
} else {
@@ -137,6 +149,7 @@ func SearchTeam(opts *SearchTeamOptions) ([]*Team, int64, error) {
137149

138150
teams := make([]*Team, 0, opts.PageSize)
139151
if err = sess.
152+
Where(cond).
140153
OrderBy("lower_name").
141154
Find(&teams); err != nil {
142155
return nil, 0, err

0 commit comments

Comments
 (0)