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
136 lines
5.9 KiB
Python
136 lines
5.9 KiB
Python
"""Prevent import caches in freshly owned release venvs, including -E/-I children.
|
|
|
|
This is an import policy, not a filesystem sandbox: -S, explicit compilation,
|
|
another environment, or resetting sys.dont_write_bytecode can bypass it. Keep
|
|
the strict before/after checkout verification; never delete resulting evidence.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import site
|
|
import subprocess
|
|
import sys
|
|
import sysconfig
|
|
import venv
|
|
|
|
|
|
FILENAME = "00_obliteratus_release_no_bytecode.pth"
|
|
CONTENT = b"import sys; sys.dont_write_bytecode=True\n"
|
|
OWNER = ".obliteratus-release-disposable.json"
|
|
POLICY = {"name": "release-import-bytecode-v1", "sha256": hashlib.sha256(CONTENT).hexdigest(),
|
|
"normal": True, "ignore_environment": True, "isolated": True}
|
|
|
|
|
|
def canonical(path: Path) -> Path:
|
|
if not path.is_absolute() or path != path.resolve():
|
|
raise ValueError("release environment paths must be absolute and have no symlinks")
|
|
return path
|
|
|
|
|
|
def ownership(environment: Path) -> dict:
|
|
canonical(environment)
|
|
stat = environment.stat()
|
|
return {"environment": str(environment), "device": stat.st_dev, "inode": stat.st_ino}
|
|
|
|
|
|
def create(environment: Path) -> None:
|
|
"""Claim only a new directory, never an existing/shared/global environment."""
|
|
canonical(environment)
|
|
environment.mkdir(mode=0o700) # Exclusive, including an existing empty directory.
|
|
venv.EnvBuilder(with_pip=False, symlinks=True).create(environment)
|
|
with (environment / OWNER).open("x", encoding="utf-8") as stream:
|
|
json.dump(ownership(environment), stream, sort_keys=True)
|
|
|
|
|
|
def owned_site(environment: Path) -> Path:
|
|
canonical(environment)
|
|
marker = environment / OWNER
|
|
if (sys.prefix == sys.base_prefix or Path(sys.prefix) != environment
|
|
or marker.is_symlink() or not marker.is_file()
|
|
or json.loads(marker.read_text(encoding="utf-8")) != ownership(environment)):
|
|
raise ValueError("startup policy requires this freshly owned disposable release venv")
|
|
directory = canonical(Path(sysconfig.get_path("purelib")))
|
|
if not directory.is_relative_to(environment) or str(directory) not in site.getsitepackages():
|
|
raise ValueError("site-packages must belong to the owned release venv")
|
|
if not directory.is_dir():
|
|
raise ValueError("owned site-packages does not exist")
|
|
return directory
|
|
|
|
|
|
def install(environment: Path) -> None:
|
|
"""Call once after the final locked sync; conflicting files are never replaced."""
|
|
path = owned_site(environment) / FILENAME
|
|
with path.open("xb") as stream:
|
|
stream.write(CONTENT)
|
|
|
|
|
|
def reject_project_bytecode(package: Path) -> None:
|
|
"""Project source hashes do not bind executable caches, including app.py."""
|
|
for path in package.rglob("*"):
|
|
if path.suffix == ".pyc" or path.is_symlink() and path.is_dir():
|
|
raise ValueError(f"project bytecode or cache directory alias is forbidden: {path}")
|
|
# app is the project's additional installed top-level Python module. Other
|
|
# top-level dependency modules may retain their verified dependency caches.
|
|
candidates = [package.parent / "app.pyc", *(package.parent / "__pycache__").glob("app.*.pyc")]
|
|
for path in candidates:
|
|
if path.exists() or path.is_symlink():
|
|
raise ValueError(f"project bytecode is forbidden: {path}")
|
|
|
|
|
|
def bytecode_identity() -> dict:
|
|
environment = Path(sys.prefix)
|
|
directory = owned_site(environment)
|
|
reject_project_bytecode(directory / "obliteratus")
|
|
path = directory / FILENAME
|
|
if path.is_symlink() or not path.is_file() or path.read_bytes() != CONTENT:
|
|
raise ValueError("missing or altered release bytecode startup policy")
|
|
if sys.dont_write_bytecode is not True:
|
|
raise ValueError("release bytecode startup policy is ineffective in current process")
|
|
# No -B or environment flag can mask an ineffective hook in these probes.
|
|
# Any pre-hook cache writes stay inside the owned environment for diagnosis.
|
|
env = {key: value for key, value in os.environ.items() if key != "PYTHONDONTWRITEBYTECODE"}
|
|
code = 'import sys; print(str(sys.dont_write_bytecode) + "\\n" + sys.prefix)'
|
|
for flags in ([], ["-E"], ["-I"]):
|
|
result = subprocess.run(
|
|
[sys.executable, "-X", f"pycache_prefix={environment / 'startup-probe-cache'}", *flags, "-c", code],
|
|
env=env, capture_output=True, text=True, check=True, timeout=15,
|
|
)
|
|
if result.stdout != f"True\n{environment}\n" or result.stderr:
|
|
raise ValueError(f"release bytecode startup policy ineffective for {flags or ['normal']}")
|
|
# Detect replacement during the subprocess probes as well.
|
|
if path.is_symlink() or path.read_bytes() != CONTENT:
|
|
raise ValueError("release bytecode startup policy changed during verification")
|
|
reject_project_bytecode(directory / "obliteratus")
|
|
return {"bytecode_policy": dict(POLICY), "bytecode_policy_path": str(path)}
|
|
|
|
|
|
def check_policy(identity: dict) -> None:
|
|
policy, path = identity.get("bytecode_policy"), identity.get("bytecode_policy_path")
|
|
if (not isinstance(policy, dict) or policy != POLICY
|
|
or any(type(policy.get(key)) is not type(value) for key, value in POLICY.items())
|
|
or not isinstance(path, str) or not Path(path).is_absolute() or Path(path).name != FILENAME):
|
|
raise ValueError("missing or invalid release bytecode policy binding")
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("command", choices=("create", "install", "verify"))
|
|
parser.add_argument("--environment", required=True, type=Path)
|
|
args = parser.parse_args()
|
|
if args.command == "create":
|
|
create(args.environment)
|
|
elif args.command == "install":
|
|
install(args.environment)
|
|
else:
|
|
owned_site(args.environment)
|
|
print(json.dumps(bytecode_identity(), sort_keys=True))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|