localfs: fix subdir check if the user has the root dir as home

This commit is contained in:
Nicola Murino 2021-04-25 14:36:29 +02:00
parent 3941255733
commit 5be4b6bd44
No known key found for this signature in database
GPG key ID: 2F1FB59433D5A8CB
2 changed files with 22 additions and 1 deletions

View file

@ -342,6 +342,21 @@ func TestWithInvalidHome(t *testing.T) {
assert.Error(t, err)
}
func TestResolveWithRootDir(t *testing.T) {
u := dataprovider.User{}
if runtime.GOOS == osWindows {
u.HomeDir = "C:\\"
} else {
u.HomeDir = "/"
}
fs, err := u.GetFilesystem("")
assert.NoError(t, err)
rel, err := filepath.Rel(u.HomeDir, os.TempDir())
assert.NoError(t, err)
p, err := fs.ResolvePath(rel)
assert.NoError(t, err, "path %v", p)
}
func TestSFTPGetUsedQuota(t *testing.T) {
u := dataprovider.User{}
u.HomeDir = "home_rel_path"

View file

@ -359,7 +359,13 @@ func (fs *OsFs) isSubDir(sub string) error {
err = fmt.Errorf("path %#v is not inside %#v", sub, parent)
return err
}
if !strings.HasPrefix(sub, parent+string(os.PathSeparator)) {
separator := string(os.PathSeparator)
if parent == filepath.Dir(parent) {
// parent is the root dir, on Windows we can have C:\, D:\ and so on here
// so we still need the prefix check
separator = ""
}
if !strings.HasPrefix(sub, parent+separator) {
err = fmt.Errorf("path %#v is not inside %#v", sub, parent)
return err
}