ЛР 7: Безопасность, mTLS и OpenBao
Skeleton contracts / contracts (pull_request) Blocked by required conditions

This commit is contained in:
2026-12-05 18:23:13 +03:00
parent 20afc2c790
commit e7d7bbbe77
66 changed files with 4865 additions and 81 deletions
+130
View File
@@ -0,0 +1,130 @@
#!/usr/bin/env python3
"""Exercise upload limits, image validation, attachment ACL, and message links."""
import hashlib
from pathlib import Path
import struct
import uuid
import zlib
from urllib.parse import quote
from client import Client, LAB, arguments, check, run
MAX_UPLOAD_BYTES = 10 * 1024 * 1024
def oversized_png_header(width, height):
def chunk(kind, data):
checksum = zlib.crc32(kind + data) & 0xFFFFFFFF
return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", checksum)
image_header = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)
return (
b"\x89PNG\r\n\x1a\n"
+ chunk(b"IHDR", image_header)
+ chunk(b"IDAT", zlib.compress(b"\x00"))
+ chunk(b"IEND", b"")
)
def main():
args = arguments("Validate attachment upload rules")
client = Client(args.base_url, args.ca_file)
alice, bob, eve, chat = client.bootstrap()
endpoint = "/api/v1/chats/" + chat["id"] + "/attachments?filename="
content = (Path(__file__).resolve().parent / "fixtures" / "sample.png").read_bytes()
uploaded = client.call(
"POST",
endpoint + quote("sample.png", safe=""),
201 if LAB == 4 else 202,
actor=alice,
raw=content,
headers={"Content-Type": "image/png"},
schema="Attachment",
)["json"]
if LAB == 4:
check(uploaded["status"] == "ready", "uploaded attachment is not ready")
check(uploaded["variants"] == [] and uploaded["error_code"] is None,
"Lab4 attachment must have no processing variants or error")
else:
check(uploaded["status"] == "queued", "Lab5 upload must initially be queued")
uploaded = client.wait_ready(alice, uploaded)
check(len(uploaded["variants"]) == 1
and uploaded["variants"][0]["name"] == "thumbnail",
"Lab5 attachment must have exactly one thumbnail")
check(uploaded["sha256"] == hashlib.sha256(content).hexdigest(),
"attachment checksum differs from original")
downloaded = client.call(
"GET",
"/api/v1/attachments/" + uploaded["id"] + "/content",
actor=bob,
)
check(downloaded["bytes"] == content, "download differs from uploaded bytes")
for suffix in ("", "/content"):
client.call("GET", "/api/v1/attachments/" + uploaded["id"] + suffix,
404, actor=eve)
def rejected(filename, data, content_type, status):
client.call(
"POST",
endpoint + quote(filename, safe=""),
status,
actor=alice,
raw=data,
headers={"Content-Type": content_type},
)
rejected("../sample.png", content, "image/png", 400)
rejected("mismatch.jpg", content, "image/jpeg", 415)
rejected("corrupt.png", b"\x89PNG\r\n\x1a\nnot-an-image", "image/png", 415)
rejected("too-large.png", b"x" * (MAX_UPLOAD_BYTES + 1), "image/png", 413)
rejected("too-many-pixels.png", oversized_png_header(20001, 2000),
"image/png", 413)
message = client.message(alice, chat, "", attachments=[uploaded["id"]])["json"]
check(message["attachment_ids"] == [uploaded["id"]],
"message did not retain attachment_ids")
history = client.call(
"GET",
"/api/v1/chats/" + chat["id"] + "/messages?limit=100",
actor=bob,
schema="MessagePage",
)["json"]["items"]
check(any(item["id"] == message["id"] and
item["attachment_ids"] == [uploaded["id"]] for item in history),
"message history did not return attachment_ids")
key = uuid.uuid4().hex
message_path = "/api/v1/chats/" + chat["id"] + "/messages"
first = client.call(
"POST", message_path, 201, actor=alice, body={"text": "semantic replay"},
headers={"Idempotency-Key": key}, schema="Message",
)["json"]
replay = client.call(
"POST", message_path, 201, actor=alice, body={"text": "semantic replay", "attachment_ids": []},
headers={"Idempotency-Key": key}, schema="Message",
)["json"]
check(replay == first, "missing attachment_ids and [] should be idempotently equivalent")
other_chat = client.call(
"POST",
"/api/v1/chats",
201,
actor=alice,
body={"title": "Different attachment scope", "member_ids": [alice["id"], bob["id"]]},
schema="Chat",
)["json"]
client.call(
"POST",
"/api/v1/chats/" + other_chat["id"] + "/messages",
404,
actor=alice,
body={"text": "", "attachment_ids": [uploaded["id"]]},
headers={"Idempotency-Key": uuid.uuid4().hex},
)
print("PASS: PNG upload/download, authorization, file limits, and message attachment links")
if __name__ == "__main__":
run(main)