Skip to content

Commit

Permalink
Merge pull request #3489 from VirajSalaka/cors-fix
Browse files Browse the repository at this point in the history
Bug fix: x-choreo-test-session-id is appended to CORS config repeatedly
  • Loading branch information
VirajSalaka authored Feb 2, 2024
2 parents 2195f51 + 3b9cc7a commit 20242c6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,8 @@ func TestGetCorsPolicy(t *testing.T) {
// Test configuration when all the fields are provided.
corsPolicy2 := getCorsPolicy(corsConfigModel2)
assert.NotNil(t, corsPolicy2, "Cors Policy should not be null.")
// To make sure that the allow headers in the config passed is not modified.
assert.Equal(t, 2, len(corsConfigModel2.AccessControlAllowHeaders), "Cors Config is modified which is not supposed to be modified.")
assert.NotEmpty(t, corsPolicy2.GetAllowOriginStringMatch(), "Cors Allowded Origins should not be null.")
assert.Equal(t, regexp.QuoteMeta("http://test.com"),
corsPolicy2.GetAllowOriginStringMatch()[0].GetSafeRegex().GetRegex(),
Expand Down
7 changes: 4 additions & 3 deletions adapter/internal/oasparser/envoyconf/routes_with_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -1454,10 +1454,11 @@ func getCorsPolicy(corsConfig *model.CorsConfig) *cors_filter_v3.CorsPolicy {
if corsConfig == nil || !corsConfig.Enabled {
return nil
}
var finalAccessControlAllowHeaders []string

conf, _ := config.ReadConfigs()
if len(conf.Envoy.Cors.MandatoryHeaders) > 0 {
corsConfig.AccessControlAllowHeaders = append(corsConfig.AccessControlAllowHeaders, conf.Envoy.Cors.MandatoryHeaders...)
finalAccessControlAllowHeaders = append(corsConfig.AccessControlAllowHeaders, conf.Envoy.Cors.MandatoryHeaders...)
}

stringMatcherArray := []*envoy_type_matcherv3.StringMatcher{}
Expand Down Expand Up @@ -1485,8 +1486,8 @@ func getCorsPolicy(corsConfig *model.CorsConfig) *cors_filter_v3.CorsPolicy {
if len(corsConfig.AccessControlAllowMethods) > 0 {
corsPolicy.AllowMethods = strings.Join(corsConfig.AccessControlAllowMethods, ", ")
}
if len(corsConfig.AccessControlAllowHeaders) > 0 {
corsPolicy.AllowHeaders = strings.Join(corsConfig.AccessControlAllowHeaders, ", ")
if len(finalAccessControlAllowHeaders) > 0 {
corsPolicy.AllowHeaders = strings.Join(finalAccessControlAllowHeaders, ", ")
}
if len(corsConfig.AccessControlExposeHeaders) > 0 {
corsPolicy.ExposeHeaders = strings.Join(corsConfig.AccessControlExposeHeaders, ", ")
Expand Down

0 comments on commit 20242c6

Please sign in to comment.