diff --git a/pkg/api/validation/validation.go b/pkg/api/validation/validation.go index 3563f8bca26..8553c82b750 100644 --- a/pkg/api/validation/validation.go +++ b/pkg/api/validation/validation.go @@ -1314,8 +1314,8 @@ func validateHTTPGetAction(http *api.HTTPGetAction, fldPath *field.Path) field.E allErrors = append(allErrors, field.NotSupported(fldPath.Child("scheme"), http.Scheme, supportedHTTPSchemes.List())) } for _, header := range http.HTTPHeaders { - if !validation.IsHTTPHeaderName(header.Name) { - allErrors = append(allErrors, field.Invalid(fldPath.Child("httpHeaders"), header.Name, fmt.Sprintf("name must match %s", validation.HTTPHeaderNameFmt))) + for _, msg := range validation.IsHTTPHeaderName(header.Name) { + allErrors = append(allErrors, field.Invalid(fldPath.Child("httpHeaders"), header.Name, msg)) } } return allErrors diff --git a/pkg/util/validation/validation.go b/pkg/util/validation/validation.go index 75aa617ac5b..ee3dba3caed 100644 --- a/pkg/util/validation/validation.go +++ b/pkg/util/validation/validation.go @@ -234,14 +234,17 @@ func IsValidPercent(percent string) []string { return nil } -const HTTPHeaderNameFmt string = "[-A-Za-z0-9]+" +const httpHeaderNameFmt string = "[-A-Za-z0-9]+" -var httpHeaderNameRegexp = regexp.MustCompile("^" + HTTPHeaderNameFmt + "$") +var httpHeaderNameRegexp = regexp.MustCompile("^" + httpHeaderNameFmt + "$") // IsHTTPHeaderName checks that a string conforms to the Go HTTP library's // definition of a valid header field name (a stricter subset than RFC7230). -func IsHTTPHeaderName(value string) bool { - return httpHeaderNameRegexp.MatchString(value) +func IsHTTPHeaderName(value string) []string { + if !httpHeaderNameRegexp.MatchString(value) { + return []string{RegexError(httpHeaderNameFmt, "X-Header-Name")} + } + return nil } // MaxLenError returns a string explanation of a "string too long" validation diff --git a/pkg/util/validation/validation_test.go b/pkg/util/validation/validation_test.go index 47f59db41cd..fe4389cddbc 100644 --- a/pkg/util/validation/validation_test.go +++ b/pkg/util/validation/validation_test.go @@ -321,8 +321,8 @@ func TestIsHTTPHeaderName(t *testing.T) { "A", "AB", "AbC", "A1", "-A", "A-", "A-B", "A-1", "A--1--2--B", "--123-ABC", } for _, val := range goodValues { - if !IsHTTPHeaderName(val) { - t.Errorf("expected true for '%s'", val) + if msgs := IsHTTPHeaderName(val); len(msgs) != 0 { + t.Errorf("expected true for '%s': %v", val, msgs) } } @@ -333,7 +333,7 @@ func TestIsHTTPHeaderName(t *testing.T) { "?", "@", "{", } for _, val := range badValues { - if IsHTTPHeaderName(val) { + if msgs := IsHTTPHeaderName(val); len(msgs) == 0 { t.Errorf("expected false for '%s'", val) } }