Files
mtusi2026lab4/src/private_check.py
T

23 lines
942 B
Python

"""Prove S3 is not public and API-scoped credentials cannot list other buckets."""
import urllib.error
import urllib.request
from botocore.exceptions import ClientError
from .storage import s3, bucket
import os
import uuid
url = os.getenv("S3_ENDPOINT", "http://s3:8333").rstrip("/") + "/" + bucket + "/random-inaccessible-key"
try:
urllib.request.urlopen(url, timeout=4)
except urllib.error.HTTPError as exc:
assert exc.code in (401, 403), f"expected S3 denied, got {exc.code}"
else:
raise AssertionError("UNSAFE: direct unsigned S3 request succeeded")
try:
s3.create_bucket(Bucket="forbidden-probe-" + uuid.uuid4().hex[:16])
except ClientError as exc:
assert exc.response["Error"]["Code"] in ("AccessDenied", "403"), "unexpected S3 result"
else:
raise AssertionError("UNSAFE: API credentials have global create-bucket privileges")
print("PASS: S3 requires authentication; API credentials restricted to own bucket")