diff --git a/evidence/clean-clone-first-failed.json b/evidence/clean-clone-first-failed.json new file mode 100644 index 00000000..4702e76d --- /dev/null +++ b/evidence/clean-clone-first-failed.json @@ -0,0 +1,11 @@ +{ + "date": "2026-11-15T18:42:26.500607+00:00", + "source_commit": "869cff59d9878ce98640a71382c79e5f94c2bf76", + "fresh_secrets_environment_and_volumes": true, + "command": "python scripts/verify_clean.py", + "fixed_host_port_for_restart": 49840, + "static_checks": ".venv/bin/python scripts/check_contract.py\nPASS: offline skeleton structure, local refs and Python syntax; application NOT tested.\n.venv/bin/python scripts/validate_openapi.py\nPASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.\n/.local/bin/uv run --locked ruff format src student-tests scripts/bootstrap.py scripts/stand.py scripts/cluster.py scripts/verify_clean.py scripts/verify_cluster.py scripts/verify_storage.py\n32 files left unchanged\n/.local/bin/uv run --locked ruff check src student-tests scripts/bootstrap.py scripts/stand.py scripts/cluster.py scripts/verify_clean.py scripts/verify_cluster.py scripts/verify_storage.py\nAll checks passed!", + "smoke": "python3 tests/smoke.py --base-url \"http://127.0.0.1:49840\" \nPASS: lab4 public API scenario; infrastructure and grading evidence remain separate.", + "failed_action": ".venv/bin/pytest student-tests -q", + "result": "INCOMPLETE" +} diff --git a/src/repository.py b/src/repository.py index d960d7d9..f9374231 100644 --- a/src/repository.py +++ b/src/repository.py @@ -92,6 +92,15 @@ class PostgresRepository: return None, None return user_dto(row), row["password_hash"] + def username_exists(self, *, username: str) -> bool: + with self.database.connection() as conn: + return ( + conn.execute( + "SELECT 1 FROM users WHERE username=%s", (username,) + ).fetchone() + is not None + ) + def add_session( self, *, digest: bytes, user_id: UUID, created_at, expires_at ): diff --git a/src/service.py b/src/service.py index 373741bd..6d5f5ace 100644 --- a/src/service.py +++ b/src/service.py @@ -2,6 +2,7 @@ import base64 import hashlib import re import secrets +import threading from datetime import timedelta from uuid import UUID, uuid4 @@ -62,6 +63,7 @@ class MessengerService: self.repository = repository self.passwords = passwords self.session_ttl = session_ttl + self.registration_locks = [threading.Lock() for _ in range(64)] def identify(self, *, value: str | None) -> User: token = bearer(value) @@ -74,11 +76,22 @@ class MessengerService: def register( self, *, username: str, display_name: str, password: str ) -> User: - encoded = self.passwords.hash(password=password) - return self.repository.add_user( - user=User(uuid4(), username, display_name, utcnow()), - password_hash=encoded, - ) + # Bound lock memory and avoid running Argon2 for every local duplicate. + # Different replicas still race safely through PostgreSQL UNIQUE. + stripe = hashlib.sha256(username.encode()).digest()[0] % 64 + lock = self.registration_locks[stripe] + if not lock.acquire(timeout=8): + raise DomainError(503, "unavailable", "Registration busy") + try: + if self.repository.username_exists(username=username): + raise DomainError(409, "conflict", "Username already exists") + encoded = self.passwords.hash(password=password) + return self.repository.add_user( + user=User(uuid4(), username, display_name, utcnow()), + password_hash=encoded, + ) + finally: + lock.release() def login(self, *, authorization: str | None): try: diff --git a/student-tests/conftest.py b/student-tests/conftest.py index c575c2a9..7c1f2dfb 100644 --- a/student-tests/conftest.py +++ b/student-tests/conftest.py @@ -34,7 +34,11 @@ def check(result: httpx.Response, status: int, schema: str) -> dict: 401: "unauthorized", 404: "not_found", 409: "conflict", + 413: "payload_too_large", + 415: "unsupported_media_type", + 429: "rate_limited", 500: "internal_error", + 503: "unavailable", }[status] ) return payload diff --git a/student-tests/test_attachments.py b/student-tests/test_attachments.py index eaad5bcf..183a4126 100644 --- a/student-tests/test_attachments.py +++ b/student-tests/test_attachments.py @@ -92,9 +92,7 @@ def test_filename_guard(client, actors, chat, name): ) def test_image_guards(client, actors, chat, data, mime, status): r = upload(client, actors[0], chat, data=data, mime=mime) - assert r.status_code == status - assert r.headers["x-instance-id"] and r.headers["x-request-id"] - assert r.json()["error"]["request_id"] == r.headers["x-request-id"] + check(r, status, "Error") def test_jpeg_original_unchanged(client, actors, chat):