ctlsock: interpret paths that point above CWD as ""
Paths that start with ".." were previously accepted as-is.
This commit is contained in:
parent
6166dad05c
commit
532ef15417
@ -2,19 +2,29 @@ package ctlsock
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SanitizePath adapts filepath.Clean for FUSE paths.
|
// SanitizePath adapts filepath.Clean for FUSE paths.
|
||||||
// 1) It always returns a relative path
|
// 1) A leading slash is dropped
|
||||||
// 2) It returns "" instead of "."
|
// 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.
|
// See the TestSanitizePath testcases for examples.
|
||||||
func SanitizePath(path string) string {
|
func SanitizePath(path string) string {
|
||||||
clean := filepath.Clean(path)
|
if len(path) == 0 {
|
||||||
if clean == "." || clean == "/" {
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
if clean[0] == '/' {
|
// Drop leading slash
|
||||||
clean = clean[1:]
|
if path[0] == '/' {
|
||||||
|
path = path[1:]
|
||||||
|
}
|
||||||
|
clean := filepath.Clean(path)
|
||||||
|
if clean == "." {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if clean == ".." || strings.HasPrefix(clean, "../") {
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
return clean
|
return clean
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,10 @@ func TestSanitizePath(t *testing.T) {
|
|||||||
{"/foo/", "foo"},
|
{"/foo/", "foo"},
|
||||||
{"/foo/./foo", "foo/foo"},
|
{"/foo/./foo", "foo/foo"},
|
||||||
{"./", ""},
|
{"./", ""},
|
||||||
|
{"..", ""},
|
||||||
|
{"foo/../..", ""},
|
||||||
|
{"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
Block a user