fix(auth): apply code-review fixes to email trust

pr-ready step 2 (code-review xhigh --fix): set email_trusted inside the
existing email block, trust a provider-confirmed email on an
identity-matched sign-in, reword the migration comment, and add tests for
delegated-manager creation and trust_email.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-11-17 09:18:54 -05:00
parent ceedecea5b
commit b69ca0e2a2
6 changed files with 68 additions and 4 deletions

View File

@@ -17,7 +17,8 @@ depends_on = None
def upgrade() -> None:
# Existing accounts keep linking by email as before; new ones start untrusted.
# Existing accounts start trusted so a pending first OIDC sign-in still links
# them by email; new ones start untrusted.
with op.batch_alter_table("users", schema=None) as batch_op:
batch_op.add_column(
sa.Column(

View File

@@ -531,6 +531,7 @@ async def update_user(
auth_handler.get_password_hash, form_data.password
)
by_admin = is_scoped_admin(request, Scope.USERS_WRITE)
if new_email is not None:
try:
validate_email(new_email)
@@ -549,10 +550,8 @@ async def update_user(
)
cleaned_data["email"] = new_email.lower() or None
by_admin = is_scoped_admin(request, Scope.USERS_WRITE)
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 = (

View File

@@ -831,6 +831,8 @@ class OpenIDHandler:
hl(user.username, color=CYAN),
hl(email, color=CYAN),
)
elif matched_by_identity and not user.email_trusted:
updates["email_trusted"] = True
if claims_provided and user.role != role:
updates["role"] = role

View File

@@ -699,6 +699,9 @@ def test_an_email_a_user_sets_for_themselves_is_not_trusted(
def test_an_email_a_delegated_user_manager_sets_is_not_trusted(
client, admin_user: User, viewer_user: User, editor_user: User
):
db_user_handler.update_user(
editor_user.id, {"email": "old-editor@example.com", "email_trusted": True}
)
token = _delegated_user_manager_token(viewer_user)
response = client.put(
@@ -712,6 +715,27 @@ def test_an_email_a_delegated_user_manager_sets_is_not_trusted(
assert updated and not updated.email_trusted
def test_a_user_a_delegated_user_manager_creates_has_an_untrusted_email(
client, admin_user: User, viewer_user: User
):
token = _delegated_user_manager_token(viewer_user)
response = client.post(
"/api/users",
json={
"username": "managed_user",
"email": "managed@example.com",
"password": "a-good-password",
"role": Role.USER.value,
},
headers=bearer(token),
)
assert response.status_code == status.HTTP_201_CREATED
created = db_user_handler.get_user(response.json()["id"])
assert created and not created.email_trusted
@pytest.mark.parametrize(
"email", ["viewer@example.com", "new@example.com"], ids=["unchanged", "changed"]
)

View File

@@ -897,6 +897,26 @@ async def test_oidc_login_follows_an_email_change_at_the_provider(
assert user.email_trusted
async def test_oidc_login_trusts_the_email_the_provider_confirms_for_a_linked_account(
mock_oidc_enabled,
mock_oidc_allow_registration_disabled,
mock_token,
mock_discovery,
):
existing = _add_user(
"testuser", "test@example.com", oidc_sub="sub-1", email_trusted=False
)
mock_token["userinfo"]["sub"] = "sub-1"
user, _ = await OpenIDHandler().get_current_active_user_from_openid_token(
mock_token
)
assert user is not None
assert user.id == existing.id
assert user.email_trusted
async def test_oidc_login_refuses_to_link_an_account_by_an_untrusted_email(
mocker, mock_oidc_enabled, mock_token, mock_discovery
):

View File

@@ -136,3 +136,21 @@ class TestUpdateLastActive:
user.set_last_active()
assert executed_statements == []
class TestTrustEmail:
def test_trusts_the_email_the_user_still_holds(self, viewer_user: User):
db_user_handler.update_user(viewer_user.id, {"email": "viewer@example.com"})
assert db_user_handler.trust_email(viewer_user.id, "Viewer@Example.com")
current = db_user_handler.get_user(viewer_user.id)
assert current and current.email_trusted
def test_leaves_a_changed_email_untrusted(self, viewer_user: User):
db_user_handler.update_user(viewer_user.id, {"email": "taken@example.com"})
assert not db_user_handler.trust_email(viewer_user.id, "viewer@example.com")
current = db_user_handler.get_user(viewer_user.id)
assert current and not current.email_trusted