Исправить обход отзыва сессии при сбое Valkey

resolve() больше не доверяет кэшу: валидность сессии всегда проверяется в PostgreSQL, поэтому неудавшаяся инвалидация при недоступном Valkey не воскрешает отозванный токен. Valkey остаётся read-through кэшем неизменяемого профиля. Добавлены scripts/check-revocation.py и student-tests/test_session_revocation.py.
This commit is contained in:
2026-10-23 19:46:34 +03:00
parent eeee24d3d7
commit c60c78b51e
18 changed files with 198 additions and 36 deletions
+6 -4
View File
@@ -213,14 +213,16 @@ def _idempotency_key(request: Request) -> str:
def current_user(request: Request, conn=Depends(get_conn)) -> Dict:
# Session validity is always checked against PostgreSQL; the cache may only
# serve the immutable user profile, never an access decision.
token = _bearer_token(request)
user_id = sessions.resolve(conn, token)
if user_id is None:
raise ApiError(401, "unauthorized", "Сессия недействительна", challenge=BEARER_CHALLENGE)
row = repo.get_user_by_id(conn, user_id)
if row is None:
user = sessions.cached_user(conn, user_id)
if user is None:
raise ApiError(401, "unauthorized", "Сессия недействительна", challenge=BEARER_CHALLENGE)
return row
return user
def _require_chat(conn, chat_id: str, user_id: str) -> str:
@@ -287,7 +289,7 @@ def revoke_session(
@app.get("/api/v1/users/me")
def current_user_endpoint(user: Dict = Depends(current_user)) -> dict:
return repo.user_json(user)
return user
# --------------------------------------------------------------------------- #