bpo-30566: Fix IndexError when using punycode codec (GH-18632)
https://github.com/python/cpython/commit/daef21ce7dfd3735101d85d6ebf7554187c... commit: daef21ce7dfd3735101d85d6ebf7554187c33ab8 branch: 3.8 author: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> committer: GitHub <noreply@github.com> date: 2020-02-25T06:42:39+03:00 summary: bpo-30566: Fix IndexError when using punycode codec (GH-18632) Trying to decode an invalid string with the punycode codec shoud raise UnicodeError. (cherry picked from commit ba22e8f174309979d90047c5dc64fcb63bc2c32e) Co-authored-by: Berker Peksag <berker.peksag@gmail.com> files: A Misc/NEWS.d/next/Library/2020-02-24-03-45-28.bpo-30566.qROxty.rst M Lib/encodings/punycode.py M Lib/test/test_codecs.py diff --git a/Lib/encodings/punycode.py b/Lib/encodings/punycode.py index 66c51013ea431..1c5726447077b 100644 --- a/Lib/encodings/punycode.py +++ b/Lib/encodings/punycode.py @@ -143,7 +143,7 @@ def decode_generalized_number(extended, extpos, bias, errors): digit = char - 22 # 0x30-26 elif errors == "strict": raise UnicodeError("Invalid extended code point '%s'" - % extended[extpos]) + % extended[extpos-1]) else: return extpos, None t = T(j, bias) diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index b37525bf66043..8c10e948e8041 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -1331,6 +1331,18 @@ def test_decode(self): puny = puny.decode("ascii").encode("ascii") self.assertEqual(uni, puny.decode("punycode")) + def test_decode_invalid(self): + testcases = [ + (b"xn--w&", "strict", UnicodeError()), + (b"xn--w&", "ignore", "xn-"), + ] + for puny, errors, expected in testcases: + with self.subTest(puny=puny, errors=errors): + if isinstance(expected, Exception): + self.assertRaises(UnicodeError, puny.decode, "punycode", errors) + else: + self.assertEqual(puny.decode("punycode", errors), expected) + # From http://www.gnu.org/software/libidn/draft-josefsson-idn-test-vectors.html nameprep_tests = [ diff --git a/Misc/NEWS.d/next/Library/2020-02-24-03-45-28.bpo-30566.qROxty.rst b/Misc/NEWS.d/next/Library/2020-02-24-03-45-28.bpo-30566.qROxty.rst new file mode 100644 index 0000000000000..c780633030090 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-02-24-03-45-28.bpo-30566.qROxty.rst @@ -0,0 +1,2 @@ +Fix :exc:`IndexError` when trying to decode an invalid string with punycode +codec.
participants (1)
-
Miss Islington (bot)