From 3201b8868a62485832961e6de41026228347b93f Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Thu, 22 Oct 2026 18:42:37 -0500 Subject: [PATCH] lab-ops-hub: percent-encode non-ASCII outside the host in the container validator (#615) --- pkgs/lab-ops-hub/container/app.py | 44 ++++++++++++++++++- pkgs/lab-ops-hub/test/lib.test.ts | 15 +++++++ .../test/test_container_validate.py | 39 ++++++++++++++++ 3 files changed, 96 insertions(+), 2 deletions(-) diff --git a/pkgs/lab-ops-hub/container/app.py b/pkgs/lab-ops-hub/container/app.py index abce693943..f9534c1865 100644 --- a/pkgs/lab-ops-hub/container/app.py +++ b/pkgs/lab-ops-hub/container/app.py @@ -357,7 +357,9 @@ def validate_crawl_url(raw): ) # A public DNS name; literal-IP policy does not apply. # A percent-decoded host is fetched in its decoded form. - return _url_with_host(u, host) if percent_decoded else raw + return _url_with_encoded_non_ascii( + _url_with_host(u, host) if percent_decoded else raw + ) embedded = _embedded_ipv4(ip) if embedded is not None: # Translation/mapped prefixes are judged by the IPv4 they embed, @@ -395,7 +397,9 @@ def validate_crawl_url(raw): elif not ip.is_global: raise ValueError(f"refusing to crawl non-public address {host}") # A percent-decoded host is fetched in its decoded form (#529). - return _url_with_host(u, host) if percent_decoded else raw + return _url_with_encoded_non_ascii( + _url_with_host(u, host) if percent_decoded else raw + ) def _judge_a_label(label, host): @@ -647,6 +651,42 @@ def _url_with_host(u, new_host): return u._replace(netloc=prefix + new_host).geturl() +def _encode_non_ascii(text): + # UTF-8 percent-encode every non-ASCII character, the way WHATWG's + # URL serializer encodes the userinfo, path, query and fragment + # components. ASCII characters — including existing %XX sequences — + # pass through byte-identical, so nothing is double-encoded. + return "".join( + "".join(f"%{byte:02X}" for byte in char.encode("utf-8")) + if ord(char) > 0x7F + else char + for char in text + ) + + +def _url_with_encoded_non_ascii(url): + # The URL validate_crawl_url returns is the URL the fetcher fetches, + # and http.client encodes the request line as ASCII: a raw non-ASCII + # character in the path, query or userinfo raises UnicodeEncodeError + # before any request is sent, so a target both validators accepted + # 502s in the container while the Worker fetches its WHATWG-encoded + # form (#615). Encode those components on the returned URL. The host + # is deliberately untouched: it is judged in mapped form above and + # fetched via IDNA, as with the existing IDN spellings. + u = urlparse(url) + netloc = u.netloc + if "@" in netloc: + userinfo, _, hostport = netloc.rpartition("@") + netloc = f"{_encode_non_ascii(userinfo)}@{hostport}" + return u._replace( + netloc=netloc, + path=_encode_non_ascii(u.path), + params=_encode_non_ascii(u.params), + query=_encode_non_ascii(u.query), + fragment=_encode_non_ascii(u.fragment), + ).geturl() + + def _embedded_ipv4(ip): # Prefixes that embed an IPv4 address are judged by that address, the # same policy as plain IPv4 (#472). ipaddress folds the IPv4-mapped diff --git a/pkgs/lab-ops-hub/test/lib.test.ts b/pkgs/lab-ops-hub/test/lib.test.ts index 31c538a6fe..7dc7b793a5 100644 --- a/pkgs/lab-ops-hub/test/lib.test.ts +++ b/pkgs/lab-ops-hub/test/lib.test.ts @@ -19,6 +19,21 @@ test("validateCrawlUrl accepts public http(s) targets", () => { assert.equal(validateCrawlUrl("https://8.8.8.8/").ok, true); }); +test("validateCrawlUrl percent-encodes non-ASCII outside the host (#615)", () => { + // The Worker's returned form is the reference the container validator + // mirrors: WHATWG UTF-8 percent-encoding in userinfo/path/query. + const cases: Array<[string, string]> = [ + ["http://example.com/café", "http://example.com/caf%C3%A9"], + ["http://example.com/?q=café", "http://example.com/?q=caf%C3%A9"], + ["http://user:café@example.com/", "http://user:caf%C3%A9@example.com/"], + ]; + 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); + } +}); + test("validateCrawlUrl refuses non-http schemes and junk", () => { assert.equal(validateCrawlUrl("ftp://example.com/").ok, false); assert.equal(validateCrawlUrl("file:///etc/passwd").ok, false); diff --git a/pkgs/lab-ops-hub/test/test_container_validate.py b/pkgs/lab-ops-hub/test/test_container_validate.py index cbcf87c8da..02503be401 100644 --- a/pkgs/lab-ops-hub/test/test_container_validate.py +++ b/pkgs/lab-ops-hub/test/test_container_validate.py @@ -884,6 +884,33 @@ def test_accepts_public_targets(self): ): self.assertAccepted(url) + def test_percent_encodes_non_ascii_outside_the_host(self): + # WHATWG percent-encodes non-ASCII characters in the userinfo, + # path, query and fragment as UTF-8; urlparse hands them through + # verbatim, and http.client then raises UnicodeEncodeError on + # the returned URL before any request is sent — a target both + # validators accepted 502s in the container (#615). The returned + # URL carries the encoded form; the host keeps its spelling + # (judged in mapped form, fetched via IDNA) and existing %XX + # sequences are not double-encoded. + cases = { + "http://example.com/café": "http://example.com/caf%C3%A9", + "http://example.com/日本語": ( + "http://example.com/%E6%97%A5%E6%9C%AC%E8%AA%9E" + ), + "http://example.com/?q=café": "http://example.com/?q=caf%C3%A9", + "http://example.com/?q=\U0001f600": ("http://example.com/?q=%F0%9F%98%80"), + "http://example.com/x#café": "http://example.com/x#caf%C3%A9", + "http://user:café@example.com/": ("http://user:caf%C3%A9@example.com/"), + "http://café@example.com/": "http://caf%C3%A9@example.com/", + "http://münchen.de/café": "http://münchen.de/caf%C3%A9", + } + for raw, expected in cases.items(): + self.assertEqual(validate_crawl_url(raw), expected, raw) + # Already-encoded spellings pass through byte-identical. + self.assertAccepted("http://example.com/caf%C3%A9") + self.assertAccepted("http://example.com/?q=caf%C3%A9") + class TestFetchCrawl(unittest.TestCase): # _fetch_crawl fetches an already-validated URL (validation is @@ -989,6 +1016,18 @@ def test_redirect_failures_raise_not_a_302_result(self): self.assertEqual(status, 200) self.assertIn(b"Missing Thing", raw) + def test_fetches_percent_encoded_non_ascii_targets(self): + # #615: the form validate_crawl_url returns for a non-ASCII + # path/query is fetchable end to end (the raw spelling raises + # UnicodeEncodeError inside http.client before any request). + url = app._url_with_encoded_non_ascii( + f"http://127.0.0.1:{self.port}/café?q=café" + ) + self.assertEqual(url, f"http://127.0.0.1:{self.port}/caf%C3%A9?q=caf%C3%A9") + status, raw = app._fetch_crawl(url) + self.assertEqual(status, 200) + self.assertIn(b"Missing Thing", raw) + def test_transport_failure_still_raises(self): import urllib.error