532ef15417
Paths that start with ".." were previously accepted as-is.
31 lines
619 B
Go
31 lines
619 B
Go
package ctlsock
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// SanitizePath adapts filepath.Clean for FUSE paths.
|
|
// 1) A leading slash is dropped
|
|
// 2) It returns "" instead of "."
|
|
// 3) If the cleaned path points above CWD (start with ".."), an empty string
|
|
// is returned
|
|
// See the TestSanitizePath testcases for examples.
|
|
func SanitizePath(path string) string {
|
|
if len(path) == 0 {
|
|
return ""
|
|
}
|
|
// Drop leading slash
|
|
if path[0] == '/' {
|
|
path = path[1:]
|
|
}
|
|
clean := filepath.Clean(path)
|
|
if clean == "." {
|
|
return ""
|
|
}
|
|
if clean == ".." || strings.HasPrefix(clean, "../") {
|
|
return ""
|
|
}
|
|
return clean
|
|
}
|