Skip to content

Commit d180b50

Browse files
ZackerySpytzpepijndevoserlend-aaslandencukou
authored
gh-63283: IDNA prefix should be case insensitive (GH-17726)
Any capitalization of "xn--" should be acceptable for the ACE prefix (see https://tools.ietf.org/html/rfc3490#section-5). Co-authored-by: Pepijn de Vos <[email protected]> Co-authored-by: Erlend E. Aasland <[email protected]> Co-authored-by: Petr Viktorin <[email protected]>
1 parent ce2c996 commit d180b50

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

Lib/encodings/idna.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def ToASCII(label):
8686
raise UnicodeError("label empty or too long")
8787

8888
# Step 5: Check ACE prefix
89-
if label.startswith(sace_prefix):
89+
if label[:4].lower() == sace_prefix:
9090
raise UnicodeError("Label starts with ACE prefix")
9191

9292
# Step 6: Encode with PUNYCODE
@@ -129,7 +129,7 @@ def ToUnicode(label):
129129
except UnicodeError:
130130
raise UnicodeError("Invalid character in IDN label")
131131
# Step 3: Check for ACE prefix
132-
if not label.startswith(ace_prefix):
132+
if not label[:4].lower() == ace_prefix:
133133
return str(label, "ascii")
134134

135135
# Step 4: Remove ACE prefix
@@ -202,7 +202,7 @@ def decode(self, input, errors='strict'):
202202
# XXX obviously wrong, see #3232
203203
input = bytes(input)
204204

205-
if ace_prefix not in input:
205+
if ace_prefix not in input.lower():
206206
# Fast path
207207
try:
208208
return input.decode('ascii'), len(input)

Lib/test/test_codecs.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,13 @@ def test_builtin_decode(self):
15471547
self.assertEqual(str(b"python.org.", "idna"), "python.org.")
15481548
self.assertEqual(str(b"xn--pythn-mua.org", "idna"), "pyth\xf6n.org")
15491549
self.assertEqual(str(b"xn--pythn-mua.org.", "idna"), "pyth\xf6n.org.")
1550+
self.assertEqual(str(b"XN--pythn-mua.org.", "idna"), "pyth\xf6n.org.")
1551+
self.assertEqual(str(b"xN--pythn-mua.org.", "idna"), "pyth\xf6n.org.")
1552+
self.assertEqual(str(b"Xn--pythn-mua.org.", "idna"), "pyth\xf6n.org.")
1553+
self.assertEqual(str(b"bugs.xn--pythn-mua.org.", "idna"),
1554+
"bugs.pyth\xf6n.org.")
1555+
self.assertEqual(str(b"bugs.XN--pythn-mua.org.", "idna"),
1556+
"bugs.pyth\xf6n.org.")
15501557

15511558
def test_builtin_encode(self):
15521559
self.assertEqual("python.org".encode("idna"), b"python.org")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
In :mod:`encodings.idna`, any capitalization of the the ACE prefix
2+
(``xn--``) is now acceptable. Patch by Pepijn de Vos and Zackery Spytz.

0 commit comments

Comments
 (0)