Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
fix(account): make case-fallback email lookup work in both directions (#42541)
Xiyuan Chena24135ae8341
diff --git a/api/services/account_service.py b/api/services/account_service.pyindex c907b421fb7..8f5e36c3e73 100644--- a/api/services/account_service.py+++ b/api/services/account_service.py@@ -838,16 +838,24 @@ class AccountService:@staticmethoddef get_account_by_email_with_case_fallback(email: str, *, session: Session) -> Account | None:"""- Retrieve an account by email and fall back to the lowercase email if the original lookup fails.+ Retrieve an account by email, matching case-insensitively.- This keeps backward compatibility for older records that stored uppercase emails while the- rest of the system gradually normalizes new inputs.+ SSO-provisioned accounts keep the identity provider's original casing, while other entry+ points (invites, registration, login) normalize input to lowercase before looking up an+ account. An exact match is tried first since it uses the plain email index; the fallback+ compares normalized_email so either direction of case mismatch still resolves to the same+ account."""account = session.execute(select(Account).where(Account.email == email)).scalar_one_or_none()- if account or email == email.lower():+ if account:return account- return session.execute(select(Account).where(Account.email == email.lower())).scalar_one_or_none()+ return session.execute(+ select(Account)+ .where(Account.normalized_email == normalize_email(email))+ .order_by(Account.created_at)+ .limit(1)+ ).scalar_one_or_none()@staticmethod@redis_fallback(default_return=None)diff --git a/api/tests/unit_tests/controllers/console/test_workspace_account.py b/api/tests/unit_tests/controllers/console/test_workspace_account.pyindex d93bcfa296d..8be7ec9dbc8 100644--- a/api/tests/unit_tests/controllers/console/test_workspace_account.py+++ b/api/tests/unit_tests/controllers/console/test_workspace_account.py@@ -28,6 +28,7 @@ from machinery.context import RequestContextfrom models import Account, AccountStatus, Tenant, TenantAccountJoinfrom models.account import TenantAccountRolefrom services import account_errors+from services.account_email import normalize_emailfrom services.account_service import AccountServicefrom services.entities.account_entities import AccountEducationActivation, ChangeEmailVerificationfrom services.entities.auth_entities import (@@ -69,7 +70,9 @@ def _stable_uuid(value: str) -> str:def _persist_account_with_tenant(session: Session, email: str, account_name: str = "account") -> tuple[Account, Tenant]:tenant = Tenant(name=f"{account_name} tenant")tenant.id = _stable_uuid(f"tenant:{account_name}")- account = Account(name=account_name, email=email, status=AccountStatus.ACTIVE)+ account = Account(+ name=account_name, email=email, normalized_email=normalize_email(email), status=AccountStatus.ACTIVE+ )account.id = _stable_uuid(f"account:{account_name}")membership = TenantAccountJoin(tenant_id=tenant.id,diff --git a/api/tests/unit_tests/services/test_account_service.py b/api/tests/unit_tests/services/test_account_service.pyindex 83e83929725..18cc02f1959 100644--- a/api/tests/unit_tests/services/test_account_service.py+++ b/api/tests/unit_tests/services/test_account_service.py@@ -20,6 +20,7 @@ from models.account import (TenantStatus,)from models.model import DifySetup+from services.account_email import normalize_emailfrom services.account_service import (AccountService,EnterpriseWorkspaceMemberAccountNotFoundError,@@ -3001,7 +3002,7 @@ class TestSessionInjectedGetters:def test_get_account_by_email_with_case_fallback_uses_lowercase(sqlite_session: Session) -> None:- account = Account(name="Case User", email="[email protected]")+ account = Account(name="Case User", email="[email protected]", normalized_email=normalize_email("[email protected]"))sqlite_session.add(account)sqlite_session.commit()@@ -3010,6 +3011,40 @@ def test_get_account_by_email_with_case_fallback_uses_lowercase(sqlite_session:assert result is account+def test_get_account_by_email_with_case_fallback_finds_uppercase_row_from_lowercase_input(+ sqlite_session: Session,+) -> None:+ """Regression test for CUS-1658: an SSO-provisioned account keeps the IdP's original casing+ (e.g. `[email protected]`), but the workspace invite flow always looks it up with an+ already-lowercased email. The lookup must still find that account.+ """+ account = Account(name="SSO User", email="[email protected]", normalized_email=normalize_email("[email protected]"))+ sqlite_session.add(account)+ sqlite_session.commit()++ result = AccountService.get_account_by_email_with_case_fallback("[email protected]", session=sqlite_session)++ assert result is account+++def test_get_account_by_email_with_case_fallback_returns_oldest_when_normalized_email_is_duplicated(+ sqlite_session: Session,+) -> None:+ """normalized_email is indexed but not unique, so installations can already hold equivalent+ accounts. The fallback must not raise and must resolve to the same (oldest) account every time.+ """+ newer = Account(name="Newer", email="[email protected]", normalized_email=normalize_email("[email protected]"))+ newer.created_at = datetime(2026, 9, 2)+ older = Account(name="Older", email="[email protected]", normalized_email=normalize_email("[email protected]"))+ older.created_at = datetime(2026, 9, 1)+ sqlite_session.add_all([newer, older])+ sqlite_session.commit()++ result = AccountService.get_account_by_email_with_case_fallback("[email protected]", session=sqlite_session)++ assert result is older++class TestIsEmailSendIpLimit:"""The 10-minute first-strike window must actually take effect (#39477)."""