#!/usr/bin/env python3 """Hardening scenarios: corrupted images, filenames, attachment order, idempotency keys, oversized bodies.""" import hashlib import os import struct import subprocess import sys 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")) sys.path.insert(0, str(ROOT / "student-tests")) from client import Client, check, run # noqa: E402 from test_attachments import SAMPLE, chunk, header_only_png # noqa: E402 JPEG_STUB = bytes.fromhex("ffd8ffc0000b080001000101011100ffda0008010100003f00ffd9") def png(width, height, filter_byte=0, level=1, corrupt_idat=False, noise=False): row = bytes([filter_byte]) + (os.urandom(width * 3) if noise else b"\x10" * (width * 3)) raw = row * height data = zlib.compress(raw, level) if corrupt_idat: mid = len(data) // 2 data = data[:mid] + bytes(b ^ 0xFF for b in data[mid:mid + 40]) + data[mid + 40:] ihdr = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0) return b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", ihdr) + chunk(b"IDAT", data) + chunk(b"IEND", b"") def main(): c = Client() alice, bob, eve, chat = c.bootstrap() path = "/api/v1/chats/%s/attachments?filename=" % chat["id"] def up(body, ctype, expected, name="a.png", **kw): return c.call("POST", path + quote(name, safe=""), expected, actor=alice, raw=body, headers={"Content-Type": ctype}, schema="Attachment" if expected == 201 else None) up(SAMPLE[:33], "image/png", 415) up(png(1, 1, filter_byte=5), "image/png", 415) up(png(1, 1, filter_byte=0xFF), "image/png", 415) up(png(64, 64, corrupt_idat=True), "image/png", 415) up(JPEG_STUB, "image/jpeg", 415) print("ok truncated PNG, bad filter byte, broken IDAT, 27-byte JPEG stub -> 415") real = subprocess.run( ["docker", "run", "--rm", "--entrypoint", "python", "mtusi-messenger:lab4", "-c", "import io,os,sys;from PIL import Image;b=io.BytesIO();" "Image.frombytes('RGB',(1000,750),os.urandom(2250000)).save(b,'JPEG',quality=90);" "sys.stdout.buffer.write(b.getvalue())"], capture_output=True, check=True).stdout whole = up(real, "image/jpeg", 201, name="real.jpg")["json"] check(c.call("GET", "/api/v1/attachments/" + whole["id"] + "/content", actor=bob)["bytes"] == real, "JPEG bytes differ") up(real[: len(real) // 2] + b"\xff\xd9", "image/jpeg", 415, name="cut.jpg") up(real[: len(real) // 2], "image/jpeg", 415, name="cut.jpg") print("ok real JPEG accepted; first half + EOI and first half alone -> 415") up(png(3000, 2000, filter_byte=7), "image/png", 415) up(png(3000, 2000, corrupt_idat=True, noise=True), "image/png", 415) print("ok same defects in images above the full-decode threshold (6 Mpx) -> 415") up(png(3000, 2000), "image/png", 201) print("ok valid 6 Mpx PNG accepted") import time big = png(6000, 6000, level=9) started = time.monotonic() r = up(big, "image/png", 201, name="36mpx.png") took = time.monotonic() - started check(took < 15, "36 Mpx PNG must be checked quickly, took %.1fs" % took) print("ok valid 36 Mpx PNG (%d KiB on the wire, 108 MiB decoded) accepted in %.1fs" % (len(big) // 1024, took)) up(png(6000, 6000, filter_byte=9, level=9), "image/png", 415) print("ok 36 Mpx PNG with bad filter byte -> 415") for name in (" ", "\t", "bad\x7f.png", "bad\x85.png", ".", "..", "a
b.png"): up(SAMPLE, "image/png", 400, name=name) print("ok whitespace-only, DEL, C1 and dot filenames -> 400") a = up(SAMPLE, "image/png", 201, name="a.png")["json"] b = up(SAMPLE, "image/png", 201, name="b.png")["json"] url = "/api/v1/chats/%s/messages" % chat["id"] key = uuid.uuid4().hex first = c.call("POST", url, 201, actor=alice, body={"text": "", "attachment_ids": [b["id"], a["id"]]}, headers={"Idempotency-Key": key}, schema="Message")["json"] check(first["attachment_ids"] == [b["id"], a["id"]], "POST keeps attachment order") replay = c.call("POST", url, 201, actor=alice, body={"attachment_ids": [b["id"], a["id"]], "text": ""}, headers={"Idempotency-Key": key}, schema="Message")["json"] check(replay == first, "replay returns the same message") c.call("POST", url, 409, actor=alice, body={"text": "", "attachment_ids": [a["id"], b["id"]]}, headers={"Idempotency-Key": key}) history = c.call("GET", url, actor=bob, schema="MessagePage")["json"]["items"] check([m for m in history if m["id"] == first["id"]][0]["attachment_ids"] == [b["id"], a["id"]], "history keeps attachment order") print("ok attachment order preserved in POST, replay and history; reordered body -> 409") key2 = uuid.uuid4().hex plain = c.call("POST", url, 201, actor=alice, body={"text": "x"}, headers={"Idempotency-Key": key2}, schema="Message")["json"] same = c.call("POST", url, 201, actor=alice, body={"text": "x", "attachment_ids": []}, headers={"Idempotency-Key": key2}, schema="Message")["json"] check(plain == same, "absent attachment_ids equals []") for bad in ("a b", "a/b", "k" * 129, ""): c.call("POST", url, 400, actor=alice, body={"text": "k"}, headers={"Idempotency-Key": bad}) other = c.call("POST", url, 201, actor=bob, body={"text": "x"}, headers={"Idempotency-Key": key2}, schema="Message")["json"] check(other["id"] != plain["id"] and other["sender_id"] == bob["id"], "idempotency key is per user") print("ok idempotency: [] == absent, invalid keys -> 400, scoped per user") huge = b"\x89PNG\r\n\x1a\n" + b"\0" * (12 * 1024 * 1024) r = c.call("POST", path + "big.png", 413, actor=alice, raw=huge, headers={"Content-Type": "image/png"}) check(r["headers"].get_content_type() == "application/json", "413 above the nginx limit is API-shaped JSON") print("ok 12 MiB body -> JSON 413") print("PASS: input hardening") run(main)