Skip to content

Commit 6b3f52f

Browse files
tamalsahasapk
authored andcommitted
Run CORS handler first for /api routes (#7967)
Signed-off-by: Tamal Saha <[email protected]>
1 parent 5409dec commit 6b3f52f

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
@@ -75,7 +75,6 @@ import (
7575
"code.gitea.io/gitea/routers/api/v1/user"
7676

7777
"gitea.com/macaron/binding"
78-
"gitea.com/macaron/cors"
7978
"gitea.com/macaron/macaron"
8079
)
8180

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

505-
var handlers []macaron.Handler
506-
if setting.EnableCORS {
507-
handlers = append(handlers, cors.CORS(setting.CORSConfig))
508-
}
509-
handlers = append(handlers, securityHeaders(), context.APIContexter(), sudo())
510-
511504
m.Group("/v1", func() {
512505
// Miscellaneous
513506
if setting.API.EnableSwagger {
@@ -853,7 +846,7 @@ func RegisterRoutes(m *macaron.Macaron) {
853846
m.Group("/topics", func() {
854847
m.Get("/search", repo.TopicSearch)
855848
})
856-
}, handlers...)
849+
}, securityHeaders(), context.APIContexter(), sudo())
857850
}
858851

859852
func securityHeaders() macaron.Handler {

routers/routes/routes.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"gitea.com/macaron/binding"
4242
"gitea.com/macaron/cache"
4343
"gitea.com/macaron/captcha"
44+
"gitea.com/macaron/cors"
4445
"gitea.com/macaron/csrf"
4546
"gitea.com/macaron/i18n"
4647
"gitea.com/macaron/macaron"
@@ -951,9 +952,14 @@ func RegisterRoutes(m *macaron.Macaron) {
951952
m.Get("/swagger.v1.json", templates.JSONRenderer(), routers.SwaggerV1Json)
952953
}
953954

955+
var handlers []macaron.Handler
956+
if setting.EnableCORS {
957+
handlers = append(handlers, cors.CORS(setting.CORSConfig))
958+
}
959+
handlers = append(handlers, ignSignIn)
954960
m.Group("/api", func() {
955961
apiv1.RegisterRoutes(m)
956-
}, ignSignIn)
962+
}, handlers...)
957963

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

0 commit comments

Comments
 (0)