feat: multiple-cors-allow-origin support (#7134)

This commit is contained in:
Alex Zhang 2021-05-24 00:13:39 +08:00 committed by GitHub
parent f6dbd93865
commit 8a55801cc0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 7 deletions

View file

@ -18,6 +18,7 @@ package cors
import (
"regexp"
"strings"
networking "k8s.io/api/networking/v1beta1"
@ -113,7 +114,14 @@ func (c cors) Parse(ing *networking.Ingress) (interface{}, error) {
}
config.CorsAllowOrigin, err = parser.GetStringAnnotation("cors-allow-origin", ing)
if err != nil || !corsOriginRegex.MatchString(config.CorsAllowOrigin) {
if err == nil {
for _, origin := range strings.Split(config.CorsAllowOrigin, ",") {
if !corsOriginRegex.MatchString(strings.TrimSpace(origin)) {
config.CorsAllowOrigin = "*"
break
}
}
} else {
config.CorsAllowOrigin = "*"
}