Skip to content

bpo-44690: Adopt binacii.a2b_base64's strict mode in base64.b64decode #27272

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

Merged
merged 4 commits into from
Aug 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Doc/library/base64.rst
Original file line number Diff line number Diff line change
@@ -78,6 +78,8 @@ The modern interface provides:
these non-alphabet characters in the input result in a
:exc:`binascii.Error`.

For more information about the strict base64 check, see :func:`binascii.a2b_base64`


.. function:: standard_b64encode(s)

7 changes: 4 additions & 3 deletions Lib/base64.py
Original file line number Diff line number Diff line change
@@ -76,15 +76,16 @@ def b64decode(s, altchars=None, validate=False):
normal base-64 alphabet nor the alternative alphabet are discarded prior
to the padding check. If validate is True, these non-alphabet characters
in the input result in a binascii.Error.
For more information about the strict base64 check, see:

https://docs.python.org/3.11/library/binascii.html#binascii.a2b_base64
"""
s = _bytes_from_decode_data(s)
if altchars is not None:
altchars = _bytes_from_decode_data(altchars)
assert len(altchars) == 2, repr(altchars)
s = s.translate(bytes.maketrans(altchars, b'+/'))
if validate and not re.fullmatch(b'[A-Za-z0-9+/]*={0,2}', s):
raise binascii.Error('Non-base64 digit found')
return binascii.a2b_base64(s)
return binascii.a2b_base64(s, strict_mode=validate)


def standard_b64encode(s):
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adopt *binacii.a2b_base64*'s strict mode in *base64.b64decode*.