"""Bounded synthetic checks. Never print environment values or credentials.""" import errno import ipaddress import json import os import re import socket from pathlib import Path def tls_path(host, port=443): try: with socket.create_connection((host, port), timeout=2): return "reachable" except (OSError, TimeoutError): return "blocked_or_unavailable" def probe_network(endpoint, canary): # Operator-owned public IPv4 canary, verified reachable outside this VPC. address = ipaddress.ip_address(canary) if address.version != 4 or not address.is_global: raise ValueError("CANARY_IP must be a public IPv4 test endpoint you own") if not re.fullmatch(r"api\.ecr\.[a-z0-9-]+\.amazonaws\.com", endpoint): raise ValueError("Use the regional ECR API endpoint as positive control") result = {"positive_control": tls_path(endpoint), "public_canary": tls_path(canary), "imds_tcp_80": tls_path("169.254.169.254", 80)} # A broken network cannot establish the boundary: the positive control must work. passed = (result["positive_control"] == "reachable" and result["public_canary"] == "blocked_or_unavailable" and result["imds_tcp_80"] == "blocked_or_unavailable") return result, passed def main(): assert os.getuid() == 10001, "Unexpected user" with open("/proc/self/status", encoding="utf-8") as source: effective = next(line.split()[1] for line in source if line.startswith("CapEff:")) assert int(effective, 16) == 0, "Unexpected effective capabilities" try: # This file is owned by this UID; permission denial alone would be inconclusive. Path("/app/rootfs-check").write_text("must not succeed", encoding="utf-8") except OSError as error: assert error.errno == errno.EROFS, f"Expected read-only filesystem, got {error.errno}" else: raise AssertionError("Root filesystem is writable") scratch = Path("/scratch/proof.txt") scratch.write_text("synthetic fixture\n", encoding="utf-8") assert scratch.read_text(encoding="utf-8") == "synthetic fixture\n" scratch.unlink() result = {"uid": os.getuid(), "effective_capabilities": effective, "rootfs": "read_only", "scratch": "writable"} passed = True if os.getenv("ECR_ENDPOINT") and os.getenv("CANARY_IP"): result["network"], passed = probe_network(os.environ["ECR_ENDPOINT"], os.environ["CANARY_IP"]) print(json.dumps(result, sort_keys=True)) return 0 if passed else 1 if __name__ == "__main__": raise SystemExit(main())