#!/usr/bin/env python3 """Negative attachment and cross-chat authorization checks against a running lab4 API.""" import uuid from pathlib import Path import sys from urllib.parse import quote ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tests")) from client import Client, arguments, check, run def upload(client, actor, chat_id, data, content_type="image/png", filename="test.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": content_type}, schema="Attachment" if expected == 201 else None, ) def main(): args = arguments("Attachment upload, size, MIME, and chat authorization checks") client = Client(args.base_url, args.ca_file) alice, bob, eve, chat = client.bootstrap() other_chat = client.call( "POST", "/api/v1/chats", 201, actor=alice, body={"title": "Second test chat", "member_ids": [alice["id"], eve["id"]]}, schema="Chat", )["json"] image = (ROOT / "tests" / "fixtures" / "sample.png").read_bytes() print("Checking accepted fixture upload.", flush=True) attachment = upload(client, alice, chat["id"], image)["json"] print("Checking damaged and spoofed image rejection.", flush=True) upload(client, alice, chat["id"], b"not an image", expected=415) upload(client, alice, chat["id"], image, content_type="image/jpeg", expected=415) print("Checking byte and pixel limits.", flush=True) upload(client, alice, chat["id"], b"x" * (10 * 1024 * 1024 + 1), expected=413) import struct import zlib def chunk(kind, payload): return ( struct.pack(">I", len(payload)) + kind + payload + struct.pack(">I", zlib.crc32(kind + payload) & 0xFFFFFFFF) ) bad_idat = bytearray(zlib.compress(b"\x00\x10\x20\x30")) bad_idat[-3] ^= 0xFF damaged_idat = ( b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", 1, 1, 8, 2, 0, 0, 0)) + chunk(b"IDAT", bytes(bad_idat)) + chunk(b"IEND", b"") ) upload(client, alice, chat["id"], damaged_idat, expected=415) too_many_pixels = ( b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", 10001, 4000, 8, 2, 0, 0, 0)) + chunk(b"IDAT", zlib.compress(b"")) + chunk(b"IEND", b"") ) upload(client, alice, chat["id"], too_many_pixels, expected=413) print("Checking filename and cross-chat authorization.", flush=True) upload(client, alice, chat["id"], image, filename="../unsafe.png", expected=400) upload(client, eve, chat["id"], image, expected=404) client.call("GET", "/api/v1/attachments/" + attachment["id"], 404, actor=eve) client.call( "POST", "/api/v1/chats/" + other_chat["id"] + "/messages", 404, actor=alice, headers={"Idempotency-Key": uuid.uuid4().hex}, body={"text": "", "attachment_ids": [attachment["id"]]}, ) client.call( "POST", "/api/v1/chats/" + chat["id"] + "/messages", 400, actor=bob, headers={"Idempotency-Key": uuid.uuid4().hex}, body={"text": ""}, ) print("PASS: oversized, damaged, spoofed, unsafe-name, outsider, and cross-chat cases") if __name__ == "__main__": run(main)