From 5be8c0073bec1d6d946e0dc26150ff5f6bba4f5f Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Thu, 22 Oct 2026 13:28:46 -0500 Subject: [PATCH] lab-ops-hub: judge Arabic-Indic digits by bidi context, not a blanket range (#605) --- pkgs/lab-ops-hub/container/app.py | 52 ++++++++++--------- pkgs/lab-ops-hub/test/lib.test.ts | 24 +++++++++ .../test/test_container_validate.py | 40 +++++++++++--- 3 files changed, 86 insertions(+), 30 deletions(-) diff --git a/pkgs/lab-ops-hub/container/app.py b/pkgs/lab-ops-hub/container/app.py index c1796bb01e..60ebbff3ac 100644 --- a/pkgs/lab-ops-hub/container/app.py +++ b/pkgs/lab-ops-hub/container/app.py @@ -241,8 +241,8 @@ def validate_crawl_url(raw): # encodes, not by its ASCII spelling: the spelling is pure ASCII, # so no check above or below ever sees the decoded characters — # "xn--9hbcp.xn--8hb.xn--8hb.xn--9hb" is ١٢٧.٠.٠.١ (127.0.0.1 in - # Arabic-Indic digits, refused in raw form by the forbidden- - # character check below) yet sailed through as an ordinary public + # Arabic-Indic digits, refused in raw form by _judge_idna_label's + # AN rule below) yet sailed through as an ordinary public # DNS name, while the Worker's WHATWG parser decodes the A-label # and rejects it outright (#580). Decode each A-label and hold the # decoded form to the same policy; an A-label that does not decode @@ -276,21 +276,24 @@ def validate_crawl_url(raw): # up front: C0 controls and space (#507), plus DEL, the C1 controls # and NBSP (0x7F-0xA0) and the forbidden host codepoints < > ^ | # (#510), plus the IDNA-disallowed codepoints that survive NFKC - # (#516), plus the Arabic-Indic digits (U+0660-U+0669): WHATWG's - # domain-to-ASCII rejects any host containing one, even a single - # digit in a mixed label, while urlparse passes the spelling - # through and this validator would otherwise certify it as a public - # DNS name — including ١٢٧.٠.٠.١, the Arabic-Indic spelling of - # 127.0.0.1, which the ASCII-only numeric checks cannot read as an - # address (#574). The other decimal-digit families (Devanagari, - # Extended Arabic-Indic U+06F0-U+06F9, Bengali, Thai, ...) are - # accepted by WHATWG as ordinary IDN characters and stay accepted - # here. Non-ASCII above NBSP that is a valid IDN character (e.g. - # é) is untouched. + # (#516). The Arabic-Indic digits (U+0660-U+0669) are NOT refused + # here: they are bidi class AN, and WHATWG's domain-to-ASCII + # refuses them only outside a right-to-left label — the blanket + # range #574 added here refused them inside one too (ا١.com, + # مرحبا١.com), while the Worker accepts that context (#605). + # _judge_idna_label's AN rule, which runs on every non-ASCII + # label above, makes the contextual judgement: it refuses the + # digits in an LTR label, in a label with no R/AL character — + # including ١٢٧.٠.٠.١, the Arabic-Indic spelling of 127.0.0.1 + # (#574) — and mixed with a European digit, and accepts them + # inside a genuine RTL label, matching the Worker. The other + # decimal-digit families (Devanagari, Extended Arabic-Indic + # U+06F0-U+06F9, Bengali, Thai, ...) are accepted by WHATWG as + # ordinary IDN characters and stay accepted here. Non-ASCII + # above NBSP that is a valid IDN character (e.g. é) is untouched. if any( ord(c) <= 0x20 or 0x7F <= ord(c) <= 0xA0 - or 0x0660 <= ord(c) <= 0x0669 or c in "<>^|" or ord(c) in _IDNA_DISALLOWED for c in host @@ -388,8 +391,10 @@ def _judge_a_label(label, host): # spelling exactly (not the canonical encoding of anything), or # the decoded label carries a character the forbidden-character # policy in validate_crawl_url refuses in raw form (controls, - # space, the Arabic-Indic digits U+0660-U+0669, < > ^ |, and the - # _IDNA_DISALLOWED set). + # space, < > ^ |, and the _IDNA_DISALLOWED set). The Arabic-Indic + # digits U+0660-U+0669 are not in that set (#605): the + # _judge_idna_label call at the end of this function judges them + # by their bidi AN context, exactly as in raw form. body = label[4:] try: decoded = body.encode("ascii").decode("punycode") @@ -410,7 +415,6 @@ def _judge_a_label(label, host): if any( ord(c) <= 0x20 or 0x7F <= ord(c) <= 0xA0 - or 0x0660 <= ord(c) <= 0x0669 or c in "<>^|" or ord(c) in _IDNA_DISALLOWED for c in decoded @@ -453,13 +457,13 @@ def _judge_idna_label(label, host): # - it carries a character of bidi class AN (Arabic number) # outside a right-to-left label, or mixes AN with EN (European # number) in one label (#595): AN is only valid inside an RTL - # label, and never alongside EN. Every BMP AN codepoint is - # caught by another arm (the Cf check, the U+0660-U+0669 - # forbidden range, the U+066B / U+066C case above, which this - # rule subsumes), but the 41 astral AN codepoints — the Hanifi - # Rohingya digits U+10D30-U+10D39 and the Rumi digits - # U+10E60-U+10E7E — passed every other check, and an AN+EN - # mix inside an RTL label passed too; + # label, and never alongside EN. The BMP AN codepoints are the + # Arabic-Indic digits U+0660-U+0669 themselves (judged by this + # rule since #605 removed #574's blanket range — the U+066B / + # U+066C case above is subsumed by it too), but the 41 astral + # AN codepoints — the Hanifi Rohingya digits U+10D30-U+10D39 + # and the Rumi digits U+10E60-U+10E7E — passed every other + # check, and an AN+EN mix inside an RTL label passed too; # - the stdlib IDNA codec rejects it. # Known residuals, measured on the #583 differential battery # (13,045 hosts vs the Worker's validator under Bun): this diff --git a/pkgs/lab-ops-hub/test/lib.test.ts b/pkgs/lab-ops-hub/test/lib.test.ts index bc66e04163..f097f40f77 100644 --- a/pkgs/lab-ops-hub/test/lib.test.ts +++ b/pkgs/lab-ops-hub/test/lib.test.ts @@ -215,6 +215,30 @@ test("validateCrawlUrl refuses Arabic numbers outside RTL labels (#595)", () => assert.equal(validateCrawlUrl("http://\u{10d00}\u{10d30}.com/").ok, true); }); +test("validateCrawlUrl judges Arabic-Indic digits by their bidi context (#605)", () => { + // U+0660-U+0669 are bidi class AN, like the astral digits in the + // #595 test: valid inside a genuine RTL label, refused outside + // one. The container validator's blanket range refused the RTL + // spellings too; it now pins the same contextual verdicts (#605). + const blocked = [ + "http://\u0661.com/", + "http://\u0661\u0662\u0667.\u0660.\u0660.\u0661/", + "http://ex\u0661ample.com/", + "http://\u05d0\u06611.com/", + "http://\u0661\u0627.com/", + ]; + for (const u of blocked) { + assert.equal(validateCrawlUrl(u).ok, false, u); + } + // Controls: the digits inside RTL labels, raw and A-label form. + assert.equal(validateCrawlUrl("http://\u0627\u0661.com/").ok, true); + assert.equal(validateCrawlUrl("http://\u0645\u0631\u062d\u0628\u0627\u0661.com/").ok, true); + assert.equal(validateCrawlUrl("http://\u05d0\u0661.com/").ok, true); + assert.equal(validateCrawlUrl("http://xn--mgb0j.com/").ok, true); + assert.equal(validateCrawlUrl("http://xn--mgbbop3hzh.com/").ok, true); + assert.equal(validateCrawlUrl("http://xn--4db40a.com/").ok, true); +}); + test("validateCrawlUrl refuses hosts whose mapping introduces a separator (#593)", () => { // U+FE12's compatibility mapping is U+3002, itself a UTS-46 label // separator: WHATWG domain-to-ASCII refuses the host outright diff --git a/pkgs/lab-ops-hub/test/test_container_validate.py b/pkgs/lab-ops-hub/test/test_container_validate.py index fb2c51481a..b4cbc4842f 100644 --- a/pkgs/lab-ops-hub/test/test_container_validate.py +++ b/pkgs/lab-ops-hub/test/test_container_validate.py @@ -582,12 +582,16 @@ def test_refuses_idna_disallowed_unicode_hosts(self): self.assertAccepted("http://exam" + ch + "ple.com/") def test_refuses_arabic_indic_digit_hosts(self): - # WHATWG's domain-to-ASCII rejects any host containing an - # Arabic-Indic digit (U+0660-U+0669) — even one digit in a - # mixed label — while urlparse passes the spelling through and - # the ASCII-only numeric checks cannot read it as an address, - # so the old code certified the hosts below as public DNS - # names and fetched their punycode forms (#574). + # WHATWG's domain-to-ASCII rejects an Arabic-Indic digit + # (U+0660-U+0669) outside a right-to-left label — even one + # digit in a mixed LTR label — while urlparse passes the + # spelling through and the ASCII-only numeric checks cannot + # read it as an address, so the old code certified the hosts + # below as public DNS names and fetched their punycode forms + # (#574). Every spelling below is outside an RTL label, so + # _judge_idna_label's AN rule refuses them all; the RTL + # spellings the Worker accepts are pinned in the #605 test + # below. self.assertRefused("http://١٢٧.٠.٠.١/") # 127.0.0.1, Arabic-Indic digits self.assertRefused("http://١٢٣.com/") self.assertRefused("http://example١٢٣.com/") @@ -645,6 +649,30 @@ def test_refuses_a_label_spellings_of_arabic_indic_digit_hosts(self): ): self.assertAccepted(url) + def test_accepts_arabic_indic_digits_inside_rtl_labels(self): + # The Arabic-Indic digits are bidi class AN: inside a genuine + # RTL label they are valid, and the Worker's WHATWG parser + # accepts them — but #574's blanket forbidden-character range + # refused every host containing one, in raw and A-label form + # alike, overriding _judge_idna_label's contextual AN rule + # (#595) in the one context where the digit is valid (#605). + # The Extended Arabic-Indic control (U+06F1) was already + # accepted in the same shape. + self.assertAccepted("http://\u0627\u0661.com/") # Arabic + ١ + self.assertAccepted("http://\u0645\u0631\u062d\u0628\u0627\u0661.com/") + self.assertAccepted("http://\u05d0\u0661.com/") # Hebrew + ١ + self.assertAccepted("http://\u0627\u0661\u0627.com/") # AN mid-label + self.assertAccepted("http://\u0627\u0660.com/") + # Their A-label spellings are accepted too. + self.assertAccepted("http://xn--mgb0j.com/") # ا١.com + self.assertAccepted("http://xn--mgbbop3hzh.com/") # مرحبا١.com + self.assertAccepted("http://xn--4db40a.com/") # א١.com + # Guards: the AN context rules still refuse an AN+EN mix + # inside an RTL label and an RTL label that does not start + # with an R/AL character (both also refused by the Worker). + self.assertRefused("http://\u05d0\u06611.com/") + self.assertRefused("http://\u0661\u0627.com/") + def test_refuses_idna_invalid_hosts(self): # A label can punycode-encode without being a valid IDNA # label at all: the mapped host was never IDNA-validated, so