#!/usr/bin/env python3 """Negative attachment cases. Prints statuses only, never tokens.""" import os import base64 from urllib.parse import urlparse import hashlib import json import socket import struct import time import uuid import urllib.error import urllib.request import zlib from pathlib import Path BASE = os.environ.get("BASE_URL", "http://127.0.0.1:8080") ROOT = Path(__file__).resolve().parents[1] JPEG = base64.b64decode( "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAIAAgDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD5soooroMD/9k=" ) def header_only_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) return signature + chunk(b"IHDR", ihdr) + chunk(b"IEND", b"") 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" + (b"\x10\x20\x30" * width) for _ in range(height)) return signature + chunk(b"IHDR", ihdr) + chunk(b"IDAT", zlib.compress(raw)) + chunk(b"IEND", b"") class Api: def __init__(self) -> None: self.opener = urllib.request.build_opener(urllib.request.ProxyHandler({})) def call(self, method, path, body=None, token=None, headers=None, raw=None, content_type=None): hdrs = {} data = raw if body is not None: data = json.dumps(body).encode("utf-8") hdrs["Content-Type"] = "application/json" if content_type: hdrs["Content-Type"] = content_type if token: hdrs["Authorization"] = "Bearer " + token if headers: hdrs.update(headers) request = urllib.request.Request(BASE + path, data=data, headers=hdrs, method=method) try: response = self.opener.open(request, timeout=30) 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, response.headers, parsed, payload def wait_ready(api: Api, attachment_id: str, token: str): 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") 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)) print("PASS", label, got) def user(api: Api, label: str): username = "g4_" + label + "_" + uuid.uuid4().hex[:10] password = "T3st-" + uuid.uuid4().hex status, _, body, _ = api.call( "POST", "/api/v1/users", {"username": username, "display_name": "Grade " + label, "password": password}, ) expect(201, status, "register " + label) basic = base64.b64encode((username + ":" + password).encode()).decode() status, _, session, _ = api.call("POST", "/api/v1/auth/sessions", headers={"Authorization": "Basic " + basic}) expect(201, status, "login " + label) return body["id"], session["token"] def declared_length(token, chat_id): parsed = urlparse(BASE) host = parsed.netloc or "127.0.0.1:8080" request = ( "POST /api/v1/chats/%s/attachments?filename=big.png HTTP/1.1\r\n" "Host: %s\r\n" "Authorization: Bearer %s\r\n" "Content-Type: image/png\r\n" "Content-Length: 10485761\r\n" "Connection: close\r\n\r\n" % (chat_id, host, token) ).encode() + b"\x89PNG" sock = socket.create_connection((parsed.hostname or "127.0.0.1", parsed.port or 80), timeout=5) try: sock.sendall(request) data = b"" while b"\r\n" not in data: chunk = sock.recv(256) if not chunk: break data += chunk finally: sock.close() line = data.split(b"\r\n", 1)[0].decode("latin1", "replace") code = int(line.split()[1]) expect(413, code, "content-length above 10MiB before body is read") def main(): api = Api() alice, alice_token = user(api, "alice") bob, bob_token = user(api, "bob") eve, eve_token = user(api, "eve") status, _, chat, _ = api.call( "POST", "/api/v1/chats", {"title": "grade4", "member_ids": [alice, bob]}, token=alice_token, ) expect(201, status, "create chat") chat_id = chat["id"] status, _, other, _ = api.call( "POST", "/api/v1/chats", {"title": "other", "member_ids": [alice, eve]}, token=alice_token, ) expect(201, status, "create other chat") status, _, message, _ = api.call( "POST", "/api/v1/chats/%s/messages" % chat_id, {"text": "только текст"}, token=alice_token, headers={"Idempotency-Key": uuid.uuid4().hex}, ) expect(201, status, "text message") if message["attachment_ids"] != []: raise SystemExit("FAIL text message lost empty attachment_ids") good = png(32, 32) status, _, attachment, _ = api.call( "POST", "/api/v1/chats/%s/attachments?filename=ok.png" % chat_id, token=alice_token, raw=good, content_type="image/png", ) expect(202, status, "png upload") if attachment["sha256"] != hashlib.sha256(good).hexdigest() or attachment["status"] != "queued": raise SystemExit("FAIL png metadata") attachment = wait_ready(api, attachment["id"], bob_token) status, headers, _, downloaded = api.call( "GET", "/api/v1/attachments/%s/content" % attachment["id"], token=bob_token, ) expect(200, status, "member download") if downloaded != good or headers.get_content_type() != "image/png": raise SystemExit("FAIL download bytes") if headers.get("Location"): raise SystemExit("FAIL download redirected") status, _, jpeg_meta, _ = api.call( "POST", "/api/v1/chats/%s/attachments?filename=ok.jpg" % chat_id, token=alice_token, raw=JPEG, content_type="image/jpeg", ) expect(202, status, "jpeg upload") if jpeg_meta["content_type"] != "image/jpeg" or jpeg_meta["sha256"] != hashlib.sha256(JPEG).hexdigest(): raise SystemExit("FAIL jpeg metadata") status, _, _, _ = api.call( "POST", "/api/v1/chats/%s/attachments?filename=a/b.png" % chat_id, token=alice_token, raw=good, content_type="image/png", ) expect(400, status, "filename with slash") status, _, _, _ = api.call( "POST", "/api/v1/chats/%s/attachments?filename=%s" % (chat_id, "bad%0Aname.png"), token=alice_token, raw=good, content_type="image/png", ) expect(400, status, "filename with control character") status, _, _, _ = api.call( "POST", "/api/v1/chats/%s/attachments?filename=lie.png" % chat_id, token=alice_token, raw=good, content_type="image/jpeg", ) expect(415, status, "png bytes declared as jpeg") status, _, _, _ = api.call( "POST", "/api/v1/chats/%s/attachments?filename=cut.png" % chat_id, token=alice_token, raw=b"\x89PNG\r\n\x1a\ntruncated", content_type="image/png", ) expect(415, status, "truncated png") status, _, _, _ = api.call( "POST", "/api/v1/chats/%s/attachments?filename=cut.jpg" % chat_id, token=alice_token, raw=JPEG[:-40], content_type="image/jpeg", ) expect(415, status, "truncated jpeg") status, _, _, _ = api.call( "POST", "/api/v1/chats/%s/attachments?filename=huge.png" % chat_id, token=alice_token, raw=header_only_png(10000, 5000), content_type="image/png", ) expect(413, status, "pixel limit") declared_length(alice_token, chat_id) status, _, _, _ = api.call( "POST", "/api/v1/chats/%s/attachments?filename=big.png" % chat_id, token=alice_token, raw=b"\x89PNG" + b"\x00" * (10_485_760), content_type="image/png", ) expect(413, status, "body above 10MiB") status, _, _, _ = api.call( "POST", "/api/v1/chats/%s/attachments?filename=nope.png" % chat_id, token=eve_token, raw=good, content_type="image/png", ) expect(404, status, "outsider upload") for suffix in ("", "/content"): status, _, _, _ = api.call("GET", "/api/v1/attachments/%s%s" % (attachment["id"], suffix), token=eve_token) expect(404, status, "outsider get " + (suffix or "metadata")) missing = str(uuid.uuid4()) status, _, _, _ = api.call( "POST", "/api/v1/chats/%s/messages" % chat_id, {"text": "", "attachment_ids": [missing]}, token=alice_token, headers={"Idempotency-Key": uuid.uuid4().hex}, ) expect(404, status, "unknown attachment id") status, _, foreign, _ = api.call( "POST", "/api/v1/chats/%s/attachments?filename=other.png" % other["id"], token=alice_token, raw=good, content_type="image/png", ) expect(202, status, "upload into other chat") status, _, _, _ = api.call( "POST", "/api/v1/chats/%s/messages" % chat_id, {"text": "", "attachment_ids": [foreign["id"]]}, token=alice_token, headers={"Idempotency-Key": uuid.uuid4().hex}, ) expect(404, status, "attachment from another chat") ids = [str(uuid.uuid4()) for _ in range(11)] status, _, _, _ = api.call( "POST", "/api/v1/chats/%s/messages" % chat_id, {"text": "x", "attachment_ids": ids}, token=alice_token, headers={"Idempotency-Key": uuid.uuid4().hex}, ) expect(400, status, "more than 10 attachment ids") status, _, linked, _ = api.call( "POST", "/api/v1/chats/%s/messages" % chat_id, {"text": "", "attachment_ids": [attachment["id"]]}, token=bob_token, headers={"Idempotency-Key": uuid.uuid4().hex}, ) expect(201, status, "message with attachment") if linked["attachment_ids"] != [attachment["id"]] or linked["text"] != "": raise SystemExit("FAIL linked message") print("PASS grade4 api checks") if __name__ == "__main__": main()