diff --git a/pkgs/lab-ops-hub/container/app.py b/pkgs/lab-ops-hub/container/app.py index cfd03651b..d38f9b6f0 100644 --- a/pkgs/lab-ops-hub/container/app.py +++ b/pkgs/lab-ops-hub/container/app.py @@ -71,6 +71,27 @@ def validate_crawl_url(raw): except ValueError: raise ValueError("url has an invalid port") from None host = (u.hostname or "").lower() + # WHATWG URL parsing percent-decodes the host before judging it — + # %65xample.com is example.com, %6cocalhost is localhost — while + # urlparse hands the spelling through verbatim (#505, #529). Decode + # first and judge the decoded form with every check below, the way + # the Worker does, instead of refusing the whole class: spellings + # that decode to blocked targets stay refused by the ordinary + # checks, and the invalid spellings WHATWG rejects outright + # (malformed % sequences, undecodable bytes, a decoded % or a + # decoded host delimiter) are refused inside _percent_decode_host. + # A % in a bracketed host is an IPv6 zone id ([fe80::1%25eth0]), + # which WHATWG also rejects outright — never decode those. When a + # host was decoded, the URL this function returns (and therefore + # the URL the crawler fetches) carries the decoded host: urllib + # does not percent-decode hosts, so returning the raw spelling + # would fetch a name that was never judged. + percent_decoded = False + if "%" in host: + if u.netloc.rsplit("@", 1)[-1].startswith("["): + raise ValueError(f"refusing to crawl host with an IPv6 zone id {host}") + host = _percent_decode_host(host) + percent_decoded = True # Judge the host in the mapped form its resolver will see: IDNA # processing (encodings.idna / getaddrinfo, like UTS-46 in the # Worker's WHATWG parser) NFKC-maps fullwidth/compatibility @@ -135,14 +156,10 @@ def validate_crawl_url(raw): raise ValueError( f"refusing to crawl host with an overlong label or name {host}" ) - # A raw % in the host is a percent-encoded spelling (or an IPv6 zone - # id): WHATWG URL parsing percent-decodes the host before judging it - # — %6cocalhost is localhost — and rejects the rest outright, while - # urlparse hands the spelling through verbatim and the checks below - # would mistake it for a public DNS name (or ipaddress would accept - # the zone id). Refuse every % host, fail-closed (#505). - if "%" in host: - raise ValueError(f"refusing to crawl percent-encoded host {host}") + # (Percent-encoded hosts are no longer refused wholesale here: + # they were decoded and judged in their decoded form above, and + # _percent_decode_host guarantees no % survives into this host — + # #529 supersedes #505's blanket refusal.) # A space, control or WHATWG-forbidden character in the host is not # a DNS name: WHATWG rejects it outright, urlparse hands it through # verbatim, and http.client only raises for it deep inside the @@ -197,7 +214,9 @@ def validate_crawl_url(raw): f"refusing to crawl host {host}: bracketed literal " "but not a valid IPv6 address" ) - return raw # a public DNS name; literal-IP policy does not apply + # 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 embedded = _embedded_ipv4(ip) if embedded is not None: # Translation/mapped prefixes are judged by the IPv4 they embed, @@ -234,7 +253,8 @@ def validate_crawl_url(raw): raise ValueError(f"refusing to crawl non-public address {host}") elif not ip.is_global: raise ValueError(f"refusing to crawl non-public address {host}") - return raw + # A percent-decoded host is fetched in its decoded form (#529). + return _url_with_host(u, host) if percent_decoded else raw _GLOBAL_UNICAST_V6 = ipaddress.IPv6Network("2000::/3") @@ -248,6 +268,65 @@ def validate_crawl_url(raw): _IPV4_6TO4_RELAY_ANYCAST = ipaddress.IPv4Network("192.88.99.0/24") +def _percent_decode_host(host): + # Percent-decode a host the way WHATWG URL parsing does before it + # judges the host (#529): every % must be a complete %XX hex escape, + # the decoded bytes must be valid UTF-8, and the decoded host must + # not still contain a % (a %25 double-encoded spelling, which + # WHATWG's domain processing rejects) or any of the host delimiters + # a raw spelling can never carry into u.hostname because urlparse + # splits on them (# / : ? @ [ \ ]) — after decoding they would + # change what the fetcher connects to. (Controls, space and the + # < > ^ | forbidden set need no arm here: validate_crawl_url's + # forbidden-character check refuses them on the decoded form.) + raw_bytes = bytearray() + i = 0 + while i < len(host): + c = host[i] + if c == "%": + esc = host[i + 1 : i + 3] + if len(esc) != 2 or any(ch not in "0123456789abcdefABCDEF" for ch in esc): + raise ValueError( + f"refusing to crawl host with an invalid percent escape {host}" + ) + raw_bytes.append(int(esc, 16)) + i += 3 + else: + raw_bytes.extend(c.encode("utf-8")) + i += 1 + try: + decoded = bytes(raw_bytes).decode("utf-8") + except UnicodeDecodeError: + raise ValueError( + f"refusing to crawl host that is not valid UTF-8 once decoded {host}" + ) from None + if "%" in decoded or any(c in "#/:?@[\\]" for c in decoded): + raise ValueError( + f"refusing to crawl host with a forbidden character once decoded {host}" + ) + return decoded + + +def _url_with_host(u, new_host): + # Rebuild a parsed URL with new_host in place of the netloc's host, + # preserving userinfo, port, path, query and fragment. Used for + # percent-decoded hosts (#529): the fetcher must connect to the + # decoded host that was judged, not the raw spelling. + netloc = u.netloc + if "@" in netloc: + userinfo, _, hostport = netloc.rpartition("@") + prefix = userinfo + "@" + else: + prefix, hostport = "", netloc + if hostport.startswith("["): + _, _, suffix = hostport.partition("]") + return u._replace(netloc=f"{prefix}[{new_host}]{suffix}").geturl() + if ":" in hostport: + _, _, port = hostport.partition(":") + return u._replace(netloc=f"{prefix}{new_host}:{port}").geturl() + return u._replace(netloc=prefix + new_host).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 @@ -285,7 +364,11 @@ def _inet_aton_ipv4(host): class _ValidatingRedirectHandler(urllib.request.HTTPRedirectHandler): def redirect_request(self, req, fp, code, msg, headers, newurl): - validate_crawl_url(newurl) + # Follow the validated form, not the raw Location: a redirect + # target is validated raw (no Worker normalisation in between), + # and a percent-encoded target must be fetched as the decoded + # host that was judged (#529). + newurl = validate_crawl_url(newurl) return super().redirect_request(req, fp, code, msg, headers, newurl) @@ -353,7 +436,10 @@ def do_POST(self): length = int(self.headers.get("content-length", "0")) url = json.loads(self.rfile.read(length))["url"] try: - validate_crawl_url(url) + # Fetch the validated form: identical to the request + # except for a percent-encoded host, which is fetched + # decoded, as judged (#529). + url = validate_crawl_url(url) except ValueError as e: return self._send(400, {"error": str(e)}) req = urllib.request.Request( diff --git a/pkgs/lab-ops-hub/test/lib.test.ts b/pkgs/lab-ops-hub/test/lib.test.ts index 6225c18c0..57b145df1 100644 --- a/pkgs/lab-ops-hub/test/lib.test.ts +++ b/pkgs/lab-ops-hub/test/lib.test.ts @@ -188,6 +188,47 @@ test("validateCrawlUrl refuses overlong DNS names (#513)", () => { ); }); +test("validateCrawlUrl judges percent-encoded hosts in their decoded form (#529)", () => { + // WHATWG percent-decodes the host before judging it; the container + // validator must reach the same verdicts on the decoded form. + const accepted: Array<[string, string]> = [ + ["http://%65xample.com/", "http://example.com/"], + ["http://example%2ecom/", "http://example.com/"], + ["http://8%2e8%2e8%2e8/", "http://8.8.8.8/"], + ["http://%38%2e8.8.8/", "http://8.8.8.8/"], + ]; + for (const [u, normalized] of accepted) { + const r = validateCrawlUrl(u); + assert.equal(r.ok, true, u); + if (r.ok) assert.equal(r.url, normalized, u); + } + const blocked = [ + "http://%6cocalhost/", // decodes to localhost + "http://127%2e0%2e0%2e1/", // decodes to 127.0.0.1 + "http://foo%2elocalhost/", // decodes to foo.localhost + "http://127.0.0.1%09/", // decodes to a forbidden tab + "http://8.8.8.8%zz/", // invalid percent escape + "http://example.com%/", // trailing bare % + "https://[fe80::1%25eth0]/", // IPv6 zone id + "http://exam%2fple.com/", // decodes to a host delimiter + "http://example%252ecom/", // double-encoded spelling + ]; + for (const u of blocked) { + assert.equal(validateCrawlUrl(u).ok, false, u); + } +}); + +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" }, + "http://example.org/final": { status: 200 }, + }); + const res = await fetchFollowingValidatedRedirects("https://example.com/start", fetchFn); + assert.equal(res.status, 200); + // The redirect target is fetched in its decoded (normalised) form. + assert.deepEqual(seen, ["https://example.com/start", "http://example.org/final"]); +}); + function stubFetcher(routes: Record) { const seen: string[] = []; const fetchFn = async (url: string): Promise => { diff --git a/pkgs/lab-ops-hub/test/test_container_validate.py b/pkgs/lab-ops-hub/test/test_container_validate.py index bbff1e852..a4a0ffcff 100644 --- a/pkgs/lab-ops-hub/test/test_container_validate.py +++ b/pkgs/lab-ops-hub/test/test_container_validate.py @@ -255,7 +255,10 @@ def test_refuses_percent_encoded_hosts_and_zone_ids(self): # (%6cocalhost is localhost) and rejects the other % spellings # outright; urlparse passes them through verbatim and the old # code mistook them for public DNS names, while ipaddress - # accepted the IPv6 zone id (#505). + # accepted the IPv6 zone id (#505). Every case here still + # refuses under decode-and-judge (#529): the first four decode + # to blocked or forbidden-character hosts, the rest are invalid + # escapes or a zone id. for url in ( "http://%6cocalhost/", "http://localhost%00/", @@ -269,6 +272,60 @@ def test_refuses_percent_encoded_hosts_and_zone_ids(self): ): self.assertRefused(url) + def test_accepts_percent_encoded_spellings_of_public_hosts(self): + # #505's blanket refusal also refused spellings that decode to + # public hosts, which the Worker validator accepts — the two + # validators diverged, including on redirect targets, which + # are validated raw (#529). The decoded form is judged, and + # the validated URL carries the decoded host so the fetcher + # connects to the host that was judged (urllib does not + # percent-decode hosts). + for url, expected in ( + ("http://%65xample.com/", "http://example.com/"), + ("http://example%2ecom/", "http://example.com/"), + ("http://8%2e8%2e8%2e8/", "http://8.8.8.8/"), + ("http://%38%2e8.8.8/", "http://8.8.8.8/"), + # Port, path and userinfo survive the host substitution. + ("http://%65xample.com:8080/page", "http://example.com:8080/page"), + ( + "http://user@%65xample.com/page", + "http://user@example.com/page", + ), + ): + self.assertEqual(validate_crawl_url(url), expected, url) + + def test_refuses_percent_decoded_blocked_and_invalid_hosts(self): + # Decode-and-judge (#529): spellings that decode to blocked + # targets refuse via the ordinary checks, and spellings WHATWG + # rejects outright (decoded delimiters, a double-encoded %, + # undecodable bytes) refuse at the decode step. + for url in ( + "http://127%2e0%2e0%2e1/", # -> 127.0.0.1 + "http://foo%2elocalhost/", # -> foo.localhost + "http://exam%2fple.com/", # -> exam/ple.com (delimiter) + "http://exam%3aple.com/", # -> exam:ple.com (delimiter) + "http://example%252ecom/", # -> example%2ecom (still has %) + "http://%ff.com/", # decoded bytes are not valid UTF-8 + "http://%c3%28.com/", # truncated UTF-8 sequence + ): + self.assertRefused(url) + + 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 + # decoded form of a percent-encoded public target, and refuse + # one that decodes to a blocked target. + import urllib.request + + handler = app._ValidatingRedirectHandler() + req = urllib.request.Request("http://example.com/") + followed = handler.redirect_request( + req, None, 302, "Found", {}, "http://%65xample.org/final" + ) + self.assertEqual(followed.full_url, "http://example.org/final") + with self.assertRaises(ValueError): + handler.redirect_request(req, None, 302, "Found", {}, "http://%6cocalhost/") + def test_refuses_space_hosts_and_ipvfuture_literals(self): # A space in the host is not a DNS name: WHATWG rejects it and # http.client only raises for it deep inside the fetcher, so the