From d4840235000be2b1f8dee693afd88b375352726f Mon Sep 17 00:00:00 2001 From: mayeut Date: Sun, 15 Mar 2026 10:18:47 +0100 Subject: [PATCH 1/3] gh-145968: fix base64.b64decode altchars translation in specific cases When `altchars` overlaps with the standard ones, the translation does not always yield to the expected outcome. This commit updates `bytes.maketrans` arguments to take those overlap cases into account. --- Lib/base64.py | 12 +++++++++++- Lib/test/test_base64.py | 7 +++++++ .../2026-03-15-10-17-51.gh-issue-145968.gZexry.rst | 2 ++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-03-15-10-17-51.gh-issue-145968.gZexry.rst diff --git a/Lib/base64.py b/Lib/base64.py index 36688ce43917ce..8a8800c8cf76e4 100644 --- a/Lib/base64.py +++ b/Lib/base64.py @@ -100,7 +100,17 @@ def b64decode(s, altchars=None, validate=_NOT_SPECIFIED, *, ignorechars=_NOT_SPE break s = s.translate(bytes.maketrans(altchars, b'+/')) else: - trans = bytes.maketrans(b'+/' + altchars, altchars + b'+/') + altchars_out = ( + altchars[0] if altchars[0] not in b'+/' else altchars[1], + altchars[1] if altchars[1] not in b'+/' else altchars[0], + ) + trans_in = bytearray(altchars) + trans_out = bytearray(b'+/') + for b, b_out in zip(b'+/', altchars_out): + if b not in altchars: + trans_in.append(b) + trans_out.append(b_out) + trans = bytes.maketrans(trans_in, trans_out) s = s.translate(trans) ignorechars = ignorechars.translate(trans) if ignorechars is _NOT_SPECIFIED: diff --git a/Lib/test/test_base64.py b/Lib/test/test_base64.py index 69aa628db7c34c..9648624b267a54 100644 --- a/Lib/test/test_base64.py +++ b/Lib/test/test_base64.py @@ -293,6 +293,13 @@ def test_b64decode_altchars(self): eq(base64.b64decode(data_str, altchars=altchars_str), res) eq(base64.b64decode(data, altchars=altchars, ignorechars=b'\n'), res) + eq(base64.b64decode(b'/----', altchars=b'-+', ignorechars=b'/'), b'\xfb\xef\xbe') + eq(base64.b64decode(b'/----', altchars=b'+-', ignorechars=b'/'), b'\xff\xff\xff') + eq(base64.b64decode(b'+----', altchars=b'-/', ignorechars=b'+'), b'\xfb\xef\xbe') + eq(base64.b64decode(b'+----', altchars=b'/-', ignorechars=b'+'), b'\xff\xff\xff') + eq(base64.b64decode(b'+/+/', altchars=b'/+', ignorechars=b''), b'\xff\xef\xfe') + eq(base64.b64decode(b'/+/+', altchars=b'+/', ignorechars=b''), b'\xff\xef\xfe') + self.assertRaises(ValueError, base64.b64decode, b'', altchars=b'+') self.assertRaises(ValueError, base64.b64decode, b'', altchars=b'+/-') self.assertRaises(ValueError, base64.b64decode, '', altchars='+') diff --git a/Misc/NEWS.d/next/Library/2026-03-15-10-17-51.gh-issue-145968.gZexry.rst b/Misc/NEWS.d/next/Library/2026-03-15-10-17-51.gh-issue-145968.gZexry.rst new file mode 100644 index 00000000000000..9eae1dc400838a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-03-15-10-17-51.gh-issue-145968.gZexry.rst @@ -0,0 +1,2 @@ +Fix translation in :func:`base64.b64decode` when altchars overlaps with the +standard ones. From 081d550cf762992fd085c37add84b9b7dd31078d Mon Sep 17 00:00:00 2001 From: Matthieu Darbois Date: Sun, 15 Mar 2026 11:36:07 +0100 Subject: [PATCH 2/3] simplification from review Co-authored-by: Serhiy Storchaka --- Lib/base64.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/Lib/base64.py b/Lib/base64.py index 8a8800c8cf76e4..e8f19eb2510373 100644 --- a/Lib/base64.py +++ b/Lib/base64.py @@ -100,17 +100,8 @@ def b64decode(s, altchars=None, validate=_NOT_SPECIFIED, *, ignorechars=_NOT_SPE break s = s.translate(bytes.maketrans(altchars, b'+/')) else: - altchars_out = ( - altchars[0] if altchars[0] not in b'+/' else altchars[1], - altchars[1] if altchars[1] not in b'+/' else altchars[0], - ) - trans_in = bytearray(altchars) - trans_out = bytearray(b'+/') - for b, b_out in zip(b'+/', altchars_out): - if b not in altchars: - trans_in.append(b) - trans_out.append(b_out) - trans = bytes.maketrans(trans_in, trans_out) + trans = bytes.maketrans(altchars + bytes(set(b'+/') - set(altchars)), + b'+/' + bytes(set(altchars) - set(b'+/'))) s = s.translate(trans) ignorechars = ignorechars.translate(trans) if ignorechars is _NOT_SPECIFIED: From e956fe6e9d5ca148378a2aefa73088fe1465fd91 Mon Sep 17 00:00:00 2001 From: mayeut Date: Sun, 15 Mar 2026 18:05:47 +0100 Subject: [PATCH 3/3] don't use the results of an unordered 2 element set --- Lib/base64.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Lib/base64.py b/Lib/base64.py index e8f19eb2510373..dcfcbcc95a39be 100644 --- a/Lib/base64.py +++ b/Lib/base64.py @@ -100,8 +100,13 @@ def b64decode(s, altchars=None, validate=_NOT_SPECIFIED, *, ignorechars=_NOT_SPE break s = s.translate(bytes.maketrans(altchars, b'+/')) else: - trans = bytes.maketrans(altchars + bytes(set(b'+/') - set(altchars)), - b'+/' + bytes(set(altchars) - set(b'+/'))) + trans_in = set(b'+/') - set(altchars) + if len(trans_in) == 2: + # we can't use the reqult of unordered sets here + trans = bytes.maketrans(altchars + b'+/', b'+/' + altchars) + else: + trans = bytes.maketrans(altchars + bytes(trans_in), + b'+/' + bytes(set(altchars) - set(b'+/'))) s = s.translate(trans) ignorechars = ignorechars.translate(trans) if ignorechars is _NOT_SPECIFIED: