Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
email_mirror: Fix exception handling unstructured headers.
Emilio López7b35234c7b0b
diff --git a/zerver/lib/email_mirror.py b/zerver/lib/email_mirror.pyindex 0344f106aa4..eb4b8dd9269 100644--- a/zerver/lib/email_mirror.py+++ b/zerver/lib/email_mirror.py@@ -1,5 +1,6 @@import loggingimport re+from email.headerregistry import AddressHeaderfrom email.message import EmailMessagefrom typing import Dict, List, Optional, Tuple@@ -310,9 +311,14 @@ def find_emailgateway_recipient(message: EmailMessage) -> str:for header_name in recipient_headers:for header_value in message.get_all(header_name, []):- for addr in header_value.addresses:- if match_email_re.match(addr.addr_spec):- return addr.addr_spec+ if isinstance(header_value, AddressHeader):+ emails = [addr.addr_spec for addr in header_value.addresses]+ else:+ emails = [str(header_value)]++ for email in emails:+ if match_email_re.match(email):+ return emailraise ZulipEmailForwardError("Missing recipient in mirror email")diff --git a/zerver/tests/test_email_mirror.py b/zerver/tests/test_email_mirror.pyindex e1fc285c647..ecb0aae4dfd 100644--- a/zerver/tests/test_email_mirror.py+++ b/zerver/tests/test_email_mirror.py@@ -232,6 +232,36 @@ class TestStreamEmailMessagesSuccess(ZulipTestCase):self.assertEqual(get_display_recipient(message.recipient), stream.name)self.assertEqual(message.topic_name(), incoming_valid_message['Subject'])+ # Test receiving an email with the address on an UnstructuredHeader+ # (e.g. Envelope-To) instead of an AddressHeader (e.g. To).+ # https://github.com/zulip/zulip/issues/15864+ def test_receive_stream_email_messages_other_header_success(self) -> None:+ user_profile = self.example_user('hamlet')+ self.login_user(user_profile)+ self.subscribe(user_profile, "Denmark")+ stream = get_stream("Denmark", user_profile.realm)++ stream_to_address = encode_email_address(stream)++ incoming_valid_message = EmailMessage()+ incoming_valid_message.set_content('TestStreamEmailMessages Body')++ incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'+ incoming_valid_message['From'] = self.example_email('hamlet')+ # Simulate a mailing list+ incoming_valid_message['To'] = "[email protected]"+ incoming_valid_message['Envelope-To'] = stream_to_address+ incoming_valid_message['Reply-to'] = self.example_email('othello')++ process_message(incoming_valid_message)++ # Hamlet is subscribed to this stream so should see the email message from Othello.+ message = most_recent_message(user_profile)++ self.assertEqual(message.content, "TestStreamEmailMessages Body")+ self.assertEqual(get_display_recipient(message.recipient), stream.name)+ self.assertEqual(message.topic_name(), incoming_valid_message['Subject'])+def test_receive_stream_email_messages_blank_subject_success(self) -> None:user_profile = self.example_user('hamlet')self.login_user(user_profile)