summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIdan Moral <idan22moral@gmail.com>2021-08-23 23:44:28 (GMT)
committerGitHub <noreply@github.com>2021-08-23 23:44:28 (GMT)
commitfa6304a5225787054067bb56089632146d288b20 (patch)
tree8c4e7cb19d92863e5e30a33a0a59dab201647221
parente41912c6348362489d2514565a70782591f23902 (diff)
downloadcpython-fa6304a5225787054067bb56089632146d288b20.zip
cpython-fa6304a5225787054067bb56089632146d288b20.tar.gz
cpython-fa6304a5225787054067bb56089632146d288b20.tar.bz2
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
-rw-r--r--Doc/library/base64.rst2
-rwxr-xr-xLib/base64.py7
-rw-r--r--Misc/NEWS.d/next/Library/2021-07-20-22-03-24.bpo-44690.tV7Zjg.rst1
3 files changed, 7 insertions, 3 deletions
diff --git a/Doc/library/base64.rst b/Doc/library/base64.rst
index f91547b..29e52ad 100644
--- a/Doc/library/base64.rst
+++ b/Doc/library/base64.rst
@@ -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)
diff --git a/Lib/base64.py b/Lib/base64.py
index e1256ad..ffe2ce7 100755
--- a/Lib/base64.py
+++ b/Lib/base64.py
@@ -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):
diff --git a/Misc/NEWS.d/next/Library/2021-07-20-22-03-24.bpo-44690.tV7Zjg.rst b/Misc/NEWS.d/next/Library/2021-07-20-22-03-24.bpo-44690.tV7Zjg.rst
new file mode 100644
index 0000000..1d11848
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-07-20-22-03-24.bpo-44690.tV7Zjg.rst
@@ -0,0 +1 @@
+Adopt *binacii.a2b_base64*'s strict mode in *base64.b64decode*. \ No newline at end of file