#!/usr/bin/env python3 """Standalone verifier for the CAIN-42 security-boundary evidence bundle. Imports nothing from CAIN; stdlib only. python3 verify_security_boundary.py SECURITY_BOUNDARY_PROBES.json check the recorded run python3 verify_security_boundary.py SECURITY_BOUNDARY_PROBES.json --live re-run every probe against the live sites What it checks (recorded mode): * the probe list is internally consistent: every probe's verdict is re-derived here from its recorded HTTP status and its expectation (the recorder's verdict is NOT trusted), and the summary counts are recomputed; * a FAIL or INCONCLUSIVE anywhere makes the result NOT VERIFIED -- inconclusive never becomes pass; * the three sites were each probed with the same set of routes. Live mode sends the same requests again. Every probe is either refused by a gate (so it changes nothing) or a pure verifier / read, so re-running is safe. Exit 0 = VERIFIED, 1 = NOT VERIFIED. """ from __future__ import annotations import json import sys import urllib.error import urllib.request def expected_ok(expect: str, status: int) -> bool: if expect == "refused": # a gate must stop an anonymous caller return status in (401, 403) if expect == "rejected-input": # the handler must refuse the malicious input itself return status == 400 if expect == "not-served": # this site does not route the path to the gateway at all (static server: 404/405) return status in (404, 405) if expect == "served": # public read / demo tenant must still work return status == 200 if expect == "not-gated": # a pure verifier must stay public (any answer but a gate refusal) return status not in (401, 403, 0) raise ValueError(expect) def derive(p: dict) -> str: if not isinstance(p.get("status"), int) or p["status"] == 0: return "INCONCLUSIVE" return "PASS" if expected_ok(p["expect"], p["status"]) else "FAIL" def send(p: dict) -> int: data = json.dumps(p["body"]).encode() if p.get("body") is not None else None req = urllib.request.Request(p["url"], data=data, method=p["method"], headers={"content-type": "application/json", "User-Agent": "cain42-security-boundary-verifier"}) try: with urllib.request.urlopen(req, timeout=30) as r: return r.status except urllib.error.HTTPError as e: return e.code except Exception: return 0 def main(argv) -> int: if not argv: print(__doc__); return 2 doc = json.load(open(argv[0])) live = "--live" in argv probes = doc["probes"] counts = {"PASS": 0, "FAIL": 0, "INCONCLUSIVE": 0} bad = [] for p in probes: if live: p = dict(p, status=send(p)) v = derive(p) counts[v] += 1 if not live and v != p.get("verdict"): bad.append(f"recorded verdict {p.get('verdict')} but status {p['status']} with expectation {p['expect']} means {v}: {p['url']}") if v != "PASS": bad.append(f"{v}: {p['method']} {p['url']} -> {p['status']} (expected {p['expect']})") if not live and doc.get("summary") != counts: bad.append(f"summary {doc.get('summary')} does not match the probes {counts}") per_host = {} for p in probes: host = p["url"].split("/")[2] per_host.setdefault(host, set()).add((p["method"], "/" + p["url"].split("/", 3)[3])) if len({frozenset(v) for v in per_host.values()}) > 1: bad.append("the sites were not probed with the same routes") print(f"{'live' if live else 'recorded'} probes: {len(probes)} across {len(per_host)} sites -> {counts}") for b in bad: print(" " + b) ok = not bad and counts["FAIL"] == 0 and counts["INCONCLUSIVE"] == 0 print("FINAL RESULT: " + ("VERIFIED" if ok else "NOT VERIFIED")) return 0 if ok else 1 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))