Projet

Général

Profil

0001-misc-only-pass-sane-addresses-to-email-backend-57510.patch

Frédéric Péters, 12 novembre 2021 08:57

Télécharger (2,5 ko)

Voir les différences:

Subject: [PATCH] misc: only pass sane addresses to email backend (#57510)

 tests/test_emails.py | 24 ++++++++++++++++++++++++
 wcs/qommon/emails.py | 10 ++++++++++
 2 files changed, 34 insertions(+)
tests/test_emails.py
95 95
    finally:
96 96
        os.environ = orig_environ
97 97

  
98
    # multiple recipients
99
    emails.empty()
100
    send_email(
101
        'test',
102
        mail_body='Hello',
103
        email_rcpt=['test@localhost', 'test2@localhost'],
104
        want_html=False,
105
        ignore_mail_redirection=True,
106
    )
107
    assert emails.count() == 1
108
    assert emails.emails['test']['email_rcpt'] == ['test@localhost', 'test2@localhost']
109

  
110
    # invalid recipient
111
    emails.empty()
112
    send_email(
113
        'test',
114
        mail_body='Hello',
115
        email_rcpt=['test@localhost', 'test@localhost@localhost'],
116
        want_html=False,
117
        ignore_mail_redirection=True,
118
    )
119
    assert emails.count() == 1
120
    assert emails.emails['test']['email_rcpt'] == ['test@localhost']
121

  
98 122

  
99 123
@pytest.mark.skipif('docutils is None')
100 124
def test_email_signature_rst(emails):
wcs/qommon/emails.py
39 39
    docutils = None
40 40

  
41 41
from django.core.mail import EmailMessage, EmailMultiAlternatives, get_connection
42
from django.core.mail.message import sanitize_address
42 43
from django.template.loader import render_to_string
43 44
from django.utils.safestring import mark_safe
44 45
from quixote import get_publisher, get_request, get_response
......
153 154
    get_logger().warning('Failed to build MIME part from %r', attachment)
154 155

  
155 156

  
157
def is_sane_address(email):
158
    try:
159
        sanitize_address(email, 'utf-8')
160
    except IndexError:
161
        return False
162
    return True
163

  
164

  
156 165
def email(
157 166
    subject,
158 167
    mail_body,
......
272 281
    if email_rcpt:
273 282
        if isinstance(email_rcpt, str):
274 283
            email_rcpt = [email_rcpt]
284
        email_rcpt = [x for x in email_rcpt if is_sane_address(x)]
275 285
        if hide_recipients:
276 286
            bcc_emails += email_rcpt[:]
277 287
        else:
278
-