Files
OBLITERATUS/scripts/release_bytecode_policy.py
T

120 lines
5.1 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 bytecode_identity() -> dict:
environment = Path(sys.prefix)
path = owned_site(environment) / 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")
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()