Files
mtusi2026lab5/student-tests/attachments.py
T

263 lines
16 KiB
Python

#!/usr/bin/env python3
"""
Вложения (lab4, адаптировано под lab5): позитивный сценарий и негативный набор.
С lab5 успешный upload — 202 queued; байты и варианты доступны после ready.
python student-tests/attachments.py --base-url http://localhost:8080
Проверяет (каждый пользовательский промах — ожидаемый 4xx, НИКОГДА не 500):
- PNG и JPEG: 202 queued → ready, sha256/size, скачивание побайтно вторым участником,
истинный Content-Type, Unicode filename, 404 постороннему (метаданные и байты);
- слишком большой файл: по Content-Length (тело не отправляется), chunked без
Content-Length больше лимита, файл ровно в лимит проходит;
- «бомба» по пикселям (маленький файл, > 40 млн пикселей) → 413;
- повреждённые изображения (усечённый PNG/JPEG, битый CRC, мусор) → 415;
- подмена MIME (PNG как image/jpeg, JPEG как image/png, текст/GIF/HTML как png) → 415;
- опасные filename (/, \\, NUL, управляющие, bidi, .., пусто, > 255) → 400;
- чужое attachment_id / вложение другого чата / несуществующее → 404;
дубликаты, > 10 id, не-UUID → 400; пустой текст без вложений → 400;
- идемпотентность с attachment_ids: отсутствие поля == [], другой набор → 409.
"""
import argparse
import uuid
from urllib.parse import quote
from common import (SAMPLE_JPG, SAMPLE_PNG, Api, Report, make_png, raw_http, sha256,
upload, wait_ready)
LIMIT = 10 * 1024 * 1024
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--base-url", default="http://localhost:8080")
args = ap.parse_args()
api, r = Api(args.base_url), Report()
statuses: list[int] = []
def expect(st, want, name):
statuses.append(st)
want = want if isinstance(want, tuple) else (want,)
r.check(st in want, f"{name} -> {'/'.join(map(str, want))} (got {st})")
alice, bob, eve = api.new_user("att_a"), api.new_user("att_b"), api.new_user("att_e")
st, chat, _ = api.call("POST", "/api/v1/chats", {"title": "files", "member_ids": [alice["id"], bob["id"]]},
token=alice["token"])
st, other, _ = api.call("POST", "/api/v1/chats", {"title": "alice second", "member_ids": [alice["id"]]},
token=alice["token"])
st, evechat, _ = api.call("POST", "/api/v1/chats", {"title": "eve", "member_ids": [eve["id"]]},
token=eve["token"])
A, B, E = alice["token"], bob["token"], eve["token"]
mpath = f"/api/v1/chats/{chat['id']}/messages"
print("== позитивный сценарий ==")
st, png, _ = upload(api, A, chat["id"], SAMPLE_PNG, "sample.png")
expect(st, 202, "upload PNG")
r.check(png and png["status"] == "queued" and png["variants"] == [] and png["error_code"] is None,
"202 queued, variants=[], error_code=null")
r.check(png and png["sha256"] == sha256(SAMPLE_PNG) and png["size_bytes"] == len(SAMPLE_PNG),
"sha256/size describe the original")
r.check(png and "object_key" not in png and "bucket" not in str(png), "no S3 key/bucket in public JSON")
st, jpg, _ = upload(api, B, chat["id"], SAMPLE_JPG, "фото 📷 отпуск.jpg", "image/jpeg")
expect(st, 202, "upload JPEG with Unicode filename")
r.check(jpg and jpg["filename"] == "фото 📷 отпуск.jpg" and jpg["content_type"] == "image/jpeg",
"filename returned as is (display only)")
png_ready, jpg_ready = wait_ready(api, B, png["id"]), wait_ready(api, A, jpg["id"])
r.check(png_ready["status"] == jpg_ready["status"] == "ready", "both become ready (worker)")
st, raw, rh = api.raw("GET", f"/api/v1/attachments/{png['id']}/content", headers={"Authorization": "Bearer " + B})
expect(st, 200, "member downloads PNG")
r.check(raw == SAMPLE_PNG and sha256(raw) == png["sha256"], "downloaded bytes are identical (sha256)")
r.check(rh.get("Content-Type", "").startswith("image/png"), "true Content-Type image/png")
r.check(rh.get("X-Content-Type-Options") == "nosniff", "nosniff header")
st, raw, rh = api.raw("GET", f"/api/v1/attachments/{jpg['id']}/content?variant=original",
headers={"Authorization": "Bearer " + A})
expect(st, 200, "member downloads JPEG (variant=original)")
r.check(raw == SAMPLE_JPG and rh.get("Content-Type", "").startswith("image/jpeg"), "JPEG bytes and type")
st, msg, _ = api.call("POST", mpath, {"text": "", "attachment_ids": [png["id"], jpg["id"]]}, token=A)
expect(st, 201, "message with empty text + 2 attachments")
r.check(msg and msg["attachment_ids"] == [png["id"], jpg["id"]] and msg["text"] == "",
"attachment_ids kept in request order")
st, page, _ = api.call("GET", mpath, token=B)
got = [m for m in (page or {}).get("items", []) if m["id"] == (msg or {}).get("id")]
r.check(len(got) == 1 and got[0]["attachment_ids"] == [png["id"], jpg["id"]], "history shows attachment_ids")
st, plain, _ = api.call("POST", mpath, {"text": "просто текст"}, token=B)
r.check(st == 201 and plain["attachment_ids"] == [], "text-only POST (lab3) still valid, attachment_ids=[]")
st, meta, _ = api.call("GET", f"/api/v1/attachments/{png['id']}", token=B)
expect(st, 200, "member reads metadata")
st, raw, _ = api.raw("GET", f"/api/v1/attachments/{png['id']}/content?variant=thumbnail",
headers={"Authorization": "Bearer " + B})
expect(st, 200, "thumbnail variant exists in lab5")
st, _, _ = api.raw("GET", f"/api/v1/attachments/{png['id']}/content?variant=bogus",
headers={"Authorization": "Bearer " + B})
expect(st, 400, "unknown variant name")
print("== доступ ==")
for suffix in ("", "/content"):
st, _, _ = api.raw("GET", f"/api/v1/attachments/{png['id']}{suffix}", headers={"Authorization": "Bearer " + E})
expect(st, 404, f"outsider GET attachment{suffix or ' metadata'}")
st, _, _ = api.raw("GET", f"/api/v1/attachments/{uuid.uuid4()}{suffix}", headers={"Authorization": "Bearer " + A})
expect(st, 404, f"unknown attachment{suffix or ' metadata'}")
st, _, _ = api.raw("GET", f"/api/v1/attachments/{png['id']}/content")
expect(st, 401, "anonymous download")
st, _, _ = api.raw("GET", "/api/v1/attachments/not-a-uuid", headers={"Authorization": "Bearer " + A})
expect(st, 400, "attachment id is not a UUID")
st, _, _ = upload(api, E, chat["id"], SAMPLE_PNG)
expect(st, 404, "outsider uploads into foreign chat")
st, _, _ = api.raw("POST", f"/api/v1/chats/{chat['id']}/attachments?filename=a.png", SAMPLE_PNG,
{"Content-Type": "image/png"})
expect(st, 401, "anonymous upload")
print("== привязка к сообщению ==")
st, eve_att, _ = upload(api, E, evechat["id"], SAMPLE_PNG)
expect(st, 202, "eve uploads into her own chat")
st, other_att, _ = upload(api, A, other["id"], SAMPLE_PNG)
expect(st, 202, "alice uploads into her second chat")
st, _, _ = api.call("POST", mpath, {"text": "x", "attachment_ids": [eve_att["id"]]}, token=A)
expect(st, 404, "foreign attachment_id (eve's chat) in message")
st, _, _ = api.call("POST", mpath, {"text": "x", "attachment_ids": [other_att["id"]]}, token=A)
expect(st, 404, "attachment of ANOTHER chat of the same user")
st, _, _ = api.call("POST", mpath, {"text": "x", "attachment_ids": [str(uuid.uuid4())]}, token=A)
expect(st, 404, "unknown attachment_id")
st, _, _ = api.call("POST", mpath, {"text": "x", "attachment_ids": [png["id"], eve_att["id"]]}, token=A)
expect(st, 404, "one valid + one foreign -> whole message rejected")
st, _, _ = api.call("POST", mpath, {"text": "x", "attachment_ids": [png["id"], png["id"]]}, token=A)
expect(st, 400, "duplicate attachment_ids")
st, _, _ = api.call("POST", mpath, {"text": "x", "attachment_ids": [png["id"].upper(), png["id"]]}, token=A)
expect(st, 400, "same UUID in different case is a duplicate")
st, _, _ = api.call("POST", mpath, {"text": "x", "attachment_ids": [str(uuid.uuid4()) for _ in range(11)]}, token=A)
expect(st, 400, "more than 10 attachment_ids")
st, _, _ = api.call("POST", mpath, {"text": "x", "attachment_ids": ["nope"]}, token=A)
expect(st, 400, "attachment_id is not a UUID")
st, _, _ = api.call("POST", mpath, {"text": "x", "attachment_ids": None}, token=A)
expect(st, 400, "attachment_ids: null")
st, _, _ = api.call("POST", mpath, {"text": " ", "attachment_ids": []}, token=A)
expect(st, 400, "blank text without attachments")
st, _, _ = api.call("POST", mpath, {"attachment_ids": [png["id"]]}, token=A)
expect(st, 400, "text field is required")
st, m2, _ = api.call("POST", mpath, {"text": "ещё раз", "attachment_ids": [png["id"]]}, token=B)
expect(st, 201, "any member may reference a chat attachment again")
st, _, _ = api.call("POST", f"/api/v1/chats/{evechat['id']}/messages",
{"text": "x", "attachment_ids": [png["id"]]}, token=E)
expect(st, 404, "eve attaches alice's file to her own chat")
print("== идемпотентность с вложениями ==")
key = uuid.uuid4().hex
st, first, _ = api.call("POST", mpath, {"text": "idem"}, token=A, headers={"Idempotency-Key": key})
st2, again, _ = api.call("POST", mpath, {"attachment_ids": [], "text": "idem"}, token=A,
headers={"Idempotency-Key": key})
r.check(st == st2 == 201 and first == again, "missing attachment_ids == [] (same Message)")
st, _, _ = api.call("POST", mpath, {"text": "idem", "attachment_ids": [png["id"]]}, token=A,
headers={"Idempotency-Key": key})
expect(st, 409, "same key, different attachment_ids")
key2 = uuid.uuid4().hex
st, a1, _ = api.call("POST", mpath, {"text": "", "attachment_ids": [jpg["id"], png["id"]]}, token=A,
headers={"Idempotency-Key": key2})
st2, a2, _ = api.call("POST", mpath, {"attachment_ids": [jpg["id"], png["id"]], "text": ""}, token=A,
headers={"Idempotency-Key": key2})
r.check(st == st2 == 201 and a1 == a2, "replay with attachments returns the same Message")
print("== размер ==")
base = f"/api/v1/chats/{chat['id']}/attachments?filename=big.png"
auth = {"Authorization": "Bearer " + A, "Content-Type": "image/png"}
st, _ = raw_http(args.base_url, "POST", base, {**auth, "Content-Length": str(LIMIT + 1)})
expect(st, 413, "Content-Length = 10 MiB + 1, body never sent (limit before reading)")
st, _ = raw_http(args.base_url, "POST", base, {**auth, "Content-Length": str(50 * LIMIT)})
expect(st, 413, "Content-Length = 500 MiB")
st, _ = raw_http(args.base_url, "POST", base, auth, chunks=(b"\0" * 65536 for _ in range(200)), chunked=True)
expect(st, 413, "chunked body > 10 MiB without Content-Length (stream cut at limit)")
big = make_png(1900, 1900, noise=True) # ≈10,8 МБ настоящего PNG
# Шлём кусками и следим за ранним ответом: сервер отвечает 413 по Content-Length,
# не дочитывая тело, и urllib получил бы обрыв соединения вместо ответа.
st, _ = raw_http(args.base_url, "POST", base, {**auth, "Content-Length": str(len(big))},
chunks=(big[i:i + 65536] for i in range(0, len(big), 65536)))
expect(st, 413, f"real PNG of {len(big)} bytes")
fit = make_png(1860, 1870, noise=True)
if len(fit) <= LIMIT:
st, body, _ = upload(api, A, chat["id"], fit, "fits.png", timeout=120)
expect(st, 202, f"real PNG of {len(fit)} bytes (≤ 10 MiB) accepted")
if st == 202:
r.check(body["size_bytes"] == len(fit) and body["sha256"] == sha256(fit), "big file sha256/size")
st, _, _ = upload(api, A, chat["id"], b"")
expect(st, 400, "empty body")
print("== пиксели ==")
bomb = make_png(8000, 8000, color_type=0, rows=(b"\0" * 8001) * 8000) # 64 млн пикс., ~60 КБ
st, _, _ = upload(api, A, chat["id"], bomb, "bomb.png")
expect(st, 413, f"decompression bomb 8000x8000 in {len(bomb)} bytes")
wide = make_png(40001, 1001, color_type=0, rows=(b"\0" * 40002) * 1001) # 40 040 001 пикс.
st, _, _ = upload(api, A, chat["id"], wide, "wide.png")
expect(st, 413, "40 040 001 pixels (just over the limit)")
print("== повреждённые файлы ==")
corrupt = bytearray(SAMPLE_PNG)
corrupt[len(corrupt) // 2] ^= 0xFF
cases = [
("truncated PNG", SAMPLE_PNG[: len(SAMPLE_PNG) // 2], "image/png"),
("PNG with flipped byte (CRC)", bytes(corrupt), "image/png"),
("PNG signature only", SAMPLE_PNG[:8], "image/png"),
("PNG header + garbage", SAMPLE_PNG[:33] + b"\xde\xad\xbe\xef" * 100, "image/png"),
("truncated JPEG", SAMPLE_JPG[: len(SAMPLE_JPG) // 2], "image/jpeg"),
("JPEG SOI + garbage", b"\xff\xd8\xff\xe0" + b"\x00" * 300, "image/jpeg"),
]
for name, data, ctype in cases:
st, body, _ = upload(api, A, chat["id"], data, "x", ctype)
expect(st, 415, name)
r.check(not body or body["error"]["code"] == "unsupported_media_type", f"{name}: error code")
print("== fuzz: случайные порчи байтов (только 202 или 415, никогда 500) ==")
import random
rnd = random.Random(2026)
fuzz_codes = []
for data, ctype in ((SAMPLE_PNG, "image/png"), (SAMPLE_JPG, "image/jpeg")):
for _ in range(15):
d = bytearray(data)
for _ in range(rnd.randint(1, 4)):
d[rnd.randrange(8, len(d))] ^= 1 << rnd.randrange(8)
if rnd.random() < 0.3:
d = d[: rnd.randrange(16, len(d))]
st, _, _ = upload(api, A, chat["id"], bytes(d), "fuzz", ctype)
statuses.append(st)
fuzz_codes.append(st)
r.check(set(fuzz_codes) <= {202, 415},
f"30 corrupted files -> only 202/415 (got {sorted(set(fuzz_codes), key=str)})")
print("== подмена MIME ==")
spoof = [
("PNG bytes sent as image/jpeg", SAMPLE_PNG, "image/jpeg"),
("JPEG bytes sent as image/png", SAMPLE_JPG, "image/png"),
("text renamed to .png", b"just text, not an image\n" * 20, "image/png"),
("GIF sent as image/png", b"GIF89a\x01\x00\x01\x00\x80\x00\x00" + b"\x00" * 40, "image/png"),
("HTML sent as image/jpeg", b"<html><script>alert(1)</script></html>", "image/jpeg"),
("PNG with Content-Type image/gif", SAMPLE_PNG, "image/gif"),
("PNG with Content-Type application/octet-stream", SAMPLE_PNG, "application/octet-stream"),
("PNG with Content-Type text/html", SAMPLE_PNG, "text/html"),
]
for name, data, ctype in spoof:
st, _, _ = upload(api, A, chat["id"], data, "evil.png", ctype)
expect(st, 415, name)
print("== опасные filename ==")
bad_names = ["../../etc/passwd", "dir/a.png", "C:\\Windows\\a.png", "a\x00.png", "a\nb.png",
"tab\t.png", "\x1b[31mred.png", "photo\u202egnp.exe", "..", ".", " ", "x" * 256]
for name in bad_names:
st, _, _ = upload(api, A, chat["id"], SAMPLE_PNG, name)
expect(st, 400, f"filename {name[:20]!r}")
st, _, _ = api.raw("POST", f"/api/v1/chats/{chat['id']}/attachments", SAMPLE_PNG,
{"Authorization": "Bearer " + A, "Content-Type": "image/png"})
expect(st, 400, "filename missing")
st, ok, _ = upload(api, A, chat["id"], SAMPLE_PNG, "x" * 255)
expect(st, 202, "filename of exactly 255 chars")
st, ok, _ = upload(api, A, chat["id"], SAMPLE_PNG, "отчёт (финал) v2 — 100%.png")
expect(st, 202, "filename with spaces, Unicode and punctuation")
st, _, _ = api.raw("POST", f"/api/v1/chats/{chat['id']}/attachments?filename={quote('a.png')}", SAMPLE_PNG,
{"Authorization": "Bearer " + A, "Content-Type": "image/png; charset=binary"})
expect(st, 202, "media type parameters are ignored")
r.check(500 not in statuses and all(s is not None for s in statuses),
f"no 500 and no dropped connection among {len(statuses)} checked responses")
r.finish()
if __name__ == "__main__":
main()