diff --git a/monkey/infection_monkey/utils/dir_utils.py b/monkey/infection_monkey/utils/dir_utils.py index 3cc839d0e..31455535e 100644 --- a/monkey/infection_monkey/utils/dir_utils.py +++ b/monkey/infection_monkey/utils/dir_utils.py @@ -19,3 +19,7 @@ def file_extension_filter(file_extensions: Set): return f.suffix in file_extensions return inner_filter + + +def is_not_symlink_filter(f: Path): + return not f.is_symlink() diff --git a/monkey/tests/unit_tests/infection_monkey/utils/test_dir_utils.py b/monkey/tests/unit_tests/infection_monkey/utils/test_dir_utils.py index b8ff47a65..089563cee 100644 --- a/monkey/tests/unit_tests/infection_monkey/utils/test_dir_utils.py +++ b/monkey/tests/unit_tests/infection_monkey/utils/test_dir_utils.py @@ -1,7 +1,13 @@ +import os + +import pytest +from tests.utils import is_user_admin + from infection_monkey.utils.dir_utils import ( file_extension_filter, filter_files, get_all_regular_files_in_directory, + is_not_symlink_filter, ) FILES = ["file.jpg.zip", "file.xyz", "1.tar", "2.tgz", "2.png", "2.mpg"] @@ -91,3 +97,19 @@ def test_file_extension_filter(tmp_path): filtered_files = filter_files(files_in_dir, [file_extension_filter(valid_extensions)]) assert sorted(files[0:2]) == sorted(filtered_files) + + +@pytest.mark.skipif( + os.name == "nt" and not is_user_admin(), reason="Test requires admin rights on Windows" +) +def test_is_not_symlink_filter(tmp_path): + files = add_files_to_dir(tmp_path) + link_path = tmp_path / "symlink.test" + link_path.symlink_to(files[0], target_is_directory=False) + + files_in_dir = get_all_regular_files_in_directory(tmp_path) + filtered_files = filter_files(files_in_dir, [is_not_symlink_filter]) + + assert link_path in files_in_dir + assert len(filtered_files) == len(FILES) + assert link_path not in filtered_files diff --git a/monkey/tests/utils.py b/monkey/tests/utils.py new file mode 100644 index 000000000..1e55e9bc3 --- /dev/null +++ b/monkey/tests/utils.py @@ -0,0 +1,9 @@ +import ctypes +import os + + +def is_user_admin(): + if os.name == "posix": + return os.getuid() == 0 + + return ctypes.windll.shell32.IsUserAnAdmin()