forked from mtusi2026/mtusi2026lab5
41 lines
1.7 KiB
Python
41 lines
1.7 KiB
Python
"""Verify bucket is private at S3 boundary (not merely hidden behind HTTP API)."""
|
|
import json
|
|
import os
|
|
import boto3
|
|
from botocore import UNSIGNED
|
|
from botocore.config import Config
|
|
from botocore.exceptions import ClientError
|
|
from .attachments import BUCKET, s3_client
|
|
|
|
|
|
def main():
|
|
client = s3_client()
|
|
client.head_bucket(Bucket=BUCKET) # restricted app identity works
|
|
unauth = boto3.client("s3", endpoint_url=os.environ["S3_ENDPOINT"],
|
|
region_name="us-east-1", config=Config(signature_version=UNSIGNED,
|
|
s3={"addressing_style":"path"}))
|
|
try:
|
|
unauth.list_objects_v2(Bucket=BUCKET, MaxKeys=1)
|
|
except ClientError as exc:
|
|
status = exc.response.get("ResponseMetadata", {}).get("HTTPStatusCode")
|
|
if status not in (401,403):
|
|
raise RuntimeError("Unexpected anonymous S3 response") from None
|
|
else:
|
|
raise RuntimeError("Private bucket leaked object list to anonymous caller")
|
|
listing = client.list_objects_v2(Bucket=BUCKET, MaxKeys=1)
|
|
keys = [entry["Key"] for entry in listing.get("Contents", [])]
|
|
if keys:
|
|
try:
|
|
unauth.get_object(Bucket=BUCKET, Key=keys[0])
|
|
except ClientError as exc:
|
|
status = exc.response.get("ResponseMetadata", {}).get("HTTPStatusCode")
|
|
if status not in (401, 403):
|
|
raise RuntimeError("Unexpected anonymous object GET response") from None
|
|
else:
|
|
raise RuntimeError("Private S3 object downloadable anonymously")
|
|
print(json.dumps({"private_bucket": True, "anonymous_listing": "denied",
|
|
"anonymous_object_get": "denied" if keys else "not_tested_empty_bucket"}))
|
|
|
|
if __name__ == "__main__":
|
|
main()
|