#35497: Long email address causes crash when generating a message
---------------------------------------------+------------------------
               Reporter:  Alexandru Chirila  |          Owner:  nobody
                   Type:  Bug                |         Status:  new
              Component:  Core (Mail)        |        Version:  5.0
               Severity:  Normal             |       Keywords:
           Triage Stage:  Unreviewed         |      Has patch:  0
    Needs documentation:  0                  |    Needs tests:  0
Patch needs improvement:  0                  |  Easy pickings:  0
                  UI/UX:  0                  |
---------------------------------------------+------------------------
 Trying to generate an email message that has a long recipient address and
 non-ASCII characters causes a crash.

 How to reproduce:

 {{{
 EmailMessage(to=["ţēśţ." * 6 + "@example.com"]).message()
 }}}

 Looking for an existing issue I have found #31784. It seems like the issue
 there was only fixed for long names and long addresses.

 ----

 Version info:

 - Django: 5.0.6
 - Python: 3.12.3

 ----

 Full log:

 {{{#!python
 In [2]: from django.core.mail import EmailMessage

 In [3]: EmailMessage(to=["ţēśţ." * 6 + "@example.com"]).message()
 ---------------------------------------------------------------------------
 UnicodeEncodeError                        Traceback (most recent call
 last)
 File .venv/lib/python3.12/site-packages/django/core/mail/message.py:64, in
 forbid_multi_line_headers(name, val, encoding)
      63 try:
 ---> 64     val.encode("ascii")
      65 except UnicodeEncodeError:

 UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3:
 ordinal not in range(128)

 During handling of the above exception, another exception occurred:

 ValueError                                Traceback (most recent call
 last)
 Cell In[3], line 1
 ----> 1 EmailMessage(to=["ţēśţ." * 6 + "@example.com"]).message()

 File .venv/lib/python3.12/site-packages/django/core/mail/message.py:267,
 in EmailMessage.message(self)
     265 msg["Subject"] = self.subject
     266 msg["From"] = self.extra_headers.get("From", self.from_email)
 --> 267 self._set_list_header_if_not_empty(msg, "To", self.to)
     268 self._set_list_header_if_not_empty(msg, "Cc", self.cc)
     269 self._set_list_header_if_not_empty(msg, "Reply-To", self.reply_to)

 File .venv/lib/python3.12/site-packages/django/core/mail/message.py:432,
 in EmailMessage._set_list_header_if_not_empty(self, msg, header, values)
     430 except KeyError:
     431     value = ", ".join(str(v) for v in values)
 --> 432 msg[header] = value

 File .venv/lib/python3.12/site-packages/django/core/mail/message.py:165,
 in SafeMIMEText.__setitem__(self, name, val)
     164 def __setitem__(self, name, val):
 --> 165     name, val = forbid_multi_line_headers(name, val,
 self.encoding)
     166     MIMEText.__setitem__(self, name, val)

 File .venv/lib/python3.12/site-packages/django/core/mail/message.py:67, in
 forbid_multi_line_headers(name, val, encoding)
      65 except UnicodeEncodeError:
      66     if name.lower() in ADDRESS_HEADERS:
 ---> 67         val = ", ".join(
      68             sanitize_address(addr, encoding) for addr in
 getaddresses((val,))
      69         )
      70     else:
      71         val = Header(val, encoding).encode()

 File .venv/lib/python3.12/site-packages/django/core/mail/message.py:68, in
 <genexpr>(.0)
      65 except UnicodeEncodeError:
      66     if name.lower() in ADDRESS_HEADERS:
      67         val = ", ".join(
 ---> 68             sanitize_address(addr, encoding) for addr in
 getaddresses((val,))
      69         )
      70     else:
      71         val = Header(val, encoding).encode()

 File .venv/lib/python3.12/site-packages/django/core/mail/message.py:120,
 in sanitize_address(addr, encoding)
     117     localpart = Header(localpart, encoding).encode()
     118 domain = punycode(domain)
 --> 120 parsed_address = Address(username=localpart, domain=domain)
     121 return formataddr((nm, parsed_address.addr_spec))

 File ~/.pyenv/versions/3.12.3/lib/python3.12/email/headerregistry.py:33,
 in Address.__init__(self, display_name, username, domain, addr_spec)
      31 inputs = ''.join(filter(None, (display_name, username, domain,
 addr_spec)))
      32 if '\r' in inputs or '\n' in inputs:
 ---> 33     raise ValueError("invalid arguments; address parts cannot
 contain CR or LF")
      35 # This clause with its potential 'raise' may only happen when an
      36 # application program creates an Address object using an addr_spec
      37 # keyword.  The email library code itself must always supply
 username
      38 # and domain.
      39 if addr_spec is not None:

 ValueError: invalid arguments; address parts cannot contain CR or LF
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35497>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018fe8283beb-dffc5f39-1e6e-4df7-8dca-5c90c4967e41-000000%40eu-central-1.amazonses.com.

Reply via email to