Change-email duplicate guards compare Account.email case-sensitively, allowing two accounts for one mailbox
Self Checks
- [x] I have read the Contributing Guide and Language Policy.
- [x] This is only for bug report, if you would like to ask a question, please head to Discussions.
- [x] I have searched for existing issues search for existing issues, including closed ones.
- [x] I confirm that I am using English to submit this report, otherwise it will be closed.
- [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
- [x] Please do not modify this template :) and fill in all the required fields.
Dify version
main (07119e3f00). The change-email repository path was introduced in #40439 (2026-08-21); email_exists predates it.
Cloud or Self Hosted
Cloud, Self Hosted (Docker), Self Hosted (Source) — the drift is in the shipped code, so it affects every deployment that carries legacy mixed-case account.email rows.
Steps to reproduce
- Have an account whose stored
account.emailis not all-lowercase — e.g.[email protected]. Registration only started normalizing in #29978 (chore: case insensitive email, 2026-01-13), and that PR shipped no data migration, so rows created before it keep whatever casing the user typed.AccountService.get_account_by_email_with_case_fallbackexists precisely for them, documented as keeping "backward compatibility for older records that stored uppercase emails while the rest of the system gradually normalizes new inputs" (api/services/account_service.py:956-967). - From a second account, complete the change-email flow (
/console/api/account/change-email/...) targeting[email protected]. - The reset succeeds and a second
Accountrow is written for the same mailbox.
Reproduced as a repository-level unit test against the shipped SQLite test schema:
def test_reset_email_rejects_case_variant_of_another_account_email(
sqlite_session: Session,
sqlite_session_factory: sessionmaker[Session],
) -> None:
_persist_account(sqlite_session) # account-1, [email protected]
_persist_account(sqlite_session, account_id="account-2", email="[email protected]")
repository = SQLAlchemyAccountRepository(sqlite_session_factory)
result = repository.reset_email(
"account-1",
expected_old_email="[email protected]",
new_email="[email protected]",
)
assert result.status == AccountEmailResetStatus.EMAIL_IN_USE
E AssertionError: assert <AccountEmail...ED: 'updated'> == <AccountEmail...email_in_use'>
E - email_in_use
E + updated
✔️ Expected Behavior
The duplicate-email guard on the change-email path folds case, consistent with every neighbouring check:
SQLAlchemyAccountRepository.reset_emailalready compares the old address case-insensitively —account.email.lower() != expected_old_email.lower()(api/repositories/account_repository.py:134).AccountService.has_active_account_with_emailusesfunc.lower(Account.email) == normalizedand is documented as "the case-insensitive existence check that backs the SSO collision rule" (api/services/account_service.py:290-301).
❌ Actual Behavior
Two lookups in api/repositories/account_repository.py compare Account.email case-sensitively, so an address that differs only in case is not seen as taken:
-
email_exists(line 120):return session.scalar(select(Account.id).where(Account.email == email).limit(1)) is not NoneIts only production caller is
AccountChangeEmailService.ensure_available(api/services/account_change_email_service.py:200), the pre-check behind the change-email validity endpoint. -
The duplicate guard inside
reset_email(line 136), three lines below the case-insensitiveexpected_old_emailcomparison:if session.scalar(select(Account.id).where(Account.email == new_email).limit(1)) is not None: return AccountEmailResetResult(status=AccountEmailResetStatus.EMAIL_IN_USE)
Account.email carries no unique constraint — __table_args__ is PrimaryKeyConstraint("id") plus a non-unique Index("account_email_idx", "email") (api/models/account.py:91), and the initial migration creates it with unique=False (api/migrations/versions/64b051264f32_init.py:99) — so these two application-level checks are the only thing preventing a duplicate on this path. Under PostgreSQL's default collation = on varchar is case-sensitive, and SQLite behaves the same for TEXT, so the guard misses [email protected] when asked about [email protected].
Result: two Account rows for one real mailbox. Downstream, has_active_account_with_email then reports a collision, and get_account_by_email_with_case_fallback can resolve either row.
Scope note, so the severity is not overstated: every current write path normalizes before insert (email_register.py:99, login.py:334, oauth.py:319, SetupService.initialize:77, commands/account.py), and the change-email flow still requires a verification code delivered to the target mailbox — mailbox delivery is itself case-insensitive, so the actor must already control that mailbox. This is not an account-takeover, and it is only reachable on deployments carrying pre-#29978 mixed-case rows. It is a data-integrity and consistency defect, not a privilege issue.
Suggested fix: use func.lower(Account.email) == email.lower() in both places, and exclude the account's own row in reset_email so a user normalizing their own stored casing ([email protected] → [email protected]) is not reported as colliding with themselves.
I have a fix with regression tests ready and will open a PR referencing this issue.
I'd like to pick this up, since #41097 was withdrawn and #41720 was closed for merge conflicts after the account modules moved in #42570. The guard is still case-sensitive on main, now in reset_email in api/repositories/account/repository.py (select(Account.id).where(Account.email == new_email)). My plan is to change only that query to func.lower(Account.email) == new_email.lower() with Account.id != account_id, the same comparison has_active_email already uses, and keep an exact account.email == new_email check in front so an unchanged email still returns EMAIL_IN_USE without clearing AccountIntegrate. I'd deliberately leave email_exists (it gates the send-code button) and the normalized_email provider rules out of scope, which is where #41097 grew too large, and since this only runs after the code is verified, the lower() lookup doesn't sit behind an unauthenticated endpoint. Would you accept that narrow fix with repository-level regression tests, or would you rather the guard compare normalized_email instead? If the narrow version works for you, I'll open a PR referencing this issue.
I'd like to take this one. I verified the defect is still present on current main, checked that no open PR covers it, and prepared a minimal patch with an offline check. Opening a focused PR from my fork now.