import hashlib import json import subprocess import re from datetime import datetime, timezone from pathlib import Path from capture import ROOT, private_values def main(): names = subprocess.check_output(["git", "ls-files", "--cached", "-z"], cwd=ROOT) files = [name.decode() for name in names.split(b"\0") if name] secrets = private_values() sources = [] for name in files: path = Path(name) assert "secrets" not in path.parts, "Private directory staged" assert path.name != ".env", "Environment secrets staged" assert path.suffix not in { ".key", ".p12", ".pfx", ".jks", ".pem", }, "Private material staged" content = subprocess.check_output(["git", "show", ":" + name], cwd=ROOT) text = content.decode("utf-8", errors="replace") assert not re.search( r"^-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----", text, re.MULTILINE ), "Private key staged" assert not any(value in text for value in secrets), "Known secret staged" if path.parts[0] != "evidence": sources.append(dict(path=name, sha256=hashlib.sha256(content).hexdigest())) for path in (ROOT / "evidence").glob("*.txt"): text = path.read_text(encoding="utf-8") if text.startswith("Command:"): assert "\nExit code: 0\n" in text, "Failed evidence: " + path.name for target in re.findall(r"\]\(([^)]+)\)", (ROOT / "REPORT.md").read_text()): if "://" not in target and target != "evidence/source-manifest.json": assert (ROOT / target).exists(), "Missing report evidence: " + target output = dict( utc=datetime.now(timezone.utc).isoformat(), upstream_base="20afc2c", branch="solution/bvt2301-ivanov", method="SHA-256 of staged source bytes; source list excludes evidence to avoid recursive hashes. Compare files after checkout with LF endings.", no_known_secrets_or_private_keys_staged=True, captured_commands_successful=True, source_files=sources, ) (ROOT / "evidence/source-manifest.json").write_text(json.dumps(output, indent=2) + "\n") print("PASS staged source integrity, secret exclusion and captured command exit codes") if __name__ == "__main__": main()