| 1 |
import os |
| 2 |
import stat |
| 3 |
import sys |
| 4 |
import tempfile |
| 5 |
import unittest |
| 6 |
import zipfile |
| 7 |
|
| 8 |
from django.core.exceptions import SuspiciousOperation |
| 9 |
from django.test import SimpleTestCase |
| 10 |
from django.utils import archive |
| 11 |
|
| 12 |
try: |
| 13 |
import bz2 # NOQA |
| 14 |
|
| 15 |
HAS_BZ2 = True |
| 16 |
except ImportError: |
| 17 |
HAS_BZ2 = False |
| 18 |
|
| 19 |
try: |
| 20 |
import lzma # NOQA |
| 21 |
|
| 22 |
HAS_LZMA = True |
| 23 |
except ImportError: |
| 24 |
HAS_LZMA = False |
| 25 |
|
| 26 |
|
| 27 |
class TestArchive(unittest.TestCase): |
| 28 |
def setUp(self): |
| 29 |
self.testdir = os.path.join(os.path.dirname(__file__), "archives") |
| 30 |
old_cwd = os.getcwd() |
| 31 |
os.chdir(self.testdir) |
| 32 |
self.addCleanup(os.chdir, old_cwd) |
| 33 |
|
| 34 |
def test_extract_function(self): |
| 35 |
with os.scandir(self.testdir) as entries: |
| 36 |
for entry in entries: |
| 37 |
with self.subTest(entry.name), tempfile.TemporaryDirectory() as tmpdir: |
| 38 |
if (entry.name.endswith(".bz2") and not HAS_BZ2) or ( |
| 39 |
entry.name.endswith((".lzma", ".xz")) and not HAS_LZMA |
| 40 |
): |
| 41 |
continue |
| 42 |
archive.extract(entry.path, tmpdir) |
| 43 |
self.assertTrue(os.path.isfile(os.path.join(tmpdir, "1"))) |
| 44 |
self.assertTrue(os.path.isfile(os.path.join(tmpdir, "2"))) |
| 45 |
self.assertTrue(os.path.isfile(os.path.join(tmpdir, "foo", "1"))) |
| 46 |
self.assertTrue(os.path.isfile(os.path.join(tmpdir, "foo", "2"))) |
| 47 |
self.assertTrue( |
| 48 |
os.path.isfile(os.path.join(tmpdir, "foo", "bar", "1")) |
| 49 |
) |
| 50 |
self.assertTrue( |
| 51 |
os.path.isfile(os.path.join(tmpdir, "foo", "bar", "2")) |
| 52 |
) |
| 53 |
|
| 54 |
@unittest.skipIf( |
| 55 |
sys.platform == "win32", "Python on Windows has a limited os.chmod()." |
| 56 |
) |
| 57 |
def test_extract_file_permissions(self): |
| 58 |
"""archive.extract() preserves file permissions.""" |
| 59 |
mask = stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO |
| 60 |
umask = os.umask(0) |
| 61 |
os.umask(umask) # Restore the original umask. |
| 62 |
with os.scandir(self.testdir) as entries: |
| 63 |
for entry in entries: |
| 64 |
if ( |
| 65 |
entry.name.startswith("leadpath_") |
| 66 |
or (entry.name.endswith(".bz2") and not HAS_BZ2) |
| 67 |
or (entry.name.endswith((".lzma", ".xz")) and not HAS_LZMA) |
| 68 |
): |
| 69 |
continue |
| 70 |
with self.subTest(entry.name), tempfile.TemporaryDirectory() as tmpdir: |
| 71 |
archive.extract(entry.path, tmpdir) |
| 72 |
# An executable file in the archive has executable |
| 73 |
# permissions. |
| 74 |
filepath = os.path.join(tmpdir, "executable") |
| 75 |
self.assertEqual(os.stat(filepath).st_mode & mask, 0o775) |
| 76 |
# A file is readable even if permission data is missing. |
| 77 |
filepath = os.path.join(tmpdir, "no_permissions") |
| 78 |
self.assertEqual(os.stat(filepath).st_mode & mask, 0o666 & ~umask) |
| 79 |
|
| 80 |
|
| 81 |
class TestArchiveInvalid(SimpleTestCase): |
| 82 |
def test_extract_function_traversal(self): |
| 83 |
archives_dir = os.path.join(os.path.dirname(__file__), "traversal_archives") |
| 84 |
tests = [ |
| 85 |
("traversal.tar", ".."), |
| 86 |
("traversal_absolute.tar", "/tmp/evil.py"), |
| 87 |
] |
| 88 |
if sys.platform == "win32": |
| 89 |
tests += [ |
| 90 |
("traversal_disk_win.tar", "d:evil.py"), |
| 91 |
("traversal_disk_win.zip", "d:evil.py"), |
| 92 |
] |
| 93 |
msg = "Archive contains invalid path: '%s'" |
| 94 |
for entry, invalid_path in tests: |
| 95 |
with self.subTest(entry), tempfile.TemporaryDirectory() as tmpdir: |
| 96 |
with self.assertRaisesMessage(SuspiciousOperation, msg % invalid_path): |
| 97 |
archive.extract(os.path.join(archives_dir, entry), tmpdir) |
| 98 |
|
| 99 |
def test_extract_function_traversal_startswith(self): |
| 100 |
with tempfile.TemporaryDirectory() as tmpdir: |
| 101 |
base = os.path.abspath(tmpdir) |
| 102 |
tarfile_handle = tempfile.NamedTemporaryFile(suffix=".zip", delete=False) |
| 103 |
tar_path = tarfile_handle.name |
| 104 |
tarfile_handle.close() |
| 105 |
self.addCleanup(os.remove, tar_path) |
| 106 |
|
| 107 |
malicious_member = os.path.join(base + "abc", "evil.txt") |
| 108 |
with zipfile.ZipFile(tar_path, "w") as zf: |
| 109 |
zf.writestr(malicious_member, "evil\n") |
| 110 |
zf.writestr("test.txt", "data\n") |
| 111 |
|
| 112 |
with self.assertRaisesMessage( |
| 113 |
SuspiciousOperation, "Archive contains invalid path" |
| 114 |
): |
| 115 |
archive.extract(tar_path, base) |