"""Собственные тесты вложений lab5 (негативные сценарии и права). Запуск: python -m unittest discover -s student-tests -p "test_attachments.py" -v (BASE_URL по умолчанию http://127.0.0.1:8080) """ import hashlib import json import os import socket import struct import sys import time import unittest import urllib.error import urllib.parse import urllib.request import uuid import zlib sys.path.insert(0, os.path.dirname(__file__)) from test_api import BASE, ROOT, call, psql, register # noqa: E402 FIX = os.path.join(os.path.dirname(__file__), "fixtures") PNG = open(os.path.join(FIX, "sample.png"), "rb").read() JPG = open(os.path.join(FIX, "sample.jpg"), "rb").read() def upload(token, chat_id, data, ctype="image/png", filename="sample.png", raw_filename=None): qs = raw_filename if raw_filename is not None else "filename=" + filename req = urllib.request.Request("%s/api/v1/chats/%s/attachments?%s" % (BASE, chat_id, qs), data=data, method="POST", headers={"Authorization": "Bearer " + token, "Content-Type": ctype}) try: with urllib.request.urlopen(req, timeout=30) as r: return r.status, r.headers, json.loads(r.read()) except urllib.error.HTTPError as err: body = err.read() return err.code, err.headers, (json.loads(body) if body else None) def download(token, att_id, suffix="/content"): req = urllib.request.Request("%s/api/v1/attachments/%s%s" % (BASE, att_id, suffix), headers={"Authorization": "Bearer " + token if token else ""}) try: with urllib.request.urlopen(req, timeout=30) as r: return r.status, r.headers, r.read() except urllib.error.HTTPError as err: return err.code, err.headers, err.read() def wait_ready(token, att_id, seconds=60): """lab5: загрузка возвращает 202 queued; готовность определяется опросом метаданных.""" deadline = time.time() + seconds while time.time() < deadline: code, _, body = download(token, att_id, "") att = json.loads(body) if att["status"] == "ready": return att assert att["status"] != "failed", att time.sleep(0.3) raise AssertionError("not ready") def png_chunk(kind, data): return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data) & 0xFFFFFFFF) def bomb_png(width=20000, height=20000): """Корректный по структуре PNG, заявляющий 400 млн пикселей, без данных изображения (отклоняется по заголовку).""" ihdr = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0) return b"\x89PNG\r\n\x1a\n" + png_chunk(b"IHDR", ihdr) + png_chunk(b"IDAT", zlib.compress(b"\x00" * 16)) + png_chunk(b"IEND", b"") class AttachmentTest(unittest.TestCase): def setUp(self): self.a, self.b, self.eve = register("att"), register("att"), register("att") self.chat = call("POST", "/api/v1/chats", {"title": "файлы", "member_ids": [self.a["user"]["id"], self.b["user"]["id"]]}, self.a["token"])[2] self.other = call("POST", "/api/v1/chats", {"title": "другой", "member_ids": [self.a["user"]["id"]]}, self.a["token"])[2] def assertError(self, result, status, code=None): self.assertEqual(result[0], status, result[2]) if code: self.assertEqual(result[2]["error"]["code"], code) def test_png_and_jpeg_roundtrip_with_checksum(self): for data, ctype, name in ((PNG, "image/png", "фото.png"), (JPG, "image/jpeg", "photo.jpg")): status, headers, att = upload(self.a["token"], self.chat["id"], data, ctype, urllib.parse.quote(name)) self.assertEqual(status, 202, att) self.assertEqual((att["status"], att["variants"], att["error_code"]), ("queued", [], None)) wait_ready(self.b["token"], att["id"]) self.assertEqual((att["size_bytes"], att["sha256"], att["content_type"]), (len(data), hashlib.sha256(data).hexdigest(), ctype)) self.assertEqual((att["chat_id"], att["owner_id"], att["filename"]), (self.chat["id"], self.a["user"]["id"], name)) code, hdr, body = download(self.b["token"], att["id"]) # другой участник чата self.assertEqual((code, body, hdr.get_content_type()), (200, data, ctype)) self.assertEqual(download(self.b["token"], att["id"], "")[0], 200) def test_too_large_is_413_and_nothing_is_stored(self): before = psql("SELECT count(*) FROM attachments") r = upload(self.a["token"], self.chat["id"], PNG[:8] + b"\x00" * (10 * 1024 * 1024 + 1)) self.assertError(r, 413, "payload_too_large") self.assertEqual(psql("SELECT count(*) FROM attachments"), before) def test_corrupted_and_spoofed_files_are_415_not_500(self): cases = { "truncated png": (PNG[: len(PNG) // 2], "image/png"), "garbage png": (b"\x89PNG\r\n\x1a\n" + b"\x00" * 200, "image/png"), "random bytes": (os.urandom(500), "image/png"), "truncated jpeg": (JPG[: len(JPG) // 2], "image/jpeg"), "garbage jpeg": (b"\xff\xd8\xff\xe0" + b"\x00" * 200, "image/jpeg"), "png as jpeg": (PNG, "image/jpeg"), "jpeg as png": (JPG, "image/png"), "text as png": (b"hello, i am not an image", "image/png"), "gif": (b"GIF89a" + b"\x00" * 50, "image/png"), } for name, (data, ctype) in cases.items(): with self.subTest(name): self.assertError(upload(self.a["token"], self.chat["id"], data, ctype), 415, "unsupported_media_type") self.assertError(upload(self.a["token"], self.chat["id"], PNG, "text/plain"), 415) self.assertError(upload(self.a["token"], self.chat["id"], PNG, "multipart/form-data; boundary=x"), 415) def test_decompression_bomb_header_is_413(self): self.assertError(upload(self.a["token"], self.chat["id"], bomb_png()), 413, "payload_too_large") # 6000x6600 = 39.6 млн пикселей < лимита: по размерам допустимо, но данных изображения нет -> файл повреждён (415) self.assertEqual(upload(self.a["token"], self.chat["id"], bomb_png(6000, 6600))[0], 415) def test_dangerous_filenames_are_400(self): bad = ["a%2Fb.png", "..%5Cx.png", "x%00.png", "x%0A.png", "x%7F.png", "", "%20%20", "n" * 256] for name in bad: with self.subTest(name): self.assertError(upload(self.a["token"], self.chat["id"], PNG, raw_filename="filename=" + name), 400, "invalid_request") self.assertError(upload(self.a["token"], self.chat["id"], PNG, raw_filename=""), 400) self.assertEqual(upload(self.a["token"], self.chat["id"], PNG, raw_filename="filename=" + "n" * 255)[0], 202) def test_empty_body_is_400(self): self.assertError(upload(self.a["token"], self.chat["id"], b""), 400) def test_foreign_user_cannot_upload_read_or_download(self): self.assertError(upload(self.eve["token"], self.chat["id"], PNG), 404) att = upload(self.a["token"], self.chat["id"], PNG)[2] wait_ready(self.a["token"], att["id"]) for suffix in ("", "/content"): self.assertEqual(download(self.eve["token"], att["id"], suffix)[0], 404) self.assertEqual(download(None, att["id"], suffix)[0], 401) self.assertEqual(download(self.a["token"], str(uuid.uuid4()))[0], 404) self.assertEqual(download(self.a["token"], "not-a-uuid")[0], 400) self.assertEqual(download(self.a["token"], att["id"], "/content?variant=thumbnail")[0], 200) # после ready вариант доступен self.assertEqual(download(self.a["token"], att["id"], "/content?variant=bogus")[0], 400) def test_message_with_attachments_and_chat_binding(self): att = upload(self.a["token"], self.chat["id"], PNG)[2] foreign_chat_att = upload(self.a["token"], self.other["id"], JPG, "image/jpeg")[2] path = "/api/v1/chats/%s/messages" % self.chat["id"] sent = call("POST", path, {"text": "", "attachment_ids": [att["id"]]}, self.a["token"]) self.assertEqual(sent[0], 201, sent[2]) self.assertEqual(sent[2]["attachment_ids"], [att["id"]]) self.assertEqual(call("POST", path, {"text": "ещё раз то же вложение", "attachment_ids": [att["id"]]}, self.b["token"])[0], 201) listed = call("GET", path + "?limit=10", token=self.b["token"])[2]["items"] self.assertEqual([m["attachment_ids"] for m in listed], [[att["id"]], [att["id"]]]) # вложение другого чата / несуществующее -> 404; дубликаты, >10, пустое сообщение без вложений -> 400 self.assertEqual(call("POST", path, {"text": "x", "attachment_ids": [foreign_chat_att["id"]]}, self.a["token"])[0], 404) self.assertEqual(call("POST", path, {"text": "x", "attachment_ids": [str(uuid.uuid4())]}, self.a["token"])[0], 404) self.assertEqual(call("POST", path, {"text": "x", "attachment_ids": [att["id"], att["id"]]}, self.a["token"])[0], 400) self.assertEqual(call("POST", path, {"text": "x", "attachment_ids": [str(uuid.uuid4()) for _ in range(11)]}, self.a["token"])[0], 400) self.assertEqual(call("POST", path, {"text": " ", "attachment_ids": []}, self.a["token"])[0], 400) self.assertEqual(call("POST", path, {"text": "x", "attachment_ids": "nope"}, self.a["token"])[0], 400) # чужой пользователь не может сослаться на вложение self.assertEqual(call("POST", path, {"text": "x", "attachment_ids": [att["id"]]}, self.eve["token"])[0], 404) def test_idempotent_replay_with_attachments(self): att = upload(self.a["token"], self.chat["id"], PNG)[2] path = "/api/v1/chats/%s/messages" % self.chat["id"] key = uuid.uuid4().hex h = {"Idempotency-Key": key} first = call("POST", path, {"text": "t", "attachment_ids": [att["id"]]}, self.a["token"], headers=h) again = call("POST", path, {"attachment_ids": [att["id"]], "text": "t"}, self.a["token"], headers=h) # другой порядок ключей self.assertEqual((first[0], again[0], first[2]), (201, 201, again[2])) self.assertEqual(call("POST", path, {"text": "t"}, self.a["token"], headers=h)[0], 409) # другое тело text_key = uuid.uuid4().hex m1 = call("POST", path, {"text": "без вложений"}, self.a["token"], headers={"Idempotency-Key": text_key}) m2 = call("POST", path, {"text": "без вложений", "attachment_ids": []}, self.a["token"], headers={"Idempotency-Key": text_key}) self.assertEqual(m1[2], m2[2]) # отсутствие == [] self.assertEqual(m1[2]["attachment_ids"], []) def test_aborted_upload_leaves_nothing(self): before = (psql("SELECT count(*) FROM attachments")) host, port = BASE.split("//")[1].split(":") with socket.create_connection((host, int(port))) as s: s.sendall(("POST /api/v1/chats/%s/attachments?filename=a.png HTTP/1.1\r\nHost: x\r\nAuthorization: Bearer %s\r\n" "Content-Type: image/png\r\nContent-Length: 200000\r\n\r\n" % (self.chat["id"], self.a["token"])).encode() + PNG[:5000]) import time time.sleep(1) self.assertEqual(psql("SELECT count(*) FROM attachments"), before) def test_bucket_is_private_outside_the_api(self): att = upload(self.a["token"], self.chat["id"], PNG)[2] wait_ready(self.a["token"], att["id"]) key = psql("SELECT s3_key FROM attachments WHERE id = '%s'" % att["id"]) import subprocess code = ("import urllib.request,urllib.error,sys\n" "try:\n urllib.request.urlopen('http://seaweedfs:8333/messenger-attachments/%s', timeout=5); print('OPEN')\n" "except urllib.error.HTTPError as e: print(e.code)\n" % key) out = subprocess.run(["docker", "compose", "--env-file", ".env", "exec", "-T", "api", "python", "-c", code], cwd=ROOT, capture_output=True, text=True).stdout.strip() self.assertEqual(out, "403", "анонимный GET объекта напрямую из S3 должен быть запрещён") if __name__ == "__main__": unittest.main()