feat(lab4): implement S3 attachments and image validation

This commit is contained in:
Otto
2026-11-03 00:59:46 +03:00
parent ec540a1a72
commit fc04eb4b7c
37 changed files with 1636 additions and 40 deletions
+29
View File
@@ -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()