"""DV-1 ยง7, the fence's proof: planted outbound connects that must FAIL, from inside the sandbox. Prints one JSON object. `fence_holds` is true only when every outbound probe failed, loopback is up, and the namespace's interface list (/proc/net/dev, read inside the new namespace) is `lo` alone. """ import json, socket, time def probe(addr): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(3) try: s.connect(addr) return {"addr": "%s:%d" % addr, "result": "CONNECTED"} except Exception as exc: # noqa: BLE001 -- the result return {"addr": "%s:%d" % addr, "result": "%s: %s" % (type(exc).__name__, exc)} finally: s.close() out = {"stamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())} out["outbound"] = [probe(("1.1.1.1", 443)), probe(("8.8.8.8", 53))] try: socket.getaddrinfo("huggingface.co", 443) out["dns_huggingface_co"] = "RESOLVED" except Exception as exc: # noqa: BLE001 out["dns_huggingface_co"] = "%s: %s" % (type(exc).__name__, exc) srv = socket.socket(); srv.bind(("127.0.0.1", 0)); srv.listen(1) port = srv.getsockname()[1] cli = socket.socket(); cli.settimeout(3) try: cli.connect(("127.0.0.1", port)); out["loopback"] = "connected" except Exception as exc: # noqa: BLE001 out["loopback"] = "%s: %s" % (type(exc).__name__, exc) cli.close(); srv.close() with open("/proc/net/dev") as fh: out["interfaces"] = sorted(l.split(":")[0].strip() for l in fh.readlines()[2:]) out["fence_holds"] = (all(p["result"] != "CONNECTED" for p in out["outbound"]) and out["dns_huggingface_co"] != "RESOLVED" and out["loopback"] == "connected" and out["interfaces"] == ["lo"]) print(json.dumps(out, sort_keys=True))