diff --git a/pkgs/lab-ops-hub/container/app.py b/pkgs/lab-ops-hub/container/app.py index d74bd186e7..54140aa785 100644 --- a/pkgs/lab-ops-hub/container/app.py +++ b/pkgs/lab-ops-hub/container/app.py @@ -109,8 +109,17 @@ def validate_crawl_url(raw): # 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). + # The backslash is structural only up to the first "?" or "#": in + # the query and fragment WHATWG keeps it as data, so replacing it + # there would fetch a different query than the Worker fetches for + # the same validated target (#620). if "\\" in raw and re.match(r"(?i)https?:", raw): - raw = raw.replace("\\", "/") + cut = len(raw) + for delim in "?#": + i = raw.find(delim) + if i != -1: + cut = min(cut, i) + raw = raw[:cut].replace("\\", "/") + raw[cut:] # 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 diff --git a/pkgs/lab-ops-hub/test/test_container_validate.py b/pkgs/lab-ops-hub/test/test_container_validate.py index b45626b157..1823e77eca 100644 --- a/pkgs/lab-ops-hub/test/test_container_validate.py +++ b/pkgs/lab-ops-hub/test/test_container_validate.py @@ -56,6 +56,22 @@ def test_backslash_ends_the_authority_like_whatwg(self): # is judged and returned exactly as before. self.assertAccepted("http://example.com/%5c@127.0.0.1/") + def test_backslash_in_query_and_fragment_is_data(self): + # #620: the #577 replacement is structural only up to the first + # "?" or "#" — WHATWG keeps a backslash in the query/fragment + # verbatim, and the Worker fetches it that way. + v = app.validate_crawl_url + self.assertEqual(v(r"http://example.com/?q=a\b"), r"http://example.com/?q=a\b") + self.assertEqual(v(r"http://example.com/#a\b"), r"http://example.com/#a\b") + self.assertEqual( + v(r"https://example.com/?q=a\b\c#x\y"), r"https://example.com/?q=a\b\c#x\y" + ) + # Control: a path backslash still normalizes, and an authority + # backslash before a query still ends the authority (#577). + self.assertEqual(v(r"http://example.com/a\b"), "http://example.com/a/b") + with self.assertRaises(ValueError): + v(r"http://127.0.0.1\@example.com/?q=a\b") + 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