Security Fix: clean paths correctly to avoid circumvention of BlacklistedPaths

This commit is contained in:
Moritz Marquardt 2023-08-27 10:13:15 +02:00
parent d720d25e42
commit 56d3e291c4
3 changed files with 72 additions and 4 deletions

View file

@ -1,6 +1,8 @@
package utils
import (
"net/url"
"path"
"strings"
)
@ -11,3 +13,15 @@ func TrimHostPort(host string) string {
}
return host
}
func CleanPath(uriPath string) string {
unescapedPath, _ := url.PathUnescape(uriPath)
cleanedPath := path.Join("/", unescapedPath)
// If the path refers to a directory, add a trailing slash.
if !strings.HasSuffix(cleanedPath, "/") && (strings.HasSuffix(unescapedPath, "/") || strings.HasSuffix(unescapedPath, "/.") || strings.HasSuffix(unescapedPath, "/..")) {
cleanedPath += "/"
}
return cleanedPath
}