]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-145968: Fix base64.b64decode altchars translation in specific cases (GH-145969)
authorMatthieu Darbois <mayeut@users.noreply.github.com>
Sun, 15 Mar 2026 17:46:44 +0000 (18:46 +0100)
committerGitHub <noreply@github.com>
Sun, 15 Mar 2026 17:46:44 +0000 (19:46 +0200)
When altchars overlaps with the standard ones, the translation does not
always yield to the expected outcome.

Lib/base64.py
Lib/test/test_base64.py
Misc/NEWS.d/next/Library/2026-03-15-10-17-51.gh-issue-145968.gZexry.rst [new file with mode: 0644]

index 36688ce43917cec2fd2553079d00c728abca30bb..dcfcbcc95a39be46387eadb5851c8077d3ceddda 100644 (file)
@@ -100,7 +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(b'+/' + altchars, altchars + 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:
index 69aa628db7c34c1a6040e6444de344b4dfa41950..9648624b267a541a6a772860512ffb431f0aa207 100644 (file)
@@ -293,6 +293,13 @@ class BaseXYTestCase(unittest.TestCase):
             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 (file)
index 0000000..9eae1dc
--- /dev/null
@@ -0,0 +1,2 @@
+Fix translation in :func:`base64.b64decode` when altchars overlaps with the
+standard ones.