Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

send_email: Strip CR in addition to LF from rendered subject.

Tim Abbott81d182722eee
diff --git a/zerver/lib/send_email.py b/zerver/lib/send_email.py
index c743c8ca244..98aa223706c 100644
--- a/zerver/lib/send_email.py
+++ b/zerver/lib/send_email.py
@@ -168,6 +168,7 @@ def build_email(
)
.strip()
.replace("\n", "")
+ .replace("\r", "")
)
message = loader.render_to_string(
template_prefix + ".txt", context=context, using="Jinja2_plaintext"
diff --git a/zerver/tests/test_send_email.py b/zerver/tests/test_send_email.py
index 1fcb663cd48..65267269017 100644
--- a/zerver/tests/test_send_email.py
+++ b/zerver/tests/test_send_email.py
@@ -80,6 +80,32 @@ class TestBuildEmail(ZulipTestCase):
)
self.assertEqual(mail.to[0], hamlet.delivery_email)
+ def test_email_subject_strips_crlf(self) -> None:
+ # Email header injection would require a CR or LF making it
+ # through email-subject rendering. Templates are trusted, but
+ # we strip both CR and LF defensively so any future template
+ # that interpolates a user-controlled value can't smuggle a
+ # header-terminating sequence into the email header.
+ hamlet = self.example_user("hamlet")
+ with mock.patch(
+ "zerver.lib.send_email.loader.render_to_string",
+ side_effect=lambda template_name, *args, **kwargs: (
+ "Subject\r\nBcc: [email protected]"
+ if template_name == "zerver/emails/password_reset.subject.txt"
+ else ""
+ ),
+ ):
+ email = build_email(
+ "zerver/emails/password_reset",
+ to_user_ids=[hamlet.id],
+ from_name="Noreply",
+ from_address=FromAddress.NOREPLY,
+ language="en",
+ )
+ self.assertNotIn("\r", email.subject)
+ self.assertNotIn("\n", email.subject)
+ self.assertEqual(email.subject, "SubjectBcc: [email protected]")
+
class TestSendEmail(ZulipTestCase):
@override_settings(