Skip to content

Commit fa6304a

Browse files
authored
bpo-44690: Adopt binacii.a2b_base64's strict mode in base64.b64decode (GH-27272)
* Use binascii.a2b_base64 to validate b64decode input. This change leads to exception messages changes (mostly). * Added more information to docstring of b64decode * Added a reference to binascii.a2b_base64 in the docs
1 parent e41912c commit fa6304a

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

Doc/library/base64.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ The modern interface provides:
7878
these non-alphabet characters in the input result in a
7979
:exc:`binascii.Error`.
8080

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

8284
.. function:: standard_b64encode(s)
8385

Lib/base64.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,16 @@ def b64decode(s, altchars=None, validate=False):
7676
normal base-64 alphabet nor the alternative alphabet are discarded prior
7777
to the padding check. If validate is True, these non-alphabet characters
7878
in the input result in a binascii.Error.
79+
For more information about the strict base64 check, see:
80+
81+
https://docs.python.org/3.11/library/binascii.html#binascii.a2b_base64
7982
"""
8083
s = _bytes_from_decode_data(s)
8184
if altchars is not None:
8285
altchars = _bytes_from_decode_data(altchars)
8386
assert len(altchars) == 2, repr(altchars)
8487
s = s.translate(bytes.maketrans(altchars, b'+/'))
85-
if validate and not re.fullmatch(b'[A-Za-z0-9+/]*={0,2}', s):
86-
raise binascii.Error('Non-base64 digit found')
87-
return binascii.a2b_base64(s)
88+
return binascii.a2b_base64(s, strict_mode=validate)
8889

8990

9091
def standard_b64encode(s):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Adopt *binacii.a2b_base64*'s strict mode in *base64.b64decode*.

0 commit comments

Comments
 (0)