"""Вложения (лаба 4): загрузка, лимиты, тип и целостность, имя, доступ, байты оригинала, ссылки из сообщений, идемпотентность с вложениями, одновременные загрузки и другая реплика. Рубрика «4»: слишком большой файл, повреждённое изображение, подмена MIME, чужое `attachment_id` и вложение другого чата — и ни одна ошибка пользователя не становится 500 (`assert_error` проверяет статус и код контракта). Изображения генерирует `images.py`, в git их нет. """ import hashlib import threading import uuid from typing import Any import httpx import pytest import images from helpers import ( BEARER_CHALLENGE, SAMPLE_JPEG, SAMPLE_PNG, ClientFactory, assert_content, assert_error, assert_replicas, assert_unauthorized, attachment_content, attachment_meta, attachments_path, create_chat, headers, instance_id, new_key, read_history, register, send, upload, uploaded, ) MAX_BYTES = 10 * 1024 * 1024 CONCURRENT_UPLOADS = 10 @pytest.fixture def members(client: httpx.Client) -> dict[str, Any]: """Чат alice и bob, посторонняя eve.""" alice, bob, eve = (register(client, label) for label in ("alice", "bob", "eve")) return { "alice": alice, "bob": bob, "eve": eve, "chat": create_chat(client, alice, bob), } # --- загрузка и оригинал --------------------------------------------------------- @pytest.mark.parametrize( ("data", "content_type"), [(SAMPLE_PNG, "image/png"), (SAMPLE_JPEG, "image/jpeg")], ids=["png", "jpeg"], ) def test_upload_and_download_original( client: httpx.Client, members: dict, data: bytes, content_type: str ) -> None: alice, bob = members["alice"], members["bob"] attachment = uploaded( client, alice, members["chat"], data, content_type=content_type, filename="фото.img", ) # метаданные — тот же Attachment, что ответ загрузки; читает любой участник meta = attachment_meta(client, bob, attachment["id"]) assert meta.status_code == 200, meta.text assert meta.json() == attachment assert_content( attachment_content(client, bob, attachment["id"]), data, content_type ) # variant по умолчанию — original assert_content( attachment_content(client, alice, attachment["id"], variant="original"), data, content_type, ) def test_content_type_parameters_are_ignored( client: httpx.Client, members: dict ) -> None: response = upload( client, members["alice"], members["chat"], content_type="IMAGE/PNG; charset=binary", ) assert response.status_code == 201, response.text assert response.json()["content_type"] == "image/png" def test_exactly_max_bytes_is_accepted(client: httpx.Client, members: dict) -> None: data = images.png_of_size(MAX_BYTES) assert len(data) == MAX_BYTES attachment = uploaded(client, members["alice"], members["chat"], data) downloaded = attachment_content(client, members["bob"], attachment["id"]) assert hashlib.sha256(downloaded.content).hexdigest() == attachment["sha256"] # --- лимиты, тип и целостность: 413 и 415, не 500 --------------------------------- def test_too_large_body_is_413(client: httpx.Client, members: dict) -> None: # Content-Length больше лимита — отказ до чтения тела data = SAMPLE_PNG + b"\x00" * (MAX_BYTES + 1 - len(SAMPLE_PNG)) assert_error( upload(client, members["alice"], members["chat"], data), 413, "payload_too_large", ) def test_too_large_stream_is_413(client: httpx.Client, members: dict) -> None: # без Content-Length (chunked) — счётчик прочитанного обрывает на лимите def chunks(): yield SAMPLE_PNG remaining = MAX_BYTES + 1 - len(SAMPLE_PNG) while remaining > 0: part = min(remaining, 1024 * 1024) yield b"\x00" * part remaining -= part response = upload(client, members["alice"], members["chat"], chunks()) assert_error(response, 413, "payload_too_large") def test_too_many_pixels_is_413(client: httpx.Client, members: dict) -> None: # 10 000 × 5 000 = 50 млн пикселей в заголовке маленького файла data = images.png_huge_header() assert len(data) < 1024 assert_error( upload(client, members["alice"], members["chat"], data), 413, "payload_too_large", ) @pytest.mark.parametrize( ("data", "content_type"), [ (b"not an image at all", "image/png"), (b"", "image/png"), (images.truncated(SAMPLE_PNG), "image/png"), (images.png_bad_crc(SAMPLE_PNG), "image/png"), (images.png_bomb(), "image/png"), (images.truncated(SAMPLE_JPEG), "image/jpeg"), (images.jpeg_without_eoi(SAMPLE_JPEG), "image/jpeg"), # подмена MIME: настоящий JPEG, объявленный как PNG, и наоборот (SAMPLE_JPEG, "image/png"), (SAMPLE_PNG, "image/jpeg"), (SAMPLE_PNG, "text/plain"), (SAMPLE_PNG, "image/webp"), (SAMPLE_PNG, None), ], ids=[ "text", "empty", "truncated-png", "bad-crc", "bomb", "truncated-jpeg", "jpeg-without-eoi", "jpeg-as-png", "png-as-jpeg", "text-plain", "webp", "no-content-type", ], ) def test_bad_or_damaged_image_is_415( client: httpx.Client, members: dict, data: bytes, content_type: str | None ) -> None: response = upload( client, members["alice"], members["chat"], data, content_type=content_type ) assert_error(response, 415, "unsupported_media_type") @pytest.mark.parametrize( "data", [ bytes.fromhex("ffd8ffc0000b080001000101011100ffda0008010100003f00ffd9"), images.jpeg_with_scan_data(SAMPLE_JPEG), images.jpeg_with_scan_data(SAMPLE_JPEG, b"\xff\xd0\xff\xd1"), images.jpeg_with_scan_data(SAMPLE_JPEG, b"\xff\xff"), ], ids=["headers-only", "empty-scan-with-tables", "restart-only", "fill-only"], ) def test_jpeg_without_scan_data_is_415( client: httpx.Client, members: dict, data: bytes ) -> None: response = upload( client, members["alice"], members["chat"], data, content_type="image/jpeg", filename="bad.jpg", ) assert_error(response, 415, "unsupported_media_type") def test_trailing_bytes_are_part_of_original( client: httpx.Client, members: dict ) -> None: data = SAMPLE_PNG + b"tail after IEND" attachment = uploaded(client, members["alice"], members["chat"], data) assert_content( attachment_content(client, members["bob"], attachment["id"]), data, "image/png" ) # --- имя файла --------------------------------------------------------------------- @pytest.mark.parametrize( "filename", [ "a/b.png", "a\\b.png", "a\x00b.png", "a\x01b.png", "a\x7fb.png", " ", "x" * 256, "", ], ids=["slash", "backslash", "nul", "control", "del", "blank", "too-long", "empty"], ) def test_dangerous_filename_is_400( client: httpx.Client, members: dict, filename: str ) -> None: response = upload(client, members["alice"], members["chat"], filename=filename) assert_error(response, 400, "invalid_request") def test_missing_or_broken_filename_is_400(client: httpx.Client, members: dict) -> None: alice, chat = members["alice"], members["chat"] assert_error(upload(client, alice, chat, filename=None), 400, "invalid_request") # байт не UTF-8 в процент-кодах: Starlette заменил бы его на U+FFFD assert_error( upload(client, alice, chat, query="filename=a%FFb.png"), 400, "invalid_request" ) def test_unicode_filename_is_kept_as_is(client: httpx.Client, members: dict) -> None: # символы направления текста и emoji — не управляющие (Cc): принимаются name = "отчёт ‮kcod.png 🖼️" attachment = uploaded(client, members["alice"], members["chat"], filename=name) assert ( attachment_meta(client, members["bob"], attachment["id"]).json()["filename"] == name ) # --- доступ ------------------------------------------------------------------------ def test_outsider_gets_404_everywhere(client: httpx.Client, members: dict) -> None: alice, eve, chat = members["alice"], members["eve"], members["chat"] attachment = uploaded(client, alice, chat) assert_error(attachment_meta(client, eve, attachment["id"]), 404, "not_found") assert_error(attachment_content(client, eve, attachment["id"]), 404, "not_found") # загрузка в чужой чат — 404 до чтения тела, как и для несуществующего assert_error(upload(client, eve, chat), 404, "not_found") assert_error(upload(client, alice, {"id": str(uuid.uuid4())}), 404, "not_found") def test_unknown_and_invalid_ids(client: httpx.Client, members: dict) -> None: alice = members["alice"] unknown = str(uuid.uuid4()) assert_error(attachment_meta(client, alice, unknown), 404, "not_found") assert_error(attachment_content(client, alice, unknown), 404, "not_found") assert_error(attachment_meta(client, alice, "not-a-uuid"), 400, "invalid_request") assert_error(upload(client, alice, {"id": "not-a-uuid"}), 400, "invalid_request") def test_unknown_variant_is_400(client: httpx.Client, members: dict) -> None: attachment = uploaded(client, members["alice"], members["chat"]) response = attachment_content( client, members["alice"], attachment["id"], variant="thumbnail" ) assert_error(response, 400, "invalid_request") def test_without_token_is_401(client: httpx.Client, members: dict) -> None: attachment = uploaded(client, members["alice"], members["chat"]) assert_unauthorized(upload(client, None, members["chat"]), BEARER_CHALLENGE) assert_unauthorized( attachment_meta(client, None, attachment["id"]), BEARER_CHALLENGE ) assert_unauthorized( attachment_content(client, None, attachment["id"]), BEARER_CHALLENGE ) # --- сообщения со ссылками на вложения --------------------------------------------- def test_message_with_attachments_in_history( client: httpx.Client, members: dict ) -> None: alice, bob, chat = members["alice"], members["bob"], members["chat"] first = uploaded(client, alice, chat) # любой участник может ссылаться на вложение чата second = uploaded(client, bob, chat, SAMPLE_JPEG, content_type="image/jpeg") ids = [second["id"], first["id"]] sent = send(client, alice, chat, "две картинки", attachment_ids=ids) assert sent.status_code == 201, sent.text assert sent.json()["attachment_ids"] == ids # пустой текст и текст из пробелов допустимы с вложением empty = send(client, bob, chat, "", attachment_ids=[first["id"]]) blank = send(client, bob, chat, " ", attachment_ids=[first["id"]]) assert empty.status_code == 201, empty.text assert blank.status_code == 201, blank.text assert blank.json()["text"] == " " text_only = send(client, alice, chat, "без вложений") assert text_only.json()["attachment_ids"] == [] items, _ = read_history(client, bob, chat, limit=50) assert [item["attachment_ids"] for item in items] == [ ids, [first["id"]], [first["id"]], [], ] def test_message_with_foreign_attachment_is_404( client: httpx.Client, members: dict ) -> None: alice, bob, eve, chat = ( members["alice"], members["bob"], members["eve"], members["chat"], ) other_chat = create_chat(client, alice, eve) elsewhere = uploaded(client, eve, other_chat) mine = uploaded(client, alice, chat) # вложение другого чата (даже своё) и неизвестное — 404, сообщения нет for ids in ([elsewhere["id"]], [mine["id"], elsewhere["id"]], [str(uuid.uuid4())]): assert_error( send(client, alice, chat, "чужое", attachment_ids=ids), 404, "not_found" ) # посторонний не ссылается на вложение чата, где его нет assert_error( send(client, eve, chat, "чужое", attachment_ids=[mine["id"]]), 404, "not_found" ) assert read_history(client, bob, chat, limit=50)[0] == [] @pytest.mark.parametrize( "body", [ {"text": ""}, {"text": " \n", "attachment_ids": []}, {"text": "x", "attachment_ids": ["not-a-uuid"]}, {"text": "x", "attachment_ids": "not-a-list"}, {"text": "x", "attachment_ids": [str(uuid.uuid4()) for _ in range(11)]}, ], ids=["empty-no-attachments", "blank-empty-list", "bad-uuid", "not-list", "eleven"], ) def test_bad_attachment_ids_are_400( client: httpx.Client, members: dict, body: dict[str, Any] ) -> None: alice, chat = members["alice"], members["chat"] response = client.post( f"/api/v1/chats/{chat['id']}/messages", json=body, headers={**headers(alice), "Idempotency-Key": new_key()}, ) assert_error(response, 400, "invalid_request") def test_duplicate_attachment_ids_are_400(client: httpx.Client, members: dict) -> None: alice, chat = members["alice"], members["chat"] attachment = uploaded(client, alice, chat) same = [attachment["id"], attachment["id"].upper()] assert_error( send(client, alice, chat, "дубль", attachment_ids=same), 400, "invalid_request" ) def test_idempotency_with_attachments(client: httpx.Client, members: dict) -> None: alice, bob, chat = members["alice"], members["bob"], members["chat"] first, second = uploaded(client, alice, chat), uploaded(client, alice, chat) key = new_key() sent = send(client, alice, chat, "с ключом", key=key, attachment_ids=[first["id"]]) assert sent.status_code == 201, sent.text # тот же ключ и то же тело — тот же Message; другие вложения — 409 again = send(client, alice, chat, "с ключом", key=key, attachment_ids=[first["id"]]) assert again.json() == sent.json() assert_error( send(client, alice, chat, "с ключом", key=key, attachment_ids=[second["id"]]), 409, "conflict", ) # отсутствующее attachment_ids и [] — одно и то же тело plain_key = new_key() plain = send(client, alice, chat, "без вложений", key=plain_key) same = send(client, alice, chat, "без вложений", key=plain_key, attachment_ids=[]) assert same.json() == plain.json() items, _ = read_history(client, bob, chat, limit=50) assert [item["id"] for item in items] == [sent.json()["id"], plain.json()["id"]] # --- одновременные загрузки и другая реплика --------------------------------------- def test_concurrent_uploads(new_client: ClientFactory, members: dict) -> None: """10 одновременных загрузок через LB: все 201, каждое скачивается тем же SHA-256; места загрузок (4 + 16 в очереди на реплику) их вмещают.""" alice, bob, chat = members["alice"], members["bob"], members["chat"] bodies = [images.png(320, 200, seed=index) for index in range(CONCURRENT_UPLOADS)] clients = [new_client() for _ in bodies] for opened in clients: assert opened.get("/health/live").status_code == 200 barrier = threading.Barrier(len(bodies)) results: list[httpx.Response | None] = [None] * len(bodies) def worker(index: int) -> None: barrier.wait() results[index] = upload(clients[index], alice, chat, bodies[index]) threads = [threading.Thread(target=worker, args=(i,)) for i in range(len(bodies))] for thread in threads: thread.start() for thread in threads: thread.join() for body, response in zip(bodies, results, strict=True): assert response is not None assert response.status_code == 201, response.text attachment = response.json() assert attachment["sha256"] == hashlib.sha256(body).hexdigest() downloaded = attachment_content(clients[0], bob, attachment["id"]) assert downloaded.content == body def test_download_through_other_replica( new_client: ClientFactory, members: dict, replicas: int ) -> None: """Загрузила одна реплика, байты отдаёт другая: на API нет локальной копии оригинала.""" alice, bob, chat = members["alice"], members["bob"], members["chat"] client = new_client() response = upload(client, alice, chat) assert response.status_code == 201, response.text uploader = instance_id(response) attachment = response.json() seen = [] for _ in range(4 * replicas): # своё соединение на запрос: round-robin LB по соединениям сеанса downloaded = attachment_content(new_client(), bob, attachment["id"]) assert_content(downloaded, SAMPLE_PNG, "image/png") seen.append(instance_id(downloaded)) assert_replicas([uploader, *seen], replicas) assert any(instance != uploader for instance in seen) def test_upload_path_is_attachments(client: httpx.Client, members: dict) -> None: """Неверный метод на пути загрузки — 405 в формате контракта.""" response = client.get( attachments_path(members["chat"]), headers=headers(members["alice"]) ) assert_error(response, 405, "invalid_request")