|
1 | 1 | import os
|
2 | 2 | import shutil
|
3 | 3 | import tempfile
|
| 4 | +from pathlib import Path |
4 | 5 |
|
| 6 | +import pytest |
| 7 | +from django.core.exceptions import SuspiciousFileOperation |
5 | 8 | from django.test import TestCase, override_settings
|
6 | 9 |
|
7 | 10 | from readthedocs.builds.storage import BuildMediaFileSystemStorage
|
@@ -84,6 +87,42 @@ def test_sync_directory(self):
|
84 | 87 | self.storage.sync_directory(tmp_files_dir, storage_dir)
|
85 | 88 | self.assertFileTree(storage_dir, tree)
|
86 | 89 |
|
| 90 | + def test_sync_directory_source_symlink(self): |
| 91 | + tmp_dir = Path(tempfile.mkdtemp()) |
| 92 | + tmp_symlink_dir = Path(tempfile.mkdtemp()) / "files" |
| 93 | + tmp_symlink_dir.symlink_to(tmp_dir) |
| 94 | + |
| 95 | + with override_settings(DOCROOT=tmp_dir): |
| 96 | + with pytest.raises(SuspiciousFileOperation, match="symbolic link"): |
| 97 | + self.storage.sync_directory(tmp_symlink_dir, "files") |
| 98 | + |
| 99 | + def test_copy_directory_source_symlink(self): |
| 100 | + tmp_dir = Path(tempfile.mkdtemp()) |
| 101 | + tmp_symlink_dir = Path(tempfile.mkdtemp()) / "files" |
| 102 | + tmp_symlink_dir.symlink_to(tmp_dir) |
| 103 | + |
| 104 | + with override_settings(DOCROOT=tmp_dir): |
| 105 | + with pytest.raises(SuspiciousFileOperation, match="symbolic link"): |
| 106 | + self.storage.copy_directory(tmp_symlink_dir, "files") |
| 107 | + |
| 108 | + def test_sync_directory_source_outside_docroot(self): |
| 109 | + tmp_dir = Path(tempfile.mkdtemp()) |
| 110 | + tmp_docroot = Path(tempfile.mkdtemp()) / "docroot" |
| 111 | + tmp_docroot.mkdir() |
| 112 | + |
| 113 | + with override_settings(DOCROOT=tmp_docroot): |
| 114 | + with pytest.raises(SuspiciousFileOperation, match="outside the docroot"): |
| 115 | + self.storage.sync_directory(tmp_dir, "files") |
| 116 | + |
| 117 | + def test_copy_directory_source_outside_docroot(self): |
| 118 | + tmp_dir = Path(tempfile.mkdtemp()) |
| 119 | + tmp_docroot = Path(tempfile.mkdtemp()) / "docroot" |
| 120 | + tmp_docroot.mkdir() |
| 121 | + |
| 122 | + with override_settings(DOCROOT=tmp_docroot): |
| 123 | + with pytest.raises(SuspiciousFileOperation, match="outside the docroot"): |
| 124 | + self.storage.copy_directory(tmp_dir, "files") |
| 125 | + |
87 | 126 | def test_delete_directory(self):
|
88 | 127 | with override_settings(DOCROOT=files_dir):
|
89 | 128 | self.storage.copy_directory(files_dir, "files")
|
|
0 commit comments