diff --git a/drivers/local/driver.go b/drivers/local/driver.go index 3a1cd4af..e685fb1f 100644 --- a/drivers/local/driver.go +++ b/drivers/local/driver.go @@ -136,6 +136,9 @@ func (d *Local) MakeDir(ctx context.Context, parentDir model.Obj, dirName string func (d *Local) Move(ctx context.Context, srcObj, dstDir model.Obj) error { srcPath := srcObj.GetPath() dstPath := filepath.Join(dstDir.GetPath(), srcObj.GetName()) + if utils.IsSubPath(srcPath, dstPath) { + return fmt.Errorf("the destination folder is a subfolder of the source folder") + } err := os.Rename(srcPath, dstPath) if err != nil { return err @@ -156,6 +159,9 @@ func (d *Local) Rename(ctx context.Context, srcObj model.Obj, newName string) er func (d *Local) Copy(ctx context.Context, srcObj, dstDir model.Obj) error { srcPath := srcObj.GetPath() dstPath := filepath.Join(dstDir.GetPath(), srcObj.GetName()) + if utils.IsSubPath(srcPath, dstPath) { + return fmt.Errorf("the destination folder is a subfolder of the source folder") + } var err error if srcObj.IsDir() { err = utils.CopyDir(srcPath, dstPath) diff --git a/internal/op/storage.go b/internal/op/storage.go index fc96490d..3acdc38e 100644 --- a/internal/op/storage.go +++ b/internal/op/storage.go @@ -242,7 +242,7 @@ func getStoragesByPath(path string) []driver.Driver { storagesMap.Range(func(mountPath string, value driver.Driver) bool { mountPath = utils.GetActualMountPath(mountPath) // is this path - if utils.IsSubPath(path, mountPath) { + if utils.IsSubPath(mountPath, path) { slashCount := strings.Count(utils.PathAddSeparatorSuffix(mountPath), "/") // not the longest match if slashCount > curSlashCount { @@ -280,7 +280,7 @@ func GetStorageVirtualFilesByPath(prefix string) []model.Obj { for _, v := range storages { mountPath := utils.GetActualMountPath(v.GetStorage().MountPath) // Exclude prefix itself and non prefix - if len(prefix) >= len(mountPath) || !utils.IsSubPath(mountPath, prefix) { + if len(prefix) >= len(mountPath) || !utils.IsSubPath(prefix, mountPath) { continue } name := strings.SplitN(strings.TrimPrefix(mountPath[len(prefix):], "/"), "/", 2)[0] diff --git a/pkg/utils/path.go b/pkg/utils/path.go index 5a80d33b..9bd42a11 100644 --- a/pkg/utils/path.go +++ b/pkg/utils/path.go @@ -40,7 +40,7 @@ func PathEqual(path1, path2 string) bool { func IsSubPath(path string, subPath string) bool { path, subPath = FixAndCleanPath(path), FixAndCleanPath(subPath) - return path == subPath || strings.HasPrefix(path, PathAddSeparatorSuffix(subPath)) + return path == subPath || strings.HasPrefix(subPath, PathAddSeparatorSuffix(path)) } func Ext(path string) string {