"""Grade 4: image validation, attachment ACLs and cross-replica persistence.""" import hashlib import json import struct import subprocess import sys import time import uuid import zlib from pathlib import Path from urllib.parse import quote ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tests")) from client import Client, Failure, check, run # noqa: E402 def compose(*args, data=None): result = subprocess.run(["docker", "compose", "--env-file", ".env", *args], cwd=ROOT, input=data, capture_output=True, text=True, check=True) return result.stdout.strip() def psql(sql): return compose("exec", "-T", "postgres", "psql", "-X", "-q", "-A", "-t", "-v", "ON_ERROR_STOP=1", "-U", "messenger", "-d", "messenger", data=sql) def png_chunk(kind, data): return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data)) def bomb_header(width=10000, height=10000): # A valid PNG header must be rejected BEFORE decoding its huge bitmap. return b"\x89PNG\r\n\x1a\n" + png_chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)) def wide_png(): # Exactly 40M pixels, 16-bit RGBA, one very wide scanline. The compressed # original is small but the decoder needs both bitmap and scanline buffers. compressor = zlib.compressobj(1) parts = [compressor.compress(b"\x00")] remaining = 40000000 * 8 block = bytes(1024 * 1024) while remaining: n = min(len(block), remaining) parts.append(compressor.compress(block[:n])) remaining -= n parts.append(compressor.flush()) return (b"\x89PNG\r\n\x1a\n" + png_chunk(b"IHDR", struct.pack(">IIBBBBB", 40000000, 1, 16, 6, 0, 0, 0)) + png_chunk(b"IDAT", b"".join(parts)) + png_chunk(b"IEND", b"")) def upload(client, actor, chat, data, kind="image/png", filename="sample.png", expected=201): return client.call("POST", "/api/v1/chats/" + chat["id"] + "/attachments?filename=" + quote(filename, safe=""), expected, actor=actor, raw=data, headers={"Content-Type": kind}, schema="Attachment" if expected == 201 else None) def wait_ready(client, timeout=60): deadline = time.monotonic() + timeout while time.monotonic() < deadline: try: client.call("GET", "/health/ready", schema="Health") return except Failure: time.sleep(.5) raise Failure("stack did not recover") def main(): client = Client(timeout=30) alice, bob, eve, chat = client.bootstrap() png = (ROOT / "tests/fixtures/sample.png").read_bytes() jpeg = (ROOT / "student-tests/fixtures/sample.jpg").read_bytes() att = upload(client, alice, chat, png)["json"] jpg = upload(client, alice, chat, jpeg, "image/jpeg", "photo.jpg")["json"] for item, raw in ((att, png), (jpg, jpeg)): check(item["size_bytes"] == len(raw) and item["sha256"] == hashlib.sha256(raw).hexdigest(), "original metadata mismatch") check(client.call("GET", "/api/v1/attachments/" + item["id"] + "/content", actor=bob)["bytes"] == raw, "original bytes changed") # A private ancillary chunk before IEND creates valid exact-size PNGs. for size in (6294458, 10485760): sized = png[:-12] + png_chunk(b"vpAg", bytes(size - len(png) - 12)) + png[-12:] large = upload(client, alice, chat, sized, filename="boundary.png")["json"] check(large["size_bytes"] == size, "boundary upload size mismatch") check(client.call("GET", "/api/v1/attachments/" + large["id"] + "/content", actor=bob)["bytes"] == sized, "boundary original bytes changed") upload(client, alice, chat, b"x" * (10485760 + 1), expected=413) upload(client, alice, chat, png[:40], expected=415) damaged = bytearray(png) damaged[len(damaged) // 2] ^= 255 upload(client, alice, chat, bytes(damaged), expected=415) upload(client, alice, chat, png[:-12], expected=415) corrupt = png[:33] + png_chunk(b"IDAT", b"invalid compressed data") + png_chunk(b"IEND", b"") upload(client, alice, chat, corrupt, expected=415) upload(client, alice, chat, png, "image/jpeg", expected=415) upload(client, alice, chat, b"not an image", expected=415) upload(client, alice, chat, bomb_header(), expected=413) upload(client, alice, chat, bomb_header(20000, 10000), expected=413) upload(client, alice, chat, bomb_header(8001, 5000), expected=413) wide = wide_png() wide_response = upload(client, alice, chat, wide, filename="wide.png") check(wide_response["json"]["sha256"] == hashlib.sha256(wide).hexdigest(), "wide PNG changed") wide_metrics = {service: json.loads(compose("exec", "-T", service, "/messenger", "memory")) for service in ("api1", "api2")} check(all(m["memory.peak"] < m["memory.max"] for m in wide_metrics.values()), "wide PNG exceeded memory limit") for filename in ("../image.png", "a\\b.png", "bad\x00.png", "bad\n.png", " " , "x" * 256): upload(client, alice, chat, png, filename=filename, expected=400) for suffix in ("", "/content"): client.call("GET", "/api/v1/attachments/" + att["id"] + suffix, 404, actor=eve) upload(client, eve, chat, png, expected=404) other = client.call("POST", "/api/v1/chats", 201, actor=alice, body={"title": "Other", "member_ids": [alice["id"]]}, schema="Chat")["json"] other_att = upload(client, alice, other, png)["json"] path = "/api/v1/chats/" + chat["id"] + "/messages" for ids in ([other_att["id"]], [str(uuid.uuid4())]): client.call("POST", path, 404, actor=alice, body={"text": "", "attachment_ids": ids}, headers={"Idempotency-Key": uuid.uuid4().hex}) for body in ({"text": "x", "attachment_ids": [att["id"], att["id"]]}, {"text": "x", "attachment_ids": None}, {"text": None, "attachment_ids": [att["id"]]}, {"attachment_ids": [att["id"]]}, {"text": 42}, {"text": "x", "attachment_ids": {}}, {"text": "x", "attachment_ids": [str(uuid.uuid4()) for _ in range(11)]}): client.call("POST", path, 400, actor=alice, body=body, headers={"Idempotency-Key": uuid.uuid4().hex}) key = uuid.uuid4().hex first = client.message(bob, chat, "", key=key, attachments=[att["id"]])["json"] check(client.message(bob, chat, "", key=key, attachments=[att["id"]])["json"] == first, "attachment replay changed") client.call("POST", path, 409, actor=bob, body={"text": "", "attachment_ids": [jpg["id"]]}, headers={"Idempotency-Key": key}) text_key = uuid.uuid4().hex text = client.message(alice, chat, "text", key=text_key)["json"] replay = client.call("POST", path, 201, actor=alice, body={"attachment_ids": [], "text": "text"}, headers={"Idempotency-Key": text_key}, schema="Message")["json"] check(text == replay, "omitted attachment_ids and [] are not equivalent") object_key = psql("SELECT object_key FROM attachments WHERE id='" + att["id"] + "';") audit = compose("exec", "-T", "api1", "/messenger", "s3-audit", object_key) # Explicitly stop the replica that accepted this particular upload. uploaded = upload(client, alice, chat, png) service = {"api-1": "api1", "api-2": "api2"}[uploaded["headers"]["X-Instance-Id"]] try: compose("stop", "--timeout", "3", service) wait_ready(client) # One ready response can arrive before the monitor removes the stopped # replica. Allow the documented ten-second failover budget, then check # the download without retries so storage/content failures stay visible. deadline = time.monotonic() + 10 while time.monotonic() < deadline: upstream = compose("exec", "-T", "nginx", "cat", "/run/nginx-upstream.conf") if "server " + service + ":8080" not in upstream: break time.sleep(.2) else: raise Failure("stopped API was not excluded within ten seconds") downloaded = client.call("GET", "/api/v1/attachments/" + uploaded["json"]["id"] + "/content", actor=bob) check(downloaded["bytes"] == png, "other replica lost uploaded file") check(downloaded["headers"]["X-Instance-Id"] != uploaded["headers"]["X-Instance-Id"], "upload replica was not stopped") finally: compose("start", service) wait_ready(client) result = {"png_jpeg": "PASS", "size_corruption_mime_pixels_filename": "PASS", "exact_size_uploads_through_lb": [6294458, 10485760], "corrupt_idat_and_missing_iend": "PASS", "pixel_limit_40005000_and_200000000": "PASS", "cross_chat_and_outsider": "PASS", "message_idempotency": "PASS", "wide_png_40000000_pixels": "PASS", "wide_memory_bytes": wide_metrics, "private_bucket_and_limited_credentials": "PASS", "upload_replica_stopped_download_equal": True} (ROOT / "evidence").mkdir(exist_ok=True) (ROOT / "evidence/attachments-grade4.json").write_text(json.dumps(result, indent=2) + "\n") print(audit) print("PASS: attachment validation, authorization, idempotency and cross-replica download") if __name__ == "__main__": run(main)