Complete Lab 4 attachment service
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Grade-4 attachment rejection checks; uses only the public HTTP contract."""
|
||||
import uuid
|
||||
import json
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments("Lab 4 grade-4 negative attachment checks")
|
||||
client = Client(args.base_url, args.ca_file)
|
||||
alice, bob, eve, chat = client.bootstrap()
|
||||
base_path = "/api/v1/chats/" + chat["id"] + "/attachments"
|
||||
valid_png = (Path(__file__).resolve().parents[1] / "tests/fixtures/sample.png").read_bytes()
|
||||
|
||||
too_large = b"\x00" * (10 * 1024 * 1024 + 1)
|
||||
oversized = client.call(
|
||||
"POST", base_path + "?filename=large.png", 413,
|
||||
actor=alice, raw=too_large, headers={"Content-Type": "image/png"},
|
||||
)
|
||||
corrupt = client.call(
|
||||
"POST", base_path + "?filename=broken.png", 415,
|
||||
actor=alice, raw=b"not a PNG", headers={"Content-Type": "image/png"},
|
||||
)
|
||||
spoofed = client.call(
|
||||
"POST", base_path + "?filename=spoofed.jpg", 415,
|
||||
actor=alice, raw=valid_png, headers={"Content-Type": "image/jpeg"},
|
||||
)
|
||||
unsafe_name = client.call(
|
||||
"POST", base_path + "?filename=..%2Foutside.png", 400,
|
||||
actor=alice, raw=valid_png, headers={"Content-Type": "image/png"},
|
||||
)
|
||||
|
||||
client.attachment(alice, chat)
|
||||
second_chat = client.call(
|
||||
"POST", "/api/v1/chats", 201, actor=bob,
|
||||
body={"title": "Другой чат", "member_ids": [bob["id"], eve["id"]]}, schema="Chat",
|
||||
)["json"]
|
||||
foreign_attachment, _ = client.attachment(bob, second_chat)
|
||||
foreign = client.call(
|
||||
"POST", "/api/v1/chats/" + chat["id"] + "/messages", 404,
|
||||
actor=alice,
|
||||
body={"text": "чужое вложение", "attachment_ids": [foreign_attachment["id"]]},
|
||||
headers={"Idempotency-Key": uuid.uuid4().hex},
|
||||
)
|
||||
unknown = client.call(
|
||||
"POST", "/api/v1/chats/" + chat["id"] + "/messages", 404,
|
||||
actor=alice,
|
||||
body={"text": "неизвестное вложение", "attachment_ids": [str(uuid.uuid4())]},
|
||||
headers={"Idempotency-Key": uuid.uuid4().hex},
|
||||
)
|
||||
summary = {
|
||||
"oversized_upload": oversized["status"],
|
||||
"corrupt_image": corrupt["status"],
|
||||
"mime_spoof": spoofed["status"],
|
||||
"unsafe_filename": unsafe_name["status"],
|
||||
"foreign_chat_attachment": foreign["status"],
|
||||
"unknown_attachment": unknown["status"],
|
||||
"result": "PASS",
|
||||
}
|
||||
evidence = Path(__file__).resolve().parents[1] / "evidence/grade4-attachments.json"
|
||||
evidence.parent.mkdir(parents=True, exist_ok=True)
|
||||
evidence.write_text(json.dumps(summary, indent=2) + "\n", encoding="utf-8")
|
||||
print(json.dumps(summary, separators=(",", ":")))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Reference in New Issue
Block a user