|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Verify every vendor-meta.json records the bytes actually on disk. |
| 3 | +
|
| 4 | +WHY THIS EXISTS |
| 5 | +--------------- |
| 6 | +`vendor-meta.json` is the provenance record for a deep capture: per file, the |
| 7 | +source URL, the upstream commit where applicable, and the sha256 + byte count of |
| 8 | +the exact bytes vendored. Every downstream argument about what upstream said, and |
| 9 | +every re-vendor decision, rests on that record being true. |
| 10 | +
|
| 11 | +Nothing checked it. Each extractor's `--check` proves the projection is a faithful |
| 12 | +derivation of the FILES, and `--self-test` proves the anchors parse — neither reads |
| 13 | +`sha256` or `bytes` at all. So a hand re-vendor that copied a new page in but |
| 14 | +recorded the wrong hash (or forgot to update it) passed every gate in the repo |
| 15 | +while the provenance quietly described a file that no longer existed. |
| 16 | +
|
| 17 | +That is not a hypothetical shape: three reference docs were re-vendored BY HAND |
| 18 | +during the freshness work, each one editing sha256 and bytes in a text editor. |
| 19 | +A silent provenance lie is worse than a stale capture, because the stale capture |
| 20 | +is at least honestly labelled. |
| 21 | +
|
| 22 | +WHAT IT CHECKS, for every specs/_vendor/upstream/<contract>/vendor-meta.json: |
| 23 | +
|
| 24 | + 1. every declared file exists; |
| 25 | + 2. its sha256 matches the bytes on disk; |
| 26 | + 3. its `bytes` matches the real size; |
| 27 | + 4. every file in the directory is DECLARED (an undeclared file is an |
| 28 | + unprovenanced input the extractor could silently start parsing); |
| 29 | + 5. the required top-level keys are present. |
| 30 | +
|
| 31 | +Stdlib only. Offline. Read-only. |
| 32 | +
|
| 33 | +Exit 0 = every capture's provenance is true; 1 = a mismatch; 2 = usage/parse error. |
| 34 | +
|
| 35 | +Usage: |
| 36 | + check-vendor-meta-integrity.py [--vendor-root DIR] |
| 37 | +""" |
| 38 | + |
| 39 | +from __future__ import annotations |
| 40 | + |
| 41 | +import argparse |
| 42 | +import hashlib |
| 43 | +import json |
| 44 | +import os |
| 45 | +import sys |
| 46 | + |
| 47 | +REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 48 | +DEFAULT_VENDOR_ROOT = os.path.join(REPO_ROOT, "specs", "_vendor", "upstream") |
| 49 | + |
| 50 | +# Files that live beside a capture but are documentation ABOUT it, not inputs to it. |
| 51 | +NOT_CAPTURE_INPUTS = {"vendor-meta.json", "projection.json", "projection.v1.json", "PROVENANCE.md"} |
| 52 | + |
| 53 | +REQUIRED_META_KEYS = ("contract", "spec_version", "files") |
| 54 | + |
| 55 | + |
| 56 | +def _sha256(path: str) -> tuple[str, int]: |
| 57 | + digest = hashlib.sha256() |
| 58 | + size = 0 |
| 59 | + with open(path, "rb") as fh: |
| 60 | + while chunk := fh.read(1 << 20): |
| 61 | + digest.update(chunk) |
| 62 | + size += len(chunk) |
| 63 | + return digest.hexdigest(), size |
| 64 | + |
| 65 | + |
| 66 | +def check_capture(capture_dir: str, problems: list[str]) -> int: |
| 67 | + """Verify one capture directory. Returns the number of files checked.""" |
| 68 | + name = os.path.basename(capture_dir) |
| 69 | + meta_path = os.path.join(capture_dir, "vendor-meta.json") |
| 70 | + try: |
| 71 | + with open(meta_path, encoding="utf-8") as fh: |
| 72 | + meta = json.load(fh) |
| 73 | + except (OSError, json.JSONDecodeError) as exc: |
| 74 | + problems.append(f"{name}: cannot read vendor-meta.json: {exc}") |
| 75 | + return 0 |
| 76 | + |
| 77 | + for key in REQUIRED_META_KEYS: |
| 78 | + if key not in meta: |
| 79 | + problems.append(f"{name}: vendor-meta.json is missing required key '{key}'") |
| 80 | + |
| 81 | + declared: set[str] = set() |
| 82 | + checked = 0 |
| 83 | + for entry in meta.get("files", []): |
| 84 | + filename = entry.get("file") |
| 85 | + if not filename: |
| 86 | + problems.append(f"{name}: a files[] entry has no 'file' key") |
| 87 | + continue |
| 88 | + declared.add(filename) |
| 89 | + path = os.path.join(capture_dir, filename) |
| 90 | + if not os.path.isfile(path): |
| 91 | + problems.append(f"{name}/{filename}: declared in vendor-meta.json but MISSING on disk") |
| 92 | + continue |
| 93 | + |
| 94 | + actual_sha, actual_bytes = _sha256(path) |
| 95 | + checked += 1 |
| 96 | + recorded_sha = entry.get("sha256") |
| 97 | + if recorded_sha and recorded_sha != actual_sha: |
| 98 | + problems.append( |
| 99 | + f"{name}/{filename}: sha256 MISMATCH — vendor-meta records {recorded_sha[:16]}…, " |
| 100 | + f"file is {actual_sha[:16]}…. The provenance record describes bytes that are not there." |
| 101 | + ) |
| 102 | + elif not recorded_sha: |
| 103 | + problems.append(f"{name}/{filename}: no sha256 recorded — the capture has no verifiable provenance") |
| 104 | + recorded_bytes = entry.get("bytes") |
| 105 | + if isinstance(recorded_bytes, int) and recorded_bytes != actual_bytes: |
| 106 | + problems.append( |
| 107 | + f"{name}/{filename}: bytes MISMATCH — vendor-meta records {recorded_bytes}, file is {actual_bytes}" |
| 108 | + ) |
| 109 | + |
| 110 | + # An UNDECLARED file in a capture dir is an input with no provenance at all. |
| 111 | + # The extractors select inputs by vendor-meta `role`, so such a file is inert |
| 112 | + # today — and one vendor-meta edit away from being parsed as authority. |
| 113 | + on_disk = { |
| 114 | + f |
| 115 | + for f in os.listdir(capture_dir) |
| 116 | + if os.path.isfile(os.path.join(capture_dir, f)) and f not in NOT_CAPTURE_INPUTS |
| 117 | + } |
| 118 | + for orphan in sorted(on_disk - declared): |
| 119 | + problems.append( |
| 120 | + f"{name}/{orphan}: present in the capture directory but NOT declared in vendor-meta.json — " |
| 121 | + "an input with no provenance record" |
| 122 | + ) |
| 123 | + return checked |
| 124 | + |
| 125 | + |
| 126 | +def main() -> int: |
| 127 | + parser = argparse.ArgumentParser(description="Verify vendored capture provenance against the bytes on disk.") |
| 128 | + parser.add_argument("--vendor-root", default=DEFAULT_VENDOR_ROOT, help="root holding <contract>/ capture dirs") |
| 129 | + args = parser.parse_args() |
| 130 | + |
| 131 | + if not os.path.isdir(args.vendor_root): |
| 132 | + print(f"ERROR: vendor root not found: {args.vendor_root}", file=sys.stderr) |
| 133 | + return 2 |
| 134 | + |
| 135 | + captures = sorted( |
| 136 | + os.path.join(args.vendor_root, d) |
| 137 | + for d in os.listdir(args.vendor_root) |
| 138 | + if os.path.isfile(os.path.join(args.vendor_root, d, "vendor-meta.json")) |
| 139 | + ) |
| 140 | + if not captures: |
| 141 | + # An empty sweep reporting success is the same failure class this repo |
| 142 | + # keeps finding: a gate that checked nothing must not look like a pass. |
| 143 | + print(f"ERROR: no captures found under {args.vendor_root} — this gate verified NOTHING.", file=sys.stderr) |
| 144 | + return 2 |
| 145 | + |
| 146 | + problems: list[str] = [] |
| 147 | + total = sum(check_capture(c, problems) for c in captures) |
| 148 | + |
| 149 | + if problems: |
| 150 | + print(f"vendor-meta integrity: {len(problems)} PROBLEM(S):") |
| 151 | + for problem in problems: |
| 152 | + print(f" - {problem}") |
| 153 | + print( |
| 154 | + "\nFix: re-derive the record from the bytes, never the other way round. " |
| 155 | + "After replacing a vendored file, recompute sha256 + bytes and re-run the extractor's --write." |
| 156 | + ) |
| 157 | + return 1 |
| 158 | + |
| 159 | + print( |
| 160 | + f"vendor-meta integrity: OK — {len(captures)} capture(s), {total} vendored file(s); " |
| 161 | + "every sha256 and byte count matches the bytes on disk, and every file is declared." |
| 162 | + ) |
| 163 | + return 0 |
| 164 | + |
| 165 | + |
| 166 | +if __name__ == "__main__": |
| 167 | + sys.exit(main()) |
0 commit comments