Skip to content

Commit d648a25

Browse files
committed
accept wildcard nginx.ingress.kubernetes.io/cors-allow-headers
1 parent 76f90ec commit d648a25

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

internal/ingress/annotations/cors/main.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ var (
4949
// Method must contain valid methods list (PUT, GET, POST, BLA)
5050
// May contain or not spaces between each verb
5151
corsMethodsRegex = regexp.MustCompile(`^([A-Za-z]+,?\s?)+$`)
52-
// Expose Headers must contain valid values only (*, X-HEADER12, X-ABC)
52+
// CORS Headers must contain valid values only (*, X-HEADER12, X-ABC)
5353
// May contain or not spaces between each Header
54-
corsExposeHeadersRegex = regexp.MustCompile(`^(([A-Za-z0-9\-\_]+|\*),?\s?)+$`)
54+
corsHeadersRegex = regexp.MustCompile(`^(([A-Za-z0-9\-\_]+|\*),?\s?)+$`)
5555
)
5656

5757
const (
@@ -83,11 +83,11 @@ var corsAnnotation = parser.Annotation{
8383
Protocol can be any lowercase string, like http, https, or mycustomprotocol.`,
8484
},
8585
corsAllowHeadersAnnotation: {
86-
Validator: parser.ValidateRegex(parser.HeadersVariable, true),
86+
Validator: parser.ValidateRegex(corsHeadersRegex, true),
8787
Scope: parser.AnnotationScopeIngress,
8888
Risk: parser.AnnotationRiskMedium,
8989
Documentation: `This annotation controls which headers are accepted.
90-
This is a multi-valued field, separated by ',' and accepts letters, numbers, _ and -`,
90+
This is a multi-valued field, separated by ',' and accepts letters, numbers, _, - and *.`,
9191
},
9292
corsAllowMethodsAnnotation: {
9393
Validator: parser.ValidateRegex(corsMethodsRegex, true),
@@ -103,7 +103,7 @@ var corsAnnotation = parser.Annotation{
103103
Documentation: `This annotation controls if credentials can be passed during CORS operations.`,
104104
},
105105
corsExposeHeadersAnnotation: {
106-
Validator: parser.ValidateRegex(corsExposeHeadersRegex, true),
106+
Validator: parser.ValidateRegex(corsHeadersRegex, true),
107107
Scope: parser.AnnotationScopeIngress,
108108
Risk: parser.AnnotationRiskMedium,
109109
Documentation: `This annotation controls which headers are exposed to response.
@@ -226,7 +226,7 @@ func (c cors) Parse(ing *networking.Ingress) (interface{}, error) {
226226
}
227227

228228
config.CorsAllowHeaders, err = parser.GetStringAnnotation(corsAllowHeadersAnnotation, ing, c.annotationConfig.Annotations)
229-
if err != nil || !parser.HeadersVariable.MatchString(config.CorsAllowHeaders) {
229+
if err != nil || !corsHeadersRegex.MatchString(config.CorsAllowHeaders) {
230230
config.CorsAllowHeaders = defaultCorsHeaders
231231
}
232232

@@ -246,7 +246,7 @@ func (c cors) Parse(ing *networking.Ingress) (interface{}, error) {
246246
}
247247

248248
config.CorsExposeHeaders, err = parser.GetStringAnnotation(corsExposeHeadersAnnotation, ing, c.annotationConfig.Annotations)
249-
if err != nil || !corsExposeHeadersRegex.MatchString(config.CorsExposeHeaders) {
249+
if err != nil || !corsHeadersRegex.MatchString(config.CorsExposeHeaders) {
250250
config.CorsExposeHeaders = ""
251251
}
252252

internal/ingress/annotations/cors/main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestIngressCorsConfigValid(t *testing.T) {
7979

8080
// Valid
8181
data[parser.GetAnnotationWithPrefix(corsEnableAnnotation)] = enableAnnotation
82-
data[parser.GetAnnotationWithPrefix(corsAllowHeadersAnnotation)] = "DNT,X-CustomHeader, Keep-Alive,User-Agent"
82+
data[parser.GetAnnotationWithPrefix(corsAllowHeadersAnnotation)] = "*, DNT,X-CustomHeader, Keep-Alive,User-Agent"
8383
data[parser.GetAnnotationWithPrefix(corsAllowCredentialsAnnotation)] = "false"
8484
data[parser.GetAnnotationWithPrefix(corsAllowMethodsAnnotation)] = "GET, PATCH"
8585
data[parser.GetAnnotationWithPrefix(corsAllowOriginAnnotation)] = "https://origin123.test.com:4443"
@@ -105,7 +105,7 @@ func TestIngressCorsConfigValid(t *testing.T) {
105105
t.Errorf("expected %v but returned %v", data[parser.GetAnnotationWithPrefix(corsAllowCredentialsAnnotation)], nginxCors.CorsAllowCredentials)
106106
}
107107

108-
if nginxCors.CorsAllowHeaders != "DNT,X-CustomHeader, Keep-Alive,User-Agent" {
108+
if nginxCors.CorsAllowHeaders != "*, DNT,X-CustomHeader, Keep-Alive,User-Agent" {
109109
t.Errorf("expected %v but returned %v", data[parser.GetAnnotationWithPrefix(corsAllowHeadersAnnotation)], nginxCors.CorsAllowHeaders)
110110
}
111111

0 commit comments

Comments
 (0)