Skip to content

Commit

Permalink
fix: Validate x-forwarded-prefix annotation with RegexPathWithCapture (
Browse files Browse the repository at this point in the history
  • Loading branch information
bodgit authored Nov 1, 2023
1 parent 9cb3919 commit 9cdd51d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion internal/ingress/annotations/parser/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var (
NGINXVariable = regexp.MustCompile(`^[A-Za-z0-9\-\_\$\{\}]*$`)
// RegexPathWithCapture allows entries that SHOULD start with "/" and may contain alphanumeric + capture
// character for regex based paths, like /something/$1/anything/$2
RegexPathWithCapture = regexp.MustCompile(`^/[` + alphaNumericChars + `\/\$]*$`)
RegexPathWithCapture = regexp.MustCompile(`^/?[` + alphaNumericChars + `\/\$]*$`)
// HeadersVariable defines a regex that allows headers separated by comma
HeadersVariable = regexp.MustCompile(`^[A-Za-z0-9-_, ]*$`)
// URLWithNginxVariableRegex defines a url that can contain nginx variables.
Expand Down
9 changes: 5 additions & 4 deletions internal/ingress/annotations/xforwardedprefix/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ var xForwardedForAnnotations = parser.Annotation{
Group: "backend",
Annotations: parser.AnnotationFields{
xForwardedForPrefixAnnotation: {
Validator: parser.ValidateRegex(parser.BasicCharsRegex, true),
Scope: parser.AnnotationScopeLocation,
Risk: parser.AnnotationRiskLow, // Low, as it allows regexes but on a very limited set
Documentation: `This annotation can be used to add the non-standard X-Forwarded-Prefix header to the upstream request with a string value`,
Validator: parser.ValidateRegex(parser.RegexPathWithCapture, true),
Scope: parser.AnnotationScopeLocation,
Risk: parser.AnnotationRiskMedium,
Documentation: `This annotation can be used to add the non-standard X-Forwarded-Prefix header to the upstream request with a string value. It can
contain regular characters and captured groups specified as '$1', '$2', etc.`,
},
},
}
Expand Down
1 change: 1 addition & 0 deletions internal/ingress/annotations/xforwardedprefix/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestParse(t *testing.T) {
{map[string]string{annotation: "true"}, "true"},
{map[string]string{annotation: "1"}, "1"},
{map[string]string{annotation: ""}, ""},
{map[string]string{annotation: "/$1"}, "/$1"},
{map[string]string{}, ""},
{nil, ""},
}
Expand Down

0 comments on commit 9cdd51d

Please sign in to comment.