"""Preserve verified wheel-supplied caches before a fresh dependency compilation. RECORD is locked-install provenance, not an independent signature. Preserved inputs are never executable compilation evidence or a successful bootstrap. """ from __future__ import annotations import base64 import csv from email.parser import BytesParser import hashlib import importlib.util import io import json import os from pathlib import Path import stat import sys from scripts.release_bytecode_policy import canonical, reject_project_bytecode def fingerprint(path: Path) -> dict: canonical(path) before = path.stat() if not stat.S_ISREG(before.st_mode): raise ValueError(f"quarantine requires a regular file: {path}") data = path.read_bytes() after = path.stat() fields = ("st_dev", "st_ino", "st_size", "st_mtime_ns", "st_ctime_ns") if any(getattr(before, key) != getattr(after, key) for key in fields): raise ValueError(f"quarantine input changed while reading: {path}") return {"sha256": hashlib.sha256(data).hexdigest(), "size": len(data), "stat": {key: getattr(after, key) for key in fields}} def dependency_sources(directory: Path) -> dict: reject_project_bytecode(directory / "obliteratus") sources = {} for path in sorted(directory.rglob("*")): if path.is_symlink() and path.is_dir(): raise ValueError(f"dependency cache scope contains a directory alias: {path}") if path.suffix == ".py" and not path.is_relative_to(directory / "obliteratus") and path != directory / "app.py": sources[path.relative_to(directory).as_posix()] = fingerprint(path)["sha256"] return sources def destination(receipt: Path, root: Path, environment: Path) -> Path: archive = canonical(receipt.with_name(receipt.stem + "-quarantine")) excluded = [root, environment, *(Path(item or os.getcwd()).resolve() for item in sys.path)] if any(archive.is_relative_to(path) for path in excluded): raise ValueError("quarantine diagnostics must be outside source, environment and import roots") if archive.exists() or archive.is_symlink(): raise ValueError("quarantine evidence already exists; never rebind or overwrite it") return archive def candidates(directory: Path, sources: dict) -> tuple[dict, dict]: expected = {Path(importlib.util.cache_from_source(str(directory / name), optimization=level)): name for name in sources for level in ("", "1", "2")} caches, files = {}, {} for path in sorted(directory.rglob("*.pyc")): info = fingerprint(path) source = expected.get(path) if source is None: raise ValueError(f"cache-free preparation refuses an orphan or foreign cache: {path}") header = path.read_bytes()[:16] if len(header) != 16 or header[:4] != importlib.util.MAGIC_NUMBER: raise ValueError(f"cache-free preparation refuses a foreign cache header: {path}") name = path.relative_to(directory).as_posix() caches[name] = {"source": source, "header": header.hex()} files[name] = info files[source] = fingerprint(directory / source) if not caches: return caches, files claims = {name: [] for name in files} selected = {str(directory / name): name for name in files} record_hashes = {} for record in sorted(directory.glob("*.dist-info/RECORD")): # Only selected claims have to be canonical. Unrelated ../../../bin # console entrypoints are legitimate installed RECORD rows. fingerprint(record) # Never read a symlink or special metadata file. data = record.read_bytes() record_hashes[record] = hashlib.sha256(data).hexdigest() rows = list(csv.reader(io.StringIO(data.decode("utf-8")), strict=True)) for row in rows: if not row: continue raw = row[0] path = directory / raw name = selected.get(str(path.resolve())) if name is not None: claims[name].append((record, row)) for name, matches in claims.items(): if len(matches) != 1: raise ValueError(f"cache-free preparation requires one RECORD claim: {name}") record, row = matches[0] info = files[name] encoded = base64.urlsafe_b64encode(bytes.fromhex(info["sha256"])).decode().rstrip("=") if row != [name, "sha256=" + encoded, str(info["size"])]: raise ValueError(f"cache-free preparation refuses noncanonical or mismatched RECORD claim: {name}") record_name = record.relative_to(directory).as_posix() files.setdefault(record_name, fingerprint(record)) if files[record_name]["sha256"] != record_hashes[record]: raise ValueError("selected RECORD changed during validation") for name, entry in caches.items(): record = claims[name][0][0] if claims[entry["source"]][0][0] != record: raise ValueError("cache and source must have the same RECORD owner") metadata = record.with_name("METADATA") metadata_name = metadata.relative_to(directory).as_posix() files.setdefault(metadata_name, fingerprint(metadata)) parsed = BytesParser().parsebytes(metadata.read_bytes()) if any(len(parsed.get_all(key, [])) != 1 or not parsed[key] for key in ("Name", "Version")): raise ValueError("quarantine owner metadata lacks a unique name/version") entry.update(record=record.relative_to(directory).as_posix(), distribution={"name": parsed["Name"], "version": parsed["Version"]}) return caches, files def persist(path: Path, data: bytes) -> None: with path.open("xb") as stream: stream.write(data) stream.flush() os.fsync(stream.fileno()) def verify_preserved(expected: dict | None) -> None: if expected is None: return path = Path(expected["manifest"]) if fingerprint(path)["sha256"] != expected["sha256"]: raise ValueError("preserved quarantine manifest differs from the bootstrap receipt") manifest = json.loads(path.read_text(encoding="utf-8")) for name, info in manifest["files"].items(): if "blob" not in info: continue blob = path.parent / info["blob"] if blob.parent != path.parent or blob.suffix != ".bin": raise ValueError("invalid quarantine blob locator") actual = fingerprint(blob) if any(actual[key] != info[key] for key in ("sha256", "size")): raise ValueError(f"preserved quarantine blob differs: {name}") def prepare(directory: Path, archive: Path, sources: dict, binding: dict) -> dict | None: """Validate all inputs, preserve all bytes, then remove only those caches.""" caches, files = candidates(directory, sources) if not caches: return None # Nothing has been moved or copied before complete candidate validation. archive.parent.mkdir(parents=True, exist_ok=True) archive.mkdir(mode=0o700) for index, (name, info) in enumerate(sorted(files.items())): if name in caches or name.endswith(("/RECORD", "/METADATA")): blob = f"{index:04d}.bin" data = (directory / name).read_bytes() if hashlib.sha256(data).hexdigest() != info["sha256"]: raise ValueError("quarantine input changed before preservation") persist(archive / blob, data) info["blob"] = blob manifest = archive / "manifest.json" payload = {"schema_version": 1, "stage": "preserved-inputs-before-removal", "binding": binding, "sources": sources, "caches": caches, "files": files} persist(manifest, (json.dumps(payload, indent=2, sort_keys=True) + "\n").encode()) expected = {"manifest": str(manifest), "sha256": fingerprint(manifest)["sha256"]} verify_preserved(expected) for directory_entry in (archive, archive.parent): fd = os.open(directory_entry, os.O_RDONLY | os.O_DIRECTORY) try: os.fsync(fd) finally: os.close(fd) # Recheck the entire input set before the first unlink, then each cache # immediately before unlink. Failures retain the archive and any originals. for name, info in files.items(): if fingerprint(directory / name) != {key: value for key, value in info.items() if key != "blob"}: raise ValueError("quarantine input changed before removal") if dependency_sources(directory) != sources or set(caches) != { path.relative_to(directory).as_posix() for path in directory.rglob("*.pyc")}: raise ValueError("quarantine source/cache inventory changed before removal") # Newly added or previously unrelated RECORDs can introduce a second owner # without changing any original input. Revalidate all current claims. current_caches, current_files = candidates(directory, sources) original_files = {name: {key: value for key, value in info.items() if key != "blob"} for name, info in files.items()} if current_caches != caches or current_files != original_files: raise ValueError("quarantine ownership inputs changed before removal") for name in caches: path = directory / name if fingerprint(path) != {key: value for key, value in files[name].items() if key != "blob"}: raise ValueError("quarantine cache changed before removal") path.unlink() if list(directory.rglob("*.pyc")) or dependency_sources(directory) != sources: raise ValueError("quarantine did not establish an unchanged cache-free source set") for name, info in files.items(): if name not in caches and fingerprint(directory / name)["sha256"] != info["sha256"]: raise ValueError("quarantine changed source or RECORD inputs") return expected