Skip to content

ISO-8859-1 HTML Part throws exception in decode #484

Description

@mkalus

Bug Metadata

  • Version of extract_msg: 0.56.0
  • Your python version: Python 3.12.3
  • How did you launch extract_msg?
    • My command line or
    • I used the extract_msg package

Describe the bug
I have a message with an ISO-8859-1 HTML body code which throws an error at position https://github.com/TeamMsgExtractor/msg-extractor/blob/master/extract_msg/msg_classes/message_base.py#L297 with this code:

bodyParts.attach(MIMEText(self.htmlBody.decode('utf-8'), 'html', c))

Due to security compliance issues, I cannot send you the mail, but I can show you the problematic part 😄

The message was like this (Python bytes):

<html...><head><metahttp-equiv=Content-Typecontent="text/html; charset=iso-8859-1">
...
<body>
...
die Repros hat f\xfcr uns
....

As you can see, there is a byte 0xfc in the body content, which will translate to the letter ü in ISO-8859-1, but will throw a "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfc in position xyz: invalid start byte" in the UTF-8 decode above.

When I change self.htmlBody.decode('utf-8') to self.htmlBody.decode('iso-8859-1), the decoding works, obviously. I also checked other issues about this, but they treated other problems (like non-UTF-8 in subject, etc.), I believe. Adding overrideEncoding='chardet' did nothing here. And #480 occures at another code position.

We could probably solve the issue by checking the character type of the body somehow. I am not 100% sure what will be an elegant solution. One might try to wrap the line above in a try/except block and check the content more thoroughly on an exception (e.g. by detecting the meta line or using chardet). I know this is a nasty solution, but, well, it's msg format, so it is bound to be nasty.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions