Skip to content

Improve definition of "invalid padding" in base64.b64decodeย #139287

@LamentXU123

Description

@LamentXU123

In the doc of b64decode:

Image

It is claimed that an Exception will be raised if input byte s is incorrectly padded. However, with the argument validate set to False, which is the default value, python check if s is wrongly padded when some padding is missing, but don't check if padding is redundant.

import base64
print(base64.b64decode('aGVsbG8gd29ybGQ=')) #  hello world
print(base64.b64decode('aGVsbG8gd29ybGQ======')) #  hello world
print(base64.b64decode('aGVsbG8gd29ybGQ======', validate=True)) # binascii.Error: Non-base64 digit found
print(base64.b64decode('aGVsbG8gd29ybGQ')) # binascii.Error: Incorrect padding

I think if we could maybe made this clearer here? This doc really confused me when I'm decoding some base64 data that could be wrong (I expect an error will be raised if the padding of the input is wrong). Thanks.

Edit1: I think the program regard the redundant padding char as char after padding which should be discarded in default mode. Humm, I'm not sure should we consider redundant padding char as an Incorrect padding . For now, we treat aGVsbG8gd29ybGQ as an Incorrect padding but not aGVsbG8gd29ybGQ==, while the correct padding is aGVsbG8gd29ybGQ=.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirpendingThe issue will be closed if no feedback is provided

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions