email_mirror: Fail more gracefully on empty EMAIL_GATEWAY_PATTERN.

Otherwise, this fails on `match.group(1)` as there is no match group.

The server would ideally respond with a 521 or 556 code[^1] on initial
connection, but aiosmtpd does not provide that option.

[^1]: https://www.rfc-editor.org/rfc/rfc7504
This commit is contained in:
Alex Vandiver
2025-07-01 10:59:21 -04:00
committed by Tim Abbott
parent 43a7035745
commit 8cde0af040
2 changed files with 22 additions and 0 deletions

View File

@@ -33,6 +33,8 @@ class ZulipEmailForwardUserError(ZulipEmailForwardError):
def get_email_gateway_message_string_from_address(address: str) -> str:
if settings.EMAIL_GATEWAY_PATTERN == "":
raise ZulipEmailForwardError("This server is not configured for incoming email.")
pattern_parts = [re.escape(part) for part in settings.EMAIL_GATEWAY_PATTERN.split("%s")]
if settings.EMAIL_GATEWAY_EXTRA_PATTERN_HACK:
# Accept mails delivered to any Zulip server

View File

@@ -2007,6 +2007,26 @@ class TestEmailMirrorServer(ZulipTestCase):
return [r.decode() for r in responses]
@override_settings(EMAIL_GATEWAY_PATTERN="")
async def test_unconfigured(self) -> None:
self.assertEqual(
await self.handler_response(
[
"HELO localhost",
"MAIL FROM: <test@example.com>",
"RCPT TO: <bogus@other.example.com>",
"QUIT",
]
),
[
"220 testhost Zulip 1.2.3\r\n",
"250 testhost\r\n",
"250 OK\r\n",
"550 5.1.1 Bad destination mailbox address: This server is not configured for incoming email.\r\n",
"221 Bye\r\n",
],
)
@override_settings(EMAIL_GATEWAY_PATTERN="%s@zulip.example.com")
async def test_handler_error(self) -> None:
with (