Skip to content

Commit 9219534

Browse files
authored
Fix incorrect CORS response in Http Git handler (#24303)
Use the general `cors.Handler` for CORS
1 parent d5e9341 commit 9219534

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

routers/web/repo/http.go

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,31 @@ import (
3232
"code.gitea.io/gitea/modules/structs"
3333
"code.gitea.io/gitea/modules/util"
3434
repo_service "code.gitea.io/gitea/services/repository"
35+
36+
"github.com/go-chi/cors"
3537
)
3638

37-
// httpBase implementation git smart HTTP protocol
38-
func httpBase(ctx *context.Context) (h *serviceHandler) {
39+
func HTTPGitEnabledHandler(ctx *context.Context) {
3940
if setting.Repository.DisableHTTPGit {
4041
ctx.Resp.WriteHeader(http.StatusForbidden)
41-
_, err := ctx.Resp.Write([]byte("Interacting with repositories by HTTP protocol is not allowed"))
42-
if err != nil {
43-
log.Error(err.Error())
44-
}
45-
return
42+
_, _ = ctx.Resp.Write([]byte("Interacting with repositories by HTTP protocol is not allowed"))
4643
}
44+
}
4745

48-
if len(setting.Repository.AccessControlAllowOrigin) > 0 {
49-
allowedOrigin := setting.Repository.AccessControlAllowOrigin
50-
// Set CORS headers for browser-based git clients
51-
ctx.Resp.Header().Set("Access-Control-Allow-Origin", allowedOrigin)
52-
ctx.Resp.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, User-Agent")
53-
54-
// Handle preflight OPTIONS request
55-
if ctx.Req.Method == "OPTIONS" {
56-
if allowedOrigin == "*" {
57-
ctx.Status(http.StatusOK)
58-
} else if allowedOrigin == "null" {
59-
ctx.Status(http.StatusForbidden)
60-
} else {
61-
origin := ctx.Req.Header.Get("Origin")
62-
if len(origin) > 0 && origin == allowedOrigin {
63-
ctx.Status(http.StatusOK)
64-
} else {
65-
ctx.Status(http.StatusForbidden)
66-
}
67-
}
68-
return
69-
}
46+
func CorsHandler() func(next http.Handler) http.Handler {
47+
if setting.Repository.AccessControlAllowOrigin != "" {
48+
return cors.Handler(cors.Options{
49+
AllowedOrigins: []string{setting.Repository.AccessControlAllowOrigin},
50+
AllowedHeaders: []string{"Content-Type", "Authorization", "User-Agent"},
51+
})
7052
}
53+
return func(next http.Handler) http.Handler {
54+
return next
55+
}
56+
}
7157

58+
// httpBase implementation git smart HTTP protocol
59+
func httpBase(ctx *context.Context) (h *serviceHandler) {
7260
username := ctx.Params(":username")
7361
reponame := strings.TrimSuffix(ctx.Params(":reponame"), ".git")
7462

routers/web/web.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1515,7 +1515,7 @@ func RegisterRoutes(m *web.Route) {
15151515
m.GetOptions("/objects/{head:[0-9a-f]{2}}/{hash:[0-9a-f]{38}}", repo.GetLooseObject)
15161516
m.GetOptions("/objects/pack/pack-{file:[0-9a-f]{40}}.pack", repo.GetPackFile)
15171517
m.GetOptions("/objects/pack/pack-{file:[0-9a-f]{40}}.idx", repo.GetIdxFile)
1518-
}, ignSignInAndCsrf, context_service.UserAssignmentWeb())
1518+
}, ignSignInAndCsrf, repo.HTTPGitEnabledHandler, repo.CorsHandler(), context_service.UserAssignmentWeb())
15191519
})
15201520
})
15211521
// ***** END: Repository *****

0 commit comments

Comments
 (0)