Changes between Version 4 and Version 6 of Ticket #36746
- Timestamp:
- Nov 25, 2025, 12:53:32 PM (2 weeks ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #36746
- Property Status assigned → closed
- Property Resolution → needsinfo
-
Ticket #36746 – Description
v4 v6 1 1 When parsing invalid email addresses (such as 'to@') in the SMTP backend's prep_address() method, Python's email parser raises an IndexError instead of the expected ValueError. This causes the test test_avoids_sending_to_invalid_addresses to fail. 2 2 3 **Steps to reproduce: 3 **Steps to reproduce:** 4 4 1. Run Django's test suite: `./runtests.py mail.tests.SMTPBackendTests.test_avoids_sending_to_invalid_addresses` 5 5 2. The test fails with IndexError when trying to parse 'to@' 6 6 7 **Root cause: 7 **Root cause:** 8 8 The prep_address() method at line 172 in `django/core/mail/backends/smtp.py` directly calls `AddressHeader.value_parser(address)` without exception handling. When parsing malformed addresses like 'to@', Python's email parser first raises HeaderParseError, then during exception handling attempts to parse the address differently, which leads to an IndexError when trying to access `value[0]` on an empty string. 9 9 10 **Proposed solution: 10 **Proposed solution:** 11 11 Wrap the `AddressHeader.value_parser()` call in a try-except block to catch `HeaderParseError`, `IndexError`, and `ValueError` exceptions, converting them to `ValueError` with an appropriate message. This matches the pattern already used in the deprecated `sanitize_address()` function in `django/core/mail/message.py` (line 115). 12 12 13 **Expected behavior: 13 **Expected behavior:** 14 14 The prep_address() method should catch parsing exceptions and raise ValueError with message "Invalid address" for invalid addresses, matching the test's expectation. 15 15 16 **Actual behavior: 16 **Actual behavior:** 17 17 IndexError: string index out of range is raised from Python's email._header_value_parser when parsing addresses like 'to@'. 18 18 19 **Error traceback: 19 **Error traceback:** 20 20 {{{ 21 21 ERROR: test_avoids_sending_to_invalid_addresses (mail.tests.SMTPBackendTests.test_avoids_sending_to_invalid_addresses) [<object object at 0x12d0aff70>] (email_address='to@')