FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[CVE-2023-27043] gh-102988: Reject malformed addresses in email.parseaddr() by vstinner · Pull Request #111116 · python/cpython · GitHub

/ cpython Public

[CVE-2023-27043] gh-102988: Reject malformed addresses in email.parseaddr() - #111116

Merged
vstinner merged 14 commits into
python:mainfrom
vstinner:parse_email
Dec 15, 2023
Merged

[CVE-2023-27043] gh-102988: Reject malformed addresses in email.parseaddr()#111116
vstinner merged 14 commits into
python:mainfrom
vstinner:parse_email

Conversation

vstinner commented Oct 20, 2023
edited by github-actions Bot
Loading

Copy link
Copy Markdown
Member

Detect email address parsing errors and return empty tuple to indicate the parsing error (old API). Add an optional 'strict' parameter to getaddresses() and parseaddr() functions. Patch by Thomas Dwyer.


📚 Documentation preview 📚: https://cpython-previews--111116.org.readthedocs.build/

Copy link
Copy Markdown
Member Author

@gpshead @serhiy-storchaka @bitdancer @warsaw: Would you mind to review this security fix?

See issue gh-102988 for the context.

This PR is a copy of PR #108250 but I added strict=True parameter, so it's possible to get the old behavior. I added tests on both modes, strict=True and strict=False.

Copy link
Copy Markdown
Member Author

This PR is a copy of PR #108250 but I added strict=True parameter

My colleague Lumir Balhar @frenzymadness ran an impact check of PR #108250 on Fedora: in short, there is no impact, the test suite of all Python packages (in Fedora) pass with the change. While there were some build errors, they were unrelated to the email issue. For details, see https://copr.fedorainfracloud.org/coprs/lbalhar/email-CVE/builds/ COPR which as more than 4300 builds.

Now with an additional strict parameter, if there is any impacted project, at least there is a way to "opt out".

Copy link
Copy Markdown
Member Author

@tdwyer: Would you mind to review my change, to see if I preserved your work correctly? (code and tests)

vstinner added type-security A security issue needs backport to 3.8 needs backport to 3.10 only security fixes needs backport to 3.11 only security fixes needs backport to 3.12 only security fixes labels Oct 27, 2023

Copy link
Copy Markdown
Member Author

I think that we should backport the change to all branches accepting security fixes. Problem: the change refer to version numbers, which as .. versionchanged:: 3.13. I suppose that if the change is backported, we should compute the next version of each branch, so backport manually.

Copy link
Copy Markdown
Member Author

@ambv @SethMichaelLarson: Would you mind to review this PR?

ambv changed the title gh-102988: email parseaddr() now rejects malformed address gh-102988: Reject malformed addresses in email.parseaddr() Oct 27, 2023

ambv commented Oct 27, 2023

Copy link
Copy Markdown
Contributor

Why is this a separate PR from #108250?

Comment thread Doc/whatsnew/3.13.rst Outdated
parameter to these two functions: use ``strict=False`` to get the old
behavior, accept malformed inputs.
(Contributed by Thomas Dwyer for :gh:`102988` to ameliorate CVE-2023-27043
(Contributed by Thomas Dwyer for :gh:`102988` to improve the CVE-2023-27043

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

TIL a new word.

serhiy-storchaka self-requested a review October 27, 2023 16:39
Comment thread Lib/email/utils.py Outdated

specialsre = re.compile(r'[][\\()<>@,:;".]')
escapesre = re.compile(r'[\\"]')
realname_comma_re = re.compile(r'"[^"]*,[^"]*"')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
realname_comma_re = re.compile(r'"[^"]*,[^"]*"')
realname_comma_re = re.compile(r'"[^",]*+,[^"]*+"')

It is faster. But I am not sure that the use of such regex is correct.

Comment thread Lib/email/utils.py Outdated
def _pre_parse_validation(email_header_fields):
accepted_values = []
for v in email_header_fields:
s = v.replace('\\(', '').replace('\\)', '')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

But what if that backslash was already escaped with a backslash? For example \\) or \\\\).

Comment thread Lib/email/utils.py Outdated
Comment thread Lib/email/utils.py Outdated

Copy link
Copy Markdown
Member Author

Why is this a separate PR from #108250?

I'm not the author of the other PR. I copied the other PR and added strict parameter.

ambv commented Oct 27, 2023

Copy link
Copy Markdown
Contributor

I'm not the author of this PR and I was able to make commits to it.

Copy link
Copy Markdown
Member Author

I'm not the author of this PR and I was able to make commits to it.

I don't feel comfortable to make significant change of a PR without asking the author. I prefer to create a separated PR and ask for review.

vstinner commented Oct 30, 2023
edited
Loading

Copy link
Copy Markdown
Member Author

Is this behavior a bug or a feature? I don't know how ; is supposed to behave.

Details
$ python
Python 3.11.6 (main, Oct  3 2023, 00:00:00) [GCC 13.2.1 20230728 (Red Hat 13.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from email.utils import getaddresses
>>> from pprint import pprint
>>> pprint(getaddresses('<bob@example.org>; <alice@example.org>'))
[('', ''),
 ('', 'b'),
 ('', 'o'),
 ('', 'b'),
 ('', ''),
 ('', 'e'),
 ('', 'x'),
 ('', 'a'),
 ('', 'm'),
 ('', 'p'),
 ('', 'l'),
 ('', 'e'),
 ('', '.'),
 ('', 'o'),
 ('', 'r'),
 ('', 'g'),
 ('', ''),
 ('', ''),
 ('', ''),
 ('', ''),
 ('', 'a'),
 ('', 'l'),
 ('', 'i'),
 ('', 'c'),
 ('', 'e'),
 ('', ''),
 ('', 'e'),
 ('', 'x'),
 ('', 'a'),
 ('', 'm'),
 ('', 'p'),
 ('', 'l'),
 ('', 'e'),
 ('', '.'),
 ('', 'o'),
 ('', 'r'),
 ('', 'g'),
 ('', '')]

Copy link
Copy Markdown
Member Author

Is this behavior a bug or a feature? I don't know how ; is supposed to behave.

Oh. getaddresses() expects a sequence, not a string :-)

Copy link
Copy Markdown
Member Author

Except of parsedate_tz() function, Lib/email/_parseaddr.py file didn't evolve much it was added in 2002 by commit 030ddf7. The file was created from Lib/rfc822.py which was added in 1992 (commit 01ca336).

The latest major change was done in... 1997 with commit be7c45e

New address parser by Ben Escoto replaces Sjoerd Mullender's parseaddr()

The latest minor change was done in 2019 to fix CVE-2019-16056: commit 8cb65d1 of issue #78336.

Copy link
Copy Markdown
Member Author

What if the input is '"Jane Doe" jane@example.net, "John Doe" john@example.net'?

Oh, realname_comma_re replaces "Jane Doe" <jane@example.net>, "John Doe" <john@example.net> with "Jane Doe <john@example.net> which is invalid...

Copy link
Copy Markdown
Member Author

vstinner marked this pull request as draft October 30, 2023 14:20

Copy link
Copy Markdown
Member Author

Is it time to backport the change to Python 3.8-3.12? So far, nobody reported any regression in Python 3.13.

mcepl commented Apr 17, 2024
edited
Loading

Copy link
Copy Markdown
Contributor

Of course, I am eager for reviews and comments.

So far, the only issue I have heard about was confusion about str/unicode objects in Python 2.7 (https://bugzilla.suse.com/1222537), which I think may be your problem as well.

Also, I left some comments on that commit.

encukou commented Apr 22, 2024

Copy link
Copy Markdown
Member

Is it time to backport the change to Python 3.8-3.12

According to this comment, not yet: #102988 (comment)

hrnciar pushed a commit to fedora-python/cpython that referenced this pull request Jun 7, 2024
…n email.parseaddr() (python#111116)

Detect email address parsing errors and return empty tuple to
indicate the parsing error (old API). Add an optional 'strict'
parameter to getaddresses() and parseaddr() functions. Patch by
Thomas Dwyer.

Co-Authored-By: Thomas Dwyer <github@tomd.tel>
hrnciar pushed a commit to fedora-python/cpython that referenced this pull request Aug 7, 2024
…n email.parseaddr() (python#111116)

Detect email address parsing errors and return empty tuple to
indicate the parsing error (old API). Add an optional 'strict'
parameter to getaddresses() and parseaddr() functions. Patch by
Thomas Dwyer.

Co-Authored-By: Thomas Dwyer <github@tomd.tel>
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
….parseaddr() (python#111116)

Detect email address parsing errors and return empty tuple to
indicate the parsing error (old API). Add an optional 'strict'
parameter to getaddresses() and parseaddr() functions. Patch by
Thomas Dwyer.

Co-Authored-By: Thomas Dwyer <github@tomd.tel>

Copy link
Copy Markdown

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.10.
🐍🍒⛏🤖

Copy link
Copy Markdown

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

Copy link
Copy Markdown

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.11.
🐍🍒⛏🤖

Copy link
Copy Markdown

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.9.
🐍🍒⛏🤖

Copy link
Copy Markdown

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.8.
🐍🍒⛏🤖

Copy link
Copy Markdown

Sorry, @vstinner, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 4a153a1d3b18803a684cd1bcc2cdf3ede3dbae19 3.12

Copy link
Copy Markdown

Sorry, @vstinner, I could not cleanly backport this to 3.10 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 4a153a1d3b18803a684cd1bcc2cdf3ede3dbae19 3.10

Copy link
Copy Markdown

Sorry, @vstinner, I could not cleanly backport this to 3.11 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 4a153a1d3b18803a684cd1bcc2cdf3ede3dbae19 3.11

Copy link
Copy Markdown

Sorry, @vstinner, I could not cleanly backport this to 3.9 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 4a153a1d3b18803a684cd1bcc2cdf3ede3dbae19 3.9

Copy link
Copy Markdown

Sorry, @vstinner, I could not cleanly backport this to 3.8 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 4a153a1d3b18803a684cd1bcc2cdf3ede3dbae19 3.8

bedevere-app Bot commented Sep 6, 2024

Copy link
Copy Markdown

GH-123766 is a backport of this pull request to the 3.12 branch.

bedevere-app Bot commented Sep 6, 2024

Copy link
Copy Markdown

GH-123767 is a backport of this pull request to the 3.11 branch.

bedevere-app Bot commented Sep 6, 2024

Copy link
Copy Markdown

GH-123768 is a backport of this pull request to the 3.10 branch.

bedevere-app Bot commented Sep 6, 2024

Copy link
Copy Markdown

GH-123769 is a backport of this pull request to the 3.9 branch.

bedevere-app Bot commented Sep 6, 2024

Copy link
Copy Markdown

GH-123770 is a backport of this pull request to the 3.8 branch.

Copy link
Copy Markdown
Contributor

supports_strict_parsing is not mentioned in the email.utils docs.

gpshead commented Jun 1, 2025

Copy link
Copy Markdown
Member

please open a new issue if there's a lingering docs problem.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type-bug An unexpected behavior, bug, or error type-security A security issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.


Back | FazBrowse Home | New Git URL