From 90b9fb86cc4cc35e57d87f8dfce34d84914c5770 Mon Sep 17 00:00:00 2001 From: JiyouProt <104832502+JiyouProt@users.noreply.github.com> Date: Tue, 24 Nov 2026 04:09:00 +0100 Subject: [PATCH] manifest_verifier: refuse cache paths that alias the input manifest (VERIFY-CACHE-COLLISION) --- tests/test_manifest_verifier.py | 52 +++++++++++++++++++++++++++++++++ tools/manifest_verifier.py | 5 ++++ 2 files changed, 57 insertions(+) diff --git a/tests/test_manifest_verifier.py b/tests/test_manifest_verifier.py index 3729b3556d..ef5b1e384c 100644 --- a/tests/test_manifest_verifier.py +++ b/tests/test_manifest_verifier.py @@ -896,6 +896,58 @@ def cached_names(self) -> set[str]: with open(self.cache, encoding="utf-8") as f: return {os.path.basename(k) for k in json.load(f)["files"]} + def assert_manifest_cache_refused(self) -> None: + with open(self.manifest, "rb") as f: + before = f.read() + with patch.object(mv, "sha256_file", wraps=mv.sha256_file) as hashing: + with self.assertRaisesRegex(mv.ManifestError, "cache must not overwrite the manifest"): + self.verify() + hashing.assert_not_called() + with open(self.manifest, "rb") as f: + self.assertEqual(f.read(), before) + with open(self.cache, "rb") as f: + self.assertEqual(f.read(), before) + self.assertFalse(os.path.exists(self.cache + ".tmp")) + + def test_cache_refuses_another_spelling_of_manifest(self) -> None: + self.cache = os.path.join(self.game, "..", "manifest.json") + self.assert_manifest_cache_refused() + + def test_cache_refuses_manifest_hard_link(self) -> None: + try: + os.link(self.manifest, self.cache) + except OSError as err: + self.skipTest(f"hard links unavailable: {err}") + self.assert_manifest_cache_refused() + + def test_cache_refuses_manifest_symlink(self) -> None: + try: + os.symlink(self.manifest, self.cache) + except OSError as err: + self.skipTest(f"symlinks unavailable: {err}") + self.assert_manifest_cache_refused() + + def test_cache_refuses_manifest_case_alias_on_folding_volume(self) -> None: + self.cache = os.path.join(self.tmp.name, "MANIFEST.JSON") + if not os.path.exists(self.cache): + self.skipTest("volume is case-sensitive") + self.assert_manifest_cache_refused() + + def test_cli_cache_equal_to_manifest_preserves_input(self) -> None: + with open(self.manifest, "rb") as f: + before = f.read() + result = subprocess.run( + [sys.executable, "-I", TOOL, self.game, "--manifest", self.manifest, + "--cache", self.manifest, "--json"], + capture_output=True, text=True, timeout=15, + ) + with open(self.manifest, "rb") as f: + self.assertEqual(f.read(), before) + self.assertEqual(result.returncode, 2) + self.assertIn("cache must not overwrite the manifest", result.stderr) + self.assertNotIn("Traceback", result.stderr) + self.assertEqual(result.stdout, "") + def test_second_run_hashes_nothing(self) -> None: for jobs in (1, 4): with self.subTest(jobs=jobs): diff --git a/tools/manifest_verifier.py b/tools/manifest_verifier.py index 49a96f9126..1b59b2faa4 100644 --- a/tools/manifest_verifier.py +++ b/tools/manifest_verifier.py @@ -28,6 +28,9 @@ import time from typing import Callable, Optional, TextIO +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from report_paths import same_report_path # noqa: E402 + CHUNK = 8 * 1024 * 1024 DEFAULT_MANIFEST = os.path.join(os.path.dirname(__file__), "..", "reports", "manifest.json") CACHE_VERSION = 1 @@ -326,6 +329,8 @@ def verify_manifest( cache_path: Optional[str] = None, ) -> ManifestReport: """Verifies target_dir against manifest_path, optionally in parallel.""" + if cache_path is not None and same_report_path(cache_path, manifest_path): + raise ManifestError(f"cache must not overwrite the manifest: {cache_path}") try: mode = os.stat(target_dir).st_mode except FileNotFoundError: