ctlsock: sanitize: handle multiple leading slashes
This commit is contained in:
parent
2bdd0ec802
commit
8bcae63a5a
|
@ -6,23 +6,25 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// SanitizePath adapts filepath.Clean for FUSE paths.
|
// SanitizePath adapts filepath.Clean for FUSE paths.
|
||||||
// 1) A leading slash is dropped
|
// 1) Leading slash(es) are dropped
|
||||||
// 2) It returns "" instead of "."
|
// 2) It returns "" instead of "."
|
||||||
// 3) If the cleaned path points above CWD (start with ".."), an empty string
|
// 3) If the cleaned path points above CWD (start with ".."), an empty string
|
||||||
// is returned
|
// is returned
|
||||||
// See the TestSanitizePath testcases for examples.
|
// See the TestSanitizePath testcases for examples.
|
||||||
func SanitizePath(path string) string {
|
func SanitizePath(path string) string {
|
||||||
|
// (1)
|
||||||
|
for len(path) > 0 && path[0] == '/' {
|
||||||
|
path = path[1:]
|
||||||
|
}
|
||||||
if len(path) == 0 {
|
if len(path) == 0 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
// Drop leading slash
|
|
||||||
if path[0] == '/' {
|
|
||||||
path = path[1:]
|
|
||||||
}
|
|
||||||
clean := filepath.Clean(path)
|
clean := filepath.Clean(path)
|
||||||
|
// (2)
|
||||||
if clean == "." {
|
if clean == "." {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
// (3)
|
||||||
if clean == ".." || strings.HasPrefix(clean, "../") {
|
if clean == ".." || strings.HasPrefix(clean, "../") {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ func TestSanitizePath(t *testing.T) {
|
||||||
{"foo/../..", ""},
|
{"foo/../..", ""},
|
||||||
{"foo/../../aaaaaa", ""},
|
{"foo/../../aaaaaa", ""},
|
||||||
{"/foo/../../aaaaaa", ""},
|
{"/foo/../../aaaaaa", ""},
|
||||||
|
{"/////", ""},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
res := SanitizePath(tc[0])
|
res := SanitizePath(tc[0])
|
||||||
|
|
Loading…
Reference in New Issue