Skip to content

armgit / dify

Public Imported from GitHub
0

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.

fix: maintenance update (#43899)

-LAN-f02c2d08ab09
diff --git a/api/services/account/login_adapters.py b/api/services/account/login_adapters.py
index 21101701de7..dc06e5bb4d9 100644
--- a/api/services/account/login_adapters.py
+++ b/api/services/account/login_adapters.py
@@ -262,6 +262,7 @@ class RedisEmailCodeGateway(EmailCodeGateway):
normalized_email: str,
recipient_email: str,
language: str,
+ registration_allowed: bool,
) -> str:
normalized_email = normalized_email.lower()
if self._rate_limiter.is_rate_limited(normalized_email):
@@ -274,7 +275,8 @@ class RedisEmailCodeGateway(EmailCodeGateway):
except EmailCodeLoginChallengeUnavailableError as error:
logger.warning("Email-code challenge creation is unavailable", exc_info=True)
raise account_errors.EmailCodeLoginUnavailableError from error
- send_email_code_login_mail_task.delay(language=language, to=recipient_email, code=code)
+ if account_id is not None or registration_allowed:
+ send_email_code_login_mail_task.delay(language=language, to=recipient_email, code=code)
self._rate_limiter.increment_rate_limit(normalized_email)
return token
diff --git a/api/services/account/login_service.py b/api/services/account/login_service.py
index 6403b86b40a..3e97df2f2ae 100644
--- a/api/services/account/login_service.py
+++ b/api/services/account/login_service.py
@@ -106,6 +106,7 @@ class EmailCodeGateway(Protocol):
normalized_email: str,
recipient_email: str,
language: str,
+ registration_allowed: bool,
) -> str: ...
def verify(self, *, normalized_email: str, code: str, token: str) -> EmailCodeChallengeStatus: ...
@@ -240,14 +241,12 @@ class ConsoleAuthenticationService:
self._ensure_email_eligible(normalized_email, command.ip_address)
account = self._preferred_account(command.email)
self._ensure_account_not_banned(account, normalized_email, command.ip_address)
- if account is None and not self._policies.is_registration_allowed():
- raise account_errors.AccountNotFoundError
-
return self._email_codes.send(
account_id=account.id if account is not None else None,
normalized_email=normalized_email,
recipient_email=account.email if account is not None else normalized_email,
language=self._email_language(command.language),
+ registration_allowed=account is not None or self._policies.is_registration_allowed(),
)
def login_with_email_code(self, command: EmailCodeLoginCommand) -> AccountSessionTokens:
diff --git a/api/tests/unit_tests/services/account/test_login_adapters.py b/api/tests/unit_tests/services/account/test_login_adapters.py
index 7484ec4cdd3..95fd55570f3 100644
--- a/api/tests/unit_tests/services/account/test_login_adapters.py
+++ b/api/tests/unit_tests/services/account/test_login_adapters.py
@@ -316,7 +316,11 @@ def test_provisioning_rejects_equivalent_normalized_email(
)
-def test_email_code_gateway_sends_and_maps_shared_challenge_status(monkeypatch: pytest.MonkeyPatch) -> None:
[email protected]("account_id", [None, "account-1"])
[email protected]("registration_allowed", [False, True])
+def test_email_code_gateway_sends_and_maps_shared_challenge_status(
+ monkeypatch: pytest.MonkeyPatch, account_id: str | None, registration_allowed: bool
+) -> None:
limiter = FakeRateLimiter()
task = FakeTask()
created: list[tuple[str | None, str, str]] = []
@@ -336,16 +340,20 @@ def test_email_code_gateway_sends_and_maps_shared_challenge_status(monkeypatch:
gateway = adapters.RedisEmailCodeGateway(rate_limiter=limiter)
token = gateway.send(
- account_id="account-1",
+ account_id=account_id,
normalized_email="[email protected]",
recipient_email="[email protected]",
language="en-US",
+ registration_allowed=registration_allowed,
)
assert token == "challenge-token"
assert created[0][1] == "[email protected]"
assert len(created[0][2]) == 6
- assert task.calls[0]["to"] == "[email protected]"
+ if account_id is not None or registration_allowed:
+ assert task.calls[0]["to"] == "[email protected]"
+ else:
+ assert task.calls == []
assert limiter.recorded == ["[email protected]"]
assert (
gateway.verify(normalized_email="[email protected]", code="123456", token="challenge-token")
@@ -370,6 +378,7 @@ def test_email_code_gateway_maps_challenge_store_outage(
normalized_email="[email protected]",
recipient_email="[email protected]",
language="en-US",
+ registration_allowed=False,
)
record = caplog.records[-1]
@@ -431,6 +440,7 @@ def test_email_gateways_raise_framework_neutral_rate_limit_errors() -> None:
normalized_email="[email protected]",
recipient_email="[email protected]",
language="en-US",
+ registration_allowed=False,
)
with pytest.raises(account_errors.ResetPasswordEmailRateLimitError) as reset_error:
adapters.RedisResetPasswordEmailGateway(rate_limiter=FakeRateLimiter(limited=True, time_window=60)).send(
diff --git a/api/tests/unit_tests/services/account/test_login_service.py b/api/tests/unit_tests/services/account/test_login_service.py
index 77984749a70..3c847c67549 100644
--- a/api/tests/unit_tests/services/account/test_login_service.py
+++ b/api/tests/unit_tests/services/account/test_login_service.py
@@ -208,7 +208,7 @@ class FakeWorkspaceProvisioning:
@dataclass
class FakeEmailCodes:
status: EmailCodeChallengeStatus = EmailCodeChallengeStatus.VERIFIED
- sent: list[tuple[str | None, str, str, str]] = field(default_factory=list)
+ sent: list[tuple[str | None, str, str, str, bool]] = field(default_factory=list)
def send(
self,
@@ -217,8 +217,9 @@ class FakeEmailCodes:
normalized_email: str,
recipient_email: str,
language: str,
+ registration_allowed: bool,
) -> str:
- self.sent.append((account_id, normalized_email, recipient_email, language))
+ self.sent.append((account_id, normalized_email, recipient_email, language, registration_allowed))
return "email-token"
def verify(self, *, normalized_email: str, code: str, token: str) -> EmailCodeChallengeStatus:
@@ -557,22 +558,56 @@ def test_send_email_code_normalizes_identity_and_preserves_account_recipient_add
assert token == "email-token"
assert dependencies.human.calls == [("turnstile", "127.0.0.1", "signin_code")]
- assert dependencies.email_codes.sent == [("account-1", "[email protected]", "[email protected]", "zh-Hans")]
+ assert dependencies.email_codes.sent == [("account-1", "[email protected]", "[email protected]", "zh-Hans", True)]
-def test_send_email_code_rejects_unknown_account_when_registration_is_disabled() -> None:
[email protected]("account_exists", [False, True])
[email protected]("registration_allowed", [False, True])
+def test_send_email_code_returns_challenge_regardless_of_account_existence(
+ account_exists: bool, registration_allowed: bool
+) -> None:
dependencies = Dependencies()
- dependencies.policies.registration_allowed = False
+ dependencies.policies.registration_allowed = registration_allowed
+ if account_exists:
+ dependencies.accounts.candidates = [_account()]
- with pytest.raises(account_errors.AccountNotFoundError):
- dependencies.service().send_email_code(
- EmailCodeSendCommand(
- language=None,
- turnstile_token=None,
- ip_address="127.0.0.1",
- )
+ token = dependencies.service().send_email_code(
+ EmailCodeSendCommand(
+ language=None,
+ turnstile_token=None,
+ ip_address="127.0.0.1",
+ )
+ )
+
+ assert token == "email-token"
+ assert dependencies.email_codes.sent == [
+ (
+ "account-1" if account_exists else None,
+ "en-US",
+ account_exists or registration_allowed,
)
+ ]
+
+
+def test_send_email_code_for_existing_account_does_not_load_registration_policy(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ dependencies = Dependencies()
+ dependencies.accounts.candidates = [_account()]
+
+ def unavailable_policy() -> bool:
+ raise AssertionError("Registration policy is irrelevant to an existing account")
+
+ monkeypatch.setattr(dependencies.policies, "is_registration_allowed", unavailable_policy)
+
+ token = dependencies.service().send_email_code(
+ EmailCodeSendCommand(email="[email protected]", language=None, turnstile_token=None, ip_address="127.0.0.1")
+ )
+
+ assert token == "email-token"
def test_send_email_code_rejects_ip_rate_limit_before_human_verification() -> None: