Skip to content

Commit

Permalink
Prevent copying folders into itself
Browse files Browse the repository at this point in the history
  • Loading branch information
jrief committed Sep 23, 2024
1 parent bb97143 commit bbd8c1d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
8 changes: 4 additions & 4 deletions finder/admin/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
Expand Down
5 changes: 5 additions & 0 deletions finder/models/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bbd8c1d

Please sign in to comment.