Skip to content

Commit e9db343

Browse files
Revert "Make validate=True by default in base64.b64decode()."
This reverts commit db32b32.
1 parent 2b75653 commit e9db343

6 files changed

Lines changed: 35 additions & 62 deletions

File tree

Doc/library/base64.rst

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ POST request.
7272
Added the *wrapcol* parameter.
7373

7474

75-
.. function:: b64decode(s, altchars=None, validate=True)
75+
.. function:: b64decode(s, altchars=None, validate=False)
7676

7777
Decode the Base64 encoded :term:`bytes-like object` or ASCII string
7878
*s* and return the decoded :class:`bytes`.
@@ -84,24 +84,15 @@ POST request.
8484
A :exc:`binascii.Error` exception is raised
8585
if *s* is incorrectly padded.
8686

87-
By default, non-alphabet characters in the input result in a
87+
If *validate* is ``False`` (the default), characters that are neither
88+
in the normal base-64 alphabet nor the alternative alphabet are
89+
discarded prior to the padding check. If *validate* is ``True``,
90+
these non-alphabet characters in the input result in a
8891
:exc:`binascii.Error`.
89-
If *validate* is false, characters that are neither in the normal base-64
90-
alphabet nor the alternative alphabet are discarded prior to the padding
91-
check, but the ``+`` and ``/`` characters keep their meaning if they are
92-
not in *altchars* (they will be discarded in future Python versions).
9392

94-
For more information about the strict base64 check, see
95-
:func:`binascii.a2b_base64`.
96-
97-
.. versionchanged:: next
98-
*validate* is now ``True`` by default.
99-
The ``+`` and ``/`` characters no longer preserve their meaning if they
100-
are not in the alternative alphabet and *validate* is true.
101-
:exc:`FutureWarning` is now emitted if the ``+`` or ``/`` characters
102-
which are not in the alternative alphabet occur in the input and
103-
*validate* is false.
93+
For more information about the strict base64 check, see :func:`binascii.a2b_base64`
10494

95+
May assert or raise a :exc:`ValueError` if the length of *altchars* is not 2.
10596

10697
.. function:: standard_b64encode(s)
10798

@@ -114,9 +105,6 @@ POST request.
114105
Decode :term:`bytes-like object` or ASCII string *s* using the standard
115106
Base64 alphabet and return the decoded :class:`bytes`.
116107

117-
.. versionchanged:: next
118-
Non-alphabet characters in the input result in a :exc:`binascii.Error`.
119-
120108

121109
.. function:: urlsafe_b64encode(s)
122110

@@ -135,9 +123,6 @@ POST request.
135123
``/`` in the standard Base64 alphabet, and return the decoded
136124
:class:`bytes`.
137125

138-
.. versionchanged:: next
139-
Non-alphabet characters in the input result in a :exc:`binascii.Error`.
140-
141126

142127
.. function:: b32encode(s)
143128

Doc/whatsnew/3.15.rst

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,18 +1467,3 @@ that may require changes to your code.
14671467
*dest* is now ``'foo'`` instead of ``'f'``.
14681468
Pass an explicit *dest* argument to preserve the old behavior.
14691469
(Contributed by Serhiy Storchaka in :gh:`138697`.)
1470-
1471-
* :func:`base64.b64decode` now rejects all characters not in the base 64
1472-
alphabet by default.
1473-
You can pass the ``validate=False`` argument to get the old behavior.
1474-
If *validate* is false, :exc:`FutureWarning` is now emitted if the ``+`` or
1475-
``/`` characters which are not in the alternative alphabet occur in the input.
1476-
To get rid of the potential warnings, either replace these characters with
1477-
the corresponding alternative characters (to keep the old behavior),
1478-
or remove them from the input (to get the future behavior).
1479-
1480-
In :func:`base64.b64decode` and :func:`base64.b64decode`, non-alphabet
1481-
characters in the input now result in a :exc:`binascii.Error`.
1482-
You can use :func:`base64.b64decode` with ``validate=False`` and optionally
1483-
the corresponding *altchars* argument to get the old behavior.
1484-
(Contributed by Serhiy Storchaka in :gh:`125346`.)

Lib/base64.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def b64encode(s, altchars=None, *, wrapcol=0):
6262
return encoded
6363

6464

65-
def b64decode(s, altchars=None, validate=True):
65+
def b64decode(s, altchars=None, validate=False):
6666
"""Decode the Base64 encoded bytes-like object or ASCII string s.
6767
6868
Optional altchars must be a bytes-like object or ASCII string of length 2
@@ -89,8 +89,8 @@ def b64decode(s, altchars=None, validate=True):
8989
if validate:
9090
s = s.translate(bytes.maketrans(b'+/' + altchars, altchars + b'+/'))
9191
else:
92-
for b in b'+/':
93-
if b not in altchars and b in s:
92+
for b in set(b'+/') - set(altchars):
93+
if b in s:
9494
badchar = b
9595
break
9696
s = s.translate(bytes.maketrans(altchars, b'+/'))

Lib/test/test_base64.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def test_b64decode(self):
243243
b"YWI=": b"ab",
244244
b"YWJj": b"abc",
245245
b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
246-
b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
246+
b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
247247
b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==":
248248

249249
b"abcdefghijklmnopqrstuvwxyz"
@@ -313,31 +313,36 @@ def test_b64decode_invalid_chars(self):
313313
(b'!', b''),
314314
(b"YWJj\n", b"abc"),
315315
(b'YWJj\nYWI=', b'abcab'))
316+
funcs = (
317+
base64.b64decode,
318+
base64.standard_b64decode,
319+
base64.urlsafe_b64decode,
320+
)
316321
for bstr, res in tests:
317-
with self.subTest(bstr=bstr):
318-
for data in bstr, bstr.decode('ascii'):
319-
self.assertEqual(base64.b64decode(data, validate=False), res)
320-
self.assertRaises(binascii.Error, base64.b64decode, data)
321-
self.assertRaises(binascii.Error, base64.standard_b64decode, data)
322-
self.assertRaises(binascii.Error, base64.urlsafe_b64decode, data)
322+
for func in funcs:
323+
with self.subTest(bstr=bstr, func=func):
324+
self.assertEqual(func(bstr), res)
325+
self.assertEqual(func(bstr.decode('ascii')), res)
326+
with self.assertRaises(binascii.Error):
327+
base64.b64decode(bstr, validate=True)
328+
with self.assertRaises(binascii.Error):
329+
base64.b64decode(bstr.decode('ascii'), validate=True)
323330

324331
# Normal alphabet characters will be discarded when alternative given
325332
with self.assertWarns(FutureWarning):
326-
self.assertEqual(base64.b64decode(b'++++', altchars=b'-_', validate=False),
333+
self.assertEqual(base64.b64decode(b'++++', altchars=b'-_'),
327334
b'\xfb\xef\xbe')
328335
with self.assertWarns(FutureWarning):
329-
self.assertEqual(base64.b64decode(b'////', altchars=b'-_', validate=False),
336+
self.assertEqual(base64.b64decode(b'////', altchars=b'-_'),
330337
b'\xff\xff\xff')
338+
self.assertEqual(base64.urlsafe_b64decode(b'++++'), b'')
339+
self.assertEqual(base64.urlsafe_b64decode(b'////'), b'')
331340
with self.assertRaises(binascii.Error):
332-
base64.urlsafe_b64decode(b'++++')
333-
with self.assertRaises(binascii.Error):
334-
base64.urlsafe_b64decode(b'////')
335-
with self.assertRaises(binascii.Error):
336-
base64.b64decode(b'++++', altchars=b'-_')
341+
base64.b64decode(b'++++', altchars=b'-_', validate=True)
337342
with self.assertRaises(binascii.Error):
338-
base64.b64decode(b'////', altchars=b'-_')
343+
base64.b64decode(b'////', altchars=b'-_', validate=True)
339344
with self.assertRaises(binascii.Error):
340-
base64.b64decode(b'+/!', altchars=b'-_', validate=False)
345+
base64.b64decode(b'+/!', altchars=b'-_')
341346

342347
def _altchars_strategy():
343348
"""Generate 'altchars' for base64 encoding."""

Misc/NEWS.d/next/Library/2025-11-06-12-03-29.gh-issue-125346.7Gfpgw.rst

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The ``+`` and ``/`` characters are no longer recognized as the part of the
2+
Base64 alphabet in :func:`base64.urlsafe_b64decode` and
3+
:func:`base64.b64decode` with the *altchars* argument that does not contain
4+
them.

0 commit comments

Comments
 (0)