diff --git a/pkgs/lab-ops-hub/container/app.py b/pkgs/lab-ops-hub/container/app.py index 357a54f644..5a155b0e09 100644 --- a/pkgs/lab-ops-hub/container/app.py +++ b/pkgs/lab-ops-hub/container/app.py @@ -60,6 +60,21 @@ def validate_crawl_url(raw): + # WHATWG URL parsing treats a backslash as a path delimiter for the + # special schemes (http/https): it ends the authority, so the host + # of "http://127.0.0.1\\@example.com/" is 127.0.0.1. urlparse instead + # hands the backslash through as a netloc character, reads the text + # after the "@" as the host, and would judge — and fetch — a host + # the WHATWG parser never named, in both directions + # ("example.com\\@127.0.0.1" is example.com to WHATWG but reads as + # the loopback address here) (#577). Normalize first and judge the + # normalized spelling: every check below, and the URL this function + # returns (and the fetcher therefore fetches), then names the same + # host the Worker's parser names. Only a raw backslash is + # structural — a percent-encoded %5C is data and is untouched, as + # are non-http(s) spellings (the scheme check below refuses them). + if "\\" in raw and re.match(r"(?i)https?:", raw): + raw = raw.replace("\\", "/") 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 8eefa77646..f59da6554b 100644 --- a/pkgs/lab-ops-hub/test/test_container_validate.py +++ b/pkgs/lab-ops-hub/test/test_container_validate.py @@ -23,6 +23,57 @@ def assertRefused(self, url): def assertAccepted(self, url): self.assertEqual(validate_crawl_url(url), url) + def test_backslash_ends_the_authority_like_whatwg(self): + # A raw backslash is a path delimiter to WHATWG for http/https, + # so it ends the authority: the host of + # "http://127.0.0.1\\@example.com/" is 127.0.0.1, not the + # example.com urlparse reads after the "@". Judge the host + # WHATWG names, in both directions (#577). + B = chr(92) + for url in ( + "http://127.0.0.1" + B + "@example.com/", + "http://localhost" + B + "@example.com/", + "http://192.168.0.1" + B + "@example.com/", + ): + self.assertRefused(url) + # The mirror spelling names example.com to WHATWG; the + # validated (and fetched) form is the normalized spelling, + # whose urlparse host is the judged host. + self.assertEqual( + validate_crawl_url("http://example.com" + B + "@127.0.0.1/"), + "http://example.com/@127.0.0.1/", + ) + # Backslash slash-runs after the scheme name example.com too. + self.assertEqual( + validate_crawl_url("http:" + B + B + "example.com/"), + "http://example.com/", + ) + self.assertEqual( + validate_crawl_url("http:/" + B + "example.com/"), + "http://example.com/", + ) + # A percent-encoded %5C is data, not a delimiter: the spelling + # is judged and returned exactly as before. + self.assertAccepted("http://example.com/%5c@127.0.0.1/") + + def test_redirect_handler_refuses_backslash_loopback_target(self): + # Redirect targets are validated by this validator alone + # (#577): a Location spelling whose WHATWG host is loopback + # must be refused, not followed as its post-"@" tail. + 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" + chr(92) + "@example.com/", + ) + def test_refuses_non_http_schemes_and_local_names(self): for url in ( "ftp://example.com/",