httpserver: Fix #1859 by cleaning paths when matching them

Signed-off-by: Matthew Holt <mholt@users.noreply.github.com>
This commit is contained in:
Matthew Holt
2017-09-08 07:19:52 -06:00
parent 32bb6a4cde
commit f6d75bb79a
3 changed files with 57 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ package httpserver
import (
"net/http"
"path"
"strings"
)
@@ -16,10 +17,27 @@ type Path string
// Path matching will probably not always be a direct
// comparison; this method assures that paths can be
// easily and consistently matched.
//
// Multiple slashes are collapsed/merged. See issue #1859.
func (p Path) Matches(base string) bool {
if base == "/" {
if base == "/" || base == "" {
return true
}
// sanitize the paths for comparison, very important
// (slightly lossy if the base path requires multiple
// consecutive forward slashes, since those will be merged)
pHasTrailingSlash := strings.HasSuffix(string(p), "/")
baseHasTrailingSlash := strings.HasSuffix(base, "/")
p = Path(path.Clean(string(p)))
base = path.Clean(base)
if pHasTrailingSlash {
p += "/"
}
if baseHasTrailingSlash {
base += "/"
}
if CaseSensitivePath {
return strings.HasPrefix(string(p), base)
}