diff --git a/swh/web/inbound_email/tests/test_utils.py b/swh/web/inbound_email/tests/test_utils.py index a0aafd52f7bbc3e9baf03ba4797b3d9f525c9a43..732f417e9127a84ad4c04439affbf5082bd5c5aa 100644 --- a/swh/web/inbound_email/tests/test_utils.py +++ b/swh/web/inbound_email/tests/test_utils.py @@ -39,6 +39,8 @@ def test_extract_recipients(): ] del message["To"] + # check invalid address header are discarded + message["x-envelope-to"] = "foo" assert utils.extract_recipients(message) == [ Address(addr_spec="test-recipient-2@example.com"), Address( diff --git a/swh/web/inbound_email/utils.py b/swh/web/inbound_email/utils.py index 88e240202f9b6d03ce06bf703a3283bc93c11f8e..775e2e5cfe5d14698cc0f5d58f502e47562a8f18 100644 --- a/swh/web/inbound_email/utils.py +++ b/swh/web/inbound_email/utils.py @@ -4,7 +4,7 @@ # See top-level LICENSE file for more information from dataclasses import dataclass -from email.headerregistry import Address +from email.headerregistry import Address, AddressHeader from email.message import EmailMessage, Message import logging from typing import List, Optional, Set, Tuple, Union @@ -48,7 +48,9 @@ def extract_recipients(message: EmailMessage) -> List[Address]: ret = [] for header_name in HEADERS: - for header in message.get_all(header_name, []): + for header in filter( + lambda h: isinstance(h, AddressHeader), message.get_all(header_name, []) + ): ret.extend(header.addresses) return ret