Skip to content

Commit

Permalink
update regex
Browse files Browse the repository at this point in the history
  • Loading branch information
haywoodsh committed Sep 27, 2022
1 parent d6330fa commit 464a2f9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
4 changes: 2 additions & 2 deletions internal/k8s/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ func validateBackend(backend *networking.IngressBackend, fieldPath *field.Path)
}

const (
pathFmt = `/[^\s;\\]*`
pathFmt = `/[^\s;]*`
pathErrMsg = "must start with / and must not include any whitespace character or `;`"
)

Expand Down Expand Up @@ -950,7 +950,7 @@ func ValidateEscapedString(body string, examples ...string) error {
}

const (
illegalKeywordFmt = `/etc|/root`
illegalKeywordFmt = `/etc/|/root|/var|\\n|\\r`
illegalKeywordErrMsg = `must not contain invalid paths`
)

Expand Down
34 changes: 24 additions & 10 deletions internal/k8s/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3343,20 +3343,20 @@ func TestValidateRegexPath(t *testing.T) {
msg string
}{
{
regexPath: "~ ^/foo.*\\.jpg",
regexPath: "/foo.*\\.jpg",
msg: "case sensitive regexp",
},
{
regexPath: "~* ^/Bar.*\\.jpg",
regexPath: "/Bar.*\\.jpg",
msg: "case insensitive regexp",
},
{
regexPath: `~ ^/f\"oo.*\\.jpg`,
regexPath: `/f\"oo.*\\.jpg`,
msg: "regexp with escaped double quotes",
},
{
regexPath: "~ [0-9a-z]{4}[0-9]+",
msg: "regexp with brackets",
regexPath: "/[0-9a-z]{4}[0-9]+",
msg: "regexp with curly braces",
},
}

Expand All @@ -3375,19 +3375,19 @@ func TestValidateRegexPathFails(t *testing.T) {
msg string
}{
{
regexPath: "~ [{",
regexPath: "[{",
msg: "invalid regexp",
},
{
regexPath: `~ /foo"`,
regexPath: `/foo"`,
msg: "unescaped double quotes",
},
{
regexPath: `~"`,
regexPath: `"`,
msg: "empty regex",
},
{
regexPath: `~ /foo\`,
regexPath: `/foo\`,
msg: "ending in backslash",
},
}
Expand All @@ -3409,6 +3409,15 @@ func TestValidatePath(t *testing.T) {
"/a-1/_A/",
"/[A-Za-z]{6}/[a-z]{1,2}",
"/[0-9a-z]{4}[0-9]",
"/foo.*\\.jpg",
"/Bar.*\\.jpg",
`/f\"oo.*\\.jpg`,
"/[0-9a-z]{4}[0-9]+",
"/[a-z]{1,2}",
"/[A-Z]{6}",
"/[A-Z]{6}/[a-z]{1,2}",
"/path",
"/abc}{abc",
}

for _, path := range validPaths {
Expand All @@ -3425,7 +3434,9 @@ func TestValidatePath(t *testing.T) {
"/abc;",
`/path\`,
`/path\n`,
`/etc/nginx/secrets`,
`/var/run/secrets`,
"/{autoindex on; root /var/run/secrets;}location /tea",
"/{root}",
}

for _, path := range invalidPaths {
Expand Down Expand Up @@ -3475,6 +3486,9 @@ func TestValidateIllegalKeywords(t *testing.T) {
"/root",
"/etc/nginx/secrets",
"/etc/passwd",
"/var/run/secrets",
`\n`,
`\r`,
}

for _, path := range invalidPaths {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/configuration/validation/virtualserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1467,7 +1467,7 @@ func TestValidateRegexPath(t *testing.T) {
},
{
regexPath: "~ [0-9a-z]{4}[0-9]+",
msg: "regexp with brackets",
msg: "regexp with curly braces",
},
}

Expand Down

0 comments on commit 464a2f9

Please sign in to comment.