Files
mtusi2026lab4/app/main.py
T

324 lines
14 KiB
Python

import base64
import hashlib
import io
import json
import os
import re
import secrets
import socket
import uuid
import warnings
from datetime import datetime, timedelta, timezone
import boto3
import psycopg
from psycopg.types.json import Jsonb
from argon2 import PasswordHasher
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse, Response
from jsonschema import Draft202012Validator, FormatChecker
from PIL import Image
from starlette.concurrency import run_in_threadpool
app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None)
INSTANCE = socket.gethostname()
LAB = int(os.getenv('LAB_NUMBER', '4'))
DSN = os.environ['DATABASE_URL']
BUCKET = os.getenv('S3_BUCKET', 'attachments')
ph = PasswordHasher()
schemas = json.load(open('contracts/openapi.json'))['components']['schemas']
Image.MAX_IMAGE_PIXELS = 40_000_000
warnings.simplefilter('error', Image.DecompressionBombWarning)
def s3():
return boto3.client('s3', endpoint_url=os.environ['S3_ENDPOINT'],
aws_access_key_id=os.environ['S3_ACCESS_KEY'],
aws_secret_access_key=os.environ['S3_SECRET_KEY'],
region_name='us-east-1',
config=boto3.session.Config(connect_timeout=2, read_timeout=5, retries={'max_attempts': 0}))
def now():
return datetime.now(timezone.utc).isoformat().replace('+00:00', 'Z')
def uid(value):
try:
return str(uuid.UUID(value))
except (ValueError, TypeError, AttributeError):
fail(400)
def fail(status):
raise APIError(status)
class APIError(Exception):
def __init__(self, status):
self.status = status
CODES = {400: 'invalid_request', 401: 'unauthorized', 404: 'not_found',
409: 'conflict', 413: 'payload_too_large', 415: 'unsupported_media_type',
503: 'unavailable', 500: 'internal_error'}
def error(status, request_id, path):
code = CODES[status]
headers = {}
if status == 401:
headers['WWW-Authenticate'] = ('Basic realm="messenger", charset="UTF-8"'
if path.endswith('/auth/sessions') else 'Bearer realm="messenger"')
return JSONResponse({'error': {'code': code, 'message': code, 'request_id': request_id}},
status_code=status, headers=headers)
@app.middleware('http')
async def headers_and_errors(request, call_next):
request_id = secrets.token_hex(16)
try:
response = await call_next(request)
except APIError as exc:
response = error(exc.status, request_id, request.url.path)
except (psycopg.OperationalError, psycopg.InterfaceError):
response = error(503, request_id, request.url.path)
except Exception:
import logging
logging.exception('Request failed')
response = error(500, request_id, request.url.path)
response.headers['X-Request-Id'] = request_id
response.headers['X-Instance-Id'] = INSTANCE
return response
def validate(body, name):
if not Draft202012Validator(schemas[name], format_checker=FormatChecker()).is_valid(body):
fail(400)
return body
def user(db, request):
auth = request.headers.get('authorization', '')
if not auth.startswith('Bearer '):
fail(401)
token = hashlib.sha256(auth[7:].encode()).hexdigest()
row = db.execute('SELECT u.doc FROM sessions s JOIN users u ON u.id=s.user_id WHERE s.token=%s AND s.expires_at>now()', (token,)).fetchone()
if not row:
fail(401)
return row[0]
def chat(db, ident, actor):
ident = uid(ident)
row = db.execute('SELECT doc FROM chats WHERE id=%s', (ident,)).fetchone()
if not row or actor['id'] not in row[0]['member_ids']:
fail(404)
return row[0]
def attachment(db, ident, actor):
row = db.execute('SELECT doc FROM attachments WHERE id=%s', (uid(ident),)).fetchone()
if not row:
fail(404)
chat(db, row[0]['chat_id'], actor)
return row[0]
def page(db, request, table, scope, actor):
raw = request.query_params.get('limit', '50')
if not re.fullmatch('[0-9]+', raw) or not 1 <= int(raw) <= 100:
fail(400)
limit = int(raw)
cursor = request.query_params.get('cursor')
after = None
if cursor:
try:
decoded = json.loads(base64.urlsafe_b64decode(cursor.encode()))
if len(decoded) != 3 or decoded[0] != scope:
fail(400)
datetime.fromisoformat(decoded[1].replace('Z', '+00:00'))
uid(decoded[2])
after = decoded[1:]
except Exception:
fail(400)
if table == 'chats':
where, args = "doc->'member_ids' ? %s", [actor['id']]
else:
where, args = "chat_id=%s", [scope]
if after:
where += " AND (doc->>'created_at',id)>(%s,%s::uuid)"
args += after
rows = db.execute(f"SELECT doc FROM {table} WHERE {where} ORDER BY doc->>'created_at',id LIMIT %s", args + [limit+1]).fetchall()
items = [r[0] for r in rows[:limit]]
next_cursor = None
if len(rows) > limit:
last = items[-1]
next_cursor = base64.urlsafe_b64encode(json.dumps([scope,last['created_at'],last['id']]).encode()).decode()
return {'items': items, 'next_cursor': next_cursor}
@app.get('/health/live')
def live():
return {'status': 'ok'}
@app.get('/health/ready')
def ready():
with psycopg.connect(DSN, connect_timeout=2) as db:
db.execute('SELECT count(*) FROM sessions')
if LAB >= 4:
try:
s3().head_bucket(Bucket=BUCKET)
except Exception:
fail(503)
return {'status': 'ok'}
@app.api_route('/api/v1/{path:path}', methods=['GET', 'POST', 'DELETE'])
async def api(request: Request, path: str):
# Read bounded chunks before JSON parsing/image decoding.
limit = 10*1024*1024 if path.endswith('/attachments') else 65536
data = bytearray()
async for chunk in request.stream():
if len(data)+len(chunk) > limit:
fail(413)
data.extend(chunk)
return await run_in_threadpool(handle, request, path, bytes(data))
def handle(request, path, raw):
method = request.method
parts = path.split('/')
body = None
if method == 'POST' and path != 'auth/sessions' and not path.endswith('/attachments'):
try:
body = json.loads(raw)
except (ValueError, UnicodeError):
fail(400)
with psycopg.connect(DSN) as db:
if path == 'users' and method == 'POST':
validate(body, 'CreateUser')
if not body['display_name'].strip():
fail(400)
doc = {'id': str(uuid.uuid4()), 'username': body['username'], 'display_name': body['display_name'], 'created_at': now()}
hashed = ph.hash(body['password'])
if not db.execute('INSERT INTO users(id,username,password,doc) VALUES (%s,%s,%s,%s) ON CONFLICT(username) DO NOTHING RETURNING id', (doc['id'],doc['username'],hashed,Jsonb(doc))).fetchone():
fail(409)
return JSONResponse(doc, 201)
if path == 'auth/sessions' and method == 'POST':
try:
auth = request.headers.get('authorization', '')
if not auth.startswith('Basic ') or raw:
fail(401)
username, password = base64.b64decode(auth[6:], validate=True).decode().split(':',1)
row = db.execute('SELECT password,doc FROM users WHERE username=%s',(username,)).fetchone()
if not row:
fail(401)
ph.verify(row[0],password)
except Exception:
fail(401)
token = secrets.token_urlsafe(32)
expiry = datetime.now(timezone.utc)+timedelta(seconds=int(os.getenv('SESSION_TTL_SECONDS','3600')))
db.execute('INSERT INTO sessions VALUES(%s,%s,%s)', (hashlib.sha256(token.encode()).hexdigest(),row[1]['id'],expiry))
return JSONResponse({'token':token,'token_type':'Bearer','expires_at':expiry.isoformat().replace('+00:00','Z'),'user':row[1]},201)
actor = user(db, request)
if path == 'users/me' and method == 'GET':
return actor
if path == 'auth/sessions/current' and method == 'DELETE':
db.execute('DELETE FROM sessions WHERE token=%s',(hashlib.sha256(request.headers['authorization'][7:].encode()).hexdigest(),))
return Response(status_code=204)
if path == 'chats':
if method == 'GET':
return page(db,request,'chats','chats:'+actor['id'],actor)
if method == 'POST':
validate(body,'CreateChat')
if not body['title'].strip() or actor['id'] not in body['member_ids']:
fail(400)
for ident in body['member_ids']:
if not db.execute('SELECT id FROM users WHERE id=%s',(uid(ident),)).fetchone():
fail(400)
doc = dict(body,id=str(uuid.uuid4()),created_at=now())
db.execute('INSERT INTO chats VALUES(%s,%s)',(doc['id'],Jsonb(doc)))
return JSONResponse(doc,201)
if parts[0] == 'chats' and len(parts) >= 2:
current = chat(db, parts[1], actor)
if len(parts) == 2 and method == 'GET':
return current
if len(parts) == 3 and parts[2] == 'messages':
if method == 'GET':
return page(db,request,'messages',current['id'],actor)
if method == 'POST':
validate(body,'CreateMessage')
attachments = body.get('attachment_ids',[]) if LAB >= 4 else []
if not body['text'].strip() and not attachments:
fail(400)
key = request.headers.get('idempotency-key','')
if not re.fullmatch(r'[A-Za-z0-9._:-]{1,128}',key):
fail(400)
for ident in attachments:
att = attachment(db,ident,actor)
if att['chat_id'] != current['id']:
fail(404)
normalized = {'text':body['text'],'attachment_ids':attachments}
# A transaction-scoped shared lock serializes the entire lookup+insert.
scope = actor['id']+':'+current['id']+':'+key
db.execute('SELECT pg_advisory_xact_lock(hashtextextended(%s,0))',(scope,))
old = db.execute('SELECT body,doc FROM messages WHERE sender_id=%s AND chat_id=%s AND idem_key=%s AND saved_at>now()-interval \'24 hours\'',(actor['id'],current['id'],key)).fetchone()
if old:
if old[0] != normalized:
fail(409)
return JSONResponse(old[1],201)
doc = {'id':str(uuid.uuid4()),'chat_id':current['id'],'sender_id':actor['id'],'text':body['text'],'created_at':now()}
if LAB >= 4:
doc['attachment_ids'] = attachments
db.execute('INSERT INTO messages(id,chat_id,sender_id,idem_key,body,doc) VALUES(%s,%s,%s,%s,%s,%s)',(doc['id'],current['id'],actor['id'],key,Jsonb(normalized),Jsonb(doc)))
return JSONResponse(doc,201)
if LAB >= 4 and len(parts) == 3 and parts[2] == 'attachments' and method == 'POST':
filename = request.query_params.get('filename','')
if not 1 <= len(filename) <= 255 or any(c in '/\\' or ord(c)<32 or 127<=ord(c)<=159 for c in filename):
fail(400)
mime = request.headers.get('content-type','')
if mime not in ('image/png','image/jpeg'):
fail(415)
try:
with Image.open(io.BytesIO(raw)) as image:
if image.width*image.height > 40_000_000:
fail(413)
if image.format not in ('PNG','JPEG') or Image.MIME[image.format] != mime:
fail(415)
image.verify()
with Image.open(io.BytesIO(raw)) as image:
image.load()
except APIError:
raise
except (Image.DecompressionBombError, Image.DecompressionBombWarning):
fail(413)
except Exception:
fail(415)
ident, stamp = str(uuid.uuid4()), now()
doc = {'id':ident,'chat_id':current['id'],'owner_id':actor['id'],'filename':filename,'content_type':mime,'size_bytes':len(raw),'sha256':hashlib.sha256(raw).hexdigest(),'status':'ready','variants':[],'error_code':None,'created_at':stamp,'updated_at':stamp}
try:
s3().put_object(Bucket=BUCKET,Key=ident,Body=raw,ContentType=mime)
except Exception:
fail(503)
db.execute('INSERT INTO attachments VALUES(%s,%s,%s)',(ident,current['id'],Jsonb(doc)))
return JSONResponse(doc,201)
if LAB >= 4 and parts[0] == 'attachments' and len(parts) in (2,3) and method == 'GET':
doc = attachment(db,parts[1],actor)
if len(parts) == 2:
return doc
if parts[2] == 'content':
if request.query_params.get('variant','original') != 'original':
fail(404)
try:
obj = s3().get_object(Bucket=BUCKET,Key=doc['id'])
try:
content = obj['Body'].read(10*1024*1024+1)
finally:
obj['Body'].close()
except Exception:
fail(503)
return Response(content,media_type=doc['content_type'])
fail(404)