emails: skip invalid emails in bcc (#62613)

This commit is contained in:
Lauréline Guérin 2022-03-15 10:55:58 +01:00 committed by Frédéric Péters
parent b00dd8ec92
commit 9579fe5fa6
2 changed files with 47 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import pytest
from wcs.qommon.emails import docutils # noqa pylint: disable=unused-import
from wcs.qommon.emails import email as send_email
from wcs.qommon.emails import is_sane_address
from wcs.qommon.upload_storage import PicklableUpload
from .utilities import clean_temporary_pub, cleanup, create_temporary_pub
@ -20,6 +21,22 @@ def teardown_module(module):
clean_temporary_pub()
@pytest.mark.parametrize(
'email, result',
[
('test@localhost', True),
('test2@localhost', True),
('test@localhost@localhost', False),
('test@example.com@example.com', False),
('', False),
(' ', False),
(None, False),
],
)
def test_is_sane_address(email, result):
assert is_sane_address(email) == result
def test_email_signature_plain(emails):
pub = create_temporary_pub()
pub.cfg['emails'] = {'footer': 'Footer\nText'}
@ -112,7 +129,32 @@ def test_email_recipients(emails):
send_email(
'test',
mail_body='Hello',
email_rcpt=['test@localhost', 'test@localhost@localhost', 'test@.fr'],
email_rcpt=[
'test@localhost',
'test@localhost@localhost',
'test@example.fr@example.com',
'test@.fr',
'',
'',
],
want_html=False,
ignore_mail_redirection=True,
)
assert emails.count() == 1
assert emails.emails['test']['email_rcpt'] == ['test@localhost']
emails.empty()
send_email(
'test',
mail_body='Hello',
email_rcpt=None,
bcc=[
'test@localhost',
'test@localhost@localhost',
'test@example.fr@example.com',
'test@.fr',
'',
'',
],
want_html=False,
ignore_mail_redirection=True,
)

View File

@ -267,6 +267,7 @@ def email(
to_emails = []
bcc_emails = bcc or []
bcc_emails = [x for x in bcc_emails if is_sane_address(x)]
if email_rcpt:
if isinstance(email_rcpt, str):
email_rcpt = [email_rcpt]
@ -302,6 +303,9 @@ def email(
if attachment:
attachments_parts.append(attachment)
if not to_emails and not bcc_emails:
return
email_msg_kwargs = {
'subject': subject,
'to': to_emails,