@@ -2409,6 +2409,43 @@ def _audit_sha256_tree(path: Path) -> str:
24092409 return digest.hexdigest()
24102410
24112411
2412+ def _audit_python_cache_artifact(relative: Path) -> bool:
2413+ return "__pycache__" in relative.parts or relative.suffix.lower() in {
2414+ ".pyc",
2415+ ".pyo",
2416+ }
2417+
2418+
2419+ def _audit_sha256_source_tree(
2420+ path: Path,
2421+ *,
2422+ reject_python_cache: bool = False,
2423+ ) -> str:
2424+ """Hash source bytes canonically without host-generated Python caches."""
2425+
2426+ _require(path.is_dir(), "reviewed source tree is missing: %s" % path)
2427+ digest = hashlib.sha256()
2428+ for item in sorted(path.rglob("*")):
2429+ _require(not item.is_symlink(), "reviewed source tree contains a symlink")
2430+ if not item.is_file():
2431+ continue
2432+ relative_path = item.relative_to(path)
2433+ if _audit_python_cache_artifact(relative_path):
2434+ _require(
2435+ not reject_python_cache,
2436+ "reviewed source tree contains Python cache artifact: %s"
2437+ % relative_path.as_posix(),
2438+ )
2439+ continue
2440+ relative = relative_path.as_posix().encode("utf-8")
2441+ value = item.read_bytes()
2442+ digest.update(len(relative).to_bytes(8, "big"))
2443+ digest.update(relative)
2444+ digest.update(len(value).to_bytes(8, "big"))
2445+ digest.update(value)
2446+ return digest.hexdigest()
2447+
2448+
24122449def _audit_artifact_requirements(artifact: dict[str, Any]) -> list[str]:
24132450 value = artifact.get("requires")
24142451 _require(isinstance(value, list), "locked artifact requires must be an array")
@@ -4145,7 +4182,7 @@ def _audit_v2_source(
41454182 path = repo_root / relative
41464183 _audit_no_symlink_components(repo_root, path, "%s source tree" % label)
41474184 _require(
4148- _audit_sha256_tree (path) == digest,
4185+ _audit_sha256_source_tree (path, reject_python_cache=True ) == digest,
41494186 "%s source tree changed" % label,
41504187 )
41514188 return copy.deepcopy(record)
0 commit comments