Skip to content

Commit ade0cdd

Browse files
secitaler9
andauthored
support multiple CORS origins (#5150)
Co-authored-by: aler9 <[email protected]>
1 parent 14ab95f commit ade0cdd

24 files changed

+441
-180
lines changed

api/openapi.yaml

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,10 @@ components:
124124
type: string
125125
apiServerCert:
126126
type: string
127-
apiAllowOrigin:
128-
type: string
127+
apiAllowOrigins:
128+
type: array
129+
items:
130+
type: string
129131
apiTrustedProxies:
130132
type: array
131133
items:
@@ -142,8 +144,10 @@ components:
142144
type: string
143145
metricsServerCert:
144146
type: string
145-
metricsAllowOrigin:
146-
type: string
147+
metricsAllowOrigins:
148+
type: array
149+
items:
150+
type: string
147151
metricsTrustedProxies:
148152
type: array
149153
items:
@@ -160,8 +164,10 @@ components:
160164
type: string
161165
pprofServerCert:
162166
type: string
163-
pprofAllowOrigin:
164-
type: string
167+
pprofAllowOrigins:
168+
type: array
169+
items:
170+
type: string
165171
pprofTrustedProxies:
166172
type: array
167173
items:
@@ -178,8 +184,10 @@ components:
178184
type: string
179185
playbackServerCert:
180186
type: string
181-
playbackAllowOrigin:
182-
type: string
187+
playbackAllowOrigins:
188+
type: array
189+
items:
190+
type: string
183191
playbackTrustedProxies:
184192
type: array
185193
items:
@@ -254,8 +262,10 @@ components:
254262
type: string
255263
hlsServerCert:
256264
type: string
257-
hlsAllowOrigin:
258-
type: string
265+
hlsAllowOrigins:
266+
type: array
267+
items:
268+
type: string
259269
hlsTrustedProxies:
260270
type: array
261271
items:
@@ -289,8 +299,10 @@ components:
289299
type: string
290300
webrtcServerCert:
291301
type: string
292-
webrtcAllowOrigin:
293-
type: string
302+
webrtcAllowOrigins:
303+
type: array
304+
items:
305+
type: string
294306
webrtcTrustedProxies:
295307
type: array
296308
items:

internal/api/api.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ type API struct {
9494
Encryption bool
9595
ServerKey string
9696
ServerCert string
97-
AllowOrigin string
97+
AllowOrigins []string
9898
TrustedProxies conf.IPNetworks
9999
ReadTimeout conf.Duration
100100
WriteTimeout conf.Duration
@@ -119,7 +119,7 @@ func (a *API) Initialize() error {
119119
router := gin.New()
120120
router.SetTrustedProxies(a.TrustedProxies.ToTrustedProxies()) //nolint:errcheck
121121

122-
router.Use(a.middlewareOrigin)
122+
router.Use(a.middlewarePreflightRequests)
123123
router.Use(a.middlewareAuth)
124124

125125
group := router.Group("/v3")
@@ -195,6 +195,7 @@ func (a *API) Initialize() error {
195195

196196
a.httpServer = &httpp.Server{
197197
Address: a.Address,
198+
AllowOrigins: a.AllowOrigins,
198199
ReadTimeout: time.Duration(a.ReadTimeout),
199200
WriteTimeout: time.Duration(a.WriteTimeout),
200201
Encryption: a.Encryption,
@@ -234,11 +235,7 @@ func (a *API) writeError(ctx *gin.Context, status int, err error) {
234235
})
235236
}
236237

237-
func (a *API) middlewareOrigin(ctx *gin.Context) {
238-
ctx.Header("Access-Control-Allow-Origin", a.AllowOrigin)
239-
ctx.Header("Access-Control-Allow-Credentials", "true")
240-
241-
// preflight requests
238+
func (a *API) middlewarePreflightRequests(ctx *gin.Context) {
242239
if ctx.Request.Method == http.MethodOptions &&
243240
ctx.Request.Header.Get("Access-Control-Request-Method") != "" {
244241
ctx.Header("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PATCH, DELETE")

internal/api/api_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func checkError(t *testing.T, msg string, body io.Reader) {
8383
func TestPreflightRequest(t *testing.T) {
8484
api := API{
8585
Address: "localhost:9997",
86-
AllowOrigin: "*",
86+
AllowOrigins: []string{"*"},
8787
ReadTimeout: conf.Duration(10 * time.Second),
8888
WriteTimeout: conf.Duration(10 * time.Second),
8989
AuthManager: test.NilAuthManager,

internal/conf/allowed_origins.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package conf
2+
3+
// AllowedOrigins is a list of allowed CORS origins.
4+
type AllowedOrigins []string

0 commit comments

Comments
 (0)