Fix auth bypasses, race condition, stale connections, and unbounded query
- Block disabled accounts at login and unimpersonate (C4, C2) - Catch IntegrityError on register commit to return 409 instead of 500 (C5) - Stop _seed_admin from overwriting existing admin password on restart (C8) - Cap public/stats query at 500 users to bound memory usage (C3) - Rate-limit PUT /me/password at 5/minute (C6) - Bump SQLAlchemy to 2.0.50 and aiomysql to 0.3.2; re-enable pool_pre_ping (C7) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@ import logging
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request, status
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from app.dependencies import get_db
|
||||
from app.limiter import limiter
|
||||
@@ -27,7 +28,11 @@ async def register(request: Request, body: UserCreate, db: AsyncSession = Depend
|
||||
display_name=body.display_name or body.email.split("@")[0],
|
||||
)
|
||||
db.add(user)
|
||||
await db.commit()
|
||||
try:
|
||||
await db.commit()
|
||||
except IntegrityError:
|
||||
await db.rollback()
|
||||
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Email already registered")
|
||||
await db.refresh(user)
|
||||
|
||||
logger.info("New user registered: %s", body.email)
|
||||
@@ -44,5 +49,9 @@ async def login(request: Request, body: LoginRequest, db: AsyncSession = Depends
|
||||
logger.warning("Failed login for: %s", body.email)
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid credentials")
|
||||
|
||||
if user.is_disabled:
|
||||
logger.warning("Login attempt on disabled account: %s", body.email)
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Account disabled")
|
||||
|
||||
logger.info("Login: %s (id=%s)", body.email, user.id)
|
||||
return Token(access_token=create_token(user.id))
|
||||
|
||||
Reference in New Issue
Block a user