#!/usr/bin/env python3 """Track 1: presigned upload, negative cases, API traffic and RAM at two sizes. The signed URL is never written to the evidence file. """ import base64 import hashlib import json import os import struct import subprocess import threading import time import urllib.error import urllib.request import uuid import zlib from pathlib import Path ROOT = Path(__file__).resolve().parents[1] OUT = ROOT / "evidence" / "track1.json" BASE = "http://127.0.0.1:8080" def expect(status, got, label): if got == 500: raise SystemExit("FAIL %s returned 500" % label) if got != status: raise SystemExit("FAIL %s expected %s got %s" % (label, status, got)) def wait_ready(api, attachment_id, token): deadline = time.monotonic() + 60 while time.monotonic() < deadline: status, body, _ = api.call("GET", "/api/v1/attachments/" + attachment_id, token=token) expect(200, status, "poll attachment") if body["status"] == "failed": raise SystemExit("FAIL attachment failed") if body["status"] == "ready": return body time.sleep(0.3) raise SystemExit("FAIL attachment did not become ready") print("PASS", label, got) class Api: def __init__(self) -> None: self.opener = urllib.request.build_opener(urllib.request.ProxyHandler({})) def call(self, method, path, body=None, token=None, raw=None, content_type=None): headers = {} data = raw if body is not None: data = json.dumps(body).encode("utf-8") headers["Content-Type"] = "application/json" if content_type: headers["Content-Type"] = content_type if token: headers["Authorization"] = "Bearer " + token request = urllib.request.Request(BASE + path, data=data, headers=headers, method=method) try: response = self.opener.open(request, timeout=60) except urllib.error.HTTPError as error: response = error with response: payload = response.read() parsed = None if payload and response.headers.get_content_type() == "application/json": parsed = json.loads(payload) return response.status, parsed, payload def png(width: int, height: int) -> bytes: signature = b"\x89PNG\r\n\x1a\n" def chunk(kind: bytes, data: bytes) -> bytes: return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data) & 0xFFFFFFFF) ihdr = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0) raw = b"".join(b"\x00" + os.urandom(width * 3) for _ in range(height)) return signature + chunk(b"IHDR", ihdr) + chunk(b"IDAT", zlib.compress(raw, 1)) + chunk(b"IEND", b"") def put_signed(url: str, data: bytes, content_type: str) -> int: request = urllib.request.Request(url, data=data, method="PUT", headers={"Content-Type": content_type}) try: response = urllib.request.urlopen(request, timeout=60) except urllib.error.HTTPError as error: response = error with response: response.read() return response.status def api_rx() -> int: ids = subprocess.check_output( ["docker", "compose", "--env-file", ".env", "ps", "-q", "api"], cwd=ROOT, text=True, ).split() total = 0 for container_id in ids: raw = subprocess.check_output(["docker", "exec", container_id, "cat", "/proc/net/dev"], text=True) for line in raw.splitlines(): if "eth0" not in line: continue total += int(line.split()[1]) return total def peak_during(action): peaks = [] stop = threading.Event() def watch(): while not stop.is_set(): stats = subprocess.run( ["docker", "stats", "--no-stream", "--format", "{{.Name}} {{.MemUsage}}"], capture_output=True, text=True, check=False, ) for line in stats.stdout.splitlines(): if "mtusi2026lab6-api-" not in line: continue value = line.split()[1] number = float(value[:-3]) if not value.lower().endswith("mib"): number = number / 1024 peaks.append(number) time.sleep(0.25) thread = threading.Thread(target=watch) thread.start() before = api_rx() result = action() after = api_rx() time.sleep(0.3) stop.set() thread.join() return {"rx_bytes": after - before, "peak_api_mib": round(max(peaks) if peaks else 0, 1), "result": result} def compose_psql(sql: str) -> None: subprocess.run( ["docker", "compose", "--env-file", ".env", "exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", "-c", sql], cwd=ROOT, check=True, capture_output=True, text=True, ) def negatives(api, alice_token, bob_token, eve_token, chat_id): small = png(16, 16) status, _, _ = api.call( "POST", "/api/v1/chats/%s/uploads" % chat_id, {"filename": "a/b.png", "content_type": "image/png", "size_bytes": len(small)}, token=alice_token, ) expect(400, status, "direct filename") status, _, _ = api.call( "POST", "/api/v1/chats/%s/uploads" % chat_id, {"filename": "big.png", "content_type": "image/png", "size_bytes": 10485761}, token=alice_token, ) expect(413, status, "direct declared size") status, _, _ = api.call( "POST", "/api/v1/chats/%s/uploads" % chat_id, {"filename": "x.png", "content_type": "image/png", "size_bytes": len(small)}, token=eve_token, ) expect(404, status, "outsider presign") status, started, _ = api.call( "POST", "/api/v1/chats/%s/uploads" % chat_id, {"filename": "wait.png", "content_type": "image/png", "size_bytes": len(small)}, token=alice_token, ) expect(201, status, "presign for incomplete") status, _, _ = api.call( "POST", "/api/v1/uploads/%s/complete" % started["upload_id"], {"sha256": hashlib.sha256(small).hexdigest()}, token=alice_token, ) expect(409, status, "complete before PUT") status, _, _ = api.call( "POST", "/api/v1/uploads/%s/complete" % started["upload_id"], {"sha256": hashlib.sha256(small).hexdigest()}, token=bob_token, ) expect(404, status, "complete by another member") tampered = put_signed(started["url"] + "x", small, "image/png") if tampered == 200: raise SystemExit("FAIL tampered signature was accepted") print("PASS tampered signature", tampered) status, started, _ = api.call( "POST", "/api/v1/chats/%s/uploads" % chat_id, {"filename": "badsum.png", "content_type": "image/png", "size_bytes": len(small)}, token=alice_token, ) expect(201, status, "presign for bad checksum") if put_signed(started["url"], small, "image/png") != 200: raise SystemExit("FAIL signed PUT") status, _, _ = api.call( "POST", "/api/v1/uploads/%s/complete" % started["upload_id"], {"sha256": "0" * 64}, token=alice_token, ) expect(400, status, "wrong checksum") status, _, _ = api.call( "POST", "/api/v1/uploads/%s/complete" % started["upload_id"], {"sha256": hashlib.sha256(small).hexdigest()}, token=alice_token, ) expect(409, status, "complete after rejected checksum") status, started, _ = api.call( "POST", "/api/v1/chats/%s/uploads" % chat_id, {"filename": "late.png", "content_type": "image/png", "size_bytes": len(small)}, token=alice_token, ) expect(201, status, "presign for expiry") compose_psql( "UPDATE direct_uploads SET expires_at = now() - interval '5 minutes' WHERE id = '%s'" % started["upload_id"] ) status, _, _ = api.call( "POST", "/api/v1/uploads/%s/complete" % started["upload_id"], {"sha256": hashlib.sha256(small).hexdigest()}, token=alice_token, ) expect(409, status, "expired upload") digest = hashlib.sha256(small).hexdigest() status, started, _ = api.call( "POST", "/api/v1/chats/%s/uploads" % chat_id, {"filename": "ok.png", "content_type": "image/png", "size_bytes": len(small)}, token=alice_token, ) expect(201, status, "presign happy path") if "originals/" in json.dumps({k: v for k, v in started.items() if k != "url"}): raise SystemExit("FAIL object key leaked outside the signed URL") if put_signed(started["url"], small, "image/png") != 200: raise SystemExit("FAIL happy PUT") status, attachment, _ = api.call( "POST", "/api/v1/uploads/%s/complete" % started["upload_id"], {"sha256": digest}, token=alice_token, ) expect(202, status, "complete happy path") if attachment["sha256"] != digest or attachment["status"] != "queued": raise SystemExit("FAIL complete metadata") attachment = wait_ready(api, attachment["id"], bob_token) status, _, downloaded = api.call("GET", "/api/v1/attachments/%s/content" % attachment["id"], token=bob_token) expect(200, status, "member downloads direct upload") if downloaded != small: raise SystemExit("FAIL direct bytes") status, _, _ = api.call( "POST", "/api/v1/uploads/%s/complete" % started["upload_id"], {"sha256": digest}, token=alice_token, ) expect(409, status, "second complete") status, proxy, _ = api.call( "POST", "/api/v1/chats/%s/attachments?filename=proxy.png" % chat_id, token=alice_token, raw=small, content_type="image/png", ) expect(202, status, "proxy endpoint still works") return proxy["id"] def compare(api, token, chat_id): rows = [] for label, image in (("small", png(200, 150)), ("large", png(900, 700))): digest = hashlib.sha256(image).hexdigest() proxy = peak_during( lambda image=image: api.call( "POST", "/api/v1/chats/%s/attachments?filename=%s.png" % (chat_id, label), token=token, raw=image, content_type="image/png", ) ) if proxy["result"][0] != 202: raise SystemExit("FAIL proxy %s %s" % (label, proxy["result"][0])) status, started, _ = api.call( "POST", "/api/v1/chats/%s/uploads" % chat_id, {"filename": label + "-direct.png", "content_type": "image/png", "size_bytes": len(image)}, token=token, ) expect(201, status, "presign " + label) direct_put = peak_during(lambda image=image, url=started["url"]: put_signed(url, image, "image/png")) if direct_put["result"] != 200: raise SystemExit("FAIL direct PUT %s %s" % (label, direct_put["result"])) direct_complete = peak_during( lambda digest=digest, upload_id=started["upload_id"]: api.call( "POST", "/api/v1/uploads/%s/complete" % upload_id, {"sha256": digest}, token=token, ) ) if direct_complete["result"][0] != 202: raise SystemExit("FAIL complete %s %s" % (label, direct_complete["result"][0])) rows.append( { "label": label, "bytes": len(image), "proxy_rx_bytes": proxy["rx_bytes"], "proxy_peak_api_mib": proxy["peak_api_mib"], "direct_put_rx_bytes": direct_put["rx_bytes"], "direct_put_peak_api_mib": direct_put["peak_api_mib"], "complete_rx_bytes": direct_complete["rx_bytes"], "complete_peak_api_mib": direct_complete["peak_api_mib"], } ) print("MEASURED", rows[-1]) return rows def main(): api = Api() def register(label): username = "t1_" + label + "_" + uuid.uuid4().hex[:10] password = "T3st-" + uuid.uuid4().hex status, body, _ = api.call( "POST", "/api/v1/users", {"username": username, "display_name": "Track " + label, "password": password}, ) expect(201, status, "register " + label) return username, password, body def login(username, password): basic = base64.b64encode((username + ":" + password).encode()).decode() request = urllib.request.Request( BASE + "/api/v1/auth/sessions", method="POST", headers={"Authorization": "Basic " + basic}, ) with urllib.request.urlopen(request, timeout=30) as response: return json.loads(response.read())["token"] alice_name, alice_password, alice = register("alice") bob_name, bob_password, bob = register("bob") eve_name, eve_password, eve = register("eve") alice_token = login(alice_name, alice_password) bob_token = login(bob_name, bob_password) eve_token = login(eve_name, eve_password) status, chat, _ = api.call( "POST", "/api/v1/chats", {"title": "track1", "member_ids": [alice["id"], bob["id"]]}, token=alice_token, ) expect(201, status, "chat") negatives(api, alice_token, bob_token, eve_token, chat["id"]) rows = compare(api, alice_token, chat["id"]) summary = { "hypothesis": "A presigned PUT does not enter the API, so API RX stays near zero at both sizes. complete reads the object once to check the checksum, and the proxy endpoint still receives the whole body.", "presign_ttl_seconds": 60, "sizes": rows, } OUT.parent.mkdir(exist_ok=True) OUT.write_text(json.dumps(summary, indent=2) + "\n", encoding="utf-8") print(json.dumps(summary, indent=2)) if __name__ == "__main__": main()