diff --git a/pkgs/lab-ops-hub/container/app.py b/pkgs/lab-ops-hub/container/app.py index e1a24c7a17..03488a63c8 100644 --- a/pkgs/lab-ops-hub/container/app.py +++ b/pkgs/lab-ops-hub/container/app.py @@ -31,6 +31,14 @@ def validate_crawl_url(raw): u = urlparse(raw) if u.scheme not in ("http", "https"): raise ValueError("only http: and https: URLs may be crawled") + # A port that is present must parse as an integer in range: urlparse + # only raises for it lazily (when .port is read, deep inside the + # fetcher), so read it here and refuse up front — the Worker's + # WHATWG URL parser rejects these spellings outright (#502). + try: + u.port + except ValueError: + raise ValueError("url has an invalid port") from None host = (u.hostname or "").lower() # 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); @@ -47,6 +55,19 @@ def validate_crawl_url(raw): except ValueError: ip = _inet_aton_ipv4(host) if ip is None: + # A host whose final label is numeric is an IP literal in + # the URL grammar (WHATWG "ends in a number"), not a DNS + # name: no numeric TLDs exist. If it reached here it is an + # IPv4 spelling nothing accepts (256.1.1.1, 1.2.3.4.5, + # 4294967296) or a name like example.123 / example.0x7f — + # refuse it instead of reclassifying it as a public DNS + # name, matching the Worker's validator (#502). + last_label = host.rsplit(".", 1)[-1] + if re.fullmatch(r"0[xX][0-9a-fA-F]+|[0-9]+", last_label): + raise ValueError( + f"refusing to crawl host {host}: numeric final label " + "but not a valid IPv4 address" + ) return raw # a public DNS name; literal-IP policy does not apply embedded = _embedded_ipv4(ip) if embedded is not None: diff --git a/pkgs/lab-ops-hub/test/test_container_validate.py b/pkgs/lab-ops-hub/test/test_container_validate.py index 698a691ee5..845eaeaf91 100644 --- a/pkgs/lab-ops-hub/test/test_container_validate.py +++ b/pkgs/lab-ops-hub/test/test_container_validate.py @@ -143,6 +143,36 @@ def test_agrees_with_worker_validator_on_special_ipv6_ranges(self): ): self.assertAccepted(url) + def test_refuses_numeric_final_label_hosts_that_are_not_ipv4(self): + # WHATWG "ends in a number" (#502): a host whose final label is + # numeric is an IP literal in the URL grammar, never a DNS name. + # These spellings parse as neither ipaddress nor inet_aton IPv4, + # so they must be refused, not fall through to "public DNS name" + # — the Worker's WHATWG validator refuses every one. + for url in ( + "http://256.1.1.1/", + "http://1.2.3.256/", + "http://1.2.3.4.5/", + "http://4294967296/", # one past the largest integer IPv4 + "http://0.0.0.0.0/", + "http://example.123/", + "http://example.0x7f/", + "http://foo.123.456/", + ): + self.assertRefused(url) + + def test_refuses_malformed_or_out_of_range_ports(self): + # urlparse reports these only lazily via .port; the validator + # must refuse them up front like the Worker's URL parser (#502). + for url in ( + "http://8.8.8.8:abc/", + "http://8.8.8.8:99999/", + "http://8.8.8.8:-1/", + "http://example.com:99999/", + "http://example.com:80abc/", + ): + self.assertRefused(url) + def test_accepts_public_targets(self): for url in ( "https://example.com/page", @@ -150,6 +180,10 @@ def test_accepts_public_targets(self): "http://134744072/", # 8.8.8.8 in integer spelling: public, allowed "https://[2606:4700:4700::1111]/", "http://example.com./", # FQDN root-dot spelling of a public name + "http://dead.beef/", # hex-looking labels, non-numeric final label + "http://a1.beef/", + "http://123.example.com/", # numeric label, but not the final one + "http://example.com:8080/", # an ordinary explicit port ): self.assertAccepted(url)