Skip to content

Commit

Permalink
generateRouteHostRegexp: Escape blanks
Browse files Browse the repository at this point in the history
Escape spaces, tabs, carriage returns, and linefeeds in the regular
expression for a route's path in HAProxy map files in order to ensure that
HAProxy can parse the resulting map files correctly.

Follow-up to commit 019c5ac.

This commit fixes bug 2074304.

https://bugzilla.redhat.com/show_bug.cgi?id=2074304

* pkg/router/template/util/haproxy/map_entry_test.go
(TestGenerateHttpRedirectMapEntry): Add a test with a path with a space, a
test with a path with a tab, a test with a path with a carriage return, and
a test with a path with a linefeed.
* pkg/router/template/util/util.go (GenerateRouteRegexp): Escape spaces,
tabs, carriage returns, and linefeeds in the pattern for the route's path.
  • Loading branch information
Miciah committed Apr 12, 2022
1 parent ee343ae commit 30dd5b7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pkg/router/template/util/haproxy/map_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,38 @@ func TestGenerateHttpRedirectMapEntry(t *testing.T) {
wildcard: false,
expectedKey: `^www\.example\.test\.?(:[0-9]+)?/x/y/z(/.*)?$`,
},
{
name: "host with path with a space",
backendKey: "test_host_path_with_space",
hostname: "www.example.test",
path: "/x y",
wildcard: false,
expectedKey: `^www\.example\.test\.?(:[0-9]+)?/x\x20y(/.*)?$`,
},
{
name: "host with path with a tab",
backendKey: "test_host_path_with_tab",
hostname: "www.example.test",
path: "/x\ty",
wildcard: false,
expectedKey: `^www\.example\.test\.?(:[0-9]+)?/x\ty(/.*)?$`,
},
{
name: "host with path with a CR",
backendKey: "test_host_path_with_cr",
hostname: "www.example.test",
path: "/x\ry",
wildcard: false,
expectedKey: `^www\.example\.test\.?(:[0-9]+)?/x\ry(/.*)?$`,
},
{
name: "host with path with a LF",
backendKey: "test_host_path_with_lf",
hostname: "www.example.test",
path: "/x\ny",
wildcard: false,
expectedKey: `^www\.example\.test\.?(:[0-9]+)?/x\ny(/.*)?$`,
},
{
name: "wildcard host",
backendKey: "test_wildcard_host",
Expand Down
8 changes: 8 additions & 0 deletions pkg/router/template/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ func GenerateRouteRegexp(hostname, path string, wildcard bool) string {
subpathRE = "(/.*)?"
}

// The path could contain space characters, which must be escaped in
// HAProxy map files. See
// <https://bugzilla.redhat.com/show_bug.cgi?id=2074304>.
pathRE = strings.ReplaceAll(pathRE, ` `, `\x20`)
pathRE = strings.ReplaceAll(pathRE, "\t", `\t`)
pathRE = strings.ReplaceAll(pathRE, "\r", `\r`)
pathRE = strings.ReplaceAll(pathRE, "\n", `\n`)

return "^" + hostRE + portRE + pathRE + subpathRE + "$"
}

Expand Down

0 comments on commit 30dd5b7

Please sign in to comment.