import hashlib import socket import unittest import urllib.parse import uuid from pathlib import Path from api import BASE_URL, call, history, new_chat, new_key, new_user, request_bytes, send from samples import REAL_IMAGES, make_png, noisy_png FIXTURE = (Path(__file__).resolve().parents[1] / "tests/fixtures/sample.png").read_bytes() MIB = 1024 * 1024 def upload(user, chat, data, content_type="image/png", filename="photo.png", headers=None): path = "/api/v1/chats/%s/attachments" % chat["id"] if filename is not None: path += "?filename=" + urllib.parse.quote(filename, safe="") return call("POST", path, raw=data, user=user, headers={"Content-Type": content_type, **(headers or {})}) def content(user, attachment_id, query=""): return request_bytes("GET", "/api/v1/attachments/%s/content%s" % (attachment_id, query), user=user) class Base(unittest.TestCase): @classmethod def setUpClass(cls): cls.alice, cls.bob, cls.eve = new_user("alice"), new_user("bob"), new_user("eve") cls.chat = new_chat(cls.alice, cls.bob) def assert_error(self, result, status, code): got, body, headers = result self.assertEqual(got, status) self.assertEqual(body["error"]["code"], code) self.assertEqual(body["error"]["request_id"], headers["X-Request-Id"]) class UploadTest(Base): def test_upload_and_download_real_images(self): for name, (content_type, data) in REAL_IMAGES.items(): with self.subTest(name): status, attachment, _ = upload(self.alice, self.chat, data, content_type, name) self.assertEqual(status, 201) self.assertEqual(attachment["content_type"], content_type) self.assertEqual(attachment["sha256"], hashlib.sha256(data).hexdigest()) got, body, headers = content(self.bob, attachment["id"]) self.assertEqual(got, 200) self.assertEqual(body, data) self.assertEqual(headers.get_content_type(), content_type) def test_attachment_fields(self): status, attachment, _ = upload(self.alice, self.chat, FIXTURE, filename="отчёт 👋.png") self.assertEqual(status, 201) self.assertEqual(attachment["status"], "ready") self.assertEqual(attachment["variants"], []) self.assertIsNone(attachment["error_code"]) self.assertEqual(attachment["filename"], "отчёт 👋.png") self.assertEqual(attachment["owner_id"], self.alice["id"]) self.assertEqual(attachment["size_bytes"], len(FIXTURE)) self.assertEqual(call("GET", "/api/v1/attachments/" + attachment["id"], user=self.bob)[1], attachment) def test_download_headers(self): _, attachment, _ = upload(self.alice, self.chat, FIXTURE, filename="отчёт.png") _, _, headers = content(self.alice, attachment["id"]) self.assertEqual(headers["X-Content-Type-Options"], "nosniff") self.assertIn("filename*=UTF-8''", headers["Content-Disposition"]) self.assertEqual(headers["Content-Length"], str(len(FIXTURE))) def test_file_of_exactly_10_mib(self): data = noisy_png(10 * MIB) self.assertEqual(len(data), 10 * MIB) status, attachment, _ = upload(self.alice, self.chat, data) self.assertEqual(status, 201) got, body, _ = content(self.bob, attachment["id"]) self.assertEqual((got, hashlib.sha256(body).hexdigest()), (200, attachment["sha256"])) def test_one_byte_over_10_mib(self): self.assert_error(upload(self.alice, self.chat, noisy_png(10 * MIB + 1)), 413, "payload_too_large") class LimitsTest(Base): def test_too_large_with_length(self): self.assert_error(upload(self.alice, self.chat, b"\x89PNG" + b"x" * (10 * MIB)), 413, "payload_too_large") def test_too_large_for_balancer(self): self.assert_error(upload(self.alice, self.chat, b"x" * (12 * MIB)), 413, "payload_too_large") def test_too_large_chunked(self): # без Content-Length размер известен только по ходу чтения address = urllib.parse.urlsplit(BASE_URL) path = "/api/v1/chats/%s/attachments?filename=a.png" % self.chat["id"] head = ("POST %s HTTP/1.1\r\nHost: x\r\nContent-Type: image/png\r\nAuthorization: Bearer %s\r\n" "Transfer-Encoding: chunked\r\n\r\n" % (path, self.alice["token"])).encode() with socket.create_connection((address.hostname, address.port), timeout=15) as connection: connection.sendall(head) block = b"x" * MIB try: for _ in range(12): connection.sendall(b"100000\r\n" + block + b"\r\n") connection.sendall(b"0\r\n\r\n") except OSError: pass # сервер закрыл соединение, не дочитав: это и нужно self.assertIn(b" 413 ", connection.recv(4096).split(b"\r\n")[0]) def test_decompression_bomb(self): data = make_png(50000, 50000, b"\x00" * 1000) self.assert_error(upload(self.alice, self.chat, data), 413, "payload_too_large") def test_pixels_over_limit_with_small_file(self): self.assert_error(upload(self.alice, self.chat, make_png(8000, 8000, b"\x00" * 100)), 413, "payload_too_large") def test_empty_file(self): self.assert_error(upload(self.alice, self.chat, b""), 415, "unsupported_media_type") class ContentCheckTest(Base): def test_broken_images(self): png, jpeg = FIXTURE, REAL_IMAGES["jpeg_baseline"][1] flipped = bytearray(png) flipped[len(png) // 2] ^= 0xFF cases = { "текст": (b"not an image at all", "image/png"), "обрезанный PNG": (png[: len(png) // 2], "image/png"), "обрезанный JPEG": (jpeg[: len(jpeg) // 2], "image/jpeg"), "только сигнатура PNG": (png[:8], "image/png"), "только сигнатура JPEG": (jpeg[:3], "image/jpeg"), "испорченный байт в PNG": (bytes(flipped), "image/png"), "GIF": (b"GIF89a" + b"\x00" * 100, "image/png"), "SVG": (b"", "image/png"), } for name, (data, content_type) in cases.items(): with self.subTest(name): self.assert_error(upload(self.alice, self.chat, data, content_type), 415, "unsupported_media_type") def test_mime_spoofing(self): png, jpeg = FIXTURE, REAL_IMAGES["jpeg_baseline"][1] self.assert_error(upload(self.alice, self.chat, png, "image/jpeg"), 415, "unsupported_media_type") self.assert_error(upload(self.alice, self.chat, jpeg, "image/png"), 415, "unsupported_media_type") def test_unsupported_content_type(self): for content_type in ("image/gif", "text/html", "application/octet-stream", "multipart/form-data"): with self.subTest(content_type): self.assert_error(upload(self.alice, self.chat, FIXTURE, content_type), 415, "unsupported_media_type") def test_extension_is_not_trusted(self): status, attachment, _ = upload(self.alice, self.chat, FIXTURE, filename="evil.exe") self.assertEqual((status, attachment["content_type"]), (201, "image/png")) class FilenameTest(Base): def test_bad_filenames(self): names = ("", " ", "a/b.png", "../../etc/passwd", "a\\b.png", "a\x00b.png", "a\nb.png", "a\tb.png", "a\x7fb.png", ".", "..", "x" * 256, "photo‮gnp.exe") for name in names: with self.subTest(name=ascii(name[:20])): self.assert_error(upload(self.alice, self.chat, FIXTURE, filename=name), 400, "invalid_request") def test_missing_filename(self): self.assert_error(upload(self.alice, self.chat, FIXTURE, filename=None), 400, "invalid_request") def test_good_filenames(self): for name in ("a", "x" * 255, "я" * 255, "two words.png", "семья 👨‍👩‍👧.jpg", ".hidden.png", "'; DROP TABLE users; --.png"): with self.subTest(name=name[:20]): status, attachment, _ = upload(self.alice, self.chat, FIXTURE, filename=name) self.assertEqual((status, attachment["filename"]), (201, name)) class AccessTest(Base): @classmethod def setUpClass(cls): super().setUpClass() _, cls.attachment, _ = upload(cls.alice, cls.chat, FIXTURE) cls.other_chat = new_chat(cls.eve) def test_outsider_gets_404(self): attachment_id = self.attachment["id"] self.assert_error(call("GET", "/api/v1/attachments/" + attachment_id, user=self.eve), 404, "not_found") self.assertEqual(content(self.eve, attachment_id)[0], 404) def test_outsider_looks_the_same_as_missing(self): _, foreign, _ = call("GET", "/api/v1/attachments/" + self.attachment["id"], user=self.eve) _, missing, _ = call("GET", "/api/v1/attachments/" + str(uuid.uuid4()), user=self.eve) self.assertEqual(foreign["error"]["message"], missing["error"]["message"]) def test_no_token(self): self.assertEqual(call("GET", "/api/v1/attachments/" + self.attachment["id"])[0], 401) self.assertEqual(content({"token": "x" * 43}, self.attachment["id"])[0], 401) self.assertEqual(upload({"token": "x" * 43}, self.chat, FIXTURE)[0], 401) def test_outsider_cannot_upload(self): self.assert_error(upload(self.eve, self.chat, FIXTURE), 404, "not_found") def test_bad_ids(self): self.assertEqual(call("GET", "/api/v1/attachments/123", user=self.alice)[0], 400) self.assertEqual(call("GET", "/api/v1/attachments/urn:uuid:" + self.attachment["id"], user=self.alice)[0], 400) def test_download_never_redirects(self): status, body, headers = content(self.bob, self.attachment["id"]) self.assertEqual(status, 200) self.assertIsNone(headers.get("Location")) self.assertNotIn(b"X-Amz", body[:200]) def test_variants(self): attachment_id = self.attachment["id"] self.assertEqual(content(self.bob, attachment_id, "?variant=original")[0], 200) self.assertEqual(content(self.bob, attachment_id, "?variant=thumbnail")[0], 404) self.assertEqual(content(self.bob, attachment_id, "?variant=nonsense")[0], 400) self.assertEqual(content(self.eve, attachment_id, "?variant=nonsense")[0], 404) class MessageLinkTest(Base): def test_message_with_attachments(self): ids = [upload(self.alice, self.chat, FIXTURE)[1]["id"] for _ in range(2)] status, message, _ = send(self.alice, self.chat, "смотри", attachment_ids=ids) self.assertEqual((status, message["attachment_ids"]), (201, ids)) items, _ = history(self.bob, self.chat, 100) self.assertIn(message, items) def test_attachment_only_message(self): attachment_id = upload(self.alice, self.chat, FIXTURE)[1]["id"] status, message, _ = send(self.bob, self.chat, "", attachment_ids=[attachment_id]) self.assertEqual((status, message["text"]), (201, "")) def test_text_message_has_empty_list(self): self.assertEqual(send(self.alice, self.chat, "текст")[1]["attachment_ids"], []) def test_empty_message_without_attachments(self): self.assert_error(send(self.alice, self.chat, "", attachment_ids=[]), 400, "invalid_request") self.assert_error(send(self.alice, self.chat, " ", attachment_ids=[]), 400, "invalid_request") def test_foreign_attachment(self): foreign_chat = new_chat(self.eve) foreign = upload(self.eve, foreign_chat, FIXTURE)[1]["id"] self.assert_error(send(self.alice, self.chat, "чужое", attachment_ids=[foreign]), 404, "not_found") def test_attachment_of_my_other_chat(self): other = new_chat(self.alice) attachment_id = upload(self.alice, other, FIXTURE)[1]["id"] self.assert_error(send(self.alice, self.chat, "не тот чат", attachment_ids=[attachment_id]), 404, "not_found") def test_unknown_attachment(self): self.assert_error(send(self.alice, self.chat, "нет", attachment_ids=[str(uuid.uuid4())]), 404, "not_found") def test_failed_link_saves_nothing_and_frees_key(self): key = new_key() attachment_id = upload(self.alice, self.chat, FIXTURE)[1]["id"] self.assertEqual(send(self.alice, self.chat, "повтор", key=key, attachment_ids=[str(uuid.uuid4())])[0], 404) self.assertEqual(send(self.alice, self.chat, "повтор", key=key, attachment_ids=[attachment_id])[0], 201) def test_limits(self): one = str(uuid.uuid4()) self.assert_error(send(self.alice, self.chat, "x", attachment_ids=[one, one]), 400, "invalid_request") many = [str(uuid.uuid4()) for _ in range(11)] self.assert_error(send(self.alice, self.chat, "x", attachment_ids=many), 400, "invalid_request") self.assert_error(send(self.alice, self.chat, "x", attachment_ids=["abc"]), 400, "invalid_request") def test_ten_attachments(self): ids = [upload(self.alice, self.chat, FIXTURE)[1]["id"] for _ in range(10)] self.assertEqual(send(self.alice, self.chat, "десять", attachment_ids=ids)[1]["attachment_ids"], ids) def test_idempotent_repeat(self): attachment_id = upload(self.alice, self.chat, FIXTURE)[1]["id"] key = new_key() first = send(self.alice, self.chat, "с файлом", key=key, attachment_ids=[attachment_id]) second = send(self.alice, self.chat, "с файлом", key=key, attachment_ids=[attachment_id]) self.assertEqual(first[1], second[1]) self.assertEqual(send(self.alice, self.chat, "с файлом", key=key)[0], 409) def test_missing_list_equals_empty_list(self): key = new_key() first = send(self.alice, self.chat, "без файла", key=key) second = send(self.alice, self.chat, "без файла", key=key, attachment_ids=[]) self.assertEqual(first[1], second[1]) def test_one_file_in_two_messages(self): attachment_id = upload(self.alice, self.chat, FIXTURE)[1]["id"] self.assertEqual(send(self.alice, self.chat, "первое", attachment_ids=[attachment_id])[0], 201) self.assertEqual(send(self.bob, self.chat, "второе", attachment_ids=[attachment_id])[0], 201) class ParallelLimitTest(Base): def test_too_many_parallel_uploads_get_429(self): address = urllib.parse.urlsplit(BASE_URL) path = "/api/v1/chats/%s/attachments?filename=a.png" % self.chat["id"] head = ("POST %s HTTP/1.1\r\nHost: x\r\nContent-Type: image/png\r\nContent-Length: 1000\r\n" "Authorization: Bearer %s\r\n\r\nabc" % (path, self.alice["token"])).encode() # начатые загрузки, которые ничего не досылают; на каждую реплику лимит 32 connections = [] answers = [] try: for _ in range(80): connection = socket.create_connection((address.hostname, address.port), timeout=5) connection.sendall(head) connections.append(connection) import time time.sleep(1.5) for connection in connections: connection.setblocking(False) try: answers.append(connection.recv(4096).split(b"\r\n")[0]) except BlockingIOError: pass finally: for connection in connections: connection.close() self.assertTrue(answers, "ни одна лишняя загрузка не получила ответ") self.assertTrue(all(b" 429 " in answer for answer in answers), answers[:3]) self.assertEqual(upload(self.alice, self.chat, FIXTURE)[0], 201) if __name__ == "__main__": unittest.main()