Skip to content

base64 roundtrip failure when altchars contains duplicate bytes #140879

@T90REAL

Description

@T90REAL

Bug report

Bug description:

base64.b64encode and base64.b64decode use asymmetric translation logic when the altchars argument contains duplicate bytes. This causes data to become corrupted during a roundtrip (encode -> decode).

import base64

data_in = b'\x00\x00\x00\x03\xe0'
altchars = b'\x00\x00'
encoded = base64.b64encode(data_in, altchars=altchars)
data_out = base64.b64decode(encoded, altchars=altchars)

assert data_in == data_out, "Roundtrip failed! Data was corrupted."
Traceback (most recent call last):
  File "/data/src/test.py", line 8, in <module>
    assert data_in == data_out, "Roundtrip failed! Data was corrupted."
           ^^^^^^^^^^^^^^^^^^^
AssertionError: Roundtrip failed! Data was corrupted.

The script should execute successfully and the assertion should pass.

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directory

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions