fix(local): disable copying or moving to subfolders (close #2760)

pull/2771/head
Noah Hsu 2022-12-20 16:27:04 +08:00
parent 146619134d
commit d756cf3e9f
3 changed files with 9 additions and 3 deletions

View File

@ -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 { func (d *Local) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
srcPath := srcObj.GetPath() srcPath := srcObj.GetPath()
dstPath := filepath.Join(dstDir.GetPath(), srcObj.GetName()) 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) err := os.Rename(srcPath, dstPath)
if err != nil { if err != nil {
return err 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 { func (d *Local) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
srcPath := srcObj.GetPath() srcPath := srcObj.GetPath()
dstPath := filepath.Join(dstDir.GetPath(), srcObj.GetName()) 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 var err error
if srcObj.IsDir() { if srcObj.IsDir() {
err = utils.CopyDir(srcPath, dstPath) err = utils.CopyDir(srcPath, dstPath)

View File

@ -242,7 +242,7 @@ func getStoragesByPath(path string) []driver.Driver {
storagesMap.Range(func(mountPath string, value driver.Driver) bool { storagesMap.Range(func(mountPath string, value driver.Driver) bool {
mountPath = utils.GetActualMountPath(mountPath) mountPath = utils.GetActualMountPath(mountPath)
// is this path // is this path
if utils.IsSubPath(path, mountPath) { if utils.IsSubPath(mountPath, path) {
slashCount := strings.Count(utils.PathAddSeparatorSuffix(mountPath), "/") slashCount := strings.Count(utils.PathAddSeparatorSuffix(mountPath), "/")
// not the longest match // not the longest match
if slashCount > curSlashCount { if slashCount > curSlashCount {
@ -280,7 +280,7 @@ func GetStorageVirtualFilesByPath(prefix string) []model.Obj {
for _, v := range storages { for _, v := range storages {
mountPath := utils.GetActualMountPath(v.GetStorage().MountPath) mountPath := utils.GetActualMountPath(v.GetStorage().MountPath)
// Exclude prefix itself and non prefix // 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 continue
} }
name := strings.SplitN(strings.TrimPrefix(mountPath[len(prefix):], "/"), "/", 2)[0] name := strings.SplitN(strings.TrimPrefix(mountPath[len(prefix):], "/"), "/", 2)[0]

View File

@ -40,7 +40,7 @@ func PathEqual(path1, path2 string) bool {
func IsSubPath(path string, subPath string) bool { func IsSubPath(path string, subPath string) bool {
path, subPath = FixAndCleanPath(path), FixAndCleanPath(subPath) 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 { func Ext(path string) string {