Skip to content

Commit cd17392

Browse files
authored
Merge pull request #274 from kjd/fix-decode-length-check
Skip the decode round-trip check for domains past encode's length limit
2 parents e2073db + c5796d7 commit cd17392

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

‎tests/fuzz_idna_api.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ def fuzz_decode(fdp):
6262
return
6363
# RFC 5891 §5.3: an A-label that decodes must re-encode to itself, so
6464
# under strict non-UTS46 processing any ASCII input that decodes is (up
65-
# to case) its own encoding. decode() does not enforce the 63-octet
66-
# label limit (nor does UTS #46 ToUnicode) but encode() does, so skip
67-
# overlong labels.
65+
# to case) its own encoding. decode()'s length checks are deliberately
66+
# lenient (no 63-octet label limit, and the whole-domain limit allows
67+
# the trailing-dot octet whether or not one is present, as UTS #46
68+
# ToUnicode checks no lengths at all) while encode() enforces both
69+
# exactly, so skip inputs past either limit.
6870
if isinstance(data, str):
6971
if not data.isascii():
7072
return
7173
data = data.encode("ascii")
72-
if all(len(label) <= 63 for label in data.split(b".")):
74+
if idna.valid_string_length(data, data.endswith(b".")) and all(len(label) <= 63 for label in data.split(b".")):
7375
assert idna.encode(decoded, strict=True) == data.lower(), (data, decoded)
7476

7577

‎tests/test_idna.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ def test_valid_label_length(self):
7979
self.assertFalse(idna.valid_label_length("a" * 64))
8080
self.assertRaises(idna.IDNAError, idna.encode, "a" * 64)
8181

82+
def test_decode_domain_length_leniency(self):
83+
# UTS #46 ToUnicode checks no lengths, so decode()'s whole-domain
84+
# bound is a lenient pre-filter that always allows the trailing-dot
85+
# octet, while encode() enforces the exact RFC 1035 limit: a
86+
# 254-octet domain without a trailing dot decodes but does not
87+
# re-encode (OSS-Fuzz 4694619275984896), and with one it round-trips.
88+
domain = b"aaa." * 63 + b"aa" # 254 octets, every label within limits
89+
decoded = idna.decode(domain, strict=True)
90+
self.assertRaises(idna.IDNAError, idna.encode, decoded, strict=True)
91+
dotted = domain[:-1] + b"."
92+
self.assertEqual(idna.encode(idna.decode(dotted, strict=True), strict=True), dotted)
93+
8294
def test_oversized_input_rejected_promptly(self):
8395
# GHSA-65pc-fj4g-8rjx: encode/decode must reject inputs that
8496
# exceed the maximum DNS domain length before per-codepoint

‎tests/test_idna_fuzz_targets.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
b"xn---bbk.example", # non-canonical spelling of xn--bbk
4444
b"XN--MNCHEN-3YA.example",
4545
b"a." * 200, # every label valid, but past the 253-octet domain limit
46+
b"aaa." * 63 + b"aa", # 254 octets, no trailing dot: decodes, too long to encode
4647
("\ufdfa" * 100).encode(), # UTS #46 maps each to 18 characters, past the input cap
4748
]
4849

0 commit comments

Comments
 (0)