diff --git a/pkgs/lab-ops-hub/container/app.py b/pkgs/lab-ops-hub/container/app.py index 03488a63c8..8702c414e7 100644 --- a/pkgs/lab-ops-hub/container/app.py +++ b/pkgs/lab-ops-hub/container/app.py @@ -48,6 +48,14 @@ def validate_crawl_url(raw): host = host[:-1] if not host: raise ValueError("url has no 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}") if host == "localhost" or host.endswith(_BLOCKED_SUFFIXES): raise ValueError(f"refusing to crawl local host {host}") try: diff --git a/pkgs/lab-ops-hub/test/test_container_validate.py b/pkgs/lab-ops-hub/test/test_container_validate.py index 845eaeaf91..f8dbf116df 100644 --- a/pkgs/lab-ops-hub/test/test_container_validate.py +++ b/pkgs/lab-ops-hub/test/test_container_validate.py @@ -173,6 +173,25 @@ def test_refuses_malformed_or_out_of_range_ports(self): ): self.assertRefused(url) + def test_refuses_percent_encoded_hosts_and_zone_ids(self): + # WHATWG URL parsing percent-decodes the host before judging it + # (%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). + for url in ( + "http://%6cocalhost/", + "http://localhost%00/", + "http://127.0.0.1%09/", + "http://8.8.8.8%09/", + "http://8.8.8.8%zz/", + "http://example.com%09/", + "http://example.com%/", # malformed: bare % + "http://example.com%2/", # malformed: truncated escape + "https://[2001:4860:4860::8888%25eth0]/", # IPv6 zone id + ): + self.assertRefused(url) + def test_accepts_public_targets(self): for url in ( "https://example.com/page",