From d7d0692dc0c9881497b9a9bd86aadce66051da73 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 10:11:23 -0600 Subject: [PATCH] fix(lab-ops-hub): judge decoded A-labels by UTS-46 mapped form + IDNA-disallowed categories (#588) --- pkgs/lab-ops-hub/container/app.py | 43 +++++++++++++++++++ .../test/test_container_validate.py | 30 +++++++++++++ 2 files changed, 73 insertions(+) diff --git a/pkgs/lab-ops-hub/container/app.py b/pkgs/lab-ops-hub/container/app.py index f7363742f2..91e60822b9 100644 --- a/pkgs/lab-ops-hub/container/app.py +++ b/pkgs/lab-ops-hub/container/app.py @@ -573,6 +573,23 @@ def _judge_a_label(label, host): f"refusing to crawl host {host}: A-label {label} " "is not the canonical encoding of its decoded label" ) + # A valid U-label is its own UTS-46 mapped form: the mapping + # validate_crawl_url applies to raw hosts (NFKC, lowercasing, + # ignored-codepoint deletion) must leave the decoded label + # unchanged. The punycode round-trip above cannot see this — + # punycode encodes any string — so a decoded label carrying a + # character UTS-46 maps away (an uppercase letter such as + # U+00C0, a compatibility character such as U+00B2 or the + # fullwidth solidus U+FF0F, a space-class character such as + # U+2000, or an ignored codepoint) passed every check while the + # Worker's WHATWG validator refuses the label outright (#588). + mapped = unicodedata.normalize("NFKC", decoded).lower() + mapped = mapped.translate({cp: None for cp in _UTS46_IGNORED}) + if mapped != decoded: + raise ValueError( + f"refusing to crawl host {host}: A-label {label} " + f"decodes to a label that is not its UTS-46 mapped form {decoded!r}" + ) if any( ord(c) <= 0x20 or 0x7F <= ord(c) <= 0xA0 @@ -642,6 +659,32 @@ def _judge_idna_label(label, host): # violation the Worker refuses, while at label start the mapped # letter is an ordinary first character, closing the former # leading-mark over-refusal (#596). + # IDNA2008/UTS-46 marks every punctuation, symbol, separator, + # private-use and surrogate codepoint DISALLOWED (the CONTEXTO / + # CONTEXTJ exceptions below are judged by their own rules: the + # Catalan middle dot and Hebrew punctuation are context-judged + # punctuation, the Arabic separators by the AN/separator rules + # below, and the undertie pair is CONTEXTJ joining punctuation). + # None of the checks around this one sees them: they are assigned + # (not Cn), not format characters (not Cf), NFKC leaves most of + # them unchanged, and the stdlib IDNA-2003 codec's stringprep + # tables do not prohibit them — so a decoded label carrying + # U+00A8, U+00B4, U+037E or a currency/math symbol passed while + # the Worker refuses it (#588). Category-based, not an enumerated + # table, so it carries no unicodedata version skew of its own. + if any( + ord(c) > 0x7F + and ord(c) not in (0x00B7, 0x05F3, 0x05F4, 0x066B, 0x066C, 0x203F, 0x2040) + and ( + unicodedata.category(c)[0] in "PSZ" + or unicodedata.category(c) in ("Co", "Cs") + ) + for c in label + ): + raise ValueError( + f"refusing to crawl host {host}: label {label!r} " + "contains an IDNA-disallowed punctuation, symbol or separator character" + ) label = label.translate({0x0345: "ι"}) if any(_maps_to_label_separator(c) for c in label if ord(c) > 0x7F): # A decoded A-label is judged unmapped, so the mapped-dot diff --git a/pkgs/lab-ops-hub/test/test_container_validate.py b/pkgs/lab-ops-hub/test/test_container_validate.py index 7deeaadc93..3863e0e423 100644 --- a/pkgs/lab-ops-hub/test/test_container_validate.py +++ b/pkgs/lab-ops-hub/test/test_container_validate.py @@ -886,6 +886,36 @@ def test_refuses_idna_invalid_hosts(self): self.assertAccepted("http://m\u00fcnchen.de/") self.assertAccepted("http://ex\u0301ample.com/") + def test_a_label_must_be_its_uts46_mapped_form_and_disallowed_free(self): + # #588 residual: a decoded A-label that is not its own UTS-46 + # mapped form, or that carries an IDNA-disallowed punctuation / + # symbol / separator character, is refused — the punycode + # round-trip alone certifies all of these. + # Mapped-form class (the issue's headline hosts): + self.assertRefused("http://xn--example-yoa.com/") # ex\u00c0ample (uppercase) + self.assertRefused("http://xn--example-rla.com/") # ex\u00b2ample (compatibility digit) + self.assertRefused("http://xn--ab-cja0631x.com/") # a\uff0fb\u00e9 (fullwidth solidus) + self.assertRefused("http://xn--mnchen-3ya5364d.com/") # m\u00fcn\u2000chen (space class) + # DISALLOWED class (survive NFKC, assigned, not format chars): + self.assertRefused("http://xn--example-hja.com/") # ex\u00a8ample (diaeresis, Sk) + self.assertRefused("http://xn--example-7la.com/") # ex\u00b4ample (acute accent, Sk) + self.assertRefused("http://xn--example-f9e.com/") # ex\u037eample (Greek question mark, Po) + self.assertRefused("http://xn--example-3ha.com/") # ex\u00a2ample (cent sign, Sc) + self.assertRefused("http://xn--example-u17c.com/") # ex\u20acample (euro sign, Sc) + # The raw spellings of the DISALLOWED class are refused too + # (the check sits in the shared label judge): + self.assertRefused("http://ex\u00a8ample.com/") + # (No raw U+037E case: NFKC folds it to an ASCII ";" before + # judging, in both validators — only the unmapped decoded + # A-label form above is the #588 divergence.) + # Controls: the PR #584 accepted A-labels and ordinary IDN + # stay accepted, and the CONTEXTO middle dot label is not + # swept up by the punctuation arm. + self.assertAccepted("http://xn--mnchen-3ya.de/") # m\u00fcnchen.de + self.assertAccepted("http://xn--bcher-kva.de/") # b\u00fccher.de + self.assertAccepted("http://xn--fsq.com/") # \u4f8b.com + self.assertAccepted("http://xn--al-0ea.com/") # a\u00b7l (Catalan middle dot) + def test_judges_length_and_blocked_names_on_the_mapped_form(self): # UTS-46 mapping deletes the "ignored" codepoints before any # judgement, so the length check must measure the mapped form