Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Email MIME base64 parser stops at the end of the first padding #137687

Copy link
Copy link
@spencerwuwu

Description

@spencerwuwu
Issue body actions

Bug report

Bug description:

Hi Cpython Developers,

I was testing and comparing different email parsers, and found a parsing discrepancy that seems to be a problem.

MIME-Version: 1.0
Content-Type: application/zip
Content-Disposition: attachment; filename=archive.zip
Content-Transfer-Encoding: base64

UEsDBBQAAAAIAA==
emVkIGZpbGUgY29udGVudA==

With the python's email get_payload method, the return content would stopped at the first "==" as it seems to be the default behavior of base64.b64decode.
Meanwhile, peer implementations (e.g. apache.commons.mal (java), MimeKit (c#), PhpMimeMailParser (php)) will return the whole content.

Below is an running example in python.

import base64
import email

"""
  Parsing the mime format
"""
request = """MIME-Version: 1.0
Content-Type: application/zip
Content-Disposition: attachment; filename=archive.zip
Content-Transfer-Encoding: base64

UEsDBBQAAAAIAA==
emVkIGZpbGUgY29udGVudA==
"""
msg = email.message_from_string(request)
print("Part content:", repr(msg.get_payload(decode=True)))
print()

"""
  Examples of base64
"""
contents = [
        "UEsDBBQAAAAIAA==\nemVkIGZpbGUgY29udGVudA==",
        "UEsDBBQAAAAIAA==emVkIGZpbGUgY29udGVudA==",
        "UEsDBBQAAAAIAA=emVkIGZpbGUgY29udGVudA==",
        "UEsDBBQAAAAIAAemVkIGZpbGUgY29udGVudA==",
        "UEsDBBQAAAAIAA==",
        "emVkIGZpbGUgY29udGVudA=="
        ]
for content in contents:
    decoded_bytes = base64.b64decode(content)
    print(repr(content), " ->")
    print("  ", decoded_bytes)

Output:

Part content: b'PK\x03\x04\x14\x00\x00\x00\x08\x00'

'UEsDBBQAAAAIAA==\nemVkIGZpbGUgY29udGVudA=='  ->
   b'PK\x03\x04\x14\x00\x00\x00\x08\x00'
'UEsDBBQAAAAIAA==emVkIGZpbGUgY29udGVudA=='  ->
   b'PK\x03\x04\x14\x00\x00\x00\x08\x00'
'UEsDBBQAAAAIAA=emVkIGZpbGUgY29udGVudA=='  ->
   b'PK\x03\x04\x14\x00\x00\x00\x08\x00\x07\xa6VB\x06f\x96\xc6R\x066\xf6\xe7FV\xe7@'
'UEsDBBQAAAAIAAemVkIGZpbGUgY29udGVudA=='  ->
   b'PK\x03\x04\x14\x00\x00\x00\x08\x00\x07\xa6VB\x06f\x96\xc6R\x066\xf6\xe7FV\xe7@'
'UEsDBBQAAAAIAA=='  ->
   b'PK\x03\x04\x14\x00\x00\x00\x08\x00'
'emVkIGZpbGUgY29udGVudA=='  ->
   b'zed file content'

Thank you,
Wei-Cheng

CPython versions tested on:

3.15

Operating systems tested on:

Linux

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-emailtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.