diff --git a/packaging/nightly-archive-tests.py b/packaging/nightly-archive-tests.py index e400268be1..c324516083 100644 --- a/packaging/nightly-archive-tests.py +++ b/packaging/nightly-archive-tests.py @@ -156,18 +156,21 @@ class ArchivePolicy(unittest.TestCase): ARCHIVE.linux_archive(self.stage, output) self.assertFalse((Path(external) / "nightly.tar.gz").exists()) - def test_nonflat_member_name_refuses_and_output_is_cleaned(self): - original = tarfile.TarFile.gettarinfo - for bad in ("../escape", "/absolute", "nested/name", "x/../photocraft"): - with self.subTest(name=bad): - def renamed(archive, *args, **kwargs): - info = original(archive, *args, **kwargs) - info.name = bad - return info - with mock.patch.object(tarfile.TarFile, "gettarinfo", renamed): - with self.assertRaises(ValueError): - ARCHIVE.linux_archive(self.stage, self.output) - self.assertFalse(self.output.exists()) + def test_nonflat_member_name_refuses(self): + stage_root = os.path.realpath(self.stage) + with open(self.stage / "README.md", "rb") as incoming: + for bad in ("../escape", "/absolute", "nested/name", "x/../README.md", "", ".", ".."): + with self.subTest(name=bad), self.assertRaises(ValueError): + ARCHIVE.member_info(stage_root, bad, incoming) + + def test_member_header_matches_what_gettarinfo_records(self): + stage_root = os.path.realpath(self.stage) + for name in sorted(self.required): + with self.subTest(name=name), open(self.stage / name, "rb") as incoming: + ours = ARCHIVE.member_info(stage_root, name, incoming) + with tarfile.open(fileobj=__import__("io").BytesIO(), mode="w") as reference: + theirs = reference.gettarinfo(fileobj=incoming, arcname=name) + self.assertEqual(ours.get_info(), theirs.get_info()) def test_member_read_budget_refuses_before_output(self): (self.stage / "OFL-Large.txt").write_bytes(b"x" * 65) @@ -180,15 +183,15 @@ class ArchivePolicy(unittest.TestCase): def test_member_growth_is_rechecked_before_copy_and_output_is_cleaned(self): growing = self.stage / "OFL-Growing.txt" growing.write_bytes(b"x" * 32) - original = ARCHIVE.tarfile.TarFile.gettarinfo + original = ARCHIVE.member_info - def grow_before_stat(archive, *args, **kwargs): - if kwargs.get("arcname") == growing.name: + def grow_before_stat(stage_root, name, incoming): + if name == growing.name: growing.write_bytes(b"x" * 65) - return original(archive, *args, **kwargs) + return original(stage_root, name, incoming) with mock.patch.object(ARCHIVE, "MAX_MEMBER_BYTES", 64), \ - mock.patch.object(ARCHIVE.tarfile.TarFile, "gettarinfo", grow_before_stat): + mock.patch.object(ARCHIVE, "member_info", grow_before_stat): with self.assertRaises(ValueError): ARCHIVE.linux_archive(self.stage, self.output) self.assertFalse(self.output.exists()) diff --git a/packaging/nightly-archive.py b/packaging/nightly-archive.py index f9e656148e..8a58119462 100644 --- a/packaging/nightly-archive.py +++ b/packaging/nightly-archive.py @@ -4,10 +4,13 @@ The stage and archive must both remain under that independently chosen root. No native build, platform signing, or malicious-same-UID sandbox is implied. """ +import grp import gzip import os import pathlib +import pwd import re +import stat import sys import tarfile @@ -47,6 +50,37 @@ def checked_entries(root, stage): return entries +def member_info(stage_root, name, incoming): + """Header for one stage file, built from the file this run opened rather than from the archive. + + Members are flat names that resolve directly inside the stage they came from. + """ + if os.path.isabs(name) or os.path.basename(name) != name or name in ("", ".", ".."): + raise ValueError(f"nightly member name must be a flat file name: {name}") + destination = os.path.realpath(os.path.join(stage_root, name)) + if not destination.startswith(stage_root + os.sep) or os.path.dirname(destination) != stage_root: + raise ValueError(f"nightly member resolves outside its stage: {name}") + status = os.fstat(incoming.fileno()) + if not stat.S_ISREG(status.st_mode): + raise ValueError(f"nightly member must be a regular file: {name}") + # The same fields TarFile.gettarinfo records for a regular file. + info = tarfile.TarInfo(os.path.basename(destination)) + info.type = tarfile.REGTYPE + info.mode = stat.S_IMODE(status.st_mode) + info.uid, info.gid = status.st_uid, status.st_gid + info.size = status.st_size + info.mtime = status.st_mtime + try: + info.uname = pwd.getpwuid(status.st_uid).pw_name + except KeyError: + pass + try: + info.gname = grp.getgrgid(status.st_gid).gr_name + except KeyError: + pass + return info + + def verify_headers(root, output): # Bounded ordinary headers, not a transparent PAX/GNU-normalizing reader. total = 0 @@ -80,15 +114,7 @@ def linux_archive(stage, output): with tarfile.open(fileobj=outgoing, mode="w:gz", format=tarfile.USTAR_FORMAT) as archive: for entry in entries: with root.reader(entry, MAX_MEMBER_BYTES) as incoming: - info = archive.gettarinfo(fileobj=incoming, arcname=entry.name) - # Members stay flat names that resolve inside the stage they came from. - if os.path.isabs(info.name) or os.path.basename(info.name) != info.name or info.name in ("", ".", ".."): - raise ValueError(f"nightly member name must be a flat file name: {info.name}") - destination = os.path.realpath(os.path.join(stage_root, info.name)) - if not destination.startswith(stage_root + os.sep) or os.path.dirname(destination) != stage_root: - raise ValueError(f"nightly member resolves outside its stage: {info.name}") - # The recorded name is the checked destination's own name. - info.name = os.path.basename(destination) + info = member_info(stage_root, entry.name, incoming) total += info.size if info.size > MAX_MEMBER_BYTES or total > MAX_TOTAL_BYTES: raise ValueError("nightly stage grew beyond its read bound")