text
stringlengths
0
252
" 'connected': False,\n"
" 'error': type(error).__name__,\n"
" 'errno': error.errno,\n"
" }\n"
" else:\n"
" egress_results[key] = {\n"
" 'connected': True,\n"
" 'error': None,\n"
" 'errno': None,\n"
" }\n"
" finally:\n"
" connection.close()\n"
"result = {\n"
" 'euid': os.geteuid(),\n"
" 'paths': path_results,\n"
" 'egress': egress_results,\n"
"}\n"
"print(json.dumps(result, sort_keys=True))\n"
"violation = any(item['readable'] for item in path_results.values())\n"
"violation = violation or any(item['connected'] for item in egress_results.values())\n"
"raise SystemExit(91 if violation else 0)\n",
encoding="utf-8",
)
try:
run = run_isolated_python(
probe_source,
{
"request": json.dumps(
{
"paths": expected_paths,
"egress": EGRESS_PROBE_TARGETS,
}
).encode(),
},
["{input:request}"],
timeout_seconds=15,
)
try:
evidence = {
"status": "failed",
"runtime_user": identity.name,
"uid": identity.uid,
"returncode": run.completed.returncode,
"stdout": run.completed.stdout,
"stderr": run.completed.stderr,
}
failure: str | None = None
try:
results = json.loads(run.completed.stdout)
except json.JSONDecodeError as error:
results = None
failure = f"invalid JSON stdout: {error}"
else:
if isinstance(results, dict):
evidence["execution_euid"] = results.get("euid")
evidence["paths"] = results.get("paths")
evidence["egress"] = results.get("egress")
if run.completed.returncode != 0:
failure = f"probe subprocess returned {run.completed.returncode}"
elif not isinstance(results, dict) or set(results) != {"euid", "paths", "egress"}:
failure = "probe JSON must contain only euid, paths, and egress"
elif results["euid"] != identity.uid:
failure = f"probe ran as UID {results['euid']}, expected runner UID {identity.uid}"
elif not isinstance(results["paths"], dict) or set(results["paths"]) != set(expected_paths):
failure = "probe JSON path set did not match the request"
elif not isinstance(results["egress"], dict) or set(results["egress"]) != set(expected_egress):
failure = "probe JSON egress set did not match the request"
else:
for path in expected_paths:
item = results["paths"][path]
if (
not isinstance(item, dict)
or set(item) != {"readable", "error"}
or item["readable"] is not False
or item["error"] != "PermissionError"
):
failure = f"{path} was not denied with PermissionError"
break
if failure is None:
for endpoint in expected_egress:
item = results["egress"][endpoint]
if (
not isinstance(item, dict)
or set(item) != {"connected", "error", "errno"}
or item["connected"] is not False
or not isinstance(item["error"], str)
or not item["error"]
):
failure = f"{endpoint} was not blocked for UID {identity.uid}"
break
if failure is None:
evidence["status"] = "denied"
else:
evidence["failure"] = failure
evidence_path.write_text(
json.dumps(evidence, indent=2, sort_keys=True) + "\n",
encoding="utf-8",
)
> assert failure is None, (
^^^^^^^^^^^^^^^