Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Commit

Permalink
chore: Fixed a bug that treated every string after a header field as …
Browse files Browse the repository at this point in the history
…valid email address
  • Loading branch information
dikayx committed Aug 13, 2024
1 parent d5024fa commit b34fae7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
26 changes: 16 additions & 10 deletions mapy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from email.parser import HeaderParser
from email import message_from_string
from email.message import Message
from email.utils import parseaddr

import dateutil.parser
import pygal
Expand Down Expand Up @@ -71,23 +72,28 @@ def parse_date(line: str) -> datetime:
def get_header_value(h: str, data: str, rex: str = r'\s*(.*?)(?:\n\S+:|$)') -> str | None:
"""
This function takes a header name and the email header data and
returns the value of the header.
Note: Changed regex from r'\s*(.*?)\n\S+:\s' to r'\s*(.*?)(?:\n\S+:|$)',
to handle headers with no trailing newline. If there are errors, revert.
returns the value of the header. For 'from', 'to', or 'cc' headers,
it validates if the value is a valid email address.
:param h: The header name
:param data: The email header data
:param rex: The regular expression pattern for matching the header value
:return: The value of the header or None if not found
:return: The value of the header or None if not found or invalid email
"""
# Use regular expressions to find header values
r = re.findall('%s:%s' % (h, rex), data, re.X | re.DOTALL | re.I)
if r:
return r[0].strip()
else:
matches = re.findall(f'{h}:{rex}', data, re.X | re.DOTALL | re.I)

if not matches:
return None

header_value = matches[0].strip()

if h.lower() not in {'from', 'to', 'cc'}:
return header_value

_, email = parseaddr(header_value)

return header_value if re.match(r'^[^@]+@[^@]+\.[^@]+$', email) else None


def parse_received_headers(mail_data: str) -> list:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ def test_get_header_value():
# Test non-existing header
assert get_header_value('Cc', header_data) is None

# Test a header that contains an invalid cc: address
header_data = "Cc: <@example.com:"
assert get_header_value('Cc', header_data) is None


def test_parse_received_headers():
received_headers = parse_received_headers(mail_data)
Expand Down

0 comments on commit b34fae7

Please sign in to comment.