Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-102988: Detect email address parsing errors and return empty tuple to indicate the parsing error (old API) #102990

Closed
wants to merge 2 commits into from

Conversation

tdwyer
Copy link
Contributor

@tdwyer tdwyer commented Mar 24, 2023

Pull Request title

gh-102988: This PR is designed to detect parsing errors and return an empty tuple to indicate the parsing error. Additionally, this PR updates the test_email.py to check for these bugs, as well as, adds some other wacky Address Headers that are in the examples of RFC 2822 and makes sure they are being parsed correctly.

I realize that this PR dose not actually track down the bug and fix it. It simply detects the error has happened and returns a parsing error. However, Lib/email/utils.py is a much simple file than Lib/email/_parseaddr.py, so it is much easier to review this change. Additionally, there are actually multiple bugs which are causing erroneous output. Tracing the code flow for each and fixing them would be prone to error considering all of the wacky stuff that RFC 2822 allows for in Address headers. Finally, this change is actually rather simple.

@tdwyer tdwyer requested a review from a team as a code owner March 24, 2023 04:32
@bedevere-bot
Copy link

Most changes to Python require a NEWS entry.

Please add it using the blurb_it web app or the blurb command-line tool.

@cpython-cla-bot
Copy link

cpython-cla-bot bot commented Mar 24, 2023

All commit authors signed the Contributor License Agreement.
CLA signed

@arhadthedev arhadthedev changed the title Detect parsing errors and return empty tuple to indicate the parsing error gh-102988: Detect parsing errors and return empty tuple to indicate the parsing error Mar 24, 2023
@arhadthedev arhadthedev added stdlib Python modules in the Lib dir topic-email labels Mar 24, 2023
@bitdancer bitdancer changed the title gh-102988: Detect parsing errors and return empty tuple to indicate the parsing error gh-102988: Detect email address parsing errors and return empty tuple to indicate the parsing error (old API) Mar 24, 2023
@bedevere-bot
Copy link

Most changes to Python require a NEWS entry.

Please add it using the blurb_it web app or the blurb command-line tool.

Copy link
Member

@gpshead gpshead left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the PR!

for v in fieldvalues:
s = str(v).replace('\\(', '').replace('\\)', '')
if s.count('(') != s.count(')'):
fieldvalues.remove(v)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this loop is modifying the apparent list it is iterating over within the loop. that makes reasoning about its exact behavior hard. removing an item could mean you wind up skipping an item, appending an item could make the loop iterate over that. furthermore remove is O(n)... you're re-finding the item to remove to remove it. Also, this code modifys the passed in list in place before returning it.

A better code pattern for this is to build up a new list and return that. never modifying the input.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like:

accepted_values = []
for v in fieldvalues:
    s = v.replace('\\(', '').replace('\\)', '')
    if s.count('(') != s.count(')'):
        v = ('', '')
    accepted_values.append(v)

return accepted_values

"""Validate the parsed values are syntactically correct"""
for v in parsedvalues:
if '[' in v[1]:
parsedvalues.remove(v)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same design comment as above, don't modify the list being iterated over and don't modify the argument in place.


n = 0
for v in fieldvalues:
n += str(v).count(',') + 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get rid of this str(v)


def getaddresses(fieldvalues):
"""Return a list of (REALNAME, EMAIL) for each fieldvalue."""
fieldvalues = _pre_parse_validation(fieldvalues)
all = COMMASPACE.join(str(v) for v in fieldvalues)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing code already calls str(v) here... this is a problem. It means the input can be anything. But we probably cannot change it in a patch release for a security fix.

To avoid propagating this mistake into more lines of code spread out, I suggest changing this function to do:

fieldvalues = [str(v) for v in fieldvalues]

on the first line and get rid of all subsequent str(v) calls on anything from fieldvalues in this function or in functions it calls.

parsedvalues.append(('', ''))

return parsedvalues


def getaddresses(fieldvalues):
"""Return a list of (REALNAME, EMAIL) for each fieldvalue."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add documentation mentioning that fieldvalues that could not be parsed may cause a ('', '') item to be returned in their place.

def _pre_parse_validation(fieldvalues):
"""Validate the field values are syntactically correct"""
for v in fieldvalues:
s = str(v).replace('\\(', '').replace('\\)', '')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See later comments on str(v) being bad, we can get rid of it here.

@@ -106,12 +106,42 @@ def formataddr(pair, charset='utf-8'):
return address


def _pre_parse_validation(fieldvalues):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the name "fieldvalues" is non-specific. i realize it comes from the name in getaddress() but we should make it more clear what these are. email_address_fields perhaps?

Related: I don't this docstring adds meaningful value. naming the function and parameter right along with it being short code is sufficiently self explanatory for this internal function. get rid of the docstring.

def _post_parse_validation(parsedvalues):
"""Validate the parsed values are syntactically correct"""
for v in parsedvalues:
if '[' in v[1]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment explaining why only [ is not allowed. ideally with a link to the relevant RFC or similar.

return fieldvalues


def _post_parse_validation(parsedvalues):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parsed_email_address_tuples perhaps?

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@gpshead gpshead self-assigned this Apr 28, 2023
@gpshead gpshead requested a review from bitdancer April 28, 2023 21:53
@bedevere-bot
Copy link

Most changes to Python require a NEWS entry.

Please add it using the blurb_it web app or the blurb command-line tool.

1 similar comment
@bedevere-bot
Copy link

Most changes to Python require a NEWS entry.

Please add it using the blurb_it web app or the blurb command-line tool.

Copy link

@theta682 theta682 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stylistic suggestions

eq(utils.getaddresses(['alice@example.org(<bob@example.com>']),
[('', '')])
eq(utils.getaddresses(['alice@example.org)<bob@example.com>']),
[('' ,'')])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[('' ,'')])
[('', '')])

eq(utils.getaddresses(['alice@example.org)<bob@example.com>']),
[('' ,'')])
eq(utils.getaddresses(['alice@example.org<<bob@example.com>']),
[('' ,'')])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[('' ,'')])
[('', '')])

eq(utils.getaddresses(['alice@example.org<<bob@example.com>']),
[('' ,'')])
eq(utils.getaddresses(['alice@example.org><bob@example.com>']),
[('' ,'')])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[('' ,'')])
[('', '')])

eq(utils.getaddresses(['alice@example.org><bob@example.com>']),
[('' ,'')])
eq(utils.getaddresses(['alice@example.org@<bob@example.com>']),
[('' ,'')])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[('' ,'')])
[('', '')])

eq(utils.getaddresses(['alice@example.org,<bob@example.com>']),
[('', 'alice@example.org'), ('', 'bob@example.com')])
eq(utils.getaddresses(['alice@example.org;<bob@example.com>']),
[('' ,'')])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[('' ,'')])
[('', '')])

eq(utils.parseaddr(['alice@example.org;<bob@example.com>']),
('' ,''))
eq(utils.parseaddr(['alice@example.org:<bob@example.com>']),
('' ,''))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
('' ,''))
[('', '')])

eq(utils.parseaddr(['alice@example.org:<bob@example.com>']),
('' ,''))
eq(utils.parseaddr(['alice@example.org.<bob@example.com>']),
('' ,''))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
('' ,''))
[('', '')])

eq(utils.parseaddr(['alice@example.org.<bob@example.com>']),
('' ,''))
eq(utils.parseaddr(['alice@example.org"<bob@example.com>']),
('' ,''))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
('' ,''))
[('', '')])

eq(utils.parseaddr(['alice@example.org"<bob@example.com>']),
('' ,''))
eq(utils.parseaddr(['alice@example.org[<bob@example.com>']),
('' ,''))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
('' ,''))
[('', '')])

eq(utils.parseaddr(['alice@example.org[<bob@example.com>']),
('' ,''))
eq(utils.parseaddr(['alice@example.org]<bob@example.com>']),
('' ,''))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
('' ,''))
[('', '')])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting changes stdlib Python modules in the Lib dir topic-email type-security A security issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants