From eb348cdd1f6b28653b71ed0e9b7d27d17dbcee40 Mon Sep 17 00:00:00 2001 From: JiyouProt <104832502+JiyouProt@users.noreply.github.com> Date: Tue, 24 Nov 2026 04:35:18 +0100 Subject: [PATCH] manifest_verifier: refuse caches that alias listed inputs (VERIFY-CACHE-INPUTS) --- tests/test_manifest_verifier.py | 55 +++++++++++++++++++++++++++++++++ tools/manifest_verifier.py | 7 +++++ 2 files changed, 62 insertions(+) diff --git a/tests/test_manifest_verifier.py b/tests/test_manifest_verifier.py index 3729b3556d..197735a55e 100644 --- a/tests/test_manifest_verifier.py +++ b/tests/test_manifest_verifier.py @@ -954,6 +954,61 @@ def test_junk_cache_is_ignored(self) -> None: report = self.verify() self.assertEqual((report.passed, report.cached), (True, 0)) + def assert_input_cache_refused(self) -> None: + with patch.object(mv, "sha256_file", wraps=mv.sha256_file) as hashing: + with self.assertRaisesRegex(mv.ManifestError, "cache must not overwrite a listed input"): + self.verify() + hashing.assert_not_called() + for rel, data in self.data.items(): + with open(os.path.join(self.game, rel), "rb") as f: + self.assertEqual(f.read(), data) + + def test_cache_refuses_normalized_input_path(self) -> None: + self.cache = os.path.join(self.game, "..", "game", "a.bin") + self.assert_input_cache_refused() + + def test_cache_refuses_input_case_alias_on_folding_volume(self) -> None: + self.cache = os.path.join(self.game, "A.BIN") + if not os.path.exists(self.cache): + self.skipTest("volume is case-sensitive") + self.assert_input_cache_refused() + + def test_cache_refuses_input_hard_link(self) -> None: + try: + os.link(os.path.join(self.game, "a.bin"), self.cache) + except OSError as err: + self.skipTest(f"hard links unavailable: {err}") + self.assert_input_cache_refused() + + def test_cache_refuses_input_symlink(self) -> None: + try: + os.symlink(os.path.join(self.game, "a.bin"), self.cache) + except OSError as err: + self.skipTest(f"symlinks unavailable: {err}") + self.assert_input_cache_refused() + + def test_cache_does_not_create_a_missing_listed_input(self) -> None: + self.cache = os.path.join(self.game, "a.bin") + os.unlink(self.cache) + with self.assertRaisesRegex(mv.ManifestError, "cache must not overwrite a listed input"): + self.verify() + self.assertFalse(os.path.exists(self.cache)) + + def test_cli_cache_equal_to_input_preserves_data(self) -> None: + self.cache = os.path.join(self.game, "a.bin") + result = subprocess.run( + [sys.executable, "-I", TOOL, self.game, "--manifest", self.manifest, + "--cache", self.cache, "--json"], + capture_output=True, text=True, timeout=15, + ) + for rel, data in self.data.items(): + with open(os.path.join(self.game, rel), "rb") as f: + self.assertEqual(f.read(), data) + self.assertEqual(result.returncode, 2) + self.assertIn("cache must not overwrite a listed input", result.stderr) + self.assertNotIn("Traceback", result.stderr) + self.assertEqual(result.stdout, "") + def test_cache_inside_target_and_strict(self) -> None: self.cache = os.path.join(self.game, "cache.json") for _ in range(2): # cold and warm diff --git a/tools/manifest_verifier.py b/tools/manifest_verifier.py index 49a96f9126..9e7e1c69b6 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 @@ -336,6 +339,10 @@ def verify_manifest( raise ManifestError(f"target path is not a directory: {target_dir}") want = load_manifest(manifest_path) + if cache_path is not None: + for rel in want: + if same_report_path(cache_path, os.path.join(target_dir, rel)): + raise ManifestError(f"cache must not overwrite a listed input: {rel}") have = dict(walk(target_dir, exclude=manifest_path if cache_path is None else (manifest_path, cache_path))) if not want and have: # A cut-short create or a mistyped --manifest would otherwise print OK without checking a file.