From c281b1a9925532b703bb324ab0cc4025df2ea7dd Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Tue, 23 Sep 2025 16:06:23 -0500 Subject: [PATCH] site_file: catch for moving directory into itself --- models/site_file.rb | 4 ++++ tests/site_file_tests.rb | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/models/site_file.rb b/models/site_file.rb index 513638fd..8a7e8c56 100644 --- a/models/site_file.rb +++ b/models/site_file.rb @@ -89,6 +89,10 @@ class SiteFile < Sequel::Model return false, 'directory name cannot end with .htm or .html' end + if new_path.start_with?(current_path + '/') + return false, 'cannot move directory into itself' + end + else # a file begin mime_type = Magic.guess_file_mime_type site.files_path(self.path) diff --git a/tests/site_file_tests.rb b/tests/site_file_tests.rb index 1a25e1d9..6d2622fb 100644 --- a/tests/site_file_tests.rb +++ b/tests/site_file_tests.rb @@ -107,6 +107,20 @@ describe 'site_files' do _(res).must_equal [false, 'directory name cannot end with .htm or .html'] end + it 'fails when trying to move directory into itself' do + @site.create_directory 'dir' + dir = @site.site_files_dataset.where(path: 'dir').first + res = dir.rename('dir/newdir') + _(res).must_equal [false, 'cannot move directory into itself'] + _(@site.site_files_dataset.where(path: 'dir').first).wont_equal nil + _(@site.site_files_dataset.where(path: 'dir/newdir').first).must_equal nil + + res = dir.rename('dir/sub/dir') + _(res).must_equal [false, 'cannot move directory into itself'] + _(@site.site_files_dataset.where(path: 'dir').first).wont_equal nil + _(@site.site_files_dataset.where(path: 'dir/sub/dir').first).must_equal nil + end + it 'wont set an empty directory' do @site.create_directory 'dirone' _(@site.site_files.select {|sf| sf.path == 'dirone'}.length).must_equal 1