]> granicus.if.org Git - python/commitdiff
bpo-33770: improve base64 exception message for encoded inputs of invalid length...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 10 Jun 2018 21:37:14 +0000 (14:37 -0700)
committerNed Deily <nad@python.org>
Sun, 10 Jun 2018 21:37:14 +0000 (17:37 -0400)
(cherry picked from commit 1b85c71a2136d3fa6a1da05b27b1fe4e4b8ee45e)

Co-authored-by: Tal Einat <taleinat+github@gmail.com>
Lib/test/test_binascii.py
Misc/NEWS.d/next/Library/2018-06-05-11-29-26.bpo-33770.oBhxxw.rst [new file with mode: 0644]
Modules/binascii.c

index 8fa57cdf1b0be36b6651b625aa215ebb9b8aca2d..7418a9ce98487901c2a852aecfecfd64d8702fa8 100644 (file)
@@ -110,6 +110,34 @@ class BinASCIITest(unittest.TestCase):
         # empty strings. TBD: shouldn't it raise an exception instead ?
         self.assertEqual(binascii.a2b_base64(self.type2test(fillers)), b'')
 
+    def test_base64errors(self):
+        # Test base64 with invalid padding
+        def assertIncorrectPadding(data):
+            with self.assertRaisesRegex(binascii.Error, r'(?i)Incorrect padding'):
+                binascii.a2b_base64(self.type2test(data))
+
+        assertIncorrectPadding(b'ab')
+        assertIncorrectPadding(b'ab=')
+        assertIncorrectPadding(b'abc')
+        assertIncorrectPadding(b'abcdef')
+        assertIncorrectPadding(b'abcdef=')
+        assertIncorrectPadding(b'abcdefg')
+        assertIncorrectPadding(b'a=b=')
+        assertIncorrectPadding(b'a\nb=')
+
+        # Test base64 with invalid number of valid characters (1 mod 4)
+        def assertInvalidLength(data):
+            with self.assertRaisesRegex(binascii.Error, r'(?i)invalid.+length'):
+                binascii.a2b_base64(self.type2test(data))
+
+        assertInvalidLength(b'a')
+        assertInvalidLength(b'a=')
+        assertInvalidLength(b'a==')
+        assertInvalidLength(b'a===')
+        assertInvalidLength(b'a' * 5)
+        assertInvalidLength(b'a' * (4 * 87 + 1))
+        assertInvalidLength(b'A\tB\nC ??DE')  # only 5 valid characters
+
     def test_uu(self):
         MAX_UU = 45
         for backtick in (True, False):
diff --git a/Misc/NEWS.d/next/Library/2018-06-05-11-29-26.bpo-33770.oBhxxw.rst b/Misc/NEWS.d/next/Library/2018-06-05-11-29-26.bpo-33770.oBhxxw.rst
new file mode 100644 (file)
index 0000000..a1529dd
--- /dev/null
@@ -0,0 +1 @@
+improve base64 exception message for encoded inputs of invalid length
index 1af6b7f98f255ff903e44824421a7c3a1b489846..2df80affb6956429eb0561e56eb2a41708f07740 100644 (file)
@@ -510,7 +510,18 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data)
     }
 
     if (leftbits != 0) {
-        PyErr_SetString(Error, "Incorrect padding");
+        if (leftbits == 6) {
+            /*
+            ** There is exactly one extra valid, non-padding, base64 character.
+            ** This is an invalid length, as there is no possible input that
+            ** could encoded into such a base64 string.
+            */
+            PyErr_SetString(Error,
+                            "Invalid base64-encoded string: "
+                            "length cannot be 1 more than a multiple of 4");
+        } else {
+            PyErr_SetString(Error, "Incorrect padding");
+        }
         _PyBytesWriter_Dealloc(&writer);
         return NULL;
     }