mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 10:01:00 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
):
|
||||
|
||||
Reference in New Issue
Block a user