Skip to content

Commit 3bdb2d9

Browse files
bpo-39055: Reject a trailing \n in base64.b64decode() with validate=True. (GH-17616)
(cherry picked from commit b19c0d7) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 880a17a commit 3bdb2d9

File tree

3 files changed

+4
-1
lines changed

3 files changed

+4
-1
lines changed

Lib/base64.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def b64decode(s, altchars=None, validate=False):
8282
altchars = _bytes_from_decode_data(altchars)
8383
assert len(altchars) == 2, repr(altchars)
8484
s = s.translate(bytes.maketrans(altchars, b'+/'))
85-
if validate and not re.match(b'^[A-Za-z0-9+/]*={0,2}$', s):
85+
if validate and not re.fullmatch(b'[A-Za-z0-9+/]*={0,2}', s):
8686
raise binascii.Error('Non-base64 digit found')
8787
return binascii.a2b_base64(s)
8888

Lib/test/test_base64.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ def test_b64decode_invalid_chars(self):
250250
(b'3d}==', b'\xdd'),
251251
(b'@@', b''),
252252
(b'!', b''),
253+
(b"YWJj\n", b"abc"),
253254
(b'YWJj\nYWI=', b'abcab'))
254255
funcs = (
255256
base64.b64decode,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`base64.b64decode` with ``validate=True`` raises now a binascii.Error
2+
if the input ends with a single ``\n``.

0 commit comments

Comments
 (0)