|
1 | 1 | """Check security fixes using the packaged Python interpreter and dependencies.""" |
2 | 2 |
|
| 3 | +import contextlib |
3 | 4 | import hashlib |
4 | 5 | import io |
| 6 | +import os |
5 | 7 | import platform |
| 8 | +import shutil |
6 | 9 | import stringprep |
7 | 10 | import sys |
8 | 11 | import tarfile |
9 | 12 | import tempfile |
10 | 13 | import unittest |
11 | 14 | from pathlib import Path |
| 15 | +from unittest import mock |
12 | 16 |
|
13 | 17 | import jwt |
14 | 18 | from build._compat.tarfile import safe_extractall |
15 | 19 |
|
16 | 20 |
|
17 | 21 | class EmbeddedPythonSecurityTests(unittest.TestCase): |
| 22 | + @unittest.skipUnless( |
| 23 | + os.name == "posix" and shutil.rmtree.avoids_symlink_attacks, |
| 24 | + "requires POSIX fd-based directory removal", |
| 25 | + ) |
| 26 | + @unittest.skipIf(hasattr(os, "geteuid") and os.geteuid() == 0, "requires a non-root user") |
| 27 | + def test_runtime_maintenance(self): |
| 28 | + with tempfile.TemporaryDirectory() as outside: |
| 29 | + target = Path(outside) / "file1" |
| 30 | + target.write_bytes(b"outside") |
| 31 | + before = target.stat() |
| 32 | + directory = tempfile.TemporaryDirectory() |
| 33 | + original = Path(directory.name) / "dir1" |
| 34 | + moved = Path(directory.name) / "dir1_moved" |
| 35 | + original.mkdir() |
| 36 | + (original / "file1").write_bytes(b"inside") |
| 37 | + original.chmod(0o500) |
| 38 | + unlink = os.unlink |
| 39 | + raced = False |
| 40 | + |
| 41 | + def replace_on_permission_error(path, *, dir_fd=None): |
| 42 | + nonlocal raced |
| 43 | + try: |
| 44 | + return unlink(path, dir_fd=dir_fd) |
| 45 | + except PermissionError: |
| 46 | + if not original.is_symlink(): |
| 47 | + original.chmod(0o700) |
| 48 | + original.rename(moved) |
| 49 | + original.symlink_to(outside, target_is_directory=True) |
| 50 | + raced = True |
| 51 | + raise |
| 52 | + |
| 53 | + try: |
| 54 | + with mock.patch("os.unlink", replace_on_permission_error), contextlib.suppress(OSError): |
| 55 | + directory.cleanup() |
| 56 | + self.assertTrue(raced, "runtime check must execute") |
| 57 | + self.assertTrue(target.is_file(), "runtime check must preserve the fixture") |
| 58 | + self.assertEqual(target.read_bytes(), b"outside") |
| 59 | + after = target.stat() |
| 60 | + self.assertEqual((after.st_mode, after.st_mtime_ns), (before.st_mode, before.st_mtime_ns)) |
| 61 | + finally: |
| 62 | + if original.is_symlink(): |
| 63 | + original.unlink() |
| 64 | + moved.rename(original) |
| 65 | + if original.exists(): |
| 66 | + original.chmod(0o700) |
| 67 | + directory.cleanup() |
| 68 | + |
18 | 69 | def test_jwt_options_reuse_preserves_claim_verification(self): |
19 | 70 | # CVE-2026-103001: an unsigned inspection must not mutate reusable |
20 | 71 | # options and disable expiry checking on a later verified decode. |
|
0 commit comments