forked from mtusi2026/mtusi2026lab4
feat(lab4): implement S3 attachments and image validation
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
"""Run inside API container. Direct unsigned S3 download must be denied."""
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from src.db import pool
|
||||
from src.storage import bucket
|
||||
|
||||
|
||||
def main():
|
||||
pool.open(wait=True, timeout=10)
|
||||
try:
|
||||
with pool.connection() as conn:
|
||||
row = conn.execute("SELECT object_key FROM attachments ORDER BY created_at DESC LIMIT 1").fetchone()
|
||||
if row is None:
|
||||
raise SystemExit("Upload a test image first")
|
||||
url = f"http://s3:8333/{bucket}/{row['object_key']}"
|
||||
req = urllib.request.Request(url)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=3) as response:
|
||||
raise SystemExit(f"FAIL: anonymous S3 GET returned {response.status}")
|
||||
except urllib.error.HTTPError as exc:
|
||||
if exc.code != 403:
|
||||
raise SystemExit(f"FAIL: expected 403, got {exc.code}")
|
||||
print("PASS: anonymous S3 GET rejected with 403 (private bucket)")
|
||||
finally:
|
||||
pool.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user