Files
mtusi2026lab7/research/compromise.py
T
NikiPshg d269a74fb2
Skeleton contracts / contracts (pull_request) Canceled after 0s
Document verified grade-five security study and deployment evidence
2026-11-16 13:21:04 +03:00

142 lines
5.0 KiB
Python

#!/usr/bin/env python3
"""Accepted jobs survive worker identity revoke + working-secret rotation."""
import hashlib
import json
import subprocess
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, check # noqa: E402
def docker(*args):
return subprocess.check_output(["docker", "compose", *args], cwd=ROOT, text=True).strip()
def images():
ids = docker("ps", "-q", "api", "worker").split()
return sorted(
set(
subprocess.check_output(
["docker", "inspect", "--format", "{{.Image}}", *ids], text=True
).split()
)
)
def main():
c = Client("https://localhost:8447", str(ROOT / "secrets/ca.pem"))
alice, bob, _, chat = c.bootstrap()
before = images()
docker("stop", "worker")
accepted = [c.attachment(alice, chat) for _ in range(3)]
start = time.monotonic()
rotate = subprocess.run(
[sys.executable, "scripts/rotate_identity.py"], cwd=ROOT, text=True, capture_output=True
)
check(rotate.returncode == 0, "operator rotation failed; inspect private operator execution")
revoked = subprocess.run(
[sys.executable, "scripts/security_checks.py", "revoked"],
cwd=ROOT,
text=True,
capture_output=True,
)
check(revoked.returncode == 0, "revoked material was not denied before original token expiry")
errors = 0
deadline = time.monotonic() + 90
pending = {a[0]["id"] for a in accepted}
while pending and time.monotonic() < deadline:
for att, _ in accepted:
if att["id"] not in pending:
continue
result = c.call(
"GET",
"/api/v1/attachments/" + att["id"],
expected=[200, 503],
actor=alice,
schema=None,
)
if result["status"] == 503:
errors += 1
else:
body = json.loads(result["bytes"])
check(body["status"] != "failed", "accepted task became failed")
if body["status"] == "ready":
pending.remove(att["id"])
time.sleep(0.2)
check(not pending, "accepted jobs did not recover")
rows = []
for att, original in accepted:
downloaded = c.call("GET", "/api/v1/attachments/" + att["id"] + "/content", actor=bob)[
"bytes"
]
thumbnail = c.call(
"GET", "/api/v1/attachments/" + att["id"] + "/content?variant=thumbnail", actor=bob
)["bytes"]
metadata = c.call(
"GET", "/api/v1/attachments/" + att["id"], actor=alice, schema="Attachment"
)["json"]
variants = []
for advertised in metadata["variants"]:
content = c.call(
"GET",
"/api/v1/attachments/" + att["id"] + "/content?variant=" + advertised["name"],
actor=bob,
)["bytes"]
actual = hashlib.sha256(content).hexdigest()
variants.append(
{
"name": advertised["name"],
"sha256": actual,
"advertised_sha256": advertised["sha256"],
"size_matches": len(content) == advertised["size_bytes"],
"passed": actual == advertised["sha256"]
and len(content) == advertised["size_bytes"],
}
)
sha = hashlib.sha256(original).hexdigest()
rows.append(
{
"attachment_id": att["id"],
"original_sha256": sha,
"download_sha256": hashlib.sha256(downloaded).hexdigest(),
"thumbnail_sha256": hashlib.sha256(thumbnail).hexdigest(),
"thumbnail_nonempty": bool(thumbnail),
"variants": variants,
"accepted_original_sha_matches": sha == att["sha256"],
}
)
after = images()
result = {
"accepted_jobs": 3,
"ready_jobs": len(rows),
"recovery_seconds": round(time.monotonic() - start, 3),
"observed_api_503": errors,
"images_before": before,
"images_after": after,
"image_unchanged": before == after,
"revoked_material_checks_passed": True,
"variants_verified": sum(len(row["variants"]) for row in rows),
"rows": rows,
"passed": before == after
and all(
r["original_sha256"] == r["download_sha256"]
and r["thumbnail_nonempty"]
and r["accepted_original_sha_matches"]
and len(r["variants"]) == 3
and all(v["passed"] for v in r["variants"])
for r in rows
),
}
(ROOT / "evidence/compromise-recovery.json").write_text(json.dumps(result, indent=2) + "\n")
check(result["passed"], "recovery invariant failed")
print(json.dumps({k: v for k, v in result.items() if k != "rows"}))
if __name__ == "__main__":
main()