From 5754eb60f46efaf3a0ace6d62a3c05c274956838 Mon Sep 17 00:00:00 2001 From: Aibek <35672535+L1ghtman2k@users.noreply.github.com> Date: Sun, 13 Feb 2022 13:39:47 -0500 Subject: [PATCH] Append elements on match, instead of removing for cors-annotations (#8185) * fixes https://github.com/kubernetes/ingress-nginx/issues/8168 by appending elements on match, instead of removing * refactor the corsOriginRegex comparison, and initialize CorsAllowOrigin --- internal/ingress/annotations/cors/main.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/ingress/annotations/cors/main.go b/internal/ingress/annotations/cors/main.go index d2f232af14..de5b8c2794 100644 --- a/internal/ingress/annotations/cors/main.go +++ b/internal/ingress/annotations/cors/main.go @@ -121,19 +121,22 @@ func (c cors) Parse(ing *networking.Ingress) (interface{}, error) { config.CorsEnabled = false } + config.CorsAllowOrigin = []string{} unparsedOrigins, err := parser.GetStringAnnotation("cors-allow-origin", ing) if err == nil { - config.CorsAllowOrigin = strings.Split(unparsedOrigins, ",") - for i, origin := range config.CorsAllowOrigin { + origins := strings.Split(unparsedOrigins, ",") + for _, origin := range origins { origin = strings.TrimSpace(origin) if origin == "*" { config.CorsAllowOrigin = []string{"*"} break } + if !corsOriginRegex.MatchString(origin) { klog.Errorf("Error parsing cors-allow-origin parameters. Supplied incorrect origin: %s. Skipping.", origin) - config.CorsAllowOrigin = append(config.CorsAllowOrigin[:i], config.CorsAllowOrigin[i+1:]...) + continue } + config.CorsAllowOrigin = append(config.CorsAllowOrigin, origin) klog.Infof("Current config.corsAllowOrigin %v", config.CorsAllowOrigin) } } else {