Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
emails: Truncate overly-long From fields for RFC compatibility.
Cyril Pletinckxb7fa41601de2
diff --git a/zerver/lib/send_email.py b/zerver/lib/send_email.pyindex 81b13955e67..2e980faf9c3 100644--- a/zerver/lib/send_email.py+++ b/zerver/lib/send_email.py@@ -11,6 +11,7 @@ from typing import Any, Dict, List, Mapping, Optional, Tupleimport orjsonfrom django.conf import settingsfrom django.core.mail import EmailMultiAlternatives+from django.core.mail.message import sanitize_addressfrom django.core.management import CommandErrorfrom django.db import transactionfrom django.template import loader@@ -148,8 +149,15 @@ def build_email(if from_address == FromAddress.support_placeholder:from_address = FromAddress.SUPPORT- # Set the "From" that is displayed separately from the envelope-from+ # Set the "From" that is displayed separately from the envelope-from.extra_headers["From"] = str(Address(display_name=from_name, addr_spec=from_address))+ # Check ASCII encoding length. Amazon SES rejects emails with+ # From names longer than 320 characters (which appears to be a+ # misinterpretation of the RFC); in that case we drop the name+ # from the From line, under the theory that it's better to send+ # the email with a simplified From field than not.+ if len(sanitize_address(extra_headers["From"], "utf-8")) > 320:+ extra_headers["From"] = str(Address(addr_spec=from_address))reply_to = Noneif reply_to_email is not None:diff --git a/zerver/tests/test_send_email.py b/zerver/tests/test_send_email.pynew file mode 100644index 00000000000..7e4b52d56ad--- /dev/null+++ b/zerver/tests/test_send_email.py@@ -0,0 +1,59 @@+from django.core.mail.message import sanitize_address++from zerver.lib.send_email import FromAddress, build_email+from zerver.lib.test_classes import ZulipTestCase++OVERLY_LONG_NAME = "Z̷̧̙̯͙̠͇̰̲̞̙͆́͐̅̌͐̔͑̚u̷̼͎̹̻̻̣̞͈̙͛͑̽̉̾̀̅̌͜͠͞ļ̛̫̻̫̰̪̩̠̣̼̏̅́͌̊͞į̴̛̛̩̜̜͕̘̂̑̀̈p̡̛͈͖͓̟͍̿͒̍̽͐͆͂̀ͅ A̰͉̹̅̽̑̕͜͟͡c̷͚̙̘̦̞̫̭͗̋͋̾̑͆̒͟͞c̵̗̹̣̲͚̳̳̮͋̈́̾̉̂͝ͅo̠̣̻̭̰͐́͛̄̂̿̏͊u̴̱̜̯̭̞̠͋͛͐̍̄n̸̡̘̦͕͓̬͌̂̎͊͐̎͌̕ť̮͎̯͎̣̙̺͚̱̌̀́̔͢͝ S͇̯̯̙̳̝͆̊̀͒͛̕ę̛̘̬̺͎͎́̔̊̀͂̓̆̕͢ͅc̨͎̼̯̩̽͒̀̏̄̌̚u̷͉̗͕̼̮͎̬͓͋̃̀͂̈̂̈͊͛ř̶̡͔̺̱̹͓̺́̃̑̉͡͞ͅi̶̺̭͈̬̞̓̒̃͆̅̿̀̄́t͔̹̪͔̥̣̙̍̍̍̉̑̏͑́̌ͅŷ̧̗͈͚̥̗͚͊͑̀͢͜͡"+++class TestBuildEmail(ZulipTestCase):+ def test_build_SES_compatible_From_field(self) -> None:+ hamlet = self.example_user("hamlet")+ from_name = FromAddress.security_email_from_name(language="en")+ mail = build_email(+ "zerver/emails/password_reset",+ to_emails=[hamlet],+ from_name=from_name,+ from_address=FromAddress.NOREPLY,+ language="en",+ )+ self.assertEqual(+ mail.extra_headers["From"], "{} <{}>".format(from_name, FromAddress.NOREPLY)+ )++ def test_build_SES_compatible_From_field_limit(self) -> None:+ hamlet = self.example_user("hamlet")+ limit_length_name = "a" * (320 - len(sanitize_address(FromAddress.NOREPLY, "utf-8")) - 3)+ mail = build_email(+ "zerver/emails/password_reset",+ to_emails=[hamlet],+ from_name=limit_length_name,+ from_address=FromAddress.NOREPLY,+ language="en",+ )+ self.assertEqual(+ mail.extra_headers["From"], "{} <{}>".format(limit_length_name, FromAddress.NOREPLY)+ )++ def test_build_SES_incompatible_From_field(self) -> None:+ hamlet = self.example_user("hamlet")+ mail = build_email(+ "zerver/emails/password_reset",+ to_emails=[hamlet],+ from_name=OVERLY_LONG_NAME,+ from_address=FromAddress.NOREPLY,+ language="en",+ )+ self.assertEqual(mail.extra_headers["From"], FromAddress.NOREPLY)++ def test_build_SES_incompatible_From_field_limit(self) -> None:+ hamlet = self.example_user("hamlet")+ limit_length_name = "a" * (321 - len(sanitize_address(FromAddress.NOREPLY, "utf-8")) - 3)+ mail = build_email(+ "zerver/emails/password_reset",+ to_emails=[hamlet],+ from_name=limit_length_name,+ from_address=FromAddress.NOREPLY,+ language="en",+ )+ self.assertEqual(mail.extra_headers["From"], FromAddress.NOREPLY)