From bac38fea34a9a906348350321344f49fe7109cba Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Thu, 22 Oct 2026 03:02:22 -0500 Subject: [PATCH] lab-ops-hub: refuse crawl hosts whose NFKC mapping creates a delimiter (#567) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _percent_decode_host refused delimiters in the percent-decoded form, but validate_crawl_url then NFKC-mapped the host with no structural re-check: a percent-encoded compatibility character (U+FF0F -> /, U+FF1A -> :, U+FF20 -> @, U+FF05 -> %, U+2100 -> a/c) spliced a delimiter into the returned URL, so the container fetched a host that was never judged — localhost%ef%bc%8fexample.com validated as http://localhost/example.com/. Re-check % # / : ? @ [ \ ] on the fully mapped form (colon exempt only inside a bracketed IPv6 literal) and refuse any mapped delimiter. Tests: Python battery of 14 mapped-delimiter spellings plus the localhost/127.0.0.1/192.168.0.1 + U+FF0F cases, a redirect-handler refusal for a mapped-delimiter target, and a re-parse invariant for accepted percent-encoded URLs (returned URL re-parses to exactly the judged host); TS parity battery in lib.test.ts (Worker already refuses all of these). Ablation: reverting app.py fails the new refusal tests. --- pkgs/lab-ops-hub/container/app.py | 24 ++++++++ pkgs/lab-ops-hub/test/lib.test.ts | 33 +++++++++++ .../test/test_container_validate.py | 59 +++++++++++++++++++ 3 files changed, 116 insertions(+) diff --git a/pkgs/lab-ops-hub/container/app.py b/pkgs/lab-ops-hub/container/app.py index d38f9b6f01..fcd202600b 100644 --- a/pkgs/lab-ops-hub/container/app.py +++ b/pkgs/lab-ops-hub/container/app.py @@ -110,6 +110,30 @@ def validate_crawl_url(raw): host = unicodedata.normalize("NFKC", host).lower() host = host.translate({0x3002: ".", 0xFF0E: ".", 0xFF61: "."}) 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 + # the decoded form, but a percent-encoded compatibility character + # decodes to an innocent-looking codepoint whose NFKC mapping is + # a delimiter — U+FF0F -> /, U+FF1A -> :, U+FF20 -> @, U+FF03 -> + # #, U+FF1F -> ?, U+FF3C -> \, U+FF3B/U+FF3D -> [ ], U+FF05 -> %, + # and expansions like U+2100 -> "a/c" and U+2105 -> "c/o" (#567). + # Raw spellings cannot smuggle these past urlparse (its NFKC + # netloc guard raises), but the percent-encoded spelling is pure + # ASCII in the raw netloc, so that guard never fires — and the + # spliced returned URL would make the fetcher connect to a host + # that was never judged ("localhost%ef%bc%8fexample.com" came + # back as "localhost/example.com"). Re-check the structural + # characters on the fully mapped form: refuse any mapped + # delimiter, including a mapped % (the decode step guarantees no + # % survives, and mapping must not reintroduce one). A colon is + # only legitimate inside a bracketed IPv6 literal; an unbracketed + # mapped colon is a delimiter, not an address. + if any(c in "%#/?@[\\]" for c in host) or ( + ":" in host and not u.netloc.rsplit("@", 1)[-1].startswith("[") + ): + raise ValueError( + f"refusing to crawl host with a forbidden character once mapped {host!r}" + ) # A single trailing dot is the DNS root dot (the FQDN spelling of the # same host: "localhost." is "localhost", "127.0.0.1." is 127.0.0.1); # strip it before the checks so the dotted spelling cannot evade diff --git a/pkgs/lab-ops-hub/test/lib.test.ts b/pkgs/lab-ops-hub/test/lib.test.ts index 57b145df1f..eba81019d4 100644 --- a/pkgs/lab-ops-hub/test/lib.test.ts +++ b/pkgs/lab-ops-hub/test/lib.test.ts @@ -218,6 +218,39 @@ test("validateCrawlUrl judges percent-encoded hosts in their decoded form (#529) } }); +test("validateCrawlUrl refuses percent-encoded hosts whose NFKC mapping is a delimiter (#567)", () => { + // Parity battery for the container fix: a percent-encoded + // compatibility character decodes to a codepoint whose NFKC + // mapping is a host delimiter (or expands to one). The Worker's + // WHATWG parser refuses every spelling outright; the container + // originally spliced the mapped delimiter into the returned URL + // and fetched an unjudged host (localhost / private addresses). + const blocked = [ + "http://localhost%ef%bc%8fexample.com/", // U+FF0F -> / + "http://127.0.0.1%ef%bc%8fexample.com/", // U+FF0F -> / + "http://192.168.0.1%ef%bc%8fadmin/", // U+FF0F -> / + "http://exam%ef%bc%8fple.com/", // U+FF0F -> / + "http://exam%ef%bc%9aple.com/", // U+FF1A -> : + "http://exam%ef%bc%a0ple.com/", // U+FF20 -> @ + "http://exam%ef%bc%83ple.com/", // U+FF03 -> # + "http://exam%ef%bc%9fple.com/", // U+FF1F -> ? + "http://exam%ef%bc%bcple.com/", // U+FF3C -> backslash + "http://exam%ef%bc%bbple.com/", // U+FF3B -> [ + "http://exam%ef%bc%bdple.com/", // U+FF3D -> ] + "http://example%ef%bc%85com/", // U+FF05 -> % + "http://%e2%84%80.com/", // U+2100 expands to a/c + "http://%e2%84%85.com/", // U+2105 expands to c/o + ]; + for (const u of blocked) { + assert.equal(validateCrawlUrl(u).ok, false, u); + } + // Control: U+FF5E maps to ~, not a delimiter — still accepted, + // and the returned URL re-parses to the judged host. + const tilde = validateCrawlUrl("http://exam%ef%bd%9eple.com/"); + assert.equal(tilde.ok, true); + if (tilde.ok) assert.equal(tilde.url, "http://exam~ple.com/"); +}); + test("fetchFollowingValidatedRedirects follows a percent-encoded public redirect target (#529)", async () => { const { fetchFn, seen } = stubFetcher({ "https://example.com/start": { status: 302, location: "http://%65xample.org/final" }, diff --git a/pkgs/lab-ops-hub/test/test_container_validate.py b/pkgs/lab-ops-hub/test/test_container_validate.py index a4a0ffcff1..2813a8bec5 100644 --- a/pkgs/lab-ops-hub/test/test_container_validate.py +++ b/pkgs/lab-ops-hub/test/test_container_validate.py @@ -310,6 +310,65 @@ def test_refuses_percent_decoded_blocked_and_invalid_hosts(self): ): self.assertRefused(url) + def test_refuses_percent_encoded_nfkc_mapped_delimiters(self): + # The decode step (#529) refuses delimiters in the decoded + # form, but a percent-encoded compatibility character decodes + # to a codepoint whose NFKC mapping IS a delimiter, and the + # mapped form is what gets spliced into the returned URL: + # "localhost%ef%bc%8fexample.com" came back ALLOW as + # "http://localhost/example.com/" — the fetcher connected to + # localhost, a host that was never judged (#567). The Worker + # refuses every one of these outright. + for url in ( + "http://localhost%ef%bc%8fexample.com/", # U+FF0F -> / + "http://127.0.0.1%ef%bc%8fexample.com/", # U+FF0F -> / + "http://192.168.0.1%ef%bc%8fadmin/", # U+FF0F -> / + "http://exam%ef%bc%8fple.com/", # U+FF0F -> / + "http://exam%ef%bc%9aple.com/", # U+FF1A -> : + "http://exam%ef%bc%a0ple.com/", # U+FF20 -> @ + "http://exam%ef%bc%83ple.com/", # U+FF03 -> # + "http://exam%ef%bc%9fple.com/", # U+FF1F -> ? + "http://exam%ef%bc%bcple.com/", # U+FF3C -> backslash + "http://exam%ef%bc%bbple.com/", # U+FF3B -> [ + "http://exam%ef%bc%bdple.com/", # U+FF3D -> ] + "http://example%ef%bc%85com/", # U+FF05 -> % + "http://%e2%84%80.com/", # U+2100 expands to a/c + "http://%e2%84%85.com/", # U+2105 expands to c/o + ): + self.assertRefused(url) + + def test_accepted_percent_urls_reparse_to_the_judged_host(self): + # For every accepted percent-encoded URL, re-parsing the + # returned URL must yield exactly the host that was judged — + # no delimiter may be spliced in by decoding or mapping + # (#567 acceptance criterion). + from urllib.parse import urlparse + + for url, expected_host in ( + ("http://%65xample.com/", "example.com"), + ("http://example%2ecom/", "example.com"), + ("http://8%2e8%2e8%2e8/", "8.8.8.8"), + ("http://%65xample.com:8080/page", "example.com"), + # U+FF5E maps to ~, not a delimiter: the mapped form is + # judged and returned, and re-parses to itself. + ("http://exam%ef%bd%9eple.com/", "exam~ple.com"), + ): + returned = validate_crawl_url(url) + self.assertEqual(urlparse(returned).hostname, expected_host, url) + + def test_redirect_handler_refuses_nfkc_mapped_delimiter_target(self): + # Redirect targets are validated raw (#529/#567): a Location + # whose percent-encoded host NFKC-maps into a private host + # plus delimiter must be refused, not followed. + import urllib.request + + handler = app._ValidatingRedirectHandler() + req = urllib.request.Request("http://example.com/") + with self.assertRaises(ValueError): + handler.redirect_request( + req, None, 302, "Found", {}, "http://192.168.0.1%ef%bc%8fadmin/" + ) + def test_redirect_handler_follows_the_decoded_target(self): # Redirect targets are validated raw, with no Worker # normalisation in between (#529): the handler must fetch the