diff --git a/pkgs/lab-ops-hub/container/app.py b/pkgs/lab-ops-hub/container/app.py index 7723a2b221..03894e3d73 100644 --- a/pkgs/lab-ops-hub/container/app.py +++ b/pkgs/lab-ops-hub/container/app.py @@ -216,6 +216,16 @@ def validate_crawl_url(raw): raise ValueError( f"refusing to crawl host with a character that maps to a label separator {host!r}" ) + # Remember whether the (possibly percent-decoded) host carried a + # literal UTS-46 label separator: the mapping below translates it + # to ".", but the non-percent return path further down returns + # `raw` verbatim, so a literal spelling came back with the + # fullwidth character still in it while its percent-encoded + # spelling (and the Worker) returned the ASCII dotted form + # (#660 — the #647 remainder for literal spellings). When it was + # 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 = unicodedata.normalize("NFKC", host).lower() host = host.translate({0x3002: ".", 0xFF0E: ".", 0xFF61: "."}) host = host.translate({cp: None for cp in _UTS46_IGNORED}) @@ -405,13 +415,17 @@ def validate_crawl_url(raw): # The returned form is WHATWG-normalized (#633). The root # dot judging stripped is re-attached, so the percent- # encoded spelling returns the same dotted form as the - # literal spelling and the Worker (#647). (IP literals - # take the canonical-address return below instead, and - # WHATWG serializes those without a root dot, as here.) + # literal spelling and the Worker (#647). A host carrying + # a literal UTS-46 separator gets the same rebuild, so + # its root-dot and interior spellings return the ASCII + # dotted form too instead of the fullwidth character + # (#660). (IP literals take the canonical-address return + # below instead, and WHATWG serializes those without a + # root dot, as here.) return _url_with_encoded_components( _normalize_validated_url( _url_with_host(u, host + "." if has_root_dot else host, raw) - if percent_decoded + if percent_decoded or has_literal_separator else raw ) ) diff --git a/pkgs/lab-ops-hub/test/lib.test.ts b/pkgs/lab-ops-hub/test/lib.test.ts index 489fac7d07..09b45d2846 100644 --- a/pkgs/lab-ops-hub/test/lib.test.ts +++ b/pkgs/lab-ops-hub/test/lib.test.ts @@ -104,6 +104,31 @@ test("validateCrawlUrl returns the root dot for both dot spellings (#647)", () = assert.equal(validateCrawlUrl("http://example.com%2e%2e/").ok, false); }); +test("validateCrawlUrl returns the ASCII dot for literal separator spellings (#660)", () => { + // The Worker's returned form is the reference the container validator + // mirrors: WHATWG host parsing maps the literal UTS-46 separators + // (U+3002, U+FF0E, U+FF61) to ".", so literal spellings return the + // same dotted URL as their percent-encoded spellings — root dot and + // interior separators alike. (IP literals serialise without a root dot.) + const cases: Array<[string, string]> = [ + ["http://example.com\u3002/", "http://example.com./"], + ["http://example.com\uff0e/", "http://example.com./"], + ["http://example.com\uff61/", "http://example.com./"], + ["http://example.com\u3002:8080/", "http://example.com.:8080/"], + ["http://user@example.com\uff61/", "http://user@example.com./"], + ["http://example\u3002com/", "http://example.com/"], + ["http://example\uff0ecom/path", "http://example.com/path"], + ["http://8\u30028\u30028\u30028/", "http://8.8.8.8/"], + ]; + for (const [raw, expected] of cases) { + const r = validateCrawlUrl(raw); + assert.equal(r.ok, true, raw); + if (r.ok) assert.equal(r.url, expected, raw); + } + assert.equal(validateCrawlUrl("http://localhost\u3002/").ok, false); + assert.equal(validateCrawlUrl("http://example.com\u3002\u3002/").ok, false); +}); + test("validateCrawlUrl returns empty components normalized (#639)", () => { // The Worker's returned form is the reference the container validator // mirrors: "/" supplied for an empty path, the empty port's colon and diff --git a/pkgs/lab-ops-hub/test/test_container_validate.py b/pkgs/lab-ops-hub/test/test_container_validate.py index 590c203530..cc966eefc9 100644 --- a/pkgs/lab-ops-hub/test/test_container_validate.py +++ b/pkgs/lab-ops-hub/test/test_container_validate.py @@ -629,7 +629,11 @@ def test_refuses_mapped_spellings_of_blocked_targets(self): self.assertRefused(url) # Controls: mapped spellings of public names stay crawlable. self.assertAccepted("http://example.com/") - self.assertAccepted("http://münchen。de/") + # The U-label spelling is kept, but its literal separator + # now returns as "." (#660), not the fullwidth character. + self.assertEqual( + validate_crawl_url("http://münchen。de/"), "http://münchen.de/" + ) def test_refuses_idna_disallowed_unicode_hosts(self): # UTS-46/IDNA disallows these codepoints in a host label, and @@ -1193,6 +1197,63 @@ def test_returns_the_root_dot_for_percent_encoded_spellings(self): self.assertRefused("http://example.com../") self.assertRefused("http://example.com%2e%2e/") + def test_returns_the_ascii_dot_for_literal_separator_spellings(self): + # PR #657 fixed the percent-encoded root-dot spellings, but + # the literal UTS-46 separator spellings (U+3002, U+FF0E, + # U+FF61) still came back with the fullwidth character in + # them: judging translated them to "." but the non-percent + # return path returned the raw spelling verbatim, so the + # literal and percent spellings of the same host character + # recorded two different URLs and the literal form disagreed + # with the Worker (#660). The return now rebuilds from the + # mapped host whenever the raw host carried a literal + # separator, for interior separators as well as the root dot. + cases = { + # Root dot, one per separator character. + "http://example.com\u3002/": "http://example.com./", + "http://example.com\uff0e/": "http://example.com./", + "http://example.com\uff61/": "http://example.com./", + "http://example.com\u3002": "http://example.com./", + "http://example.com\u3002/path": "http://example.com./path", + "http://sub.example.com\uff61/": "http://sub.example.com./", + "http://example.com\u3002:8080/path": "http://example.com.:8080/path", + "http://example.com\u3002:80/": "http://example.com./", + "http://user@example.com\uff61/": "http://user@example.com./", + "http://example.com\u3002?#": "http://example.com./?#", + "http://example.com\u3002/path?#frag": ("http://example.com./path?#frag"), + # Interior separators map to "." too. + "http://example\u3002com/": "http://example.com/", + "http://example\uff0ecom/path": "http://example.com/path", + "http://sub\u3002example\u3002com/": "http://sub.example.com/", + # An IDN host keeps its U-label spelling; only the + # separator changes (#659 covers the A-label form). + "http://m\u00fcnchen\u3002de/": "http://m\u00fcnchen.de/", + # IP literals serialize without a root dot in WHATWG, + # as before — separator spellings agree there already. + "http://8.8.8.8\u3002/": "http://8.8.8.8/", + "http://8\u30028\u30028\u30028/": "http://8.8.8.8/", + } + 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. + self.assertRefused("http://localhost\u3002/") + self.assertRefused("http://localhost\uff61/") + self.assertRefused("http://127.0.0.1\u3002/") + self.assertRefused("http://example.com\u3002\u3002/") + self.assertRefused("http://example.com\uff0e\uff0e/") + # Redirect targets inherit the fix: the handler fetches the + # validated (dotted) form of a raw Location carrying one. + import urllib.request + + handler = app._ValidatingRedirectHandler() + req = urllib.request.Request("http://example.com/") + followed = handler.redirect_request( + req, None, 302, "Found", {}, "http://example.org\u3002/path" + ) + self.assertEqual(followed.full_url, "http://example.org./path") + def test_returns_empty_password_userinfo_without_the_colon(self): # WHATWG serialises userinfo from the parsed username/password: # the ":" separator appears only when the password is non-empty,