-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-102988: Detect email address parsing errors and return empty tuple to indicate the parsing error (old API) #108250
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3319,32 +3319,96 @@ def test_getaddresses(self): | |||||||||||||
[('Al Person', 'aperson@dom.ain'), | ||||||||||||||
('Bud Person', 'bperson@dom.ain')]) | ||||||||||||||
|
||||||||||||||
def test_getaddresses_comma_in_name(self): | ||||||||||||||
"""GH-106669 regression test.""" | ||||||||||||||
self.assertEqual( | ||||||||||||||
utils.getaddresses( | ||||||||||||||
[ | ||||||||||||||
'"Bud, Person" <bperson@dom.ain>', | ||||||||||||||
'aperson@dom.ain (Al Person)', | ||||||||||||||
'"Mariusz Felisiak" <to@example.com>', | ||||||||||||||
] | ||||||||||||||
), | ||||||||||||||
[ | ||||||||||||||
('Bud, Person', 'bperson@dom.ain'), | ||||||||||||||
('Al Person', 'aperson@dom.ain'), | ||||||||||||||
('Mariusz Felisiak', 'to@example.com'), | ||||||||||||||
], | ||||||||||||||
) | ||||||||||||||
def test_getaddresses_parsing_errors(self): | ||||||||||||||
"""Test for parsing errors from CVE-2023-27043 and CVE-2019-16056""" | ||||||||||||||
eq = self.assertEqual | ||||||||||||||
eq(utils.getaddresses(['alice@example.org(<bob@example.com>']), | ||||||||||||||
[('', '')]) | ||||||||||||||
eq(utils.getaddresses(['alice@example.org)<bob@example.com>']), | ||||||||||||||
[('', '')]) | ||||||||||||||
eq(utils.getaddresses(['alice@example.org<<bob@example.com>']), | ||||||||||||||
[('', '')]) | ||||||||||||||
eq(utils.getaddresses(['alice@example.org><bob@example.com>']), | ||||||||||||||
[('', '')]) | ||||||||||||||
eq(utils.getaddresses(['alice@example.org@<bob@example.com>']), | ||||||||||||||
[('', '')]) | ||||||||||||||
eq(utils.getaddresses(['alice@example.org,<bob@example.com>']), | ||||||||||||||
[('', 'alice@example.org'), ('', 'bob@example.com')]) | ||||||||||||||
eq(utils.getaddresses(['alice@example.org;<bob@example.com>']), | ||||||||||||||
[('', '')]) | ||||||||||||||
eq(utils.getaddresses(['alice@example.org:<bob@example.com>']), | ||||||||||||||
[('', '')]) | ||||||||||||||
eq(utils.getaddresses(['alice@example.org.<bob@example.com>']), | ||||||||||||||
[('', '')]) | ||||||||||||||
eq(utils.getaddresses(['alice@example.org"<bob@example.com>']), | ||||||||||||||
[('', '')]) | ||||||||||||||
eq(utils.getaddresses(['alice@example.org[<bob@example.com>']), | ||||||||||||||
[('', '')]) | ||||||||||||||
eq(utils.getaddresses(['alice@example.org]<bob@example.com>']), | ||||||||||||||
[('', '')]) | ||||||||||||||
Comment on lines
+3347
to
+3348
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. However, not a bad idea to add extra tests. That way if someone else changes this code in the future these other corner cases will be tested for. |
||||||||||||||
eq(utils.getaddresses(['"Alice, alice@example.org" <bob@example.com>']), | ||||||||||||||
[('Alice, alice@example.org', 'bob@example.com')]) | ||||||||||||||
|
||||||||||||||
def test_parseaddr_parsing_errors(self): | ||||||||||||||
"""Test for parsing errors from CVE-2023-27043 and CVE-2019-16056""" | ||||||||||||||
eq = self.assertEqual | ||||||||||||||
eq(utils.parseaddr(['alice@example.org(<bob@example.com>']), | ||||||||||||||
('', '')) | ||||||||||||||
eq(utils.parseaddr(['alice@example.org)<bob@example.com>']), | ||||||||||||||
('', '')) | ||||||||||||||
eq(utils.parseaddr(['alice@example.org<<bob@example.com>']), | ||||||||||||||
('', '')) | ||||||||||||||
eq(utils.parseaddr(['alice@example.org><bob@example.com>']), | ||||||||||||||
('', '')) | ||||||||||||||
eq(utils.parseaddr(['alice@example.org@<bob@example.com>']), | ||||||||||||||
('', '')) | ||||||||||||||
eq(utils.parseaddr(['alice@example.org,<bob@example.com>']), | ||||||||||||||
('', '')) | ||||||||||||||
eq(utils.parseaddr(['alice@example.org;<bob@example.com>']), | ||||||||||||||
('', '')) | ||||||||||||||
eq(utils.parseaddr(['alice@example.org:<bob@example.com>']), | ||||||||||||||
('', '')) | ||||||||||||||
eq(utils.parseaddr(['alice@example.org.<bob@example.com>']), | ||||||||||||||
('', '')) | ||||||||||||||
eq(utils.parseaddr(['alice@example.org"<bob@example.com>']), | ||||||||||||||
('', '')) | ||||||||||||||
eq(utils.parseaddr(['alice@example.org[<bob@example.com>']), | ||||||||||||||
('', '')) | ||||||||||||||
eq(utils.parseaddr(['alice@example.org]<bob@example.com>']), | ||||||||||||||
('', '')) | ||||||||||||||
eq(utils.parseaddr(['"Alice, alice@example.org" <bob@example.com>']), | ||||||||||||||
('Alice, alice@example.org', 'bob@example.com')) | ||||||||||||||
|
||||||||||||||
def test_getaddresses_nasty(self): | ||||||||||||||
eq = self.assertEqual | ||||||||||||||
eq(utils.getaddresses(['"Sürname, Firstname" <to@example.com>']), | ||||||||||||||
[('Sürname, Firstname', 'to@example.com')]) | ||||||||||||||
eq(utils.getaddresses(['foo: ;']), [('', '')]) | ||||||||||||||
eq(utils.getaddresses( | ||||||||||||||
['[]*-- =~$']), | ||||||||||||||
[('', ''), ('', ''), ('', '*--')]) | ||||||||||||||
eq(utils.getaddresses(['[]*-- =~$']), [('', '')]) | ||||||||||||||
eq(utils.getaddresses( | ||||||||||||||
['foo: ;', '"Jason R. Mastaler" <jason@dom.ain>']), | ||||||||||||||
[('', ''), ('Jason R. Mastaler', 'jason@dom.ain')]) | ||||||||||||||
eq(utils.getaddresses( | ||||||||||||||
[r'Pete(A nice \) chap) <pete(his account)@silly.test(his host)>']), | ||||||||||||||
[('Pete (A nice ) chap his account his host)', 'pete@silly.test')]) | ||||||||||||||
eq(utils.getaddresses( | ||||||||||||||
['(Empty list)(start)Undisclosed recipients :(nobody(I know))']), | ||||||||||||||
[('', '')]) | ||||||||||||||
eq(utils.getaddresses( | ||||||||||||||
['Mary <@machine.tld:mary@example.net>, , jdoe@test . example']), | ||||||||||||||
[('Mary', 'mary@example.net'), ('', ''), ('', 'jdoe@test.example')]) | ||||||||||||||
eq(utils.getaddresses( | ||||||||||||||
['John Doe <jdoe@machine(comment). example>']), | ||||||||||||||
[('John Doe (comment)', 'jdoe@machine.example')]) | ||||||||||||||
eq(utils.getaddresses( | ||||||||||||||
['"Mary Smith: Personal Account" <smith@home.example>']), | ||||||||||||||
[('Mary Smith: Personal Account', 'smith@home.example')]) | ||||||||||||||
eq(utils.getaddresses( | ||||||||||||||
['Undisclosed recipients:;']), | ||||||||||||||
[('', '')]) | ||||||||||||||
eq(utils.getaddresses( | ||||||||||||||
[r'<boss@nil.test>, "Giant; \"Big\" Box" <bob@example.net>']), | ||||||||||||||
[('', 'boss@nil.test'), ('Giant; "Big" Box', 'bob@example.net')]) | ||||||||||||||
|
||||||||||||||
def test_getaddresses_embedded_comment(self): | ||||||||||||||
"""Test proper handling of a nested comment""" | ||||||||||||||
|
@@ -3712,16 +3776,6 @@ def test_bytes_header_parser(self): | |||||||||||||
self.assertIsInstance(msg.get_payload(), str) | ||||||||||||||
self.assertIsInstance(msg.get_payload(decode=True), bytes) | ||||||||||||||
|
||||||||||||||
def test_header_parser_multipart_is_valid(self): | ||||||||||||||
# Don't flag valid multipart emails as having defects | ||||||||||||||
with openfile('msg_47.txt', encoding="utf-8") as fp: | ||||||||||||||
msgdata = fp.read() | ||||||||||||||
|
||||||||||||||
parser = email.parser.Parser(policy=email.policy.default) | ||||||||||||||
parsed_msg = parser.parsestr(msgdata, headersonly=True) | ||||||||||||||
|
||||||||||||||
self.assertEqual(parsed_msg.defects, []) | ||||||||||||||
|
||||||||||||||
def test_bytes_parser_does_not_close_file(self): | ||||||||||||||
with openfile('msg_02.txt', 'rb') as fp: | ||||||||||||||
email.parser.BytesParser().parse(fp) | ||||||||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not check
)(
or any other combination when)
proceeds(
. Probably it could be better to iterate through the string and manually increment and decrement verifying that the counter never goes below0
and it is exactly0
at the endThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this is not necessary. The fix already accounts for this and none of those are an issue.
My current solution works, because instead of trying to actually parse things I just detect an error has occurred by making sure that the resulting number of address 2-Tuples matches the number of Headers that were parsed. Which is why I went this direction. It's super hard to fix the actual parsing logic of RFC 2822 headers :P
With the un-patched python your attack results in multiple Tuples being returned
A cleaner way to say it is this. There are countless ways to trigger this parsing error but there is only one error e.g. the parser will return an abnormal number of output tuples. So, rather than trying to detect every possible input which could trigger the bug, I just detect the one error that they all result in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After looking at this again, it is interesting that my solution works here… why do I check for matching parentheses at all ?
blah@example.com)(<bob&@example.com
I have ideas, but I’ll look into it.