Skip to content

Commit ea784fa

Browse files
authored
gh-161090: Reject a leading delimiter in the punycode decoder (GH-161091)
RFC 3492 section 6.2 only consumes the delimiter when at least one basic code point precedes it. A leading "-" is now an invalid digit. ANT-2026-0IEDOPPK
1 parent 2789841 commit ea784fa

3 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎Lib/encodings/punycode.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,10 @@ def punycode_decode(text, errors):
190190
text = text.encode("ascii")
191191
if isinstance(text, memoryview):
192192
text = bytes(text)
193-
pos = text.rfind(b"-")
193+
# RFC 3492, section 6.2: the delimiter is only consumed if at least one
194+
# basic code point precedes it. A leading "-" is left in the extended
195+
# part, where it is an invalid digit.
196+
pos = text.rfind(b"-", 1)
194197
if pos == -1:
195198
base = ""
196199
extended = text.upper()

‎Lib/test/test_codecs.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,11 @@ def test_decode_invalid(self):
13901390
(b"Proprostnemluvesky-&uyb24dma41a", "strict", UnicodeDecodeError("punycode", b"", 19, 20, "")),
13911391
(b"Proprostnemluvesky-uyb24d&ma41a", "strict", UnicodeDecodeError("punycode", b"", 25, 26, "")),
13921392
(b"Proprostnemluvesky-uyb24dma41a&", "strict", UnicodeDecodeError("punycode", b"", 30, 31, "")),
1393+
# A delimiter not preceded by any basic code point (RFC 3492, 6.2)
1394+
(b"-", "strict", UnicodeDecodeError("punycode", b"", 0, 1, "")),
1395+
(b"-80a1acny", "strict", UnicodeDecodeError("punycode", b"", 0, 1, "")),
13931396
(b"xn--w&", "ignore", "xn-"),
1397+
(b"-80a1acny", "ignore", ""),
13941398
]
13951399
for puny, errors, expected in testcases:
13961400
with self.subTest(puny=puny, errors=errors):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix the ``punycode`` codec accepting a delimiter (``-``) that is not preceded
2+
by any basic code point. Such input, for example ``b'-80a1acny'``, now raises
3+
:exc:`UnicodeDecodeError`, as required by :rfc:`3492`.

0 commit comments

Comments
 (0)