diff --git a/packaging/macos/nightly_plist_tests.py b/packaging/macos/nightly_plist_tests.py index 8a0444190e..32f8e6d5ba 100644 --- a/packaging/macos/nightly_plist_tests.py +++ b/packaging/macos/nightly_plist_tests.py @@ -116,7 +116,7 @@ class NightlyMetadata(unittest.TestCase): external = self.root / "sentinel" external.write_bytes(sentinel) self.output.symlink_to(external) - with self.assertRaises(FileExistsError): + with self.assertRaisesRegex(ValueError, "symlinks or aliases"): POLICY.write_plist(self.template, self.output, VERSION, COMMIT) self.assertEqual(external.read_bytes(), sentinel) self.assertTrue(self.output.is_symlink()) diff --git a/packaging/nightly-archive-tests.py b/packaging/nightly-archive-tests.py index 74e9a3d231..e400268be1 100644 --- a/packaging/nightly-archive-tests.py +++ b/packaging/nightly-archive-tests.py @@ -73,7 +73,7 @@ class ArchivePolicy(unittest.TestCase): icon = self.stage / "photocraft-nightly.png" icon.unlink() icon.symlink_to(self.stage / "README.md") - with self.assertRaisesRegex(ValueError, "regular file"): + with self.assertRaisesRegex(ValueError, "regular file|symlinks or aliases"): ARCHIVE.linux_archive(self.stage, self.output) self.assertFalse(self.output.exists()) @@ -156,6 +156,19 @@ 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_member_read_budget_refuses_before_output(self): (self.stage / "OFL-Large.txt").write_bytes(b"x" * 65) with mock.patch.object(ARCHIVE, "MAX_MEMBER_BYTES", 64, create=True): diff --git a/packaging/nightly-archive.py b/packaging/nightly-archive.py index d46dcef646..f5244fe242 100644 --- a/packaging/nightly-archive.py +++ b/packaging/nightly-archive.py @@ -74,12 +74,21 @@ def verify_headers(root, output): def linux_archive(stage, output): with OwnedBuildRoot() as root: entries = checked_entries(root, stage) + stage_root = os.path.realpath(os.path.join(root.directory, stage)) total = 0 with root.create(output) as outgoing: 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. + member = os.path.normpath(info.name) + if member != info.name or os.path.isabs(member) or os.path.basename(member) != member or member in ("", ".", ".."): + raise ValueError(f"nightly member name must be a flat file name: {info.name}") + destination = os.path.realpath(os.path.join(stage_root, member)) + if os.path.commonpath([stage_root, destination]) != stage_root: + raise ValueError(f"nightly member resolves outside its stage: {info.name}") + info.name = member total += info.size if info.size > MAX_MEMBER_BYTES or total > MAX_TOTAL_BYTES: raise ValueError("nightly stage grew beyond its read bound") diff --git a/packaging/nightly_paths.py b/packaging/nightly_paths.py index bbe529d603..792198589e 100644 --- a/packaging/nightly_paths.py +++ b/packaging/nightly_paths.py @@ -10,6 +10,13 @@ from pathlib import Path import stat +def _component(name): + # A descriptor-relative open must name exactly one entry in its directory. + if not name or name in (".", "..") or os.path.basename(name) != name: + raise ValueError("build path components must be single names") + return os.path.basename(name) + + class OwnedBuildRoot: def __enter__(self): self.directory = Path.cwd() @@ -40,15 +47,26 @@ class OwnedBuildRoot: raise ValueError("resolved path escapes the owned build directory") return relative - def _parent(self, path): + def _checked_parts(self, path): relative = self._relative(path) + root = os.path.realpath(self.directory) + lexical = os.path.join(root, *relative.parts) + target = os.path.realpath(lexical) + if target == root or os.path.commonpath([root, target]) != root: + raise ValueError("resolved path escapes the owned build directory") + if target != lexical: + raise ValueError("build path must not pass through symlinks or aliases") + return [_component(part) for part in os.path.relpath(target, root).split(os.sep)] + + def _parent(self, path): + parts = self._checked_parts(path) current = os.dup(self.fd) try: - for part in relative.parts[:-1]: - next_fd = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=current) + for part in parts[:-1]: + next_fd = os.open(_component(part), os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=current) os.close(current) current = next_fd - return current, relative.name + return current, parts[-1] except OSError as error: os.close(current) raise ValueError("build path parents must be non-symlink directories") from error @@ -58,7 +76,7 @@ class OwnedBuildRoot: parent, name = self._parent(path) try: try: - descriptor = os.open(name, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=parent) + descriptor = os.open(_component(name), os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=parent) except OSError as error: raise ValueError("build input must be a regular file without aliases") from error finally: @@ -82,7 +100,7 @@ class OwnedBuildRoot: parent, name = self._parent(path) try: try: - descriptor = os.open(name, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=parent) + descriptor = os.open(_component(name), os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=parent) except OSError as error: raise ValueError("build stage must be a non-symlink directory") from error finally: @@ -101,6 +119,7 @@ class OwnedBuildRoot: @contextlib.contextmanager def create(self, path): parent, name = self._parent(path) + name = _component(name) try: descriptor = os.open(name, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o666, dir_fd=parent) info = os.fstat(descriptor) @@ -112,7 +131,7 @@ class OwnedBuildRoot: try: current = os.stat(name, dir_fd=parent, follow_symlinks=False) if (current.st_dev, current.st_ino) == identity: - os.unlink(name, dir_fd=parent) + os.unlink(_component(name), dir_fd=parent) except FileNotFoundError: pass raise