fix(auth): validate reset passwords and make the last-admin guard atomic, with tests (#5106)

This commit is contained in:
Sam Dornan
2026-10-04 10:18:15 -04:00
committed by GitHub
parent 8a243c71e9
commit 3204037797
9 changed files with 860 additions and 29 deletions

View File

@@ -303,6 +303,7 @@ class AuthHandler:
user_id: int,
data: dict[str, Any],
revoke_sessions_for: str | None = None,
keep_an_admin: bool = False,
) -> None:
"""
Write an update to a user, revoking that account's sessions around it.
@@ -311,15 +312,25 @@ class AuthHandler:
data (dict[str, Any]): The fields to write.
revoke_sessions_for (str | None): Username the sessions are keyed by,
or None to write without revoking.
keep_an_admin (bool): Refuse, with LastAdminError, a role change
that would leave no admin.
"""
from handler.database import db_user_handler
from handler.database.base_handler import sync_session
if revoke_sessions_for:
# Ahead of the write: an unreachable Redis then aborts the change
# rather than committing it with the account's sessions left live.
await RedisSessionMiddleware.clear_user_sessions(revoke_sessions_for)
with sync_session.begin() as session:
# First, so a refused update hasn't already logged the account out.
if keep_an_admin:
db_user_handler.refuse_removing_the_last_admin(
user_id, data, session=session
)
db_user_handler.update_user(user_id, data)
if revoke_sessions_for:
# Ahead of the write: an unreachable Redis then aborts the change
# rather than committing it with the account's sessions left live.
await RedisSessionMiddleware.clear_user_sessions(revoke_sessions_for)
db_user_handler.update_user(user_id, data, session=session)
if revoke_sessions_for:
# After it, for a login the old password was still good for. The