From bbd8c1d707ec0a84c1d9bebba02a0af225506b54 Mon Sep 17 00:00:00 2001 From: Jacob Rief Date: Mon, 23 Sep 2024 16:36:40 +0200 Subject: [PATCH] Prevent copying folders into itself --- finder/admin/folder.py | 8 ++++---- finder/models/folder.py | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/finder/admin/folder.py b/finder/admin/folder.py index c17162cfa..72f2e50c0 100644 --- a/finder/admin/folder.py +++ b/finder/admin/folder.py @@ -237,10 +237,10 @@ def copy_inodes(self, request, folder_id): current_folder = self.get_object(request, folder_id) inode_ids = body.get('inode_ids', []) for inode in FolderModel.objects.filter_inodes(id__in=inode_ids): - if next(current_folder.listdir(name=inode.name, is_folder=True), None): - msg = gettext("A folder named “{name}” already exists in destination folder.") - return HttpResponseBadRequest(msg.format(name=inode.name), status=409) - inode.copy_to(current_folder, owner=request.user) + try: + inode.copy_to(current_folder, owner=request.user) + except RecursionError as exc: + return HttpResponseBadRequest(str(exc), status=409) return JsonResponse({ 'inodes': self.get_inodes(parent=current_folder), }) diff --git a/finder/models/folder.py b/finder/models/folder.py index 528073bd9..c3cce15fc 100644 --- a/finder/models/folder.py +++ b/finder/models/folder.py @@ -159,6 +159,11 @@ def copy_to(self, folder, **kwargs): """ Copies the folder to a destination folder and returns it. """ + for ancestor in folder.ancestors: + if ancestor.id == self.id: + msg = "Folder named “{source}” can not become the descendant of destination folder “{target}”." + raise RecursionError(gettext(msg).format(source=self.name, target=folder.name)) + kwargs.setdefault('name', self.name) kwargs.setdefault('owner', self.owner) kwargs.update(parent=folder)