diff --git a/pkgs/lab-ops-hub/container/app.py b/pkgs/lab-ops-hub/container/app.py index b89dfb70d9..d0f479121d 100644 --- a/pkgs/lab-ops-hub/container/app.py +++ b/pkgs/lab-ops-hub/container/app.py @@ -75,6 +75,18 @@ def validate_crawl_url(raw): # are non-http(s) spellings (the scheme check below refuses them). if "\\" in raw and re.match(r"(?i)https?:", raw): raw = raw.replace("\\", "/") + # WHATWG consumes the whole delimiter run after a special scheme's + # colon — 0, 1, 3 or more slashes name the same host as exactly 2 — + # while urlparse only sees a netloc behind exactly "//", so the + # other run lengths parse as hostless and were refused as "url has + # no host" even for public targets the Worker accepts and + # normalizes (#586). Collapse the run to exactly "//" before + # parsing and judge (and return) the normalized spelling, as with + # the backslash normalization above. Blocked hosts in these + # spellings are then refused by the ordinary checks below, for the + # right reason. + if re.match(r"(?i)https?:", raw): + raw = re.sub(r"(?i)^(https?:)/*", r"\g<1>//", raw) u = urlparse(raw) if u.scheme not in ("http", "https"): raise ValueError("only http: and https: URLs may be crawled") diff --git a/pkgs/lab-ops-hub/test/test_container_validate.py b/pkgs/lab-ops-hub/test/test_container_validate.py index 59375bef46..99adbe243c 100644 --- a/pkgs/lab-ops-hub/test/test_container_validate.py +++ b/pkgs/lab-ops-hub/test/test_container_validate.py @@ -74,6 +74,52 @@ def test_redirect_handler_refuses_backslash_loopback_target(self): "http://127.0.0.1" + chr(92) + "@example.com/", ) + def test_slash_run_authority_spellings_name_the_whatwg_host(self): + # WHATWG consumes the whole delimiter run after the scheme + # colon (0, 1, 3+ slashes, and backslash runs after the #577 + # replacement); urlparse only sees a netloc behind exactly + # "//", so those spellings were refused as hostless even for + # public targets (#586). Every run length now validates — and + # is fetched — as the exactly-two spelling. + B = chr(92) + for raw in ( + "http:example.com/x", + "http:/example.com/x", + "http:///example.com/x", + "http:////example.com/x", + "http:" + B + "example.com/x", + "http:" + B * 3 + "example.com/x", + "http://" + B + "example.com/x", + "https:example.com/x", + "https:///example.com/x", + ): + scheme = raw.split(":", 1)[0] + self.assertEqual(validate_crawl_url(raw), f"{scheme}://example.com/x", raw) + # Blocked hosts in slash-run spellings are refused by the + # ordinary checks (non-public / local), not as hostless URLs. + for url in ( + "http:///127.0.0.1/x", + "http:localhost/x", + "https:" + B * 3 + "192.168.0.1/x", + "http:/10.0.0.5/x", + ): + with self.assertRaises(ValueError, msg=url) as cm: + validate_crawl_url(url) + self.assertNotIn("no host", str(cm.exception), url) + + def test_redirect_handler_refuses_slash_run_loopback_target(self): + # Redirect targets are validated by this validator alone: a + # slash-run Location naming loopback must be refused, not + # passed through as hostless-or-public (#586). + 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:///127.0.0.1/admin" + ) + def test_refuses_non_http_schemes_and_local_names(self): for url in ( "ftp://example.com/",