From eac17571ccdb007dd986e79f7d162385386b875a Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Wed, 21 Oct 2026 21:01:31 -0500 Subject: [PATCH] lab-ops-hub: judge container validator length limits on the IDNA-encoded host (#517) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The #513 overlong check counted characters of the raw host, while the Worker's u.hostname is already punycode — so an IDN label whose characters fit in 63 but whose A-label exceeds 63 octets (or a name encoding past 253) passed the container validator and failed deep inside the fetcher. Judge each label's punycode-encoded length instead. Regression test covers the encoded-overlong label and name plus an at-encoded-limit IDN control. --- pkgs/lab-ops-hub/container/app.py | 28 +++++++++++++------ .../test/test_container_validate.py | 17 +++++++++++ 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/pkgs/lab-ops-hub/container/app.py b/pkgs/lab-ops-hub/container/app.py index 096dc9c270..99866e09c8 100644 --- a/pkgs/lab-ops-hub/container/app.py +++ b/pkgs/lab-ops-hub/container/app.py @@ -63,15 +63,25 @@ def validate_crawl_url(raw): # the strip above already removed) — refuse it up front instead of # certifying it as a public DNS name and letting the resolver fail # on it deep inside the fetcher, matching the Worker's validator - # (#513). Character length is a lower bound on the IDNA-encoded - # length, so this can never refuse a name that encodes within the - # limits. IPv6 literals are judged as addresses, as above. - if ":" not in host and ( - len(host) > 253 or any(len(label) > 63 for label in host.split(".")) - ): - raise ValueError( - f"refusing to crawl host with an overlong label or name {host}" - ) + # (#513). The limits apply to the IDNA-encoded (punycode) form — + # the Worker's u.hostname is already punycode — so judge each + # label's A-label length, not its character count: character + # length is only a lower bound, and an IDN label whose characters + # fit can still encode past the limit (#517). IPv6 literals are + # judged as addresses, as above. + if ":" not in host: + encoded_labels = [ + label + if label.isascii() + else "xn--" + label.encode("punycode").decode("ascii") + for label in host.split(".") + ] + if len(".".join(encoded_labels)) > 253 or any( + len(label) > 63 for label in encoded_labels + ): + raise ValueError( + f"refusing to crawl host with an overlong label or name {host}" + ) # A raw % in the host is a percent-encoded spelling (or an IPv6 zone # id): WHATWG URL parsing percent-decodes the host before judging it # — %6cocalhost is localhost — and rejects the rest outright, while diff --git a/pkgs/lab-ops-hub/test/test_container_validate.py b/pkgs/lab-ops-hub/test/test_container_validate.py index e98ff0eae0..db31a10cf7 100644 --- a/pkgs/lab-ops-hub/test/test_container_validate.py +++ b/pkgs/lab-ops-hub/test/test_container_validate.py @@ -67,6 +67,23 @@ def test_refuses_overlong_dns_names(self): at_limit_name = ".".join(["a" * 63] * 3 + ["a" * 61]) # 253 chars self.assertAccepted("http://" + at_limit_name + "/") + def test_refuses_idn_names_overlong_once_encoded(self): + # The RFC 1035 limits apply to the IDNA-encoded form, which is + # what the Worker's u.hostname carries: a 42-char CJK label is + # 65 chars once punycode-encoded, and five 40-char CJK labels + # are a 204-char name that encodes to 309 — both over the + # limits even though their character counts fit (#517). + def cjk_label(n): + return "".join(chr(0x4E00 + i) for i in range(n)) + + self.assertRefused("http://" + cjk_label(42) + ".com/") + overlong_idn_name = ".".join([cjk_label(40)] * 5) + self.assertRefused("http://" + overlong_idn_name + "/") + # Control at the encoded limit stays allowed: a 40-char CJK + # label encodes to 61 chars, and an ordinary IDN host is fine. + self.assertAccepted("http://" + cjk_label(40) + ".com/") + self.assertAccepted("http://examéple.com/") + def test_refuses_dotted_quad_non_public(self): for url in ( "http://127.0.0.1/",