#!/usr/bin/env python3 """Negative HTTP cases beyond the supplied smoke.""" import ssl import socket import sys import uuid from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tests")) from client import Client, arguments, run # noqa: E402 def main(): args = arguments(__doc__) client = Client(args.base_url, args.ca_file) alice, _, eve, chat = client.bootstrap() upload = f"/api/v1/chats/{chat['id']}/attachments?filename=probe.png" headers = {"Content-Type": "image/png"} client.call("POST", upload, 413, actor=alice, raw=b"x" * (10 * 1024 * 1024 + 1), headers=headers) client.call("POST", upload, 415, actor=alice, raw=b"not an image", headers=headers) attachment, _ = client.attachment(alice, chat) path = "/api/v1/attachments/" + attachment["id"] spoofed = {"X-User-Id": alice["id"]} for suffix in ("", "/content"): client.call("GET", path + suffix, 404, actor=eve, headers=spoofed) other = client.call("POST", "/api/v1/chats", 201, actor=eve, body={"title": "ACL probe", "member_ids": [eve["id"], alice["id"]]}, schema="Chat")["json"] client.call("POST", f"/api/v1/chats/{other['id']}/messages", 404, actor=eve, body={"text": "", "attachment_ids": [attachment["id"]]}, headers={"Idempotency-Key": uuid.uuid4().hex, **spoofed}) context = ssl.create_default_context(cafile=args.ca_file) from urllib.parse import urlsplit endpoint = urlsplit(client.base) with socket.create_connection((endpoint.hostname, endpoint.port or 443), timeout=5) as connection: try: with context.wrap_socket(connection, server_hostname="wrong.lab.invalid"): raise AssertionError("TLS accepted a wrong hostname") except ssl.SSLCertVerificationError as error: if error.verify_code != 62: raise print("PASS: upload size/MIME, forged identity, cross-chat attachment, wrong TLS hostname") if __name__ == "__main__": run(main)