Skip to content

Commit 9ca1853

Browse files
GiteaBotlunny
andauthored
Fix http protocol auth (#27875) (#27876)
Backport #27875 by @lunny Co-authored-by: Lunny Xiao <[email protected]>
1 parent d6f7c49 commit 9ca1853

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

routers/web/githttp.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package web
5+
6+
import (
7+
"net/http"
8+
9+
"code.gitea.io/gitea/modules/context"
10+
"code.gitea.io/gitea/modules/setting"
11+
"code.gitea.io/gitea/modules/web"
12+
"code.gitea.io/gitea/routers/web/repo"
13+
context_service "code.gitea.io/gitea/services/context"
14+
)
15+
16+
func requireSignIn(ctx *context.Context) {
17+
if !setting.Service.RequireSignInView {
18+
return
19+
}
20+
21+
// rely on the results of Contexter
22+
if !ctx.IsSigned {
23+
// TODO: support digit auth - which would be Authorization header with digit
24+
ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="Gitea"`)
25+
ctx.Error(http.StatusUnauthorized)
26+
}
27+
}
28+
29+
func gitHTTPRouters(m *web.Route) {
30+
m.Group("", func() {
31+
m.PostOptions("/git-upload-pack", repo.ServiceUploadPack)
32+
m.PostOptions("/git-receive-pack", repo.ServiceReceivePack)
33+
m.GetOptions("/info/refs", repo.GetInfoRefs)
34+
m.GetOptions("/HEAD", repo.GetTextFile("HEAD"))
35+
m.GetOptions("/objects/info/alternates", repo.GetTextFile("objects/info/alternates"))
36+
m.GetOptions("/objects/info/http-alternates", repo.GetTextFile("objects/info/http-alternates"))
37+
m.GetOptions("/objects/info/packs", repo.GetInfoPacks)
38+
m.GetOptions("/objects/info/{file:[^/]*}", repo.GetTextFile(""))
39+
m.GetOptions("/objects/{head:[0-9a-f]{2}}/{hash:[0-9a-f]{38}}", repo.GetLooseObject)
40+
m.GetOptions("/objects/pack/pack-{file:[0-9a-f]{40}}.pack", repo.GetPackFile)
41+
m.GetOptions("/objects/pack/pack-{file:[0-9a-f]{40}}.idx", repo.GetIdxFile)
42+
}, ignSignInAndCsrf, requireSignIn, repo.HTTPGitEnabledHandler, repo.CorsHandler(), context_service.UserAssignmentWeb())
43+
}
File renamed without changes.
File renamed without changes.

routers/web/web.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,16 @@ func Routes() *web.Route {
275275
return routes
276276
}
277277

278+
var ignSignInAndCsrf = verifyAuthWithOptions(&common.VerifyOptions{DisableCSRF: true})
279+
278280
// registerRoutes register routes
279281
func registerRoutes(m *web.Route) {
280282
reqSignIn := verifyAuthWithOptions(&common.VerifyOptions{SignInRequired: true})
281283
reqSignOut := verifyAuthWithOptions(&common.VerifyOptions{SignOutRequired: true})
282284
// TODO: rename them to "optSignIn", which means that the "sign-in" could be optional, depends on the VerifyOptions (RequireSignInView)
283285
ignSignIn := verifyAuthWithOptions(&common.VerifyOptions{SignInRequired: setting.Service.RequireSignInView})
284286
ignExploreSignIn := verifyAuthWithOptions(&common.VerifyOptions{SignInRequired: setting.Service.RequireSignInView || setting.Service.Explore.RequireSigninView})
285-
ignSignInAndCsrf := verifyAuthWithOptions(&common.VerifyOptions{DisableCSRF: true})
287+
286288
validation.AddBindingRules()
287289

288290
linkAccountEnabled := func(ctx *context.Context) {
@@ -1511,19 +1513,7 @@ func registerRoutes(m *web.Route) {
15111513
})
15121514
}, ignSignInAndCsrf, lfsServerEnabled)
15131515

1514-
m.Group("", func() {
1515-
m.PostOptions("/git-upload-pack", repo.ServiceUploadPack)
1516-
m.PostOptions("/git-receive-pack", repo.ServiceReceivePack)
1517-
m.GetOptions("/info/refs", repo.GetInfoRefs)
1518-
m.GetOptions("/HEAD", repo.GetTextFile("HEAD"))
1519-
m.GetOptions("/objects/info/alternates", repo.GetTextFile("objects/info/alternates"))
1520-
m.GetOptions("/objects/info/http-alternates", repo.GetTextFile("objects/info/http-alternates"))
1521-
m.GetOptions("/objects/info/packs", repo.GetInfoPacks)
1522-
m.GetOptions("/objects/info/{file:[^/]*}", repo.GetTextFile(""))
1523-
m.GetOptions("/objects/{head:[0-9a-f]{2}}/{hash:[0-9a-f]{38}}", repo.GetLooseObject)
1524-
m.GetOptions("/objects/pack/pack-{file:[0-9a-f]{40}}.pack", repo.GetPackFile)
1525-
m.GetOptions("/objects/pack/pack-{file:[0-9a-f]{40}}.idx", repo.GetIdxFile)
1526-
}, ignSignInAndCsrf, repo.HTTPGitEnabledHandler, repo.CorsHandler(), context_service.UserAssignmentWeb())
1516+
gitHTTPRouters(m)
15271517
})
15281518
})
15291519
// ***** END: Repository *****

0 commit comments

Comments
 (0)