124 lines
4.3 KiB
Python
124 lines
4.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Exercise upload limits, image validation, attachment ACL, and message links."""
|
|
import hashlib
|
|
from pathlib import Path
|
|
import struct
|
|
import uuid
|
|
import zlib
|
|
from urllib.parse import quote
|
|
|
|
from client import Client, arguments, check, run
|
|
|
|
MAX_UPLOAD_BYTES = 10 * 1024 * 1024
|
|
|
|
|
|
def oversized_png_header(width, height):
|
|
def chunk(kind, data):
|
|
checksum = zlib.crc32(kind + data) & 0xFFFFFFFF
|
|
return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", checksum)
|
|
|
|
image_header = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)
|
|
return (
|
|
b"\x89PNG\r\n\x1a\n"
|
|
+ chunk(b"IHDR", image_header)
|
|
+ chunk(b"IDAT", zlib.compress(b"\x00"))
|
|
+ chunk(b"IEND", b"")
|
|
)
|
|
|
|
|
|
def main():
|
|
args = arguments("Validate Lab4 attachment upload rules")
|
|
client = Client(args.base_url, args.ca_file)
|
|
alice, bob, eve, chat = client.bootstrap()
|
|
endpoint = "/api/v1/chats/" + chat["id"] + "/attachments?filename="
|
|
|
|
content = (Path(__file__).resolve().parent / "fixtures" / "sample.png").read_bytes()
|
|
uploaded = client.call(
|
|
"POST",
|
|
endpoint + quote("sample.png", safe=""),
|
|
201,
|
|
actor=alice,
|
|
raw=content,
|
|
headers={"Content-Type": "image/png"},
|
|
schema="Attachment",
|
|
)["json"]
|
|
check(uploaded["status"] == "ready", "uploaded attachment is not ready")
|
|
check(uploaded["variants"] == [] and uploaded["error_code"] is None,
|
|
"Lab4 attachment must have no processing variants or error")
|
|
check(uploaded["sha256"] == hashlib.sha256(content).hexdigest(),
|
|
"attachment checksum differs from original")
|
|
|
|
downloaded = client.call(
|
|
"GET",
|
|
"/api/v1/attachments/" + uploaded["id"] + "/content",
|
|
actor=bob,
|
|
)
|
|
check(downloaded["bytes"] == content, "download differs from uploaded bytes")
|
|
for suffix in ("", "/content"):
|
|
client.call("GET", "/api/v1/attachments/" + uploaded["id"] + suffix,
|
|
404, actor=eve)
|
|
|
|
def rejected(filename, data, content_type, status):
|
|
client.call(
|
|
"POST",
|
|
endpoint + quote(filename, safe=""),
|
|
status,
|
|
actor=alice,
|
|
raw=data,
|
|
headers={"Content-Type": content_type},
|
|
)
|
|
|
|
rejected("../sample.png", content, "image/png", 400)
|
|
rejected("mismatch.jpg", content, "image/jpeg", 415)
|
|
rejected("corrupt.png", b"\x89PNG\r\n\x1a\nnot-an-image", "image/png", 415)
|
|
rejected("too-large.png", b"x" * (MAX_UPLOAD_BYTES + 1), "image/png", 413)
|
|
rejected("too-many-pixels.png", oversized_png_header(20001, 2000),
|
|
"image/png", 413)
|
|
|
|
message = client.message(alice, chat, "", attachments=[uploaded["id"]])["json"]
|
|
check(message["attachment_ids"] == [uploaded["id"]],
|
|
"message did not retain attachment_ids")
|
|
history = client.call(
|
|
"GET",
|
|
"/api/v1/chats/" + chat["id"] + "/messages?limit=100",
|
|
actor=bob,
|
|
schema="MessagePage",
|
|
)["json"]["items"]
|
|
check(any(item["id"] == message["id"] and
|
|
item["attachment_ids"] == [uploaded["id"]] for item in history),
|
|
"message history did not return attachment_ids")
|
|
|
|
key = uuid.uuid4().hex
|
|
message_path = "/api/v1/chats/" + chat["id"] + "/messages"
|
|
first = client.call(
|
|
"POST", message_path, 201, actor=alice, body={"text": "semantic replay"},
|
|
headers={"Idempotency-Key": key}, schema="Message",
|
|
)["json"]
|
|
replay = client.call(
|
|
"POST", message_path, 201, actor=alice, body={"text": "semantic replay", "attachment_ids": []},
|
|
headers={"Idempotency-Key": key}, schema="Message",
|
|
)["json"]
|
|
check(replay == first, "missing attachment_ids and [] should be idempotently equivalent")
|
|
|
|
other_chat = client.call(
|
|
"POST",
|
|
"/api/v1/chats",
|
|
201,
|
|
actor=alice,
|
|
body={"title": "Different attachment scope", "member_ids": [alice["id"], bob["id"]]},
|
|
schema="Chat",
|
|
)["json"]
|
|
client.call(
|
|
"POST",
|
|
"/api/v1/chats/" + other_chat["id"] + "/messages",
|
|
404,
|
|
actor=alice,
|
|
body={"text": "", "attachment_ids": [uploaded["id"]]},
|
|
headers={"Idempotency-Key": uuid.uuid4().hex},
|
|
)
|
|
print("PASS: PNG upload/download, authorization, file limits, and message attachment links")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run(main)
|