diff --git a/pkgs/lab-ops-hub/container/app.py b/pkgs/lab-ops-hub/container/app.py index 03894e3d73..657db1d378 100644 --- a/pkgs/lab-ops-hub/container/app.py +++ b/pkgs/lab-ops-hub/container/app.py @@ -226,8 +226,19 @@ def validate_crawl_url(raw): # there, the return path rebuilds from the mapped host exactly # as it does for a percent-decoded host. has_literal_separator = any(c in "\u3002\uff0e\uff61" for c in host) + host_before_mapping = host host = unicodedata.normalize("NFKC", host).lower() host = host.translate({0x3002: ".", 0xFF0E: ".", 0xFF61: "."}) + # The NFKC mapping folded the host to a different ASCII spelling + # (a fullwidth or other compatibility spelling of an ASCII name): + # the return path must then rebuild from the mapped host, exactly + # as for a percent-decoded or literal-separator host, or the raw + # spelling is returned while the Worker returns the form it + # judged (#665). Scoped to folds landing on ASCII, measured + # before the ignored-codepoint deletion below: IDN-internal + # mappings (e.g. U+0345, #596) and that deletion's return form + # (#652) are separate questions. + host_was_mapped = host != host_before_mapping and host.isascii() host = host.translate({cp: None for cp in _UTS46_IGNORED}) # NFKC mapping can itself CREATE a host delimiter the earlier # checks never saw: _percent_decode_host refuses delimiters in @@ -425,7 +436,7 @@ def validate_crawl_url(raw): return _url_with_encoded_components( _normalize_validated_url( _url_with_host(u, host + "." if has_root_dot else host, raw) - if percent_decoded or has_literal_separator + if percent_decoded or has_literal_separator or host_was_mapped else raw ) ) diff --git a/pkgs/lab-ops-hub/test/test_container_validate.py b/pkgs/lab-ops-hub/test/test_container_validate.py index cc966eefc9..ceee675551 100644 --- a/pkgs/lab-ops-hub/test/test_container_validate.py +++ b/pkgs/lab-ops-hub/test/test_container_validate.py @@ -627,8 +627,11 @@ def test_refuses_mapped_spellings_of_blocked_targets(self): "http://localhost。/", # -> localhost. ): self.assertRefused(url) - # Controls: mapped spellings of public names stay crawlable. - self.assertAccepted("http://example.com/") + # Controls: mapped spellings of public names stay crawlable, + # and return in the mapped form the Worker returns (#665). + self.assertEqual( + validate_crawl_url("http://example.com/"), "http://example.com/" + ) # The U-label spelling is kept, but its literal separator # now returns as "." (#660), not the fullwidth character. self.assertEqual( @@ -1235,6 +1238,23 @@ def test_returns_the_ascii_dot_for_literal_separator_spellings(self): } for raw, expected in cases.items(): self.assertEqual(validate_crawl_url(raw), expected, raw) + + def test_returns_the_mapped_host_for_compatibility_spellings(self): + # Judging NFKC-maps the host (app.py: fullwidth/compatibility + # characters fold to their ordinary forms), but the return + # path rebuilt from the mapped host only for percent-decoded + # and literal-separator spellings, so a fullwidth spelling + # came back verbatim while the Worker returns the mapped + # form it judged (#665). The return now rebuilds whenever + # the mapping changed the host spelling. + cases = { + "http://\uff45\uff58\uff41\uff4d\uff50\uff4c\uff45.com/": "http://example.com/", + "http://\uff25\uff38\uff21\uff2d\uff30\uff2c\uff25.com/": "http://example.com/", + "http://\uff47\uff50\uff54\uff12.example.com/": "http://gpt2.example.com/", + "http://\uff45\uff58\uff41\uff4d\uff50\uff4c\uff45.com/path": "http://example.com/path", + } + for raw, expected in cases.items(): + self.assertEqual(validate_crawl_url(raw), expected, raw) # Verdicts are unchanged: dotted spellings of blocked # targets stay refused, and a second separator is still an # empty label, in every separator spelling.