Skip to content

Commit

Permalink
Verify paths using Perl5-compatible regex engine
Browse files Browse the repository at this point in the history
  • Loading branch information
jjngx committed Oct 3, 2023
1 parent 07928e2 commit ab72f32
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/aws/aws-sdk-go-v2/config v1.18.42
github.com/aws/aws-sdk-go-v2/service/marketplacemetering v1.16.0
github.com/cert-manager/cert-manager v1.13.1
github.com/dlclark/regexp2 v1.10.0
github.com/go-chi/chi/v5 v5.0.10
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/golang/glog v1.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.10.0 h1:+/GIL799phkJqYW+3YbOd8LCcbHzT0Pbo8zl70MHsq0=
github.com/dlclark/regexp2 v1.10.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g=
Expand Down
6 changes: 5 additions & 1 deletion internal/k8s/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sort"
"strings"

"github.com/dlclark/regexp2"
"github.com/nginxinc/kubernetes-ingress/internal/configs"
ap_validation "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/validation"
networking "k8s.io/api/networking/v1"
Expand Down Expand Up @@ -871,8 +872,11 @@ func validatePath(path string, pathType *networking.PathType, fieldPath *field.P
return allErrs
}

// validateRegexPath validates correctness of the string representing the path.
//
// Internally it uses Perl5 compatible regexp2 package.
func validateRegexPath(path string, fieldPath *field.Path) field.ErrorList {
if _, err := regexp.Compile(path); err != nil {
if _, err := regexp2.Compile(path, 0); err != nil {
return field.ErrorList{field.Invalid(fieldPath, path, fmt.Sprintf("must be a valid regular expression: %v", err))}
}
if err := ValidateEscapedString(path, "*.jpg", "^/images/image_*.png$"); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions internal/k8s/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3630,6 +3630,10 @@ func TestValidateRegexPath(t *testing.T) {
regexPath: "/[0-9a-z]{4}[0-9]+",
msg: "regexp with curly braces",
},
{
regexPath: "~ ^/coffee/(?!.*\\/latte)(?!.*\\/americano)(.*)",
msg: "regexp with Perl5 regex",
},
}

for _, test := range tests {
Expand Down
6 changes: 5 additions & 1 deletion pkg/apis/configuration/validation/virtualserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"strings"

"github.com/dlclark/regexp2"
"github.com/nginxinc/kubernetes-ingress/internal/configs"
v1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1"
"k8s.io/apimachinery/pkg/util/sets"
Expand Down Expand Up @@ -1233,8 +1234,11 @@ func validateRoutePath(path string, fieldPath *field.Path) field.ErrorList {
return allErrs
}

// validateRegexPath validates correctness of the string representing the path.
//
// Internally it uses Perl5 compatible regexp2 package.
func validateRegexPath(path string, fieldPath *field.Path) field.ErrorList {
if _, err := regexp.Compile(path); err != nil {
if _, err := regexp2.Compile(path, 0); err != nil {
return field.ErrorList{field.Invalid(fieldPath, path, fmt.Sprintf("must be a valid regular expression: %v", err))}
}
if err := ValidateEscapedString(path, "*.jpg", "^/images/image_*.png$"); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/configuration/validation/virtualserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,10 @@ func TestValidateRegexPath(t *testing.T) {
regexPath: "~ [0-9a-z]{4}[0-9]+",
msg: "regexp with curly braces",
},
{
regexPath: "~ ^/coffee/(?!.*\\/latte)(?!.*\\/americano)(.*)",
msg: "regex with backtracking",
},
}

for _, test := range tests {
Expand Down

0 comments on commit ab72f32

Please sign in to comment.