Skip to content

Commit 41f1627

Browse files
authored
CBG-3594: Check if CORS config is actually empty before adding CORS response headers (#7792)
1 parent b532b5b commit 41f1627

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

auth/cors.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ func (cors *CORSConfig) AddResponseHeaders(request *http.Request, response http.
3232
}
3333
}
3434

35+
// IsEmpty returns true if the CORS configuration is empty - used instead of a nil check since we always initialize the CORS config struct.
36+
func (cors *CORSConfig) IsEmpty() bool {
37+
return cors == nil ||
38+
(len(cors.Origin) == 0 && len(cors.LoginOrigin) == 0 && len(cors.Headers) == 0 && cors.MaxAge == 0)
39+
}
40+
3541
func MatchedOrigin(allowOrigins []string, rqOrigins []string) string {
3642
for _, rv := range rqOrigins {
3743
for _, av := range allowOrigins {

rest/cors_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,24 @@ func TestCORSUserNoAccess(t *testing.T) {
288288
}
289289
}
290290

291+
// TestCORSResponseHeadersEmptyConfig ensures that an empty CORS config results in no CORS headers being set on the response.
292+
func TestCORSResponseHeadersEmptyConfig(t *testing.T) {
293+
rt := NewRestTester(t, nil)
294+
// RestTester initializes using defaultTestingCORSOrigin - override to empty for this test
295+
rt.ServerContext().Config.API.CORS = &auth.CORSConfig{}
296+
defer rt.Close()
297+
298+
reqHeaders := map[string]string{
299+
"Origin": "http://example.com",
300+
}
301+
response := rt.SendRequestWithHeaders(http.MethodGet, "/{{.db}}/", "", reqHeaders)
302+
RequireStatus(t, response, http.StatusUnauthorized)
303+
require.Contains(t, response.Body.String(), ErrLoginRequired.Message)
304+
assert.NotContains(t, response.Header(), "Access-Control-Allow-Origin")
305+
assert.NotContains(t, response.Header(), "Access-Control-Allow-Credentials")
306+
assert.NotContains(t, response.Header(), "Access-Control-Allow-Headers")
307+
}
308+
291309
func TestCORSOriginPerDatabase(t *testing.T) {
292310
// Override the default (example.com) CORS configuration in the DbConfig for /db:
293311
const perDBMaxAge = 1234

rest/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ func (h *handler) validateAndWriteHeaders(method handlerMethod, accessPermission
339339
if h.db != nil {
340340
cors = h.db.CORS
341341
}
342-
if cors != nil {
342+
if !cors.IsEmpty() {
343343
cors.AddResponseHeaders(h.rq, h.response)
344344
}
345345
}

rest/routing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ func wrapRouter(sc *ServerContext, privs handlerPrivs, serverType serverType, ro
456456
cors = db.CORS
457457
}
458458
}
459-
if cors != nil && privs != adminPrivs && privs != metricsPrivs {
459+
if !cors.IsEmpty() && privs != adminPrivs && privs != metricsPrivs {
460460
cors.AddResponseHeaders(rq, response)
461461
}
462462
if len(options) == 0 {

0 commit comments

Comments
 (0)