allow curly braces to be used in regex paths

This commit is contained in:
Zenara Daley 2018-10-04 10:58:38 -04:00
parent b678c413f9
commit bd3f56eaa0
4 changed files with 46 additions and 48 deletions

View file

@ -320,8 +320,6 @@ func enforceRegexModifier(input interface{}) bool {
// buildLocation produces the location string, if the ingress has redirects
// (specified through the nginx.ingress.kubernetes.io/rewrite-target annotation)
// TODO: return quotes around returned location path to prevent regex from breaking under certain conditions, see:
// https://github.com/kubernetes/ingress-nginx/issues/3155
func buildLocation(input interface{}, enforceRegex bool) string {
location, ok := input.(*ingress.Location)
if !ok {
@ -340,11 +338,11 @@ func buildLocation(input interface{}, enforceRegex bool) string {
// Not treat the slash after "location path" as a part of baseuri
baseuri = fmt.Sprintf(`\/?%s`, baseuri)
}
return fmt.Sprintf(`~* ^%s%s`, path, baseuri)
return fmt.Sprintf(`~* "^%s%s"`, path, baseuri)
}
if enforceRegex {
return fmt.Sprintf(`~* ^%s`, path)
return fmt.Sprintf(`~* "^%s"`, path)
}
return path
}
@ -528,15 +526,15 @@ subs_filter '%v' '$1<base href="%v://$http_host%v">' ro;
// special case redirect to /
// ie /something to /
return fmt.Sprintf(`
rewrite (?i)%s(.*) /$1 break;
rewrite (?i)%s$ / break;
rewrite "(?i)%s(.*)" /$1 break;
rewrite "(?i)%s$" / break;
%v%v %s%s;
%v`, path, location.Path, xForwardedPrefix, proxyPass, proto, upstreamName, abu)
}
return fmt.Sprintf(`
rewrite (?i)%s(.*) %s/$1 break;
rewrite (?i)%s$ %s/ break;
rewrite "(?i)%s(.*)" %s/$1 break;
rewrite "(?i)%s$" %s/ break;
%v%v %s%s;
%v`, path, location.Rewrite.Target, location.Path, location.Rewrite.Target, xForwardedPrefix, proxyPass, proto, upstreamName, abu)
}

View file

@ -129,8 +129,8 @@ var (
"/jenkins",
"~* ^/",
`
rewrite (?i)/(.*) /jenkins/$1 break;
rewrite (?i)/$ /jenkins/ break;
rewrite "(?i)/(.*)" /jenkins/$1 break;
rewrite "(?i)/$" /jenkins/ break;
proxy_pass http://upstream-name;
`,
false,
@ -143,10 +143,10 @@ proxy_pass http://upstream-name;
"redirect /something to /": {
"/something",
"/",
`~* ^/something\/?(?<baseuri>.*)`,
`~* "^/something\/?(?<baseuri>.*)"`,
`
rewrite (?i)/something/(.*) /$1 break;
rewrite (?i)/something$ / break;
rewrite "(?i)/something/(.*)" /$1 break;
rewrite "(?i)/something$" / break;
proxy_pass http://upstream-name;
`,
false,
@ -159,10 +159,10 @@ proxy_pass http://upstream-name;
"redirect /end-with-slash/ to /not-root": {
"/end-with-slash/",
"/not-root",
"~* ^/end-with-slash/(?<baseuri>.*)",
`~* "^/end-with-slash/(?<baseuri>.*)"`,
`
rewrite (?i)/end-with-slash/(.*) /not-root/$1 break;
rewrite (?i)/end-with-slash/$ /not-root/ break;
rewrite "(?i)/end-with-slash/(.*)" /not-root/$1 break;
rewrite "(?i)/end-with-slash/$" /not-root/ break;
proxy_pass http://upstream-name;
`,
false,
@ -175,10 +175,10 @@ proxy_pass http://upstream-name;
"redirect /something-complex to /not-root": {
"/something-complex",
"/not-root",
`~* ^/something-complex\/?(?<baseuri>.*)`,
`~* "^/something-complex\/?(?<baseuri>.*)"`,
`
rewrite (?i)/something-complex/(.*) /not-root/$1 break;
rewrite (?i)/something-complex$ /not-root/ break;
rewrite "(?i)/something-complex/(.*)" /not-root/$1 break;
rewrite "(?i)/something-complex$" /not-root/ break;
proxy_pass http://upstream-name;
`,
false,
@ -193,8 +193,8 @@ proxy_pass http://upstream-name;
"/jenkins",
"~* ^/",
`
rewrite (?i)/(.*) /jenkins/$1 break;
rewrite (?i)/$ /jenkins/ break;
rewrite "(?i)/(.*)" /jenkins/$1 break;
rewrite "(?i)/$" /jenkins/ break;
proxy_pass http://upstream-name;
set_escape_uri $escaped_base_uri $baseuri;
@ -210,10 +210,10 @@ subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1<base href="
"redirect /something to / and rewrite": {
"/something",
"/",
`~* ^/something\/?(?<baseuri>.*)`,
`~* "^/something\/?(?<baseuri>.*)"`,
`
rewrite (?i)/something/(.*) /$1 break;
rewrite (?i)/something$ / break;
rewrite "(?i)/something/(.*)" /$1 break;
rewrite "(?i)/something$" / break;
proxy_pass http://upstream-name;
set_escape_uri $escaped_base_uri $baseuri;
@ -229,10 +229,10 @@ subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1<base href="
"redirect /end-with-slash/ to /not-root and rewrite": {
"/end-with-slash/",
"/not-root",
`~* ^/end-with-slash/(?<baseuri>.*)`,
`~* "^/end-with-slash/(?<baseuri>.*)"`,
`
rewrite (?i)/end-with-slash/(.*) /not-root/$1 break;
rewrite (?i)/end-with-slash/$ /not-root/ break;
rewrite "(?i)/end-with-slash/(.*)" /not-root/$1 break;
rewrite "(?i)/end-with-slash/$" /not-root/ break;
proxy_pass http://upstream-name;
set_escape_uri $escaped_base_uri $baseuri;
@ -248,10 +248,10 @@ subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1<base href="
"redirect /something-complex to /not-root and rewrite": {
"/something-complex",
"/not-root",
`~* ^/something-complex\/?(?<baseuri>.*)`,
`~* "^/something-complex\/?(?<baseuri>.*)"`,
`
rewrite (?i)/something-complex/(.*) /not-root/$1 break;
rewrite (?i)/something-complex$ /not-root/ break;
rewrite "(?i)/something-complex/(.*)" /not-root/$1 break;
rewrite "(?i)/something-complex$" /not-root/ break;
proxy_pass http://upstream-name;
set_escape_uri $escaped_base_uri $baseuri;
@ -267,10 +267,10 @@ subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1<base href="
"redirect /something to / and rewrite with specific scheme": {
"/something",
"/",
`~* ^/something\/?(?<baseuri>.*)`,
`~* "^/something\/?(?<baseuri>.*)"`,
`
rewrite (?i)/something/(.*) /$1 break;
rewrite (?i)/something$ / break;
rewrite "(?i)/something/(.*)" /$1 break;
rewrite "(?i)/something$" / break;
proxy_pass http://upstream-name;
set_escape_uri $escaped_base_uri $baseuri;
@ -288,8 +288,8 @@ subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1<base href="
"/something",
`~* ^/`,
`
rewrite (?i)/(.*) /something/$1 break;
rewrite (?i)/$ /something/ break;
rewrite "(?i)/(.*)" /something/$1 break;
rewrite "(?i)/$" /something/ break;
proxy_pass http://sticky-upstream-name;
`,
false,
@ -304,8 +304,8 @@ proxy_pass http://sticky-upstream-name;
"/something",
`~* ^/`,
`
rewrite (?i)/(.*) /something/$1 break;
rewrite (?i)/$ /something/ break;
rewrite "(?i)/(.*)" /something/$1 break;
rewrite "(?i)/$" /something/ break;
proxy_pass http://upstream_balancer;
`,
false,
@ -318,10 +318,10 @@ proxy_pass http://upstream_balancer;
"add the X-Forwarded-Prefix header": {
"/there",
"/something",
`~* ^/there\/?(?<baseuri>.*)`,
`~* "^/there\/?(?<baseuri>.*)"`,
`
rewrite (?i)/there/(.*) /something/$1 break;
rewrite (?i)/there$ /something/ break;
rewrite "(?i)/there/(.*)" /something/$1 break;
rewrite "(?i)/there$" /something/ break;
proxy_set_header X-Forwarded-Prefix "/there/";
proxy_pass http://sticky-upstream-name;
`,
@ -335,7 +335,7 @@ proxy_pass http://sticky-upstream-name;
"use ~* location modifier when ingress does not use rewrite/regex target but at least one other ingress does": {
"/something",
"/something",
"~* ^/something",
`~* "^/something"`,
"proxy_pass http://upstream-name;",
false,
"",