From ceedecea5b74e1654f68e4f1617ea8be8313a4fd Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Mon, 16 Nov 2026 21:13:33 -0500 Subject: [PATCH] fix(user): trust an admin-saved email only while the user still holds it Saving an unchanged email as an admin wrote email_trusted=True by id alone, so a user who changed their email between the admin's read and that write had the new address trusted. The trust now goes through a conditional UPDATE matching the address the admin submitted. Co-Authored-By: Claude Opus 5.5 --- backend/endpoints/user.py | 13 +++++++++-- backend/handler/database/users_handler.py | 16 ++++++++++++++ backend/tests/endpoints/test_user.py | 27 +++++++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/backend/endpoints/user.py b/backend/endpoints/user.py index b3190b67e..60d198a94 100644 --- a/backend/endpoints/user.py +++ b/backend/endpoints/user.py @@ -550,10 +550,16 @@ async def update_user( cleaned_data["email"] = new_email.lower() or None - # Saving a user's email as an admin vouches for it, even when unchanged. by_admin = is_scoped_admin(request, Scope.USERS_WRITE) - if new_email is not None or (by_admin and email and not db_user.email_trusted): + if new_email is not None: cleaned_data["email_trusted"] = by_admin + # Saving an unchanged email as an admin vouches for it, but only for the + # address the admin saw, in case the user changed it meanwhile. + vouch_for = ( + email + if by_admin and new_email is None and email and not db_user.email_trusted + else None + ) # You can't change your own role if form_data.role and request.user.id != id: @@ -649,6 +655,9 @@ async def update_user( db_client_token_handler.get_token_ids_by_users([id]) ) + if vouch_for: + db_user_handler.trust_email(id, vouch_for) + before = db_user db_user = db_user_handler.get_user(id) if not db_user: diff --git a/backend/handler/database/users_handler.py b/backend/handler/database/users_handler.py index 1f10195b9..3278c23e4 100644 --- a/backend/handler/database/users_handler.py +++ b/backend/handler/database/users_handler.py @@ -130,6 +130,22 @@ class DBUsersHandler(DBBaseHandler): ) return session.scalars(select(User).filter_by(id=id)).one() + @begin_session + def trust_email( + self, + id: int, + email: str, + session: Session = INJECTED_SESSION, + ) -> bool: + """Mark the user's email trusted only if it is still `email`.""" + query = self.filter(update(User), ids=[id], emails=[email]) + result = session.execute( + query.values(email_trusted=True).execution_options( + synchronize_session=False + ) + ) + return affected_rows(result) > 0 + @begin_session def set_ra_progression( self, diff --git a/backend/tests/endpoints/test_user.py b/backend/tests/endpoints/test_user.py index d59cd897b..345fb6184 100644 --- a/backend/tests/endpoints/test_user.py +++ b/backend/tests/endpoints/test_user.py @@ -731,6 +731,33 @@ def test_an_admin_saving_a_users_email_trusts_it( assert updated and updated.email_trusted +def test_an_admin_vouches_only_for_the_email_they_saved( + client, access_token: str, viewer_user: User, mocker +): + db_user_handler.update_user(viewer_user.id, {"email": "viewer@example.com"}) + stale = db_user_handler.get_user(viewer_user.id) + # The user changes their email after the admin's request has read the old one. + db_user_handler.update_user(viewer_user.id, {"email": "taken@example.com"}) + get_user = db_user_handler.get_user + reads = iter([stale]) + mocker.patch.object( + db_user_handler, + "get_user", + side_effect=lambda id: next(reads, None) or get_user(id), + ) + + response = client.put( + f"/api/users/{viewer_user.id}", + data={"email": "viewer@example.com"}, + headers=bearer(access_token), + ) + + assert response.status_code == status.HTTP_200_OK + current = get_user(viewer_user.id) + assert current and current.email == "taken@example.com" + assert not current.email_trusted + + def test_a_password_less_account_cannot_set_a_password_with_a_token( client, viewer_access_token: str, viewer_user: User ):