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

bpo-37764: Fix infinite loop when parsing unstructured email headers. #15239

Merged
merged 17 commits into from
Aug 31, 2019

Conversation

epicfaace
Copy link
Contributor

@epicfaace epicfaace commented Aug 12, 2019

Fixes a case in which email._header_value_parser.get_unstructured hangs the system for some invalid headers. This covers the cases in which the header contains either:

  • a case without trailing whitespace
  • an invalid encoded word

https://bugs.python.org/issue37764

This fix should also be backported to 3.7 and 3.8

https://bugs.python.org/issue37764

Automerge-Triggered-By: @maxking

Copy link
Contributor

@maxking maxking 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 this PR.
I've made inline comments. Please also see my comments on BPO.

Lib/email/_header_value_parser.py Outdated Show resolved Hide resolved
Lib/email/_header_value_parser.py Outdated Show resolved Hide resolved
Lib/test/test_email/test__header_value_parser.py Outdated Show resolved Hide resolved
@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.

@epicfaace
Copy link
Contributor Author

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@maxking: please review the changes made to this pull request.

Lib/email/_header_value_parser.py Outdated Show resolved Hide resolved
Lib/email/_header_value_parser.py Outdated Show resolved Hide resolved
@@ -1039,7 +1039,7 @@ def get_encoded_word(value):
raise errors.HeaderParseError(
"expected encoded word but found {}".format(value))
remstr = ''.join(remainder)
if len(remstr) > 1 and remstr[0] in hexdigits and remstr[1] in hexdigits:
if len(remstr) > 1 and remstr[0] in hexdigits and remstr[1] in hexdigits and tok.count('?') < 2:
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably needs to be split on multiple line for pep8.

Copy link
Contributor Author

@epicfaace epicfaace Aug 23, 2019

Choose a reason for hiding this comment

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

Done.

Is there a way to check for this automatically? What's the standard I should usually apply? (for example, this file doesn't have two blank lines between each class, which also violates pep8)

Lib/email/_header_value_parser.py Outdated Show resolved Hide resolved
Lib/email/_header_value_parser.py Outdated Show resolved Hide resolved
@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.

And if you don't make the requested changes, you will be poked with soft cushions!

@DeltaF1
Copy link

DeltaF1 commented Aug 22, 2019

I applied this patch to a local install of python 3.7, and the problem seemed to persist with my test case. The attached file has the email I parsed, with some content redacted for privacy. It appears to be very mangled, but even so the library should probably not fall into an infinite loop if it receives mangled data.

Hopefully you can reproduce the error, otherwise it might just be an artefact of my patching method.

mangled_message.txt

@epicfaace
Copy link
Contributor Author

I applied this patch to a local install of python 3.7, and the problem seemed to persist with my test case. The attached file has the email I parsed, with some content redacted for privacy. It appears to be very mangled, but even so the library should probably not fall into an infinite loop if it receives mangled data.

Hopefully you can reproduce the error, otherwise it might just be an artefact of my patching method.

mangled_message.txt

I did add a test in this PR with that mangled message, but it doesn't seem to hang. Either it's an artefact with your patching method, or a different issue with 3.7.

@epicfaace
Copy link
Contributor Author

I have made the requested changes; please review again.

Copy link
Contributor

@maxking maxking left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @epicfaace !

@maxking maxking changed the title bpo-37764: fix hang case in get_unstructured bpo-37764: Fix infinite loop when parsing unstrucuted email headers. Aug 31, 2019
@maxking maxking changed the title bpo-37764: Fix infinite loop when parsing unstrucuted email headers. bpo-37764: Fix infinite loop when parsing unstructured email headers. Aug 31, 2019
@miss-islington miss-islington merged commit c5b242f into python:master Aug 31, 2019
@miss-islington
Copy link
Contributor

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

@miss-islington
Copy link
Contributor

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

@miss-islington miss-islington self-assigned this Aug 31, 2019
@miss-islington
Copy link
Contributor

Sorry @epicfaace, I had trouble checking out the 3.7 backport branch.
Please backport using cherry_picker on command line.
cherry_picker c5b242f87f31286ad38991bc3868cf4cfbf2b681 3.7

@maxking maxking added the type-security A security issue label Aug 31, 2019
epicfaace added a commit to epicfaace/cpython that referenced this pull request Sep 3, 2019
…aders. (pythonGH-15239)

Fixes a case in which email._header_value_parser.get_unstructured hangs the system for some invalid headers. This covers the cases in which the header contains either:
- a case without trailing whitespace
- an invalid encoded word

https://bugs.python.org/issue37764

This fix should also be backported to 3.7 and 3.8

https://bugs.python.org/issue37764
(cherry picked from commit c5b242f)

Co-authored-by: Ashwin Ramaswami <aramaswamis@gmail.com>
@bedevere-bot
Copy link

GH-15654 is a backport of this pull request to the 3.7 branch.

@epicfaace epicfaace deleted the issue37764 branch September 3, 2019 04:42
miss-islington pushed a commit that referenced this pull request Sep 3, 2019
…aders. (GH-15239) (GH-15654)

…aders. (GH-15239)

Fixes a case in which email._header_value_parser.get_unstructured hangs the system for some invalid headers. This covers the cases in which the header contains either:
- a case without trailing whitespace
- an invalid encoded word

https://bugs.python.org/issue37764

This fix should also be backported to 3.7 and 3.8

https://bugs.python.org/issue37764
(cherry picked from commit c5b242f)

Co-authored-by: Ashwin Ramaswami <aramaswamis@gmail.com>





https://bugs.python.org/issue37764
maxking pushed a commit to maxking/cpython-1 that referenced this pull request Sep 5, 2019
…aders. (pythonGH-15239)

Fixes a case in which email._header_value_parser.get_unstructured hangs the system for some invalid headers. This covers the cases in which the header contains either:
- a case without trailing whitespace
- an invalid encoded word

https://bugs.python.org/issue37764

This fix should also be backported to 3.7 and 3.8

https://bugs.python.org/issue37764
(cherry picked from commit c5b242f)

Co-authored-by: Ashwin Ramaswami <aramaswamis@gmail.com>
@bedevere-bot
Copy link

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

maxking added a commit that referenced this pull request Sep 5, 2019
…aders. (GH-15239) (GH-15686)

Fixes a case in which email._header_value_parser.get_unstructured hangs the system for some invalid headers. This covers the cases in which the header contains either:
- a case without trailing whitespace
- an invalid encoded word

https://bugs.python.org/issue37764

This fix should also be backported to 3.7 and 3.8

https://bugs.python.org/issue37764
(cherry picked from commit c5b242f)

Co-authored-by: Ashwin Ramaswami <aramaswamis@gmail.com>
lisroach pushed a commit to lisroach/cpython that referenced this pull request Sep 10, 2019
…pythonGH-15239)

Fixes a case in which email._header_value_parser.get_unstructured hangs the system for some invalid headers. This covers the cases in which the header contains either:
- a case without trailing whitespace
- an invalid encoded word

https://bugs.python.org/issue37764

This fix should also be backported to 3.7 and 3.8


https://bugs.python.org/issue37764
DinoV pushed a commit to DinoV/cpython that referenced this pull request Jan 14, 2020
…pythonGH-15239)

Fixes a case in which email._header_value_parser.get_unstructured hangs the system for some invalid headers. This covers the cases in which the header contains either:
- a case without trailing whitespace
- an invalid encoded word

https://bugs.python.org/issue37764

This fix should also be backported to 3.7 and 3.8


https://bugs.python.org/issue37764
websurfer5 pushed a commit to websurfer5/cpython that referenced this pull request Jul 20, 2020
…pythonGH-15239)

Fixes a case in which email._header_value_parser.get_unstructured hangs the system for some invalid headers. This covers the cases in which the header contains either:
- a case without trailing whitespace
- an invalid encoded word

https://bugs.python.org/issue37764

This fix should also be backported to 3.7 and 3.8


https://bugs.python.org/issue37764
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-security A security issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants