выполнение ЛР4 на 4

- S3: SeaweedFS 4.51 (MinIO-образы недоступны), приватный bucket,
  отдельные ограниченные ключи API, служебные порты закрыты;
  идемпотентный s3-init с проверкой приватности и прогревом записи
- вложения: потоковая загрузка PNG/JPEG, лимиты 10 MiB / 40 Мпикс,
  тип по содержимому, проверка filename, скачивание только участникам
- согласованность PG/S3: pending -> PUT -> ready, компенсация, sweeper
  с grace и dry-run; attachment_ids в сообщениях, FK по чату
- back-pressure на S3 PUT после найденного OOM SeaweedFS
- тесты student-tests, evidence, REPORT.md, DEMO.md
This commit is contained in:
Ilya
2026-10-28 18:06:07 +03:00
parent ec540a1a72
commit a97c35fc12
60 changed files with 4369 additions and 17 deletions
+154
View File
@@ -0,0 +1,154 @@
#!/usr/bin/env python3
"""Negative/positive suite for attachments (lab4, level 4). User errors must never become 500."""
import hashlib
import os
import uuid
from urllib.parse import quote
from stack import SAMPLE_JPG, SAMPLE_PNG, png, png_header_only, psql
from client import Client, arguments, check, run
LIMIT = 10 * 1024 * 1024
def main():
args = arguments("Attachment validation, ACL and linking through the LB")
c = Client(args.base_url, timeout=30)
alice, bob, eve, chat = c.bootstrap()
other = c.call("POST", "/api/v1/chats", 201, actor=alice, body={"title": "Другой чат", "member_ids": [alice["id"], eve["id"]]}, schema="Chat")["json"]
results = []
def case(name, fn):
fn()
results.append(name)
print("PASS", name)
def upload(data, ctype="image/png", filename="f.png", actor=alice, target=chat, expected=201):
path = "/api/v1/chats/" + target["id"] + "/attachments"
if filename is not None:
path += "?filename=" + quote(filename, safe="")
headers = {"Content-Type": ctype} if ctype else {}
r = c.call("POST", path, expected, actor=actor, raw=data, headers=headers, schema="Attachment" if expected == 201 else None)
return r["json"]
def post(body, actor=alice, target=chat, expected=201):
return c.call("POST", "/api/v1/chats/" + target["id"] + "/messages", expected, actor=actor, body=body,
headers={"Idempotency-Key": uuid.uuid4().hex}, schema="Message" if expected == 201 else None)["json"]
def download(att_id, actor=bob, suffix="/content"):
return c.call("GET", "/api/v1/attachments/" + att_id + suffix, actor=actor)
# --- positive -----------------------------------------------------------------------------
att_png = upload(SAMPLE_PNG, filename="фото 😀.png")
att_jpg = upload(SAMPLE_JPG, "image/jpeg", "sample.jpg")
def roundtrip():
check(att_png["filename"] == "фото 😀.png" and att_png["status"] == "ready" and att_png["variants"] == [], "png metadata")
check(att_jpg["content_type"] == "image/jpeg" and att_jpg["sha256"] == hashlib.sha256(SAMPLE_JPG).hexdigest(), "jpeg metadata")
for att, data, ctype in ((att_png, SAMPLE_PNG, "image/png"), (att_jpg, SAMPLE_JPG, "image/jpeg")):
d = download(att["id"])
check(d["bytes"] == data and d["headers"].get_content_type() == ctype, "download differs")
check(d["headers"].get("X-Content-Type-Options") == "nosniff", "nosniff missing")
meta = c.call("GET", "/api/v1/attachments/" + att["id"], actor=bob, schema="Attachment")["json"]
check(meta == att, "metadata via GET differs from upload response")
case("png+jpeg upload, member download is byte-identical, metadata stable", roundtrip)
def message_links():
m = post({"text": "", "attachment_ids": [att_jpg["id"], att_png["id"]]})
check(m["attachment_ids"] == [att_jpg["id"], att_png["id"]] and m["text"] == "", "order/text of attachment message")
plain = post({"text": "без вложений"})
check(plain["attachment_ids"] == [], "default attachment_ids must be []")
hist = c.call("GET", "/api/v1/chats/" + chat["id"] + "/messages?limit=100", actor=bob, schema="MessagePage")["json"]["items"]
by_id = {x["id"]: x for x in hist}
check(by_id[m["id"]]["attachment_ids"] == m["attachment_ids"] and by_id[plain["id"]]["attachment_ids"] == [], "history lost links")
post({"text": "второй раз то же вложение", "attachment_ids": [att_png["id"]]}, actor=bob)
case("message with only attachments, order kept, history shows links, re-use by other member", message_links)
def idempotency():
key = uuid.uuid4().hex
h = {"Idempotency-Key": key}
path = "/api/v1/chats/" + chat["id"] + "/messages"
a = c.call("POST", path, 201, actor=alice, body={"text": "x"}, headers=h, schema="Message")["json"]
b = c.call("POST", path, 201, actor=alice, body={"attachment_ids": [], "text": "x"}, headers=h, schema="Message")["json"]
check(a == b, "missing attachment_ids and [] must be the same request")
c.call("POST", path, 409, actor=alice, body={"text": "x", "attachment_ids": [att_png["id"]]}, headers=h)
case("idempotency: missing attachment_ids == [], other attachments -> 409", idempotency)
def boundary_pixels():
att = upload(png(8000, 5000, 0), filename="40mpx.png") # exactly 40 000 000 px, gray
check(att["size_bytes"] > 0, "boundary image rejected")
case("exactly 40 000 000 px accepted", boundary_pixels)
# --- size / pixels ------------------------------------------------------------------------
big = b"\x89PNG\r\n\x1a\n" + os.urandom(LIMIT + 1 - 8)
case("10 MiB + 1 byte with Content-Length -> 413", lambda: upload(big, expected=413))
def chunked():
def gen():
for i in range(0, len(big), 256 * 1024):
yield big[i:i + 256 * 1024]
upload(gen(), expected=413)
case("10 MiB + 1 byte chunked (no Content-Length) -> 413 while streaming", chunked)
case("decompression bomb 8000x5001 px (~1 KB/40 MB raw) -> 413", lambda: upload(png(8000, 5001, 0), expected=413))
case("RGB 20000x20000 header -> 413 before decode", lambda: upload(png_header_only(20000, 20000), expected=413))
# --- corrupted / MIME ---------------------------------------------------------------------
case("truncated PNG -> 415", lambda: upload(SAMPLE_PNG[:len(SAMPLE_PNG) // 2], expected=415))
flipped = bytearray(SAMPLE_PNG)
flipped[len(flipped) // 2] ^= 0xFF
case("PNG with damaged IDAT byte (CRC) -> 415", lambda: upload(bytes(flipped), expected=415))
case("truncated JPEG -> 415", lambda: upload(SAMPLE_JPG[:len(SAMPLE_JPG) // 2], "image/jpeg", expected=415))
case("PNG bytes declared image/jpeg -> 415", lambda: upload(SAMPLE_PNG, "image/jpeg", expected=415))
case("JPEG bytes declared image/png -> 415", lambda: upload(SAMPLE_JPG, "image/png", expected=415))
case("text declared image/png -> 415", lambda: upload(b"<?php echo 1; ?>" * 10, expected=415))
case("GIF declared image/png -> 415", lambda: upload(b"GIF89a" + bytes(100), expected=415))
case("SVG/HTML declared image/png -> 415", lambda: upload(b"<svg xmlns='http://www.w3.org/2000/svg'><script>1</script></svg>", expected=415))
case("PNG magic + garbage -> 415", lambda: upload(b"\x89PNG\r\n\x1a\n" + os.urandom(2000), expected=415))
case("empty body -> 415", lambda: upload(b"", expected=415))
case("Content-Type application/octet-stream -> 415", lambda: upload(SAMPLE_PNG, "application/octet-stream", expected=415))
case("Content-Type multipart/form-data -> 415", lambda: upload(SAMPLE_PNG, "multipart/form-data; boundary=x", expected=415))
# --- filename -----------------------------------------------------------------------------
for label, name in (("missing", None), ("path ../", "../etc/passwd.png"), ("slash", "a/b.png"), ("backslash", "a\\b.png"),
("NUL", "a\x00.png"), ("newline", "a\nb.png"), ("bidi RLO", "gpj‮.exe"), ("dot-dot", ".."),
("blank", " "), ("256 chars", "a" * 252 + ".png")):
case("filename %s -> 400" % label, lambda name=name: upload(SAMPLE_PNG, filename=name, expected=400))
# --- access -------------------------------------------------------------------------------
case("outsider uploads into chat -> 404", lambda: upload(SAMPLE_PNG, actor=eve, expected=404))
case("outsider metadata -> 404", lambda: c.call("GET", "/api/v1/attachments/" + att_png["id"], 404, actor=eve))
case("outsider content -> 404", lambda: c.call("GET", "/api/v1/attachments/" + att_png["id"] + "/content", 404, actor=eve))
case("unknown attachment -> 404", lambda: c.call("GET", "/api/v1/attachments/" + str(uuid.uuid4()), 404, actor=alice))
case("invalid attachment UUID -> 400", lambda: c.call("GET", "/api/v1/attachments/not-a-uuid", 400, actor=alice))
case("no session -> 401", lambda: c.call("GET", "/api/v1/attachments/" + att_png["id"] + "/content", 401))
case("variant thumbnail not produced in lab4 -> 404", lambda: c.call("GET", "/api/v1/attachments/" + att_png["id"] + "/content?variant=thumbnail", 404, actor=bob))
case("unknown variant -> 400", lambda: c.call("GET", "/api/v1/attachments/" + att_png["id"] + "/content?variant=raw", 400, actor=bob))
case("variant=original explicit -> 200", lambda: c.call("GET", "/api/v1/attachments/" + att_png["id"] + "/content?variant=original", 200, actor=bob))
# --- linking ------------------------------------------------------------------------------
foreign = upload(SAMPLE_PNG, actor=alice, target=other)
case("attachment of another chat (same author) -> 404", lambda: post({"text": "x", "attachment_ids": [foreign["id"]]}, expected=404))
case("outsider links chat attachment into own chat -> 404", lambda: post({"text": "x", "attachment_ids": [att_png["id"]]}, actor=eve, target=other, expected=404))
case("unknown attachment id in message -> 404", lambda: post({"text": "x", "attachment_ids": [str(uuid.uuid4())]}, expected=404))
case("11 attachment ids -> 400", lambda: post({"text": "x", "attachment_ids": [str(uuid.uuid4()) for _ in range(11)]}, expected=400))
case("duplicate attachment ids -> 400", lambda: post({"text": "x", "attachment_ids": [att_png["id"], att_png["id"]]}, expected=400))
case("blank text without attachments -> 400", lambda: post({"text": " "}, expected=400))
case("attachment_ids wrong type -> 400", lambda: post({"text": "x", "attachment_ids": "abc"}, expected=400))
def db_guard():
msg = post({"text": "db guard"})
sql = ("BEGIN; INSERT INTO message_attachments (message_id, attachment_id, chat_id, position) VALUES ('%s','%s','%s',0); ROLLBACK;"
% (msg["id"], foreign["id"], chat["id"]))
try:
psql(sql)
except RuntimeError as exc:
check("foreign key" in str(exc), "unexpected DB error")
return
check(False, "DB accepted a cross-chat attachment link")
case("DB composite FK rejects cross-chat link even bypassing API", db_guard)
print("PASS: %d attachment cases" % len(results))
if __name__ == "__main__":
run(main)