feat: Саватеев А трек 4

This commit is contained in:
Enterxx2
2026-11-02 23:13:38 +03:00
parent 445cfe4789
commit 081d0fd2c8
76 changed files with 6387 additions and 77 deletions
+384
View File
@@ -0,0 +1,384 @@
"""
Вложения: загрузка, побайтное скачивание, доступ, лимиты и проверка содержимого.
Любая ошибка пользователя — 4xx в формате контракта, никогда не 5xx.
"""
import hashlib
import uuid
from urllib.parse import unquote
import httpx
import pytest
from helpers import (
FIXTURE_PNG,
MAX_IMAGE_PIXELS,
MAX_UPLOAD_BYTES,
assert_error,
assert_not_server_error,
auth,
bilevel_png,
jpeg_bytes,
png_bytes,
png_of_size,
upload_raw,
)
ATTACHMENT_FIELDS = {
"id",
"chat_id",
"owner_id",
"filename",
"content_type",
"size_bytes",
"sha256",
"status",
"variants",
"error_code",
"created_at",
"updated_at",
}
def download(api: httpx.Client, user: dict, attachment_id: str) -> httpx.Response:
return api.get(f"/api/v1/attachments/{attachment_id}/content", headers=auth(user))
@pytest.mark.parametrize(
("content", "content_type"),
[
(FIXTURE_PNG.read_bytes(), "image/png"),
(jpeg_bytes(640, 480), "image/jpeg"),
(jpeg_bytes(640, 480, progressive=True), "image/jpeg"),
],
ids=["png-fixture", "jpeg-baseline", "jpeg-progressive"],
)
def test_upload_and_download_byte_exact(
api: httpx.Client, make_user, make_chat, upload, content: bytes, content_type: str
) -> None:
alice = make_user("alice")
bob = make_user("bob")
chat = make_chat(alice, [alice, bob])
attachment = upload(alice, chat, content, content_type, "photo")
assert set(attachment) == ATTACHMENT_FIELDS
assert attachment["status"] == "ready"
assert attachment["variants"] == [] and attachment["error_code"] is None
assert attachment["chat_id"] == chat["id"] and attachment["owner_id"] == alice["id"]
assert attachment["content_type"] == content_type
assert attachment["size_bytes"] == len(content)
assert attachment["sha256"] == hashlib.sha256(content).hexdigest()
assert attachment["created_at"].endswith("Z")
metadata = api.get(f"/api/v1/attachments/{attachment['id']}", headers=auth(bob))
assert metadata.status_code == 200 and metadata.json() == attachment
response = download(api, bob, attachment["id"])
assert response.status_code == 200
assert response.content == content
assert response.headers["content-type"] == content_type
assert response.headers["content-length"] == str(len(content))
assert response.headers["x-content-type-options"] == "nosniff"
assert response.headers["X-Instance-Id"]
def test_public_api_does_not_expose_storage_details(
api: httpx.Client, make_user, make_chat, upload
) -> None:
alice = make_user("alice")
chat = make_chat(alice, [alice])
attachment = upload(alice, chat)
response = download(api, alice, attachment["id"])
exposed = (str(attachment) + str(dict(response.headers))).lower()
for marker in ("originals/", "s3:9000", "messenger-attachments", "amz"):
assert marker not in exposed
assert response.status_code == 200
assert not response.history, "download must not redirect to storage"
def test_unicode_filename_is_preserved(
api: httpx.Client, make_user, make_chat, upload
) -> None:
alice = make_user("alice")
chat = make_chat(alice, [alice])
name = "Фото отпуска 😀 «море» — 1.png"
attachment = upload(alice, chat, filename=name)
assert attachment["filename"] == name
disposition = download(api, alice, attachment["id"]).headers["content-disposition"]
assert disposition.startswith("inline; filename*=UTF-8''")
assert unquote(disposition.split("''", 1)[1]) == name
def test_outsider_and_unknown_get_indistinguishable_404(
api: httpx.Client, make_user, make_chat, upload
) -> None:
alice = make_user("alice")
eve = make_user("eve")
chat = make_chat(alice, [alice])
attachment = upload(alice, chat)
for suffix in ("", "/content"):
foreign = api.get(f"/api/v1/attachments/{attachment['id']}{suffix}", headers=auth(eve))
missing = api.get(f"/api/v1/attachments/{uuid.uuid4()}{suffix}", headers=auth(eve))
assert_error(foreign, 404, "not_found")
assert_error(missing, 404, "not_found")
assert foreign.json()["error"]["message"] == missing.json()["error"]["message"]
def test_outsider_cannot_upload_into_foreign_chat(
api: httpx.Client, make_user, make_chat
) -> None:
alice = make_user("alice")
eve = make_user("eve")
chat = make_chat(alice, [alice])
assert_error(upload_raw(api, eve, chat["id"], png_bytes()), 404, "not_found")
assert_error(upload_raw(api, eve, str(uuid.uuid4()), png_bytes()), 404, "not_found")
def test_authentication_is_required(api: httpx.Client, make_user, make_chat, upload) -> None:
alice = make_user("alice")
chat = make_chat(alice, [alice])
attachment = upload(alice, chat)
anonymous = {"token": "x" * 43}
for response in (
api.post(
f"/api/v1/chats/{chat['id']}/attachments",
params={"filename": "a.png"},
content=png_bytes(),
headers={"Content-Type": "image/png"},
),
api.get(f"/api/v1/attachments/{attachment['id']}"),
download(api, anonymous, attachment["id"]),
):
assert_error(response, 401, "unauthorized")
def test_variants_and_malformed_ids(api: httpx.Client, make_user, make_chat, upload) -> None:
alice = make_user("alice")
chat = make_chat(alice, [alice])
attachment = upload(alice, chat)
path = f"/api/v1/attachments/{attachment['id']}/content"
original = api.get(path, params={"variant": "original"}, headers=auth(alice))
assert original.status_code == 200
# Вариант из контракта, но в lab4 его нет у ready-вложения
missing = api.get(path, params={"variant": "thumbnail"}, headers=auth(alice))
assert_error(missing, 404, "not_found")
unknown = api.get(path, params={"variant": "../../etc"}, headers=auth(alice))
assert_error(unknown, 400, "invalid_request")
malformed = api.get("/api/v1/attachments/not-a-uuid", headers=auth(alice))
assert_error(malformed, 400, "invalid_request")
# --- Размер ---
def test_exactly_max_bytes_is_accepted(api: httpx.Client, make_user, make_chat, upload) -> None:
alice = make_user("alice")
chat = make_chat(alice, [alice])
content = png_of_size(MAX_UPLOAD_BYTES)
attachment = upload(alice, chat, content)
assert attachment["size_bytes"] == MAX_UPLOAD_BYTES
assert download(api, alice, attachment["id"]).content == content
def test_too_large_file_is_rejected_by_api(api: httpx.Client, make_user, make_chat) -> None:
"""10 MiB + 1 байт отклоняет реплика (X-Instance-Id), а не только LB"""
alice = make_user("alice")
chat = make_chat(alice, [alice])
response = upload_raw(api, alice, chat["id"], png_of_size(MAX_UPLOAD_BYTES + 1))
assert_error(response, 413, "payload_too_large")
assert response.headers["X-Instance-Id"]
def test_huge_body_is_rejected_by_balancer(api: httpx.Client, make_user, make_chat) -> None:
alice = make_user("alice")
chat = make_chat(alice, [alice])
response = upload_raw(api, alice, chat["id"], b"\x00" * (12 * 1024 * 1024))
assert_error(response, 413, "payload_too_large")
def test_empty_body_is_rejected(api: httpx.Client, make_user, make_chat) -> None:
alice = make_user("alice")
chat = make_chat(alice, [alice])
assert_error(upload_raw(api, alice, chat["id"], b""), 400, "invalid_request")
@pytest.mark.parametrize(
("width", "height", "status"),
[
(8000, 5000, 201),
(8000, 5001, 413),
(20000, 20000, 413),
],
)
def test_pixel_limit_applies_before_decoding(
api: httpx.Client, make_user, make_chat, width: int, height: int, status: int
) -> None:
"""Файл крошечный по байтам: лимит считается по заголовку, а не по размеру"""
alice = make_user("alice")
chat = make_chat(alice, [alice])
content = bilevel_png(width, height)
assert len(content) < 1024 * 1024
response = upload_raw(api, alice, chat["id"], content)
if status == 201:
assert width * height == MAX_IMAGE_PIXELS
assert response.status_code == 201, response.text
else:
assert_error(response, 413, "payload_too_large")
# --- Содержимое и MIME ---
def corrupted_payloads() -> list[tuple[str, bytes, str]]:
png = png_bytes(300, 200, noise=True)
jpeg = jpeg_bytes(300, 200)
flipped = bytearray(png)
flipped[len(png) // 2] ^= 0xFF
return [
("png-truncated", png[: len(png) // 2], "image/png"),
("png-bit-flip-crc", bytes(flipped), "image/png"),
("png-signature-garbage", png[:8] + b"\x00garbage" * 64, "image/png"),
("png-without-iend", png[:-12], "image/png"),
("jpeg-truncated", jpeg[: len(jpeg) // 2], "image/jpeg"),
("jpeg-signature-garbage", b"\xff\xd8\xff" + b"garbage" * 64, "image/jpeg"),
]
@pytest.mark.parametrize(
("name", "content", "content_type"),
corrupted_payloads(),
ids=[item[0] for item in corrupted_payloads()],
)
def test_corrupted_image_is_rejected(
api: httpx.Client, make_user, make_chat, name: str, content: bytes, content_type: str
) -> None:
alice = make_user("alice")
chat = make_chat(alice, [alice])
response = upload_raw(api, alice, chat["id"], content, content_type)
assert_not_server_error(response)
assert_error(response, 415, "unsupported_media_type")
@pytest.mark.parametrize(
("content", "content_type"),
[
(png_bytes(), "image/jpeg"),
(jpeg_bytes(), "image/png"),
(b"GIF89a\x01\x00\x01\x00\x00\x00\x00;", "image/png"),
(b"<svg xmlns='http://www.w3.org/2000/svg'/>", "image/png"),
(b"%PDF-1.7\n", "image/jpeg"),
(png_bytes(), "application/octet-stream"),
(png_bytes(), "image/gif"),
(png_bytes(), "text/html"),
(png_bytes(), None),
],
ids=[
"png-as-jpeg",
"jpeg-as-png",
"gif-as-png",
"svg-as-png",
"pdf-as-jpeg",
"octet-stream",
"image-gif",
"text-html",
"no-content-type",
],
)
def test_mime_spoofing_is_rejected(
api: httpx.Client, make_user, make_chat, content: bytes, content_type: str | None
) -> None:
alice = make_user("alice")
chat = make_chat(alice, [alice])
response = upload_raw(api, alice, chat["id"], content, content_type)
assert_not_server_error(response)
assert_error(response, 415, "unsupported_media_type")
def test_content_type_parameters_are_ignored(
api: httpx.Client, make_user, make_chat
) -> None:
alice = make_user("alice")
chat = make_chat(alice, [alice])
response = upload_raw(api, alice, chat["id"], png_bytes(), "Image/PNG; foo=bar")
assert response.status_code == 201, response.text
assert response.json()["content_type"] == "image/png"
# --- Имя файла ---
@pytest.mark.parametrize(
"filename",
[
"../../etc/passwd",
"dir/photo.png",
"C:\\photo.png",
"a\x00b.png",
"line\nbreak.png",
"tab\t.png",
"bell\x07.png",
"photo\u202egnp.exe",
"",
" ",
".",
"..",
"x" * 256,
],
ids=[
"traversal",
"slash",
"backslash",
"nul",
"newline",
"tab",
"control",
"bidi-override",
"empty",
"blank",
"dot",
"dotdot",
"too-long",
],
)
def test_dangerous_filename_is_rejected(
api: httpx.Client, make_user, make_chat, filename: str
) -> None:
alice = make_user("alice")
chat = make_chat(alice, [alice])
response = upload_raw(api, alice, chat["id"], png_bytes(), filename=filename)
assert_not_server_error(response)
assert_error(response, 400, "invalid_request")
def test_missing_filename_is_rejected(api: httpx.Client, make_user, make_chat) -> None:
alice = make_user("alice")
chat = make_chat(alice, [alice])
response = api.post(
f"/api/v1/chats/{chat['id']}/attachments",
content=png_bytes(),
headers={**auth(alice), "Content-Type": "image/png"},
)
assert_error(response, 400, "invalid_request")
def test_longest_filename_is_accepted(make_user, make_chat, upload) -> None:
alice = make_user("alice")
chat = make_chat(alice, [alice])
name = "я" * 251 + ".png"
assert upload(alice, chat, filename=name)["filename"] == name
def test_same_filename_gets_separate_attachments(
api: httpx.Client, make_user, make_chat, upload
) -> None:
"""filename — только отображение: одинаковые имена не перезаписывают файл"""
alice = make_user("alice")
chat = make_chat(alice, [alice])
first_content, second_content = png_bytes(10, 10), png_bytes(20, 20)
first = upload(alice, chat, first_content, filename="same.png")
second = upload(alice, chat, second_content, filename="same.png")
assert first["id"] != second["id"]
assert download(api, alice, first["id"]).content == first_content
assert download(api, alice, second["id"]).content == second_content