Skip to content

Commit 0e3ac7d

Browse files
authored
legacy: lingering PEP 527 changes (#13881)
Signed-off-by: William Woodruff <[email protected]>
1 parent b0736e6 commit 0e3ac7d

File tree

2 files changed

+5
-82
lines changed

2 files changed

+5
-82
lines changed

tests/unit/forklift/test_legacy.py

+3-45
Original file line numberDiff line numberDiff line change
@@ -487,17 +487,9 @@ class TestFileValidation:
487487
def test_defaults_to_true(self):
488488
assert legacy._is_valid_dist_file("", "")
489489

490-
@pytest.mark.parametrize(
491-
("filename", "filetype"),
492-
[("test.exe", "bdist_msi"), ("test.msi", "bdist_wininst")],
493-
)
494-
def test_bails_with_invalid_package_type(self, filename, filetype):
495-
assert not legacy._is_valid_dist_file(filename, filetype)
496-
497490
@pytest.mark.parametrize(
498491
("filename", "filetype"),
499492
[
500-
("test.exe", "bdist_wininst"),
501493
("test.zip", "sdist"),
502494
("test.egg", "bdist_egg"),
503495
("test.whl", "bdist_wheel"),
@@ -511,9 +503,7 @@ def test_bails_with_invalid_zipfile(self, tmpdir, filename, filetype):
511503

512504
assert not legacy._is_valid_dist_file(f, filetype)
513505

514-
@pytest.mark.parametrize(
515-
"filename", ["test.tar", "test.tar.gz", "test.tgz", "test.tar.bz2", "test.tbz2"]
516-
)
506+
@pytest.mark.parametrize("filename", ["test.tar.gz"])
517507
def test_bails_with_invalid_tarfile(self, tmpdir, filename):
518508
fake_tar = str(tmpdir.join(filename))
519509

@@ -522,7 +512,7 @@ def test_bails_with_invalid_tarfile(self, tmpdir, filename):
522512

523513
assert not legacy._is_valid_dist_file(fake_tar, "sdist")
524514

525-
@pytest.mark.parametrize("compression", ("", "gz", "bz2"))
515+
@pytest.mark.parametrize("compression", ("gz",))
526516
def test_tarfile_validation_invalid(self, tmpdir, compression):
527517
file_extension = f".{compression}" if compression else ""
528518
tar_fn = str(tmpdir.join(f"test.tar{file_extension}"))
@@ -538,7 +528,7 @@ def test_tarfile_validation_invalid(self, tmpdir, compression):
538528
tar_fn, "sdist"
539529
), "no PKG-INFO; should fail"
540530

541-
@pytest.mark.parametrize("compression", ("", "gz", "bz2"))
531+
@pytest.mark.parametrize("compression", ("gz",))
542532
def test_tarfile_validation_valid(self, tmpdir, compression):
543533
file_extension = f".{compression}" if compression else ""
544534
tar_fn = str(tmpdir.join(f"test.tar{file_extension}"))
@@ -554,38 +544,6 @@ def test_tarfile_validation_valid(self, tmpdir, compression):
554544

555545
assert legacy._is_valid_dist_file(tar_fn, "sdist")
556546

557-
def test_wininst_unsafe_filename(self, tmpdir):
558-
f = str(tmpdir.join("test.exe"))
559-
560-
with zipfile.ZipFile(f, "w") as zfp:
561-
zfp.writestr("something/bar.py", b"the test file")
562-
563-
assert not legacy._is_valid_dist_file(f, "bdist_wininst")
564-
565-
def test_wininst_safe_filename(self, tmpdir):
566-
f = str(tmpdir.join("test.exe"))
567-
568-
with zipfile.ZipFile(f, "w") as zfp:
569-
zfp.writestr("purelib/bar.py", b"the test file")
570-
571-
assert legacy._is_valid_dist_file(f, "bdist_wininst")
572-
573-
def test_msi_invalid_header(self, tmpdir):
574-
f = str(tmpdir.join("test.msi"))
575-
576-
with open(f, "wb") as fp:
577-
fp.write(b"this isn't the correct header for an msi")
578-
579-
assert not legacy._is_valid_dist_file(f, "bdist_msi")
580-
581-
def test_msi_valid_header(self, tmpdir):
582-
f = str(tmpdir.join("test.msi"))
583-
584-
with open(f, "wb") as fp:
585-
fp.write(b"\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1")
586-
587-
assert legacy._is_valid_dist_file(f, "bdist_msi")
588-
589547
def test_zip_no_pkg_info(self, tmpdir):
590548
f = str(tmpdir.join("test.zip"))
591549

warehouse/forklift/legacy.py

+2-37
Original file line numberDiff line numberDiff line change
@@ -660,11 +660,6 @@ def _validate_filename(filename):
660660
)
661661

662662

663-
_safe_zipnames = re.compile(r"(purelib|platlib|headers|scripts|data).+", re.I)
664-
# .tar uncompressed, .tar.gz .tgz, .tar.bz2 .tbz2
665-
_tar_filenames_re = re.compile(r"\.(?:tar$|t(?:ar\.)?(?P<z_type>gz|bz2)$)")
666-
667-
668663
def _is_valid_dist_file(filename, filetype):
669664
"""
670665
Perform some basic checks to see whether the indicated file could be
@@ -698,15 +693,13 @@ def _is_valid_dist_file(filename, filetype):
698693
}:
699694
return False
700695

701-
tar_fn_match = _tar_filenames_re.search(filename)
702-
if tar_fn_match:
696+
if filename.endswith(".tar.gz"):
703697
# TODO: Ideally Ensure the compression ratio is not absurd
704698
# (decompression bomb), like we do for wheel/zip above.
705699

706700
# Ensure that this is a valid tar file, and that it contains PKG-INFO.
707-
z_type = tar_fn_match.group("z_type") or ""
708701
try:
709-
with tarfile.open(filename, f"r:{z_type}") as tar:
702+
with tarfile.open(filename, "r:gz") as tar:
710703
# This decompresses the entire stream to validate it and the
711704
# tar within. Easy CPU DoS attack. :/
712705
bad_tar = True
@@ -720,34 +713,6 @@ def _is_valid_dist_file(filename, filetype):
720713
return False
721714
except (tarfile.ReadError, EOFError):
722715
return False
723-
elif filename.endswith(".exe"):
724-
# The only valid filetype for a .exe file is "bdist_wininst".
725-
if filetype != "bdist_wininst":
726-
return False
727-
728-
# Ensure that the .exe is a valid zip file, and that all of the files
729-
# contained within it have safe filenames.
730-
try:
731-
with zipfile.ZipFile(filename, "r") as zfp:
732-
# We need the no branch below to work around a bug in
733-
# coverage.py where it's detecting a missed branch where there
734-
# isn't one.
735-
for zipname in zfp.namelist(): # pragma: no branch
736-
if not _safe_zipnames.match(zipname):
737-
return False
738-
except zipfile.BadZipFile:
739-
return False
740-
elif filename.endswith(".msi"):
741-
# The only valid filetype for a .msi is "bdist_msi"
742-
if filetype != "bdist_msi":
743-
return False
744-
745-
# Check the first 8 bytes of the MSI file. This was taken from the
746-
# legacy implementation of PyPI which itself took it from the
747-
# implementation of `file` I believe.
748-
with open(filename, "rb") as fp:
749-
if fp.read(8) != b"\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1":
750-
return False
751716
elif filename.endswith(".zip") or filename.endswith(".egg"):
752717
# Ensure that the .zip/.egg is a valid zip file, and that it has a
753718
# PKG-INFO file.

0 commit comments

Comments
 (0)