diff --git a/app.py b/app.py index 7ba1314..a7d43e3 100644 --- a/app.py +++ b/app.py @@ -525,8 +525,14 @@ def validate_image_and_get_dimensions(data: bytes, content_type: str): if content_type == "image/png": if not data.startswith(b'\x89PNG\r\n\x1a\n'): raise HTTPException(status_code=415, detail="Invalid PNG signature") - if len(data) >= 24: - width, height = struct.unpack('>II', data[16:24]) + if len(data) < 33: + raise HTTPException(status_code=415, detail="Truncated PNG") + if data[12:16] != b'IHDR': + raise HTTPException(status_code=415, detail="PNG missing IHDR chunk") + ihdr_len = struct.unpack('>I', data[8:12])[0] + if ihdr_len != 13: + raise HTTPException(status_code=415, detail="Invalid IHDR length") + width, height = struct.unpack('>II', data[16:24]) elif content_type == "image/jpeg": if not data.startswith(b'\xff\xd8'): raise HTTPException(status_code=415, detail="Invalid JPEG signature") @@ -544,6 +550,9 @@ def validate_image_and_get_dimensions(data: bytes, content_type: str): else: raise HTTPException(status_code=415, detail="Unsupported media type") + if width == 0 or height == 0: + raise HTTPException(status_code=415, detail="Could not read image dimensions") + if width * height > 40_000_000: raise HTTPException(status_code=413, detail="Image exceeds 40 million pixels limit") @@ -569,7 +578,7 @@ async def upload_attachment( raise HTTPException(status_code=404, detail="Chat not found or inaccessible") conn.close() - if any(ord(c) < 0x20 or ord(c) == 0x7f for c in filename): + if any(ord(c) < 0x20 or ord(c) == 0x7f for c in filename) or '/' in filename or '\\' in filename: raise HTTPException(400, "Invalid filename") content_type = request.headers.get("content-type") @@ -577,7 +586,6 @@ async def upload_attachment( raise HTTPException(status_code=415, detail="Unsupported media type") MAX_UPLOAD = 10 * 1024 * 1024 - cl = request.headers.get("content-length") if cl is not None: try: @@ -595,12 +603,31 @@ async def upload_attachment( chunks.append(chunk) body_bytes = b"".join(chunks) + validate_image_and_get_dimensions(body_bytes, content_type) + size_bytes = len(body_bytes) sha256_hash = hashlib.sha256(body_bytes).hexdigest() attachment_id = str(uuid.uuid4()) now = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z") s3_key = f"attachments/{chat_id_str}/{attachment_id}/{filename}" + + conn = get_db_connection() + try: + with conn.cursor() as cur: + cur.execute(""" + INSERT INTO attachments (id, chat_id, owner_id, filename, content_type, size_bytes, sha256, status, variants, error_code, created_at, updated_at) + VALUES (%s, %s, %s, %s, %s, %s, %s, 'pending', '[]', NULL, %s, %s) + """, (attachment_id, chat_id_str, user["id"], filename, content_type, size_bytes, sha256_hash, now, now)) + conn.commit() + except Exception: + conn.rollback() + conn.close() + raise HTTPException(status_code=500, detail="Internal error initializing attachment") + finally: + if not conn.closed: + conn.close() + try: s3_client.put_object( Bucket=S3_BUCKET, @@ -615,15 +642,15 @@ async def upload_attachment( try: with conn.cursor() as cur: cur.execute(""" - INSERT INTO attachments (id, chat_id, owner_id, filename, content_type, size_bytes, sha256, status, variants, error_code, created_at, updated_at) - VALUES (%s, %s, %s, %s, %s, %s, %s, 'ready', '[]', NULL, %s, %s) - """, (attachment_id, chat_id_str, user["id"], filename, content_type, size_bytes, sha256_hash, now, now)) + UPDATE attachments SET status = 'ready', updated_at = %s WHERE id = %s + """, (now, attachment_id)) conn.commit() except Exception: conn.rollback() - raise HTTPException(status_code=500, detail="Internal error saving attachment metadata") + raise HTTPException(status_code=500, detail="Internal error finalizing attachment status") finally: - conn.close() + if not conn.closed: + conn.close() return { "id": attachment_id, diff --git a/compose.yaml b/compose.yaml index d695254..c6f6760 100644 --- a/compose.yaml +++ b/compose.yaml @@ -60,6 +60,10 @@ services: - SESSION_TTL_SECONDS=3600 - INSTANCE_ID=api-replica-1 - SECRET_HMAC_KEY=${SECRET_HMAC_KEY} + - S3_ENDPOINT=http://s3:8333 + - S3_BUCKET=messenger-attachments + - S3_ACCESS_KEY_ID=appuser + - S3_SECRET_ACCESS_KEY=appsecretpassword deploy: resources: limits: @@ -72,6 +76,8 @@ services: condition: service_healthy migrate: condition: service_completed_successfully + s3-init: + condition: service_completed_successfully healthcheck: test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8080/health/ready')"] interval: 5s @@ -87,6 +93,10 @@ services: - SESSION_TTL_SECONDS=3600 - INSTANCE_ID=api-replica-2 - SECRET_HMAC_KEY=${SECRET_HMAC_KEY} + - S3_ENDPOINT=http://s3:8333 + - S3_BUCKET=messenger-attachments + - S3_ACCESS_KEY_ID=appuser + - S3_SECRET_ACCESS_KEY=appsecretpassword deploy: resources: limits: @@ -99,6 +109,8 @@ services: condition: service_healthy migrate: condition: service_completed_successfully + s3-init: + condition: service_completed_successfully healthcheck: test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8080/health/ready')"] interval: 5s @@ -129,11 +141,9 @@ services: image: chrislusf/seaweedfs:latest command: "mini -dir=/data" environment: - - AWS_ACCESS_KEY_ID=minioadmin - - AWS_SECRET_ACCESS_KEY=minioadminpassword + - AWS_ACCESS_KEY_ID=appuser + - AWS_SECRET_ACCESS_KEY=appsecretpassword - S3_BUCKET=messenger-attachments - ports: - - "8333:8333" volumes: - s3data:/data deploy: @@ -147,6 +157,24 @@ services: timeout: 3s retries: 3 + s3-init: + image: python:3.11-slim + volumes: + - ./scripts:/scripts + - ./requirements.txt:/requirements.txt + environment: + - S3_ENDPOINT=http://s3:8333 + - S3_BUCKET=messenger-attachments + - S3_ROOT_ACCESS_KEY=appuser + - S3_ROOT_SECRET_KEY=appsecretpassword + command: > + sh -c "pip install --no-cache-dir -r /requirements.txt && + python /scripts/bootstrap_s3.py" + depends_on: + s3: + condition: service_healthy + restart: "no" + volumes: pgdata: redisdata: diff --git a/evidence/bucket_private.txt b/evidence/bucket_private.txt new file mode 100644 index 0000000..01e3dcc --- /dev/null +++ b/evidence/bucket_private.txt @@ -0,0 +1 @@ +PASS: anonymous GET -> 403 diff --git a/evidence/compose_ps.txt b/evidence/compose_ps.txt new file mode 100644 index 0000000..4d1a227 --- /dev/null +++ b/evidence/compose_ps.txt @@ -0,0 +1,7 @@ +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS +mtusi2026lab4-api-1-1 mtusi2026lab4-api-1 "uvicorn app:app --h…" api-1 15 minutes ago Up 12 minutes (healthy) 8080/tcp +mtusi2026lab4-api-2-1 mtusi2026lab4-api-2 "uvicorn app:app --h…" api-2 15 minutes ago Up 23 seconds (healthy) 8080/tcp +mtusi2026lab4-nginx-1 nginx:1.25.4-alpine "/docker-entrypoint.…" nginx 15 minutes ago Up 12 minutes (healthy) 0.0.0.0:8080->8080/tcp, [::]:8080->8080/tcp +mtusi2026lab4-postgres-1 postgres:15.5-alpine3.19 "docker-entrypoint.s…" postgres 15 minutes ago Up 12 minutes (healthy) 5432/tcp +mtusi2026lab4-redis-1 redis:7.2.4-alpine "docker-entrypoint.s…" redis 15 minutes ago Up 12 minutes (healthy) 6379/tcp +mtusi2026lab4-s3-1 chrislusf/seaweedfs:latest "/entrypoint.sh mini…" s3 15 minutes ago Up 12 minutes (healthy) 7333/tcp, 8080/tcp, 8333/tcp, 8888/tcp, 9333/tcp, 18080/tcp, 18888/tcp, 19333/tcp diff --git a/evidence/failover.txt b/evidence/failover.txt new file mode 100644 index 0000000..6cde139 --- /dev/null +++ b/evidence/failover.txt @@ -0,0 +1,4 @@ +python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:8080" +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 82, "failed_attempts": 2, "first_recovery_seconds": 1.312} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/level4.txt b/evidence/level4.txt new file mode 100644 index 0000000..7429115 --- /dev/null +++ b/evidence/level4.txt @@ -0,0 +1,17 @@ +test_corrupted_image_signature (__main__.TestMessengerLevel4Complete.test_corrupted_image_signature) +Ïðîâåðêà 415: áèòàÿ ñòðóêòóðà / íåâåðíûå ðàçìåðû PNG. ... ok +test_cross_chat_attachment_isolation (__main__.TestMessengerLevel4Complete.test_cross_chat_attachment_isolation) +Ïðîâåðêà 404: ïîïûòêà ïðèêðåïèòü âëîæåíèå èç ÷óæîãî ÷àòà. ... ok +test_invalid_filename_characters (__main__.TestMessengerLevel4Complete.test_invalid_filename_characters) +Ïðîâåðêà 400: íåäîïóñòèìûå ñèìâîëû (path traversal) â èìåíè ôàéëà. ... ok +test_nonexistent_attachment_id (__main__.TestMessengerLevel4Complete.test_nonexistent_attachment_id) +Ïðîâåðêà 404: ïåðåäà÷à íåñóùåñòâóþùåãî ID âëîæåíèÿ ïðè îòïðàâêå ñîîáùåíèÿ. ... ok +test_payload_too_large (__main__.TestMessengerLevel4Complete.test_payload_too_large) +Ïðîâåðêà 413: ôàéë ðàçìåðîì áîëüøå 10 MiB. ... ok +test_unsupported_media_type (__main__.TestMessengerLevel4Complete.test_unsupported_media_type) +Ïðîâåðêà 415: ïåðåäà÷à òåêñòîâîãî ôàéëà ïîä âèäîì êàðòèíêè. ... ok + +---------------------------------------------------------------------- +Ran 6 tests in 19.521s + +OK diff --git a/evidence/persistence.txt b/evidence/persistence.txt new file mode 100644 index 0000000..793b001 --- /dev/null +++ b/evidence/persistence.txt @@ -0,0 +1,3 @@ +python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8080" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/reconcile_dry_run.txt b/evidence/reconcile_dry_run.txt new file mode 100644 index 0000000..de093e2 --- /dev/null +++ b/evidence/reconcile_dry_run.txt @@ -0,0 +1,4 @@ + Container mtusi2026lab4-api-1-run-9e5b0d92d11b Creating + Container mtusi2026lab4-api-1-run-9e5b0d92d11b Created +Starting reconciliation (dry_run=True)... +Reconciliation completed successfully. diff --git a/evidence/smoke.txt b/evidence/smoke.txt new file mode 100644 index 0000000..1e7b9ff --- /dev/null +++ b/evidence/smoke.txt @@ -0,0 +1,2 @@ +python3 tests/smoke.py --base-url "http://localhost:8080" +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/upload_load.json b/evidence/upload_load.json new file mode 100644 index 0000000..7e96bac --- /dev/null +++ b/evidence/upload_load.json @@ -0,0 +1,7 @@ +CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS +de2d9b4cdf10 mtusi2026lab4-nginx-1 2.71% 2.355MiB / 128MiB 1.84% 389kB / 408kB 0B / 12.3kB 2 +bd85245e25be mtusi2026lab4-api-1-1 14.75% 74.22MiB / 256MiB 28.99% 518kB / 647kB 0B / 53.2kB 2 +dd34824ed0c5 mtusi2026lab4-api-2-1 13.03% 74.76MiB / 256MiB 29.20% 750kB / 879kB 0B / 53.2kB 2 +650e2ec7ac6a mtusi2026lab4-s3-1 1.02% 68.62MiB / 512MiB 13.40% 559kB / 248kB 0B / 1.09MB 8 +1938313cd5da mtusi2026lab4-redis-1 0.24% 3.16MiB / 256MiB 1.23% 99.6kB / 46.7kB 24.6kB / 12.3kB 5 +9348c37fb0f0 mtusi2026lab4-postgres-1 0.01% 34.39MiB / 512MiB 6.72% 679kB / 765kB 0B / 55.1MB 6 diff --git a/evidence/upload_load_stats.txt b/evidence/upload_load_stats.txt new file mode 100644 index 0000000..d2f737a --- /dev/null +++ b/evidence/upload_load_stats.txt @@ -0,0 +1,40 @@ +mtusi2026lab4-api-1-1 63.46MiB / 256MiB 0.20% +mtusi2026lab4-api-2-1 0B / 0B 0.00% +mtusi2026lab4-api-1-1 63.46MiB / 256MiB 0.20% +mtusi2026lab4-api-2-1 0B / 0B 0.00% +mtusi2026lab4-api-1-1 63.49MiB / 256MiB 0.39% +mtusi2026lab4-api-2-1 0B / 0B 0.00% +mtusi2026lab4-api-1-1 63.49MiB / 256MiB 0.24% +mtusi2026lab4-api-2-1 0B / 0B 0.00% +mtusi2026lab4-api-1-1 72.76MiB / 256MiB 7.76% +mtusi2026lab4-api-2-1 0B / 0B 0.00% +mtusi2026lab4-api-1-1 63.53MiB / 256MiB 0.22% +mtusi2026lab4-api-2-1 0B / 0B 0.00% +mtusi2026lab4-api-1-1 63.53MiB / 256MiB 0.20% +mtusi2026lab4-api-2-1 0B / 0B 0.00% +mtusi2026lab4-api-1-1 63.61MiB / 256MiB 22.90% +mtusi2026lab4-api-2-1 0B / 0B 0.00% +mtusi2026lab4-api-1-1 63.56MiB / 256MiB 0.20% +mtusi2026lab4-api-2-1 0B / 0B 0.00% +mtusi2026lab4-api-1-1 63.57MiB / 256MiB 50.53% +mtusi2026lab4-api-2-1 0B / 0B 0.00% +mtusi2026lab4-api-1-1 63.65MiB / 256MiB 14.81% +mtusi2026lab4-api-2-1 0B / 0B 0.00% +mtusi2026lab4-api-1-1 63.68MiB / 256MiB 0.22% +mtusi2026lab4-api-2-1 0B / 0B 0.00% +mtusi2026lab4-api-1-1 63.68MiB / 256MiB 0.30% +mtusi2026lab4-api-2-1 0B / 0B 0.00% +mtusi2026lab4-api-1-1 63.71MiB / 256MiB 0.21% +mtusi2026lab4-api-2-1 0B / 0B 0.00% +mtusi2026lab4-api-1-1 63.71MiB / 256MiB 0.20% +mtusi2026lab4-api-2-1 0B / 0B 0.00% +mtusi2026lab4-api-1-1 73.85MiB / 256MiB 14.64% +mtusi2026lab4-api-2-1 0B / 0B 0.00% +mtusi2026lab4-api-1-1 63.75MiB / 256MiB 0.22% +mtusi2026lab4-api-2-1 0B / 0B 0.00% +mtusi2026lab4-api-1-1 63.75MiB / 256MiB 0.24% +mtusi2026lab4-api-2-1 0B / 0B 0.00% +mtusi2026lab4-api-1-1 63.8MiB / 256MiB 27.08% +mtusi2026lab4-api-2-1 0B / 0B 0.00% +mtusi2026lab4-api-1-1 63.78MiB / 256MiB 0.24% +mtusi2026lab4-api-2-1 0B / 0B 0.00% diff --git a/scripts/bootstrap_s3.py b/scripts/bootstrap_s3.py new file mode 100644 index 0000000..72fc848 --- /dev/null +++ b/scripts/bootstrap_s3.py @@ -0,0 +1,29 @@ +import os +import boto3 +from botocore.client import Config + +S3_ENDPOINT = os.getenv("S3_ENDPOINT", "http://s3:8333") +S3_BUCKET = os.getenv("S3_BUCKET", "messenger-attachments") +S3_ROOT_ACCESS_KEY = os.getenv("S3_ROOT_ACCESS_KEY", "minioadmin") +S3_ROOT_SECRET_KEY = os.getenv("S3_ROOT_SECRET_KEY", "minioadminpassword") + +s3_client = boto3.client( + "s3", + endpoint_url=S3_ENDPOINT, + aws_access_key_id=S3_ROOT_ACCESS_KEY, + aws_secret_access_key=S3_ROOT_SECRET_KEY, + config=Config(signature_version="s3v4"), + region_name="us-east-1" +) + +def init_bucket(): + print(f"Checking/Creating bucket: {S3_BUCKET}") + try: + s3_client.head_bucket(Bucket=S3_BUCKET) + print("Bucket already exists.") + except Exception: + s3_client.create_bucket(Bucket=S3_BUCKET) + print("Bucket successfully created.") + +if __name__ == "__main__": + init_bucket() \ No newline at end of file diff --git a/scripts/check_bucket_private.py b/scripts/check_bucket_private.py new file mode 100644 index 0000000..dc66599 --- /dev/null +++ b/scripts/check_bucket_private.py @@ -0,0 +1,11 @@ +import os, sys, urllib.request, urllib.error +BUCKET = os.getenv("S3_BUCKET", "messenger-attachments") +KEY = os.getenv("S3_KEY") +URL = f"http://s3:8333/{BUCKET}/{KEY}" +try: + urllib.request.urlopen(URL) + print("FAIL: object accessible without auth"); sys.exit(1) +except urllib.error.HTTPError as e: + if e.code in (401, 403): + print(f"PASS: anonymous GET -> {e.code}"); sys.exit(0) + print(f"FAIL: unexpected {e.code}"); sys.exit(1) \ No newline at end of file diff --git a/scripts/reconcile.py b/scripts/reconcile.py new file mode 100644 index 0000000..64e3fba --- /dev/null +++ b/scripts/reconcile.py @@ -0,0 +1,81 @@ +import os +import sys +import boto3 +from botocore.client import Config +import psycopg2 +import psycopg2.extras +from datetime import datetime, timezone, timedelta + +DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://postgres:postgres@postgres:5432/messenger") +S3_ENDPOINT = os.getenv("S3_ENDPOINT", "http://s3:8333") +S3_BUCKET = os.getenv("S3_BUCKET", "messenger-attachments") +S3_ACCESS_KEY = os.getenv("S3_ACCESS_KEY_ID", "appuser") +S3_SECRET_KEY = os.getenv("S3_SECRET_ACCESS_KEY", "appsecretpassword") + +s3_client = boto3.client( + "s3", + endpoint_url=S3_ENDPOINT, + aws_access_key_id=S3_ACCESS_KEY, + aws_secret_access_key=S3_SECRET_KEY, + config=Config(signature_version="s3v4"), + region_name="us-east-1" +) + +def run_reconciliation(dry_run: bool = False): + print(f"Starting reconciliation (dry_run={dry_run})...") + conn = psycopg2.connect(DATABASE_URL, cursor_factory=psycopg2.extras.RealDictCursor) + + threshold = datetime.now(timezone.utc) - timedelta(hours=1) + + with conn.cursor() as cur: + cur.execute(""" + SELECT id, chat_id, filename, created_at FROM attachments + WHERE status = 'pending' AND created_at < %s + """, (threshold,)) + stale_pending = cur.fetchall() + + for row in stale_pending: + att_id = str(row['id']) + chat_id = str(row['chat_id']) + filename = row['filename'] + s3_key = f"attachments/{chat_id}/{att_id}/{filename}" + print(f"[DB Stale Pending] Found stale attachment {att_id}, marking as failed and cleaning S3.") + + if not dry_run: + try: + s3_client.delete_object(Bucket=S3_BUCKET, Key=s3_key) + except Exception as e: + print(f"Failed to delete S3 key {s3_key}: {e}") + with conn.cursor() as cur: + cur.execute("UPDATE attachments SET status = 'failed', error_code = 'timeout_stale' WHERE id = %s", (att_id,)) + conn.commit() + + with conn.cursor() as cur: + cur.execute("SELECT id, chat_id, filename FROM attachments") + all_db_attachments = cur.fetchall() + + valid_s3_keys = set() + for row in all_db_attachments: + valid_s3_keys.add(f"attachments/{str(row['chat_id'])}/{str(row['id'])}/{row['filename']}") + + paginator = s3_client.get_paginator('list_objects_v2') + for page in paginator.paginate(Bucket=S3_BUCKET, Prefix="attachments/"): + for obj in page.get('Contents', []): + key = obj['Key'] + last_modified = obj['LastModified'] + + if key not in valid_s3_keys and last_modified < threshold: + print(f"[S3 Orphan] Found orphan S3 object {key} (Modified: {last_modified})") + if not dry_run: + try: + s3_client.delete_object(Bucket=S3_BUCKET, Key=key) + print(f"Deleted orphan object {key} from S3.") + except Exception as e: + print(f"Failed to delete orphan {key}: {e}") + + conn.close() + print("Reconciliation completed successfully.") + +if __name__ == "__main__": + dry_run_mode = "--dry-run" in sys.argv + run_reconciliation(dry_run=dry_run_mode) \ No newline at end of file diff --git a/scripts/upload_load_test.py b/scripts/upload_load_test.py new file mode 100644 index 0000000..cdd2e2e --- /dev/null +++ b/scripts/upload_load_test.py @@ -0,0 +1,54 @@ +import base64, concurrent.futures, json, pathlib, time, urllib.request, uuid + +BASE = "http://localhost:8080" + +def _req(method, path, body=None, headers=None, token=None): + h = {"Accept": "application/json"} + if token: h["Authorization"] = f"Bearer {token}" + h.update(headers or {}) + req = urllib.request.Request(BASE + path, data=body, headers=h, method=method) + with urllib.request.urlopen(req) as r: + return r.status, r.read() + +def register_and_login(): + u = f"u_{uuid.uuid4().hex[:10]}" + p = "T3st-" + uuid.uuid4().hex + _req("POST", "/api/v1/users", + json.dumps({"username": u, "display_name": "Load", "password": p}).encode(), + {"Content-Type": "application/json"}) + basic = base64.b64encode(f"{u}:{p}".encode()).decode() + _, body = _req("POST", "/api/v1/auth/sessions", + headers={"Authorization": f"Basic {basic}"}) + return json.loads(body)["token"] + +def make_chat(token): + _, me = _req("GET", "/api/v1/users/me", token=token) + uid = json.loads(me)["id"] + _, chat = _req("POST", "/api/v1/chats", + json.dumps({"title": "load", "member_ids": [uid]}).encode(), + {"Content-Type": "application/json", "Idempotency-Key": uuid.uuid4().hex}, + token=token) + return json.loads(chat)["id"] + +def main(): + token = register_and_login() + chat = make_chat(token) + png = pathlib.Path("tests/fixtures/sample.png").read_bytes() + + def one(_): + try: + status, _ = _req("POST", + f"/api/v1/chats/{chat}/attachments?filename=f.png", + png, {"Content-Type": "image/png"}, token) + return status + except Exception as e: + return repr(e) + + t0 = time.monotonic() + with concurrent.futures.ThreadPoolExecutor(max_workers=10) as ex: + results = list(ex.map(one, range(10))) + print(json.dumps({"results": results, + "elapsed_s": round(time.monotonic() - t0, 3)})) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/tests/level4_tests.py b/tests/level4_tests.py new file mode 100644 index 0000000..38489a7 --- /dev/null +++ b/tests/level4_tests.py @@ -0,0 +1,139 @@ +import urllib.request +import urllib.error +import json +import unittest +from client import Client + +class TestMessengerLevel4Complete(unittest.TestCase): + def setUp(self): + self.client = Client() + self.alice = self.client.user("alice") + self.bob = self.client.user("bob") + self.eve = self.client.user("eve") + + res_chat1 = self.client.call( + 'POST', '/api/v1/chats', 201, + actor=self.alice, + body={"title": "Team Chat", "member_ids": [self.alice["id"], self.bob["id"]]}, + schema='Chat' + ) + self.chat1 = res_chat1['json'] + + res_chat2 = self.client.call( + 'POST', '/api/v1/chats', 201, + actor=self.eve, + body={"title": "Private Chat", "member_ids": [self.eve["id"], self.bob["id"]]}, + schema='Chat' + ) + self.chat2 = res_chat2['json'] + + def test_unsupported_media_type(self): + """Проверка 415: передача текÑ�тового файла под видом картинки.""" + fake_bytes = b"Just a simple plain text file content." + url = self.client.base + f"/api/v1/chats/{self.chat1['id']}/attachments?filename=notes.txt" + req = urllib.request.Request( + url, data=fake_bytes, + headers={"Authorization": f"Bearer {self.alice['token']}", "Content-Type": "image/png"}, + method="POST" + ) + try: + with urllib.request.urlopen(req) as resp: + self.fail("Expected HTTP 415") + except urllib.error.HTTPError as e: + self.assertEqual(e.code, 415) + + def test_payload_too_large(self): + """Проверка 413: файл размером больше 10 MiB.""" + large_data = b'\x89PNG\r\n\x1a\n' + b'\x00' * (10 * 1024 * 1024 + 100) + url = self.client.base + f"/api/v1/chats/{self.chat1['id']}/attachments?filename=huge.png" + req = urllib.request.Request( + url, data=large_data, + headers={"Authorization": f"Bearer {self.alice['token']}", "Content-Type": "image/png"}, + method="POST" + ) + try: + with urllib.request.urlopen(req) as resp: + self.fail("Expected HTTP 413") + except urllib.error.HTTPError as e: + self.assertEqual(e.code, 413) + + def test_corrupted_image_signature(self): + """Проверка 415: битаÑ� Ñ�труктура / неверные размеры PNG.""" + corrupted_png = b'\x89PNG\r\n\x1a\n' + b'\xFF' * 50 + url = self.client.base + f"/api/v1/chats/{self.chat1['id']}/attachments?filename=corrupt.png" + req = urllib.request.Request( + url, data=corrupted_png, + headers={"Authorization": f"Bearer {self.alice['token']}", "Content-Type": "image/png"}, + method="POST" + ) + try: + with urllib.request.urlopen(req) as resp: + self.fail("Expected HTTP 415") + except urllib.error.HTTPError as e: + self.assertEqual(e.code, 415) + + def test_invalid_filename_characters(self): + """Проверка 400: недопуÑ�тимые Ñ�имволы (path traversal) в имени файла.""" + valid_png = ( + b'\x89PNG\r\n\x1a\n' + b'\x00\x00\x00\rIHDR' + b'\x00\x00\x00\n\x00\x00\x00\n' + b'\x08\x02\x00\x00\x00' + b'\x02p\xdd\xb5' + b'\x00\x00\x00\x0fcIDATx\x9cc`\x00\x00\x00\x02\x00\x01H\xaf\xa4q' + b'\x00\x00\x00\x00IEND\xaeB`\x82' + ) + url = self.client.base + f"/api/v1/chats/{self.chat1['id']}/attachments?filename=../../etc/passwd" + req = urllib.request.Request( + url, data=valid_png, + headers={"Authorization": f"Bearer {self.alice['token']}", "Content-Type": "image/png"}, + method="POST" + ) + try: + with urllib.request.urlopen(req) as resp: + self.fail("Expected HTTP 400") + except urllib.error.HTTPError as e: + self.assertEqual(e.code, 400) + + def test_nonexistent_attachment_id(self): + """Проверка 404: передача неÑ�ущеÑ�твующего ID вложениÑ� при отправке Ñ�ообщениÑ�.""" + fake_uuid = "12345678-1234-4123-8123-123456789abc" + res = self.client.call( + 'POST', f"/api/v1/chats/{self.chat1['id']}/messages", 404, + actor=self.alice, + body={"text": "Check this out", "attachment_ids": [fake_uuid]}, + headers={"Idempotency-Key": "test-key-lvl4-1"} + ) + self.assertEqual(res['status'], 404) + + def test_cross_chat_attachment_isolation(self): + """Проверка 404: попытка прикрепить вложение из чужого чата.""" + valid_png = ( + b'\x89PNG\r\n\x1a\n' + b'\x00\x00\x00\rIHDR' + b'\x00\x00\x00\n\x00\x00\x00\n' + b'\x08\x02\x00\x00\x00' + b'\x02p\xdd\xb5' + b'\x00\x00\x00\x0fcID1Tx\x9cc`\x00\x00\x00\x02\x00\x01H\xaf\xa4q' + b'\x00\x00\x00\x00IEND\xaeB`\x82' + ) + url = self.client.base + f"/api/v1/chats/{self.chat2['id']}/attachments?filename=secret.png" + req = urllib.request.Request( + url, data=valid_png, + headers={"Authorization": f"Bearer {self.eve['token']}", "Content-Type": "image/png"}, + method="POST" + ) + with urllib.request.urlopen(req) as resp: + data = json.loads(resp.read().decode()) + eve_att_id = data["id"] + + res = self.client.call( + 'POST', f"/api/v1/chats/{self.chat1['id']}/messages", 404, + actor=self.alice, + body={"text": "Stealing file", "attachment_ids": [eve_att_id]}, + headers={"Idempotency-Key": "test-key-lvl4-2"} + ) + self.assertEqual(res['status'], 404) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file