Skip to content

Commit 2cbea23

Browse files
drewmnoelKN4CK3Rjolheiserlunny
authored
Add configuration for CORS allowed headers (#21747)
This PR enhances the CORS middleware usage by allowing for the headers to be configured in `app.ini`. Fixes #21746 Co-authored-by: KN4CK3R <[email protected]> Co-authored-by: John Olheiser <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent fb704f6 commit 2cbea23

File tree

5 files changed

+8
-1
lines changed

5 files changed

+8
-1
lines changed

custom/conf/app.example.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,9 @@ ROUTER = console
11381138
;; allow request with credentials
11391139
;ALLOW_CREDENTIALS = false
11401140
;;
1141+
;; headers to permit
1142+
;HEADERS = Content-Type,User-Agent
1143+
;;
11411144
;; set X-FRAME-OPTIONS header
11421145
;X_FRAME_OPTIONS = SAMEORIGIN
11431146

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ The following configuration set `Content-Type: application/vnd.android.package-a
200200
- `METHODS`: **GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS**: list of methods allowed to request
201201
- `MAX_AGE`: **10m**: max time to cache response
202202
- `ALLOW_CREDENTIALS`: **false**: allow request with credentials
203+
- `HEADERS`: **Content-Type,User-Agent**: additional headers that are permitted in requests
203204
- `X_FRAME_OPTIONS`: **SAMEORIGIN**: Set the `X-Frame-Options` header value.
204205

205206
## UI (`ui`)

modules/setting/cors.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ var CORSConfig = struct {
1919
Methods []string
2020
MaxAge time.Duration
2121
AllowCredentials bool
22+
Headers []string
2223
XFrameOptions string
2324
}{
2425
Enabled: false,
2526
MaxAge: 10 * time.Minute,
27+
Headers: []string{"Content-Type", "User-Agent"},
2628
XFrameOptions: "SAMEORIGIN",
2729
}
2830

routers/api/v1/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ func Routes(ctx gocontext.Context) *web.Route {
617617
// setting.CORSConfig.AllowSubdomain // FIXME: the cors middleware needs allowSubdomain option
618618
AllowedMethods: setting.CORSConfig.Methods,
619619
AllowCredentials: setting.CORSConfig.AllowCredentials,
620-
AllowedHeaders: []string{"Authorization", "X-Gitea-OTP"},
620+
AllowedHeaders: append([]string{"Authorization", "X-Gitea-OTP"}, setting.CORSConfig.Headers...),
621621
MaxAge: int(setting.CORSConfig.MaxAge.Seconds()),
622622
}))
623623
}

routers/web/web.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func CorsHandler() func(next http.Handler) http.Handler {
6767
// setting.CORSConfig.AllowSubdomain // FIXME: the cors middleware needs allowSubdomain option
6868
AllowedMethods: setting.CORSConfig.Methods,
6969
AllowCredentials: setting.CORSConfig.AllowCredentials,
70+
AllowedHeaders: setting.CORSConfig.Headers,
7071
MaxAge: int(setting.CORSConfig.MaxAge.Seconds()),
7172
})
7273
}

0 commit comments

Comments
 (0)