import hashlib import io import struct import zlib from dataclasses import replace from urllib.parse import quote from uuid import uuid4 import psycopg import pytest from botocore.exceptions import ClientError from conftest import ROOT from PIL import Image from psycopg.rows import dict_row from test_api import error, headers from messenger.domain import DomainError from messenger.reconcile import Reconciler from messenger.storage import ObjectStore PNG = (ROOT / "tests/fixtures/sample.png").read_bytes() def upload(client, actor, room, content=PNG, media="image/png", name="sample.png"): return client.post( f"/api/v1/chats/{room['id']}/attachments?filename={quote(name, safe='')}", headers={**headers(actor), "Content-Type": media}, content=content, ) @pytest.fixture def admin(settings): store = ObjectStore( replace( settings, s3_access_key=(ROOT / "secrets/s3_root_user").read_text().strip(), s3_secret_key=(ROOT / "secrets/s3_root_password").read_text().strip(), ) ) yield store store.close() def age(conn, id): conn.execute( "UPDATE attachments SET updated_at=clock_timestamp()-interval '10 minutes' WHERE id=%s", (id,), ) def test_original_png_jpeg_and_acl(client, actors, room): jpg = io.BytesIO() Image.new("RGB", (17, 11), "red").save(jpg, format="JPEG") for data, media in ((PNG, "image/png"), (jpg.getvalue(), "image/jpeg")): response = upload(client, actors[0], room, data, media, "Фото 1.png") assert response.status_code == 201, response.text value = response.json() assert value["status"] == "ready" and value["variants"] == [] assert value["error_code"] is None and value["sha256"] == hashlib.sha256(data).hexdigest() assert value["size_bytes"] == len(data) and value["content_type"] == media assert "object_key" not in value path = "/api/v1/attachments/" + value["id"] assert client.get(path, headers=headers(actors[1])).json() == value download = client.get(path + "/content", headers=headers(actors[1])) assert download.status_code == 200 and download.content == data assert download.headers["Content-Type"] == media for suffix in ("", "/content"): error(client.get(path + suffix, headers=headers(actors[2])), 404, "not_found") error(client.get(path + suffix), 401, "unauthorized") error( client.get(path + "/content?variant=thumbnail", headers=headers(actors[0])), 400, "invalid_request", ) @pytest.mark.parametrize("name", ["", "../a.png", "a\\b.png", "a\x00.png", "\n.png", "x" * 256]) def test_unsafe_names(client, actors, room, name): error(upload(client, actors[0], room, name=name), 400, "invalid_request") @pytest.mark.parametrize( "data,media", [ (b"", "image/png"), (b"not a file", "image/png"), (PNG[:30], "image/png"), (PNG, "image/jpeg"), (PNG, "text/plain"), ], ) def test_invalid_images(client, actors, room, data, media): error(upload(client, actors[0], room, data, media), 415, "unsupported_media_type") def test_byte_limit_without_content_length_and_pixel_bomb(client, actors, room): error(upload(client, actors[0], room, b"x" * (10485760 + 1)), 413, "payload_too_large") stream = (b"x" * 1048576 for _ in range(11)) error(upload(client, actors[0], room, stream), 413, "payload_too_large") bomb = bytearray(PNG) bomb[16:24] = struct.pack(">II", 10000, 4001) bomb[29:33] = struct.pack(">I", zlib.crc32(bomb[12:29]) & 0xFFFFFFFF) error(upload(client, actors[0], room, bytes(bomb)), 413, "payload_too_large") def test_attachment_message_relations_and_replay(client, actors, room): att = upload(client, actors[0], room).json() path = f"/api/v1/chats/{room['id']}/messages" h = headers(actors[1]) body = {"text": "", "attachment_ids": [att["id"]]} result = client.post(path, headers=h, json=body) assert result.status_code == 201 and result.json()["attachment_ids"] == [att["id"]] assert client.post(path, headers=h, json=body).json() == result.json() assert client.get(path, headers=headers(actors[0])).json()["items"] == [result.json()] error( client.post( path, headers=headers(actors[0]), json={"text": "", "attachment_ids": [att["id"], att["id"]]}, ), 400, "invalid_request", ) error( client.post( path, headers=headers(actors[0]), json={"text": "", "attachment_ids": [str(uuid4())]} ), 404, "not_found", ) other = client.post( "/api/v1/chats", headers=headers(actors[0]), json={"title": "other", "member_ids": [actors[0]["id"]]}, ).json() error( client.post(f"/api/v1/chats/{other['id']}/messages", headers=headers(actors[0]), json=body), 404, "not_found", ) legacy = headers(actors[0]) one = client.post(path, headers=legacy, json={"text": "legacy"}) two = client.post(path, headers=legacy, json={"attachment_ids": [], "text": "legacy"}) assert one.status_code == 201 and two.json() == one.json() def test_scoped_credentials_cannot_delete_or_access_other_bucket(client, settings): store = ObjectStore(settings) try: with pytest.raises(ClientError) as denied: store.client.delete_object(Bucket=store.bucket, Key="originals/" + str(uuid4())) assert denied.value.response["ResponseMetadata"]["HTTPStatusCode"] == 403 with pytest.raises(ClientError) as denied: store.client.list_objects_v2(Bucket="unrelated-bucket") assert denied.value.response["ResponseMetadata"]["HTTPStatusCode"] == 403 finally: store.close() def test_db_failure_after_put_is_recoverable_with_dry_run( client, actors, room, settings, monkeypatch, admin ): repo = client.app.state.repository def fail(*args): raise RuntimeError("injected after durable PUT") monkeypatch.setattr(repo, "finish_attachment", fail) error(upload(client, actors[0], room), 500, "internal_error") store = client.app.state.attachments.store with psycopg.connect(settings.database_url, autocommit=True, row_factory=dict_row) as conn: row = conn.execute("SELECT * FROM attachments").fetchone() assert row["storage_state"] == "pending" and store.head(row["object_key"]) is not None assert Reconciler(conn, admin, apply=True).run()["counts"].get("restore_ready", 0) == 0 age(conn, row["id"]) dry = Reconciler(conn, admin).run() assert dry["counts"]["restore_ready"] == 1 assert ( conn.execute("SELECT storage_state FROM attachments").fetchone()["storage_state"] == "pending" ) assert Reconciler(conn, admin, apply=True).run()["counts"]["restore_ready"] == 1 assert Reconciler(conn, admin, apply=True).run()["actions"] == [] row = conn.execute("SELECT * FROM attachments").fetchone() assert row["storage_state"] == "ready" assert ( client.get( "/api/v1/attachments/" + str(row["id"]) + "/content", headers=headers(actors[1]) ).content == PNG ) def test_failed_put_and_missing_used_object(client, actors, room, settings, admin, monkeypatch): store = client.app.state.attachments.store original = store.put def fail(*args): raise DomainError(503, "unavailable", "injected transport failure") monkeypatch.setattr(store, "put", fail) error(upload(client, actors[0], room), 503, "unavailable") with psycopg.connect(settings.database_url, autocommit=True, row_factory=dict_row) as conn: pending = conn.execute("SELECT id FROM attachments").fetchone()["id"] age(conn, pending) assert Reconciler(conn, admin, apply=True).run()["counts"]["delete_abandoned_intent"] == 1 monkeypatch.setattr(store, "put", original) att = upload(client, actors[0], room).json() response = client.post( f"/api/v1/chats/{room['id']}/messages", headers=headers(actors[0]), json={"text": "used", "attachment_ids": [att["id"]]}, ) assert response.status_code == 201 row = conn.execute("SELECT * FROM attachments WHERE id=%s", (att["id"],)).fetchone() age(conn, row["id"]) admin.client.delete_object(Bucket=store.bucket, Key=row["object_key"]) assert Reconciler(conn, admin).run()["counts"]["mark_missing"] == 1 assert ( conn.execute("SELECT storage_state FROM attachments").fetchone()["storage_state"] == "ready" ) assert Reconciler(conn, admin, apply=True).run()["counts"]["mark_missing"] == 1 error( client.get("/api/v1/attachments/" + att["id"], headers=headers(actors[0])), 409, "conflict", ) assert conn.execute("SELECT count(*) AS n FROM message_attachments").fetchone()["n"] == 1 admin.put( row["object_key"], io.BytesIO(PNG), len(PNG), "image/png", hashlib.sha256(PNG).hexdigest(), ) assert Reconciler(conn, admin, apply=True).run()["counts"]["restore_ready"] == 1 assert ( client.get( "/api/v1/attachments/" + att["id"] + "/content", headers=headers(actors[0]) ).content == PNG )