2020-05-09 17:36:41 +02:00
|
|
|
package ctlsocksrv
|
2016-12-10 12:59:54 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
2017-01-29 15:24:47 +01:00
|
|
|
"strings"
|
2016-12-10 12:59:54 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// SanitizePath adapts filepath.Clean for FUSE paths.
|
2017-02-05 18:05:35 +01:00
|
|
|
// 1) Leading slash(es) are dropped
|
2016-12-10 12:59:54 +01:00
|
|
|
// 2) It returns "" instead of "."
|
2017-01-29 15:24:47 +01:00
|
|
|
// 3) If the cleaned path points above CWD (start with ".."), an empty string
|
|
|
|
// is returned
|
2016-12-10 12:59:54 +01:00
|
|
|
// See the TestSanitizePath testcases for examples.
|
|
|
|
func SanitizePath(path string) string {
|
2017-02-05 18:05:35 +01:00
|
|
|
// (1)
|
|
|
|
for len(path) > 0 && path[0] == '/' {
|
|
|
|
path = path[1:]
|
|
|
|
}
|
2017-01-29 15:24:47 +01:00
|
|
|
if len(path) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
2016-12-10 12:59:54 +01:00
|
|
|
clean := filepath.Clean(path)
|
2017-02-05 18:05:35 +01:00
|
|
|
// (2)
|
2017-01-29 15:24:47 +01:00
|
|
|
if clean == "." {
|
2016-12-10 12:59:54 +01:00
|
|
|
return ""
|
|
|
|
}
|
2017-02-05 18:05:35 +01:00
|
|
|
// (3)
|
2017-01-29 15:24:47 +01:00
|
|
|
if clean == ".." || strings.HasPrefix(clean, "../") {
|
|
|
|
return ""
|
2016-12-10 12:59:54 +01:00
|
|
|
}
|
|
|
|
return clean
|
|
|
|
}
|