diff --git a/cmd/admin-handlers.go b/cmd/admin-handlers.go index 6b7a68029..b9084368a 100644 --- a/cmd/admin-handlers.go +++ b/cmd/admin-handlers.go @@ -36,6 +36,7 @@ import ( "net/url" "os" "path" + "path/filepath" "regexp" "runtime" "sort" @@ -3172,11 +3173,11 @@ func (a adminAPIHandlers) InspectDataHandler(w http.ResponseWriter, r *http.Requ writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrInvalidRequest), r.URL) return } - file = strings.ReplaceAll(file, string(os.PathSeparator), "/") + file = filepath.ToSlash(file) // Reject attempts to traverse parent or absolute paths. - if strings.Contains(file, "..") || strings.Contains(volume, "..") { - writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAccessDenied), r.URL) + if hasBadPathComponent(volume) || hasBadPathComponent(file) { + writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrInvalidResourceName), r.URL) return } diff --git a/cmd/generic-handlers.go b/cmd/generic-handlers.go index 951a637e5..8c7a40270 100644 --- a/cmd/generic-handlers.go +++ b/cmd/generic-handlers.go @@ -310,6 +310,13 @@ func hasBadHost(host string) error { // Check if the incoming path has bad path components, // such as ".." and "." func hasBadPathComponent(path string) bool { + if len(path) > 4096 { + // path cannot be greater than Linux PATH_MAX + // this is to avoid a busy loop, that can happen + // if the caller sends path of following style + // a/a/a/a/a/a/a/a... + return true + } path = filepath.ToSlash(strings.TrimSpace(path)) // For windows '\' must be converted to '/' for _, p := range strings.Split(path, SlashSeparator) { switch strings.TrimSpace(p) {