Skip to content

Commit 44759fd

Browse files
silverwindtechknowlogick
authored andcommitted
Add proper CORS preflight origin validation (#5740)
1 parent ca3b9aa commit 44759fd

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

routers/repo/http.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,25 @@ import (
2828
// HTTP implmentation git smart HTTP protocol
2929
func HTTP(ctx *context.Context) {
3030
if len(setting.Repository.AccessControlAllowOrigin) > 0 {
31+
allowedOrigin := setting.Repository.AccessControlAllowOrigin
3132
// Set CORS headers for browser-based git clients
32-
ctx.Resp.Header().Set("Access-Control-Allow-Origin", setting.Repository.AccessControlAllowOrigin)
33+
ctx.Resp.Header().Set("Access-Control-Allow-Origin", allowedOrigin)
3334
ctx.Resp.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, User-Agent")
3435

3536
// Handle preflight OPTIONS request
3637
if ctx.Req.Method == "OPTIONS" {
37-
ctx.Status(http.StatusOK)
38+
if allowedOrigin == "*" {
39+
ctx.Status(http.StatusOK)
40+
} else if allowedOrigin == "null" {
41+
ctx.Status(http.StatusForbidden)
42+
} else {
43+
origin := ctx.Req.Header.Get("Origin")
44+
if len(origin) > 0 && origin == allowedOrigin {
45+
ctx.Status(http.StatusOK)
46+
} else {
47+
ctx.Status(http.StatusForbidden)
48+
}
49+
}
3850
return
3951
}
4052
}

0 commit comments

Comments
 (0)