2011-12-23 06:38:02 +08:00
|
|
|
import os
|
2016-12-02 00:05:08 +08:00
|
|
|
import stat
|
|
|
|
import sys
|
2011-12-23 06:38:02 +08:00
|
|
|
import tempfile
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2011-12-23 06:38:02 +08:00
|
|
|
|
2021-01-22 19:23:18 +08:00
|
|
|
from django.core.exceptions import SuspiciousOperation
|
|
|
|
from django.test import SimpleTestCase
|
2019-02-16 07:59:51 +08:00
|
|
|
from django.utils import archive
|
2011-12-23 06:38:02 +08:00
|
|
|
|
2021-02-04 21:08:43 +08:00
|
|
|
try:
|
|
|
|
import bz2 # NOQA
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2021-02-04 21:08:43 +08:00
|
|
|
HAS_BZ2 = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_BZ2 = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
import lzma # NOQA
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2021-02-04 21:08:43 +08:00
|
|
|
HAS_LZMA = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_LZMA = False
|
|
|
|
|
2011-12-23 06:38:02 +08:00
|
|
|
|
2019-02-16 07:59:51 +08:00
|
|
|
class TestArchive(unittest.TestCase):
|
2011-12-23 06:38:02 +08:00
|
|
|
def setUp(self):
|
2019-02-16 07:59:51 +08:00
|
|
|
self.testdir = os.path.join(os.path.dirname(__file__), "archives")
|
2011-12-23 06:38:02 +08:00
|
|
|
self.old_cwd = os.getcwd()
|
2019-02-16 07:59:51 +08:00
|
|
|
os.chdir(self.testdir)
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
os.chdir(self.old_cwd)
|
|
|
|
|
|
|
|
def test_extract_function(self):
|
2021-06-06 14:56:34 +08:00
|
|
|
with os.scandir(self.testdir) as entries:
|
|
|
|
for entry in entries:
|
|
|
|
with self.subTest(entry.name), tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
if (entry.name.endswith(".bz2") and not HAS_BZ2) or (
|
|
|
|
entry.name.endswith((".lzma", ".xz")) and not HAS_LZMA
|
|
|
|
):
|
|
|
|
continue
|
|
|
|
archive.extract(entry.path, tmpdir)
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(tmpdir, "1")))
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(tmpdir, "2")))
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(tmpdir, "foo", "1")))
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(tmpdir, "foo", "2")))
|
|
|
|
self.assertTrue(
|
|
|
|
os.path.isfile(os.path.join(tmpdir, "foo", "bar", "1"))
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2021-06-06 14:56:34 +08:00
|
|
|
self.assertTrue(
|
|
|
|
os.path.isfile(os.path.join(tmpdir, "foo", "bar", "2"))
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2014-05-22 20:12:22 +08:00
|
|
|
|
2016-12-02 00:05:08 +08:00
|
|
|
@unittest.skipIf(
|
|
|
|
sys.platform == "win32", "Python on Windows has a limited os.chmod()."
|
|
|
|
)
|
|
|
|
def test_extract_file_permissions(self):
|
2019-02-16 07:59:51 +08:00
|
|
|
"""archive.extract() preserves file permissions."""
|
|
|
|
mask = stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO
|
|
|
|
umask = os.umask(0)
|
|
|
|
os.umask(umask) # Restore the original umask.
|
2021-06-06 14:56:34 +08:00
|
|
|
with os.scandir(self.testdir) as entries:
|
|
|
|
for entry in entries:
|
|
|
|
if (
|
|
|
|
entry.name.startswith("leadpath_")
|
|
|
|
or (entry.name.endswith(".bz2") and not HAS_BZ2)
|
|
|
|
or (entry.name.endswith((".lzma", ".xz")) and not HAS_LZMA)
|
|
|
|
):
|
|
|
|
continue
|
|
|
|
with self.subTest(entry.name), tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
archive.extract(entry.path, tmpdir)
|
|
|
|
# An executable file in the archive has executable
|
|
|
|
# permissions.
|
|
|
|
filepath = os.path.join(tmpdir, "executable")
|
|
|
|
self.assertEqual(os.stat(filepath).st_mode & mask, 0o775)
|
|
|
|
# A file is readable even if permission data is missing.
|
|
|
|
filepath = os.path.join(tmpdir, "no_permissions")
|
|
|
|
self.assertEqual(os.stat(filepath).st_mode & mask, 0o666 & ~umask)
|
2021-01-22 19:23:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestArchiveInvalid(SimpleTestCase):
|
|
|
|
def test_extract_function_traversal(self):
|
|
|
|
archives_dir = os.path.join(os.path.dirname(__file__), "traversal_archives")
|
|
|
|
tests = [
|
|
|
|
("traversal.tar", ".."),
|
|
|
|
("traversal_absolute.tar", "/tmp/evil.py"),
|
|
|
|
]
|
|
|
|
if sys.platform == "win32":
|
|
|
|
tests += [
|
|
|
|
("traversal_disk_win.tar", "d:evil.py"),
|
|
|
|
("traversal_disk_win.zip", "d:evil.py"),
|
|
|
|
]
|
|
|
|
msg = "Archive contains invalid path: '%s'"
|
|
|
|
for entry, invalid_path in tests:
|
|
|
|
with self.subTest(entry), tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
with self.assertRaisesMessage(SuspiciousOperation, msg % invalid_path):
|
|
|
|
archive.extract(os.path.join(archives_dir, entry), tmpdir)
|