Skip to content

Commit 9bea8d8

Browse files
tamalsahalafriks
authored andcommitted
Run CORS handler first for /api routes (#7967) (#8053)
Signed-off-by: Tamal Saha <[email protected]>
1 parent 3cc7288 commit 9bea8d8

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

routers/api/v1/api.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ import (
7474
"code.gitea.io/gitea/routers/api/v1/user"
7575

7676
"github.com/go-macaron/binding"
77-
"github.com/go-macaron/cors"
7877
macaron "gopkg.in/macaron.v1"
7978
)
8079

@@ -501,12 +500,6 @@ func RegisterRoutes(m *macaron.Macaron) {
501500
m.Get("/swagger", misc.Swagger) //Render V1 by default
502501
}
503502

504-
var handlers []macaron.Handler
505-
if setting.EnableCORS {
506-
handlers = append(handlers, cors.CORS(setting.CORSConfig))
507-
}
508-
handlers = append(handlers, securityHeaders(), context.APIContexter(), sudo())
509-
510503
m.Group("/v1", func() {
511504
// Miscellaneous
512505
if setting.API.EnableSwagger {
@@ -852,7 +845,7 @@ func RegisterRoutes(m *macaron.Macaron) {
852845
m.Group("/topics", func() {
853846
m.Get("/search", repo.TopicSearch)
854847
})
855-
}, handlers...)
848+
}, securityHeaders(), context.APIContexter(), sudo())
856849
}
857850

858851
func securityHeaders() macaron.Handler {

routers/routes/routes.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/go-macaron/binding"
3939
"github.com/go-macaron/cache"
4040
"github.com/go-macaron/captcha"
41+
"github.com/go-macaron/cors"
4142
"github.com/go-macaron/csrf"
4243
"github.com/go-macaron/i18n"
4344
"github.com/go-macaron/session"
@@ -947,9 +948,14 @@ func RegisterRoutes(m *macaron.Macaron) {
947948
m.Get("/swagger.v1.json", templates.JSONRenderer(), routers.SwaggerV1Json)
948949
}
949950

951+
var handlers []macaron.Handler
952+
if setting.EnableCORS {
953+
handlers = append(handlers, cors.CORS(setting.CORSConfig))
954+
}
955+
handlers = append(handlers, ignSignIn)
950956
m.Group("/api", func() {
951957
apiv1.RegisterRoutes(m)
952-
}, ignSignIn)
958+
}, handlers...)
953959

954960
m.Group("/api/internal", func() {
955961
// package name internal is ideal but Golang is not allowed, so we use private as package name.

0 commit comments

Comments
 (0)