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

AttributeError in email.message.get_body() #87058

Closed
xkobal mannequin opened this issue Jan 11, 2021 · 10 comments
Closed

AttributeError in email.message.get_body() #87058

xkobal mannequin opened this issue Jan 11, 2021 · 10 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes topic-email type-bug An unexpected behavior, bug, or error

Comments

@xkobal
Copy link
Mannequin

xkobal mannequin commented Jan 11, 2021

BPO 42892
Nosy @warsaw, @bitdancer, @ambv, @miss-islington, @iritkatriel, @akulakov, @xkobal
PRs
  • bpo-42892 Fix AttributeError in email.message.get_body() #24192
  • bpo-42892: fix email multipart attribute error #26903
  • [3.10] bpo-42892: fix email multipart attribute error (GH-26903) #27492
  • [3.9] bpo-42892: fix email multipart attribute error (GH-26903) #27493
  • Files
  • email_bad_formatted.eml
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-07-30.17:31:19.876>
    created_at = <Date 2021-01-11.16:45:57.858>
    labels = ['3.11', 'type-bug', 'expert-email', '3.10', '3.9']
    title = 'AttributeError in email.message.get_body()'
    updated_at = <Date 2021-07-30.17:31:19.875>
    user = 'https://github.com/xkobal'

    bugs.python.org fields:

    activity = <Date 2021-07-30.17:31:19.875>
    actor = 'lukasz.langa'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-07-30.17:31:19.876>
    closer = 'lukasz.langa'
    components = ['email']
    creation = <Date 2021-01-11.16:45:57.858>
    creator = 'xavier2'
    dependencies = []
    files = ['49734']
    hgrepos = []
    issue_num = 42892
    keywords = ['patch']
    message_count = 10.0
    messages = ['384847', '384848', '393982', '393991', '393996', '396497', '396506', '398574', '398585', '398587']
    nosy_count = 8.0
    nosy_names = ['barry', 'r.david.murray', 'lukasz.langa', 'python-dev', 'miss-islington', 'iritkatriel', 'andrei.avk', 'xavier2']
    pr_nums = ['24192', '26903', '27492', '27493']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue42892'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @xkobal
    Copy link
    Mannequin Author

    xkobal mannequin commented Jan 11, 2021

    Following this issue: https://bugs.python.org/issue33972

    Same bug apply to email.message.get_body() with attached email example and the following code:

            from email.policy import default
            import email
    
            with open('email_bad_formatted.eml', 'rb') as fp:
                msg = email.message_from_binary_file(fp, policy=default)
            body = msg.get_body()

    Result:
    E AttributeError: 'str' object has no attribute 'is_attachment'

    /usr/local/lib/python3.9/email/message.py:978: AttributeError

    @xkobal xkobal mannequin added 3.9 only security fixes topic-email labels Jan 11, 2021
    @xkobal
    Copy link
    Mannequin Author

    xkobal mannequin commented Jan 11, 2021

    Attached PR fix the issue.

    @iritkatriel
    Copy link
    Member

    Can you see how it happened that part is a str?

    @bitdancer
    Copy link
    Member

    Yes, that's the real question. That's what needs to be fixed, otherwise we'll just keep finding new bugs. For example, try calling iter_parts() on that message. It isn't pretty :)

    @bitdancer
    Copy link
    Member

    Actually, I'm wrong. The body of a part can be a string, and that's what's going to happen with a malformed body of something claiming to be a multipart. The problem is that there is code that doesn't guard against this possibility. The following patch against master fixes the bug listed here, as well as iter_parts(). But it causes one test suite failure so it isn't a correct patch as it stands:

    diff --git a/Lib/email/message.py b/Lib/email/message.py
    index 3701b30553..d5d4a2385a 100644
    --- a/Lib/email/message.py
    +++ b/Lib/email/message.py
    @@ -982,7 +982,7 @@ def _find_body(self, part, preferencelist):
                 if subtype in preferencelist:
                     yield (preferencelist.index(subtype), part)
                 return
    -        if maintype != 'multipart':
    +        if maintype != 'multipart' or not self.is_multipart():
                 return
             if subtype != 'related':
                 for subpart in part.iter_parts():
    @@ -1087,7 +1087,7 @@ def iter_parts(self):
     
             Return an empty iterator for a non-multipart.
             """
    -        if self.get_content_maintype() == 'multipart':
    +        if self.is_multipart():
                 yield from self.get_payload()
     
         def get_content(self, *args, content_manager=None, **kw):

    Maybe someone can take this and finish it (with tests)...I may or may not have time to get back to this.

    @akulakov
    Copy link
    Contributor

    I'll try to fix the unit test today.

    @akulakov
    Copy link
    Contributor

    Put up the PR here: #26903

    @iritkatriel iritkatriel added 3.10 only security fixes 3.11 only security fixes labels Jun 28, 2021
    @ambv
    Copy link
    Contributor

    ambv commented Jul 30, 2021

    New changeset e3f877c by andrei kulakov in branch 'main':
    bpo-42892: fix email multipart attribute error (GH-26903)
    e3f877c

    @ambv
    Copy link
    Contributor

    ambv commented Jul 30, 2021

    New changeset 6f95002 by Miss Islington (bot) in branch '3.10':
    bpo-42892: fix email multipart attribute error (GH-26903) (GH-27492)
    6f95002

    @ambv
    Copy link
    Contributor

    ambv commented Jul 30, 2021

    New changeset 0f42b72 by Miss Islington (bot) in branch '3.9':
    bpo-42892: fix email multipart attribute error (GH-26903) (GH-27493)
    0f42b72

    @ambv ambv closed this as completed Jul 30, 2021
    @ambv ambv added the type-bug An unexpected behavior, bug, or error label Jul 30, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes 3.11 only security fixes topic-email type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants