mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Prepare only locked dependencies in owned release environments before installing the exact project. Preserve verified wheel-supplied caches outside import paths, require zero caches before compilation, and bind generated cache bytes to source and retained diagnostics. Keep project caches forbidden, preserve the no-write startup policy, sanitize inherited compilation settings, and retain bootstrap evidence separately without changing test selection or release gates. Refs #306
296 lines
13 KiB
Python
296 lines
13 KiB
Python
"""Wheel-supplied caches are preserved inputs, never accepted compiler output."""
|
|
|
|
import base64
|
|
import csv
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
|
|
import pytest
|
|
|
|
from scripts import release_dependency_quarantine as quarantine
|
|
from tests import test_release_dependency_bytecode as bytecode_tests
|
|
|
|
|
|
setup = bytecode_tests.setup
|
|
|
|
|
|
def supplied(setup, names=("dependency.py",), owner="wheel"):
|
|
paths = []
|
|
for name in names:
|
|
source = setup.site / name
|
|
source.parent.mkdir(parents=True, exist_ok=True)
|
|
source.write_text("value = 1\n")
|
|
paths.extend([source, bytecode_tests.cache(source)])
|
|
metadata = setup.site / f"{owner}-1.dist-info"
|
|
metadata.mkdir()
|
|
(metadata / "METADATA").write_text(f"Name: {owner}\nVersion: 1\n")
|
|
rows = [[p.relative_to(setup.site).as_posix(), "sha256=" + base64.urlsafe_b64encode(
|
|
hashlib.sha256(p.read_bytes()).digest()).decode().rstrip("="), str(p.stat().st_size)] for p in paths]
|
|
# These unrelated installer entrypoints must not be rejected or touched.
|
|
rows.extend([["../../../bin/example", "sha256=entrypoint", "17"],
|
|
[f"{owner}-1.dist-info/RECORD", "", ""]])
|
|
record = metadata / "RECORD"
|
|
write_rows(record, rows)
|
|
return paths, record, rows
|
|
|
|
|
|
def write_rows(path, rows):
|
|
with path.open("w", newline="") as stream:
|
|
csv.writer(stream).writerows(rows)
|
|
|
|
|
|
def prepare(setup):
|
|
return quarantine.prepare(setup.site, quarantine.destination(setup.receipt, setup.root, setup.environment),
|
|
quarantine.dependency_sources(setup.site), {"test_binding": True})
|
|
|
|
|
|
def archive(setup):
|
|
return setup.receipt.with_name("receipt-quarantine")
|
|
|
|
|
|
def test_supplied_dotted_cache_is_preserved_before_zero_cache_compilation_and_bound_in_final_receipt(setup):
|
|
paths, record, _ = supplied(setup, ("v0.9.0.a.py",))
|
|
original = {p: p.read_bytes() for p in [*paths, record]}
|
|
|
|
def inspect(command):
|
|
if "--compile-bytecode" in command:
|
|
assert not list(setup.site.rglob("*.pyc"))
|
|
manifest = json.loads((archive(setup) / "manifest.json").read_text())
|
|
assert manifest["stage"] == "preserved-inputs-before-removal"
|
|
for name, info in manifest["files"].items():
|
|
if "blob" in info:
|
|
assert (archive(setup) / info["blob"]).read_bytes() == (original.get(setup.site / name)
|
|
or (setup.site / name).read_bytes())
|
|
setup.inject = inspect
|
|
bytecode_tests.compile_dependencies(setup)
|
|
final = json.loads(setup.receipt.read_text())
|
|
pre = json.loads(setup.receipt.with_name("receipt-precompile.json").read_text())
|
|
assert final["preparation"] == pre["preparation"]
|
|
assert final["preparation"]["sha256"] == bytecode_tests.bootstrap.digest(archive(setup) / "manifest.json")
|
|
assert record.read_bytes() == original[record] and paths[0].read_bytes() == original[paths[0]]
|
|
assert not paths[1].exists()
|
|
bytecode_tests.install_project(setup)
|
|
bytecode_tests.verify(setup)
|
|
|
|
|
|
@pytest.mark.parametrize("target", [0, 1])
|
|
@pytest.mark.parametrize("change", ["dot", "parent", "absolute", "duplicate", "alias-duplicate", "symlink-alias",
|
|
"duplicate-owner", "hash", "size", "unlisted"])
|
|
def test_selected_raw_record_claims_refuse_before_copying_anything(setup, target, change):
|
|
paths, record, rows = supplied(setup)
|
|
original = paths[1].read_bytes()
|
|
if change in {"dot", "parent", "absolute"}:
|
|
raw = rows[target][0]
|
|
rows[target][0] = {"dot": "./" + raw, "parent": "unused/../" + raw,
|
|
"absolute": str(setup.site / raw)}[change]
|
|
elif change == "duplicate":
|
|
rows.append(list(rows[target]))
|
|
elif change == "alias-duplicate":
|
|
rows.append(["./" + rows[target][0], *rows[target][1:]])
|
|
elif change == "symlink-alias":
|
|
alias = setup.root.parent / "external-alias"
|
|
alias.symlink_to(setup.site, target_is_directory=True)
|
|
rows.append([str(alias / rows[target][0]), *rows[target][1:]])
|
|
elif change == "duplicate-owner":
|
|
other = setup.site / "other-1.dist-info"
|
|
other.mkdir()
|
|
write_rows(other / "RECORD", [rows[target]])
|
|
elif change == "hash":
|
|
rows[target][1] = "sha256=wrong"
|
|
elif change == "size":
|
|
rows[target][2] = "999"
|
|
else:
|
|
rows.pop(target)
|
|
write_rows(record, rows)
|
|
with pytest.raises(ValueError, match="RECORD claim"):
|
|
prepare(setup)
|
|
assert paths[1].read_bytes() == original and not archive(setup).exists()
|
|
|
|
|
|
@pytest.mark.parametrize("change", ["cache", "source", "cache-link", "record-link", "directory-link", "orphan", "foreign"])
|
|
def test_tampered_unowned_and_symlink_inputs_are_preserved_and_refused(setup, change):
|
|
paths, record, _ = supplied(setup)
|
|
if change in {"cache", "source"}:
|
|
target = paths[change == "cache"]
|
|
target.write_bytes(target.read_bytes() + b"tampered")
|
|
elif change in {"cache-link", "record-link"}:
|
|
target = paths[1] if change == "cache-link" else record
|
|
saved = target.with_name("retained-original")
|
|
target.rename(saved)
|
|
target.symlink_to(saved)
|
|
elif change == "directory-link":
|
|
(setup.site / "alias").symlink_to(paths[1].parent, target_is_directory=True)
|
|
elif change == "orphan":
|
|
(setup.site / "orphan.pyc").write_bytes(b"unowned")
|
|
else:
|
|
paths[1].rename(paths[1].with_name("dependency.cpython-999.pyc"))
|
|
before = {p: p.read_bytes() for p in setup.site.rglob("*.pyc")}
|
|
with pytest.raises(ValueError):
|
|
prepare(setup)
|
|
assert not archive(setup).exists()
|
|
assert all(p.read_bytes() == data for p, data in before.items())
|
|
|
|
|
|
def test_later_invalid_candidate_prevents_preservation_or_removal_of_first(setup):
|
|
paths, _, _ = supplied(setup, ("dependency.py", "z.py"))
|
|
paths[-1].write_bytes(paths[-1].read_bytes() + b"changed")
|
|
with pytest.raises(ValueError, match="RECORD claim"):
|
|
prepare(setup)
|
|
assert not archive(setup).exists() and all(path.exists() for path in paths)
|
|
|
|
|
|
@pytest.mark.parametrize("stage", ["copy", "remove", "compiler"])
|
|
def test_failure_preserves_inputs_without_success_receipt_or_retry_overwrite(setup, monkeypatch, stage):
|
|
paths, _, _ = supplied(setup, ("dependency.py", "second.py"))
|
|
original = {p: p.read_bytes() for p in paths if p.suffix == ".pyc"}
|
|
if stage == "copy":
|
|
real = quarantine.persist
|
|
count = 0
|
|
|
|
def fail(path, data):
|
|
nonlocal count
|
|
count += 1
|
|
if count == 2:
|
|
raise OSError("copy failed")
|
|
real(path, data)
|
|
monkeypatch.setattr(quarantine, "persist", fail)
|
|
elif stage == "remove":
|
|
real = Path.unlink
|
|
|
|
def fail(path, *args, **kwargs):
|
|
if path == paths[-1]:
|
|
raise OSError("removal failed")
|
|
return real(path, *args, **kwargs)
|
|
monkeypatch.setattr(Path, "unlink", fail)
|
|
else:
|
|
def fail(command):
|
|
if "--compile-bytecode" in command:
|
|
assert not list(setup.site.rglob("*.pyc"))
|
|
raise subprocess.CalledProcessError(42, command)
|
|
setup.inject = fail
|
|
with pytest.raises((OSError, subprocess.CalledProcessError)):
|
|
bytecode_tests.compile_dependencies(setup)
|
|
assert not setup.receipt.exists()
|
|
if stage == "copy":
|
|
assert all(p.read_bytes() == data for p, data in original.items())
|
|
assert not (archive(setup) / "manifest.json").exists()
|
|
else:
|
|
manifest = json.loads((archive(setup) / "manifest.json").read_text())
|
|
for p, data in original.items():
|
|
info = manifest["files"][p.relative_to(setup.site).as_posix()]
|
|
assert (archive(setup) / info["blob"]).read_bytes() == data
|
|
retained = {p: p.read_bytes() for p in archive(setup).iterdir()}
|
|
with pytest.raises(ValueError, match="never rebind"):
|
|
bytecode_tests.compile_dependencies(setup)
|
|
assert all(p.read_bytes() == data for p, data in retained.items())
|
|
|
|
|
|
@pytest.mark.parametrize("change", ["manifest", "cache-blob", "record-blob", "missing-blob", "self-consistent"])
|
|
def test_final_verifier_requires_the_bound_manifest_and_all_preserved_blobs(setup, change):
|
|
supplied(setup)
|
|
bytecode_tests.compile_dependencies(setup)
|
|
bytecode_tests.install_project(setup)
|
|
path = archive(setup) / "manifest.json"
|
|
manifest = json.loads(path.read_text())
|
|
if change == "manifest":
|
|
path.write_bytes(path.read_bytes() + b" ")
|
|
else:
|
|
name = next(n for n in manifest["files"] if n.endswith("/RECORD" if change == "record-blob" else ".pyc"))
|
|
info = manifest["files"][name]
|
|
blob = archive(setup) / info["blob"]
|
|
if change == "missing-blob":
|
|
blob.unlink()
|
|
else:
|
|
blob.write_bytes(b"unbound")
|
|
if change == "self-consistent":
|
|
info.update(sha256=hashlib.sha256(b"unbound").hexdigest(), size=7)
|
|
path.write_text(json.dumps(manifest))
|
|
with pytest.raises((ValueError, FileNotFoundError)):
|
|
bytecode_tests.verify(setup)
|
|
|
|
|
|
@pytest.mark.parametrize("where", ["source", "environment", "existing", "symlink"])
|
|
def test_unsafe_diagnostic_destination_refuses_before_sync(setup, where):
|
|
if where in {"source", "environment"}:
|
|
setup.receipt = (setup.root if where == "source" else setup.environment) / "receipt.json"
|
|
elif where == "existing":
|
|
archive(setup).mkdir(parents=True)
|
|
else:
|
|
setup.receipt.parent.mkdir()
|
|
archive(setup).symlink_to(setup.root, target_is_directory=True)
|
|
with pytest.raises(ValueError):
|
|
bytecode_tests.compile_dependencies(setup)
|
|
assert not setup.calls
|
|
|
|
|
|
@pytest.mark.parametrize("change", ["source", "record", "cache", "extra-cache"])
|
|
def test_changes_after_preservation_refuse_before_first_removal(setup, monkeypatch, change):
|
|
paths, record, _ = supplied(setup)
|
|
original = paths[1].read_bytes()
|
|
real = quarantine.persist
|
|
|
|
def mutate(path, data):
|
|
real(path, data)
|
|
if path.name == "manifest.json":
|
|
target = {"source": paths[0], "record": record, "cache": paths[1],
|
|
"extra-cache": setup.site / "new.pyc"}[change]
|
|
target.write_bytes(b"changed after preservation")
|
|
monkeypatch.setattr(quarantine, "persist", mutate)
|
|
with pytest.raises(ValueError, match="changed before removal"):
|
|
bytecode_tests.compile_dependencies(setup)
|
|
assert paths[1].exists() and not setup.receipt.exists()
|
|
manifest = json.loads((archive(setup) / "manifest.json").read_text())
|
|
info = manifest["files"][paths[1].relative_to(setup.site).as_posix()]
|
|
assert (archive(setup) / info["blob"]).read_bytes() == original
|
|
|
|
|
|
@pytest.mark.parametrize("existing", [False, True])
|
|
@pytest.mark.parametrize("target", [0, 1])
|
|
def test_new_claim_after_preservation_refuses_before_first_removal(setup, monkeypatch, existing, target):
|
|
paths, _, rows = supplied(setup)
|
|
originals = {path: path.read_bytes() for path in paths}
|
|
other = setup.site / "other-1.dist-info/RECORD"
|
|
if existing:
|
|
other.parent.mkdir()
|
|
write_rows(other, [["../../../bin/unrelated", "sha256=entrypoint", "17"]])
|
|
real = quarantine.persist
|
|
|
|
def add_claim(path, data):
|
|
real(path, data)
|
|
if path.name == "manifest.json":
|
|
other.parent.mkdir(exist_ok=True)
|
|
write_rows(other, [rows[target]])
|
|
monkeypatch.setattr(quarantine, "persist", add_claim)
|
|
with pytest.raises(ValueError, match="one RECORD claim"):
|
|
bytecode_tests.compile_dependencies(setup)
|
|
assert all(path.read_bytes() == data for path, data in originals.items())
|
|
assert not setup.receipt.exists() and len(setup.calls) == 1
|
|
assert "--compile-bytecode" not in setup.calls[0]
|
|
manifest = json.loads((archive(setup) / "manifest.json").read_text())
|
|
info = manifest["files"][paths[1].relative_to(setup.site).as_posix()]
|
|
assert (archive(setup) / info["blob"]).read_bytes() == originals[paths[1]]
|
|
|
|
|
|
@pytest.mark.parametrize("changed", ["RECORD", "METADATA"])
|
|
def test_revalidated_unique_owner_must_still_match_preserved_inputs(setup, monkeypatch, changed):
|
|
paths, record, _ = supplied(setup)
|
|
original = paths[1].read_bytes()
|
|
real, calls = quarantine.candidates, []
|
|
|
|
def replace(directory, sources):
|
|
calls.append(True)
|
|
if len(calls) == 2:
|
|
path = record.with_name(changed)
|
|
path.write_text(path.read_text() + ("../../../bin/other,,\n" if changed == "RECORD" else "Summary: changed\n"))
|
|
return real(directory, sources)
|
|
monkeypatch.setattr(quarantine, "candidates", replace)
|
|
with pytest.raises(ValueError, match="ownership inputs changed before removal"):
|
|
bytecode_tests.compile_dependencies(setup)
|
|
assert paths[1].read_bytes() == original and not setup.receipt.exists()
|
|
assert len(setup.calls) == 1 and len(calls) == 2
|
|
manifest = json.loads((archive(setup) / "manifest.json").read_text())
|
|
info = manifest["files"][paths[1].relative_to(setup.site).as_posix()]
|
|
assert (archive(setup) / info["blob"]).read_bytes() == original
|