forked from mtusi2026/mtusi2026lab4
fix(auth): avoid redundant password hashing in registration races
This commit is contained in:
@@ -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<home>/.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<home>/.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"
|
||||||
|
}
|
||||||
@@ -92,6 +92,15 @@ class PostgresRepository:
|
|||||||
return None, None
|
return None, None
|
||||||
return user_dto(row), row["password_hash"]
|
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(
|
def add_session(
|
||||||
self, *, digest: bytes, user_id: UUID, created_at, expires_at
|
self, *, digest: bytes, user_id: UUID, created_at, expires_at
|
||||||
):
|
):
|
||||||
|
|||||||
+18
-5
@@ -2,6 +2,7 @@ import base64
|
|||||||
import hashlib
|
import hashlib
|
||||||
import re
|
import re
|
||||||
import secrets
|
import secrets
|
||||||
|
import threading
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from uuid import UUID, uuid4
|
from uuid import UUID, uuid4
|
||||||
|
|
||||||
@@ -62,6 +63,7 @@ class MessengerService:
|
|||||||
self.repository = repository
|
self.repository = repository
|
||||||
self.passwords = passwords
|
self.passwords = passwords
|
||||||
self.session_ttl = session_ttl
|
self.session_ttl = session_ttl
|
||||||
|
self.registration_locks = [threading.Lock() for _ in range(64)]
|
||||||
|
|
||||||
def identify(self, *, value: str | None) -> User:
|
def identify(self, *, value: str | None) -> User:
|
||||||
token = bearer(value)
|
token = bearer(value)
|
||||||
@@ -74,11 +76,22 @@ class MessengerService:
|
|||||||
def register(
|
def register(
|
||||||
self, *, username: str, display_name: str, password: str
|
self, *, username: str, display_name: str, password: str
|
||||||
) -> User:
|
) -> User:
|
||||||
encoded = self.passwords.hash(password=password)
|
# Bound lock memory and avoid running Argon2 for every local duplicate.
|
||||||
return self.repository.add_user(
|
# Different replicas still race safely through PostgreSQL UNIQUE.
|
||||||
user=User(uuid4(), username, display_name, utcnow()),
|
stripe = hashlib.sha256(username.encode()).digest()[0] % 64
|
||||||
password_hash=encoded,
|
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):
|
def login(self, *, authorization: str | None):
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -34,7 +34,11 @@ def check(result: httpx.Response, status: int, schema: str) -> dict:
|
|||||||
401: "unauthorized",
|
401: "unauthorized",
|
||||||
404: "not_found",
|
404: "not_found",
|
||||||
409: "conflict",
|
409: "conflict",
|
||||||
|
413: "payload_too_large",
|
||||||
|
415: "unsupported_media_type",
|
||||||
|
429: "rate_limited",
|
||||||
500: "internal_error",
|
500: "internal_error",
|
||||||
|
503: "unavailable",
|
||||||
}[status]
|
}[status]
|
||||||
)
|
)
|
||||||
return payload
|
return payload
|
||||||
|
|||||||
@@ -92,9 +92,7 @@ def test_filename_guard(client, actors, chat, name):
|
|||||||
)
|
)
|
||||||
def test_image_guards(client, actors, chat, data, mime, status):
|
def test_image_guards(client, actors, chat, data, mime, status):
|
||||||
r = upload(client, actors[0], chat, data=data, mime=mime)
|
r = upload(client, actors[0], chat, data=data, mime=mime)
|
||||||
assert r.status_code == status
|
check(r, status, "Error")
|
||||||
assert r.headers["x-instance-id"] and r.headers["x-request-id"]
|
|
||||||
assert r.json()["error"]["request_id"] == r.headers["x-request-id"]
|
|
||||||
|
|
||||||
|
|
||||||
def test_jpeg_original_unchanged(client, actors, chat):
|
def test_jpeg_original_unchanged(client, actors, chat):
|
||||||
|
|||||||
Reference in New Issue
Block a user