Skip to content

Commit 000deb2

Browse files
committed
accept wildcard nginx.ingress.kubernetes.io/cors-allow-headers
1 parent 56dbba3 commit 000deb2

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 (
@@ -82,11 +82,11 @@ var corsAnnotation = parser.Annotation{
8282
It also supports single level wildcard subdomains and follows this format: http(s)://*.foo.bar, http(s)://*.bar.foo:8080 or http(s)://*.abc.bar.foo:9000`,
8383
},
8484
corsAllowHeadersAnnotation: {
85-
Validator: parser.ValidateRegex(parser.HeadersVariable, true),
85+
Validator: parser.ValidateRegex(corsHeadersRegex, true),
8686
Scope: parser.AnnotationScopeIngress,
8787
Risk: parser.AnnotationRiskMedium,
8888
Documentation: `This annotation controls which headers are accepted.
89-
This is a multi-valued field, separated by ',' and accepts letters, numbers, _ and -`,
89+
This is a multi-valued field, separated by ',' and accepts letters, numbers, _, - and *.`,
9090
},
9191
corsAllowMethodsAnnotation: {
9292
Validator: parser.ValidateRegex(corsMethodsRegex, true),
@@ -102,7 +102,7 @@ var corsAnnotation = parser.Annotation{
102102
Documentation: `This annotation controls if credentials can be passed during CORS operations.`,
103103
},
104104
corsExposeHeadersAnnotation: {
105-
Validator: parser.ValidateRegex(corsExposeHeadersRegex, true),
105+
Validator: parser.ValidateRegex(corsHeadersRegex, true),
106106
Scope: parser.AnnotationScopeIngress,
107107
Risk: parser.AnnotationRiskMedium,
108108
Documentation: `This annotation controls which headers are exposed to response.
@@ -225,7 +225,7 @@ func (c cors) Parse(ing *networking.Ingress) (interface{}, error) {
225225
}
226226

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

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

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

internal/ingress/annotations/cors/main_test.go

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

7878
// Valid
7979
data[parser.GetAnnotationWithPrefix(corsEnableAnnotation)] = "true"
80-
data[parser.GetAnnotationWithPrefix(corsAllowHeadersAnnotation)] = "DNT,X-CustomHeader, Keep-Alive,User-Agent"
80+
data[parser.GetAnnotationWithPrefix(corsAllowHeadersAnnotation)] = "*, DNT,X-CustomHeader, Keep-Alive,User-Agent"
8181
data[parser.GetAnnotationWithPrefix(corsAllowCredentialsAnnotation)] = "false"
8282
data[parser.GetAnnotationWithPrefix(corsAllowMethodsAnnotation)] = "GET, PATCH"
8383
data[parser.GetAnnotationWithPrefix(corsAllowOriginAnnotation)] = "https://origin123.test.com:4443"
@@ -103,7 +103,7 @@ func TestIngressCorsConfigValid(t *testing.T) {
103103
t.Errorf("expected %v but returned %v", data[parser.GetAnnotationWithPrefix(corsAllowCredentialsAnnotation)], nginxCors.CorsAllowCredentials)
104104
}
105105

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

0 commit comments

Comments
 (0)