|
| 1 | +from pathlib import Path |
| 2 | +from tempfile import mkdtemp |
| 3 | + |
| 4 | +import pytest |
| 5 | +from django.core.exceptions import SuspiciousFileOperation |
| 6 | +from django.test import TestCase, override_settings |
| 7 | + |
| 8 | +from readthedocs.core.utils.filesystem import safe_copytree, safe_open, safe_rmtree |
| 9 | +from readthedocs.doc_builder.exceptions import ( |
| 10 | + FileIsNotRegularFile, |
| 11 | + SymlinkOutsideBasePath, |
| 12 | + UnsupportedSymlinkFileError, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +class TestFileSystemUtils(TestCase): |
| 17 | + def assert_files_equal(self, directory, files): |
| 18 | + self.assertEqual( |
| 19 | + {str(p.relative_to(directory)) for p in directory.iterdir()}, files |
| 20 | + ) |
| 21 | + |
| 22 | + def test_copytree(self): |
| 23 | + from_directory = Path(mkdtemp()) |
| 24 | + docroot_path = from_directory.parent |
| 25 | + to_directory = Path(mkdtemp()) / "target" |
| 26 | + |
| 27 | + (from_directory / "test.txt").touch() |
| 28 | + |
| 29 | + self.assertFalse(to_directory.exists()) |
| 30 | + |
| 31 | + with override_settings(DOCROOT=docroot_path): |
| 32 | + safe_copytree(from_directory, to_directory) |
| 33 | + |
| 34 | + self.assert_files_equal(to_directory, {"test.txt"}) |
| 35 | + |
| 36 | + def test_copytree_outside_docroot(self): |
| 37 | + from_directory = Path(mkdtemp()) |
| 38 | + (from_directory / "test.txt").touch() |
| 39 | + to_directory = Path(mkdtemp()) / "target" |
| 40 | + docroot_path = Path(mkdtemp()) |
| 41 | + |
| 42 | + with pytest.raises(SuspiciousFileOperation): |
| 43 | + with override_settings(DOCROOT=docroot_path): |
| 44 | + safe_copytree(from_directory, to_directory) |
| 45 | + |
| 46 | + def test_copytree_with_symlinks(self): |
| 47 | + from_directory = Path(mkdtemp()) |
| 48 | + docroot_path = from_directory.parent |
| 49 | + to_directory = Path(mkdtemp()) / "target" |
| 50 | + |
| 51 | + file_a = from_directory / "test.txt" |
| 52 | + file_a.touch() |
| 53 | + |
| 54 | + symlink_a = from_directory / "symlink.txt" |
| 55 | + symlink_a.symlink_to(file_a) |
| 56 | + symlink_b = from_directory / "symlink-dir" |
| 57 | + symlink_b.symlink_to(to_directory.parent) |
| 58 | + |
| 59 | + self.assertFalse(to_directory.exists()) |
| 60 | + |
| 61 | + with override_settings(DOCROOT=docroot_path): |
| 62 | + safe_copytree(from_directory, to_directory) |
| 63 | + |
| 64 | + # Symlinks are copied as symlinks, not as files. |
| 65 | + self.assert_files_equal( |
| 66 | + to_directory, {"test.txt", "symlink.txt", "symlink-dir"} |
| 67 | + ) |
| 68 | + self.assertTrue((to_directory / "symlink.txt").is_symlink()) |
| 69 | + self.assertTrue((to_directory / "symlink-dir").is_symlink()) |
| 70 | + |
| 71 | + def test_copytree_from_dir_as_symlink(self): |
| 72 | + root_directory = Path(mkdtemp()) |
| 73 | + docroot_path = root_directory |
| 74 | + from_directory = root_directory / "a" |
| 75 | + from_directory.mkdir() |
| 76 | + (from_directory / "test.txt").touch() |
| 77 | + |
| 78 | + to_directory = root_directory / "b" |
| 79 | + |
| 80 | + from_directory_symlink = root_directory / "symlink-a" |
| 81 | + from_directory_symlink.symlink_to(from_directory) |
| 82 | + |
| 83 | + self.assertFalse(to_directory.exists()) |
| 84 | + |
| 85 | + with override_settings(DOCROOT=docroot_path): |
| 86 | + self.assertFalse(safe_copytree(from_directory_symlink, to_directory)) |
| 87 | + |
| 88 | + self.assertFalse(to_directory.exists()) |
| 89 | + |
| 90 | + def test_open(self): |
| 91 | + root_directory = Path(mkdtemp()) |
| 92 | + docroot_path = root_directory |
| 93 | + file_a = root_directory / "test.txt" |
| 94 | + file_a.touch() |
| 95 | + |
| 96 | + with override_settings(DOCROOT=docroot_path): |
| 97 | + context_manager = safe_open(file_a, allow_symlinks=False) |
| 98 | + self.assertIsNotNone(context_manager) |
| 99 | + |
| 100 | + with override_settings(DOCROOT=docroot_path): |
| 101 | + context_manager = safe_open( |
| 102 | + file_a, allow_symlinks=True, base_path=root_directory |
| 103 | + ) |
| 104 | + self.assertIsNotNone(context_manager) |
| 105 | + |
| 106 | + def test_open_outside_docroot(self): |
| 107 | + root_directory = Path(mkdtemp()) |
| 108 | + docroot_path = Path(mkdtemp()) |
| 109 | + file_a = root_directory / "test.txt" |
| 110 | + file_a.touch() |
| 111 | + |
| 112 | + with pytest.raises(SuspiciousFileOperation): |
| 113 | + with override_settings(DOCROOT=docroot_path): |
| 114 | + safe_open(file_a) |
| 115 | + |
| 116 | + def test_open_with_symlinks(self): |
| 117 | + root_directory = Path(mkdtemp()) |
| 118 | + docroot_path = root_directory |
| 119 | + file_a = root_directory / "test.txt" |
| 120 | + file_a.touch() |
| 121 | + |
| 122 | + symlink_a = root_directory / "symlink.txt" |
| 123 | + symlink_a.symlink_to(file_a) |
| 124 | + |
| 125 | + # Symlinks aren't allowed. |
| 126 | + with pytest.raises(UnsupportedSymlinkFileError): |
| 127 | + with override_settings(DOCROOT=docroot_path): |
| 128 | + safe_open(symlink_a, allow_symlinks=False) |
| 129 | + |
| 130 | + # Symlinks are allowed if they are under the root_directory. |
| 131 | + with override_settings(DOCROOT=docroot_path): |
| 132 | + context_manager = safe_open( |
| 133 | + symlink_a, allow_symlinks=True, base_path=root_directory |
| 134 | + ) |
| 135 | + self.assertIsNotNone(context_manager) |
| 136 | + |
| 137 | + # Symlinks aren't allowed if they aren't under the root_directory. |
| 138 | + with pytest.raises(SymlinkOutsideBasePath): |
| 139 | + with override_settings(DOCROOT=docroot_path): |
| 140 | + new_root_directory = root_directory / "dir" |
| 141 | + new_root_directory.mkdir() |
| 142 | + safe_open(symlink_a, allow_symlinks=True, base_path=new_root_directory) |
| 143 | + |
| 144 | + def test_rmtree(self): |
| 145 | + root_directory = Path(mkdtemp()) |
| 146 | + docroot_path = root_directory |
| 147 | + (root_directory / "test.txt").touch() |
| 148 | + |
| 149 | + self.assertTrue(root_directory.exists()) |
| 150 | + |
| 151 | + with override_settings(DOCROOT=docroot_path): |
| 152 | + safe_rmtree(root_directory) |
| 153 | + self.assertFalse(root_directory.exists()) |
| 154 | + |
| 155 | + def test_rmtree_outside_docroot(self): |
| 156 | + root_directory = Path(mkdtemp()) |
| 157 | + docroot_path = Path(mkdtemp()) |
| 158 | + (root_directory / "test.txt").touch() |
| 159 | + |
| 160 | + self.assertTrue(root_directory.exists()) |
| 161 | + |
| 162 | + with pytest.raises(SuspiciousFileOperation): |
| 163 | + with override_settings(DOCROOT=docroot_path): |
| 164 | + safe_rmtree(root_directory) |
| 165 | + |
| 166 | + def test_rmtree_with_symlinks(self): |
| 167 | + root_directory = Path(mkdtemp()) |
| 168 | + docroot_path = root_directory |
| 169 | + |
| 170 | + dir_a = root_directory / "test" |
| 171 | + dir_a.mkdir() |
| 172 | + (dir_a / "test.txt").touch() |
| 173 | + |
| 174 | + symlink_a = root_directory / "symlink" |
| 175 | + symlink_a.symlink_to(dir_a) |
| 176 | + |
| 177 | + # Directories that point to a symlink aren't deleted. |
| 178 | + self.assertTrue(symlink_a.exists()) |
| 179 | + with override_settings(DOCROOT=docroot_path): |
| 180 | + safe_rmtree(symlink_a) |
| 181 | + self.assertTrue(symlink_a.exists()) |
0 commit comments