Skip to content

Use PCRE on proxy redirect property #13264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/ingress/annotations/parser/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ var (
// URLWithNginxVariableRegex defines a url that can contain nginx variables.
// It is a risky operation
URLWithNginxVariableRegex = regexp.MustCompile("^[" + extendedAlphaNumeric + urlEnabledChars + "$]*$")
// Used for NGINX properties that accepts URLs with PCRE regex
URLWithPCRERegex = regexp.MustCompile("^[" + regexEnabledChars + alphaNumericChars + urlEnabledChars + "]*$")
// MaliciousRegex defines chars that are known to inject RCE
MaliciousRegex = regexp.MustCompile(`\r|\n`)
)
Expand Down
4 changes: 2 additions & 2 deletions internal/ingress/annotations/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ var proxyAnnotations = parser.Annotation{
Documentation: `This annotation enables or disables buffering of a client request body.`,
},
proxyRedirectFromAnnotation: {
Validator: parser.ValidateRegex(parser.URLIsValidRegex, true),
Validator: parser.ValidateRegex(parser.URLWithPCRERegex, true),
Scope: parser.AnnotationScopeLocation,
Risk: parser.AnnotationRiskMedium,
Documentation: `The annotations proxy-redirect-from and proxy-redirect-to will set the first and second parameters of NGINX's proxy_redirect directive respectively`,
},
proxyRedirectToAnnotation: {
Validator: parser.ValidateRegex(parser.URLIsValidRegex, true),
Validator: parser.ValidateRegex(parser.URLWithPCRERegex, true),
Scope: parser.AnnotationScopeLocation,
Risk: parser.AnnotationRiskMedium,
Documentation: `The annotations proxy-redirect-from and proxy-redirect-to will set the first and second parameters of NGINX's proxy_redirect directive respectively`,
Expand Down
17 changes: 17 additions & 0 deletions test/e2e/annotations/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ var _ = framework.DescribeAnnotation("proxy-*", func() {
})
})

ginkgo.It("should set proxy_redirect with PCRE", func() {
proxyRedirectFrom := "~^(http|https)://hello.com/v1/(.*)$"
proxyRedirectTo := "$1://$host/$2"

annotations := make(map[string]string)
annotations["nginx.ingress.kubernetes.io/proxy-redirect-from"] = proxyRedirectFrom
annotations["nginx.ingress.kubernetes.io/proxy-redirect-to"] = proxyRedirectTo

ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, annotations)
f.EnsureIngress(ing)

f.WaitForNginxServer(host,
func(server string) bool {
return strings.Contains(server, fmt.Sprintf("proxy_redirect %s %s;", proxyRedirectFrom, proxyRedirectTo))
})
})

ginkgo.It("should set proxy client-max-body-size to 8m", func() {
proxyBodySize := "8m"

Expand Down