From 679645463a4e720e8c51ee0f2b8f96a90ff22ce3 Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Fri, 23 Oct 2026 01:39:43 -0500 Subject: [PATCH 1/2] fix(lab-ops-hub): keep a bare-semicolon dot-spelled segment in the container validator's returned URL (#655) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit urlparse splits the last path segment's ;params tail off the path and a bare trailing ';' leaves params empty, so rebuilds from the split parts dropped the ';' and _resolve_dot_segments then treated '..;', '.;' and '%2e;' as dot segments — collapsing the path (and popping the parent for '..;') where the Worker's WHATWG parser keeps the segment as ordinary data. Take the full path from urlsplit (which never splits params) at every rebuild site: _normalize_validated_url, _url_with_encoded_components, _url_with_host and _split_userinfo. New regression test pins the empty-params spellings alongside the existing '..;foo' controls; ablation-verified (fails pre-fix with the exact divergence), Python suite 57/57. --- pkgs/lab-ops-hub/container/app.py | 47 ++++++++++++++----- .../test/test_container_validate.py | 40 ++++++++++++++++ 2 files changed, 76 insertions(+), 11 deletions(-) diff --git a/pkgs/lab-ops-hub/container/app.py b/pkgs/lab-ops-hub/container/app.py index 1294c8065f..040ee990d1 100644 --- a/pkgs/lab-ops-hub/container/app.py +++ b/pkgs/lab-ops-hub/container/app.py @@ -20,7 +20,7 @@ import urllib.request from html.parser import HTMLParser from http.server import BaseHTTPRequestHandler, HTTPServer -from urllib.parse import unquote, urlparse +from urllib.parse import unquote, urlparse, urlsplit # Crawl-target policy (#441): the Worker validates before dispatching, # and this service re-validates independently (defence in depth — it is @@ -732,13 +732,23 @@ def _url_with_host(u, new_host, source_url=None): prefix = userinfo + "@" else: prefix, hostport = "", netloc + # urlparse splits the last segment's ";params" tail off the path and + # a bare trailing ";" leaves params empty, so a geturl() rebuild from + # the split parts silently drops that ";" (#655). Take the full path + # from urlsplit (which never splits params) when the source spelling + # is available, as the rebuild sites below do. + rebuild = ( + u._replace(path=urlsplit(source_url).path, params="") + if source_url + else u + ) if hostport.startswith("["): _, _, suffix = hostport.partition("]") - return u._replace(netloc=f"{prefix}[{new_host}]{suffix}").geturl() + markers + return rebuild._replace(netloc=f"{prefix}[{new_host}]{suffix}").geturl() + markers if ":" in hostport: _, _, port = hostport.partition(":") - return u._replace(netloc=f"{prefix}{new_host}:{port}").geturl() + markers - return u._replace(netloc=prefix + new_host).geturl() + markers + return rebuild._replace(netloc=f"{prefix}{new_host}:{port}").geturl() + markers + return rebuild._replace(netloc=prefix + new_host).geturl() + markers _ASCII_LOWER = str.maketrans("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz") @@ -813,10 +823,11 @@ def _normalize_validated_url(url): # the Worker byte for byte). Only ASCII case folds in the # host: the IDN spelling itself is deliberately kept, as before # (judged in mapped form, fetched via IDNA — #615); urlparse has - # already lowercased the scheme. The path's ";params" tail is - # rejoined before resolving: urlparse splits it off the last + # already lowercased the scheme. The path is taken whole (see + # below): urlparse splits the ";params" tail off the last # segment, but WHATWG treats ";..." as part of that segment, so - # a trailing "..;foo" is data, not a double-dot segment. + # a trailing "..;foo" — or a bare "..;" — is data, not a + # double-dot segment. u = urlparse(url) netloc = u.netloc if "@" in netloc: @@ -846,7 +857,13 @@ def _normalize_validated_url(url): port_norm = f":{port}" else: port_norm = f":{port_span}" - full_path = u.path + (f";{u.params}" if u.params else "") + # Take the full path from urlsplit, which never splits the + # ";params" tail: urlparse hands "..;foo" back as path "/.." + + # params "foo" (rejoinable), but a bare trailing ";" ("..;", + # "%2e;") leaves params empty and the ";" is unrecoverable from + # the split parts — rejoining only non-empty params dropped it and + # the segment was then resolved as a dot segment (#655). + full_path = urlsplit(url).path resolved_path = _resolve_dot_segments(full_path) if not resolved_path: # A special-scheme URL with an empty path serializes with @@ -936,10 +953,14 @@ def _url_with_encoded_components(url): # (both components empty, #651), so its "@" drops too. encoded_userinfo = _encode_userinfo(userinfo) netloc = f"{encoded_userinfo}@{hostport}" if encoded_userinfo else hostport + # Encode the full path (urlsplit never splits the ";params" tail; + # urlparse's split parts lose a bare trailing ";" — #655). The + # params tail was encoded with the same _PATH_EXTRA set, so one + # pass over the whole path is equivalent for non-empty params. return u._replace( netloc=netloc, - path=_encode_url_component(u.path, _PATH_EXTRA), - params=_encode_url_component(u.params, _PATH_EXTRA), + path=_encode_url_component(urlsplit(url).path, _PATH_EXTRA), + params="", query=_encode_url_component(u.query, _QUERY_EXTRA), fragment=_encode_url_component(u.fragment, _FRAGMENT_EXTRA), ).geturl() + _empty_query_fragment_markers(url) @@ -1007,7 +1028,11 @@ def _split_userinfo(url): username, _, password = userinfo.partition(":") username = unquote(username) password = unquote(password) - stripped = u._replace(netloc=hostport).geturl() + # Keep the full path: a geturl() rebuild from urlparse's split + # parts drops a bare trailing ";" in the path (#655). + stripped = u._replace( + netloc=hostport, path=urlsplit(url).path, params="" + ).geturl() if not username and not password: return stripped, None token = base64.b64encode(f"{username}:{password}".encode("utf-8")) diff --git a/pkgs/lab-ops-hub/test/test_container_validate.py b/pkgs/lab-ops-hub/test/test_container_validate.py index 8b4600ef75..103c6e49d8 100644 --- a/pkgs/lab-ops-hub/test/test_container_validate.py +++ b/pkgs/lab-ops-hub/test/test_container_validate.py @@ -989,6 +989,46 @@ def test_returns_dot_segments_host_case_and_ports_normalized(self): self.assertRefused("http://LOCALHOST/a/../b") self.assertRefused("http://127.0.0.1:80/x") + def test_returns_bare_semicolon_dot_spelled_segments_verbatim(self): + # A trailing segment that is a dot spelling plus a bare ";" + # ("..;", ".;", "%2e;") is an ordinary segment to WHATWG — + # the ";" is data — and the Worker returns it unchanged. + # urlparse splits the last segment's ";params" tail off the + # path and a bare ";" leaves params empty, so the split parts + # lost the ";" and the segment was resolved as a dot segment: + # "..;" collapsed the path (popping its parent), ".;" was + # dropped (#655). The non-empty-params spellings ("..;foo") + # were already pinned above; these are their empty-params + # counterparts, including before "?" / "#" and on the + # userinfo and percent-decoded-host return paths. + for url in ( + "http://example.com/..;", + "http://example.com/.;", + "http://example.com/foo/..;", + "http://example.com/a/.;", + "http://example.com/a/b/..;", + "http://example.com/%2e;", + "http://example.com/%2e%2e;", + "http://example.com/a/%2E;", + "http://example.com/..;?q", + "http://example.com/..;#f", + "http://example.com/..;?", + "http://user@example.com/..;", + "http://8.8.8.8/..;", + ): + self.assertAccepted(url) + # The percent-decoded-host return path rebuilds the URL with + # the decoded host (#529): the bare-";" segment survives that + # rebuild too. + self.assertEqual( + validate_crawl_url("http://%65xample.com/..;"), + "http://example.com/..;", + ) + # The fetcher's userinfo strip rebuilds the URL too: the + # stripped form keeps the bare-";" segment. + stripped, _ = app._split_userinfo("http://user@example.com/foo/..;") + self.assertEqual(stripped, "http://example.com/foo/..;") + def test_redirect_handler_follows_the_normalized_target(self): # Redirect targets inherit the normalization (#633): the # handler validates the raw Location and fetches the form From e41600204de550f0810403a2312af9be015cc938 Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Fri, 23 Oct 2026 01:43:49 -0500 Subject: [PATCH 2/2] style(lab-ops-hub): ruff-format the #655 rebuild sites (treefmt) --- pkgs/lab-ops-hub/container/app.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/pkgs/lab-ops-hub/container/app.py b/pkgs/lab-ops-hub/container/app.py index 040ee990d1..e107f85d60 100644 --- a/pkgs/lab-ops-hub/container/app.py +++ b/pkgs/lab-ops-hub/container/app.py @@ -737,14 +737,12 @@ def _url_with_host(u, new_host, source_url=None): # the split parts silently drops that ";" (#655). Take the full path # from urlsplit (which never splits params) when the source spelling # is available, as the rebuild sites below do. - rebuild = ( - u._replace(path=urlsplit(source_url).path, params="") - if source_url - else u - ) + rebuild = u._replace(path=urlsplit(source_url).path, params="") if source_url else u if hostport.startswith("["): _, _, suffix = hostport.partition("]") - return rebuild._replace(netloc=f"{prefix}[{new_host}]{suffix}").geturl() + markers + return ( + rebuild._replace(netloc=f"{prefix}[{new_host}]{suffix}").geturl() + markers + ) if ":" in hostport: _, _, port = hostport.partition(":") return rebuild._replace(netloc=f"{prefix}{new_host}:{port}").geturl() + markers @@ -1030,9 +1028,7 @@ def _split_userinfo(url): password = unquote(password) # Keep the full path: a geturl() rebuild from urlparse's split # parts drops a bare trailing ";" in the path (#655). - stripped = u._replace( - netloc=hostport, path=urlsplit(url).path, params="" - ).geturl() + stripped = u._replace(netloc=hostport, path=urlsplit(url).path, params="").geturl() if not username and not password: return stripped, None token = base64.b64encode(f"{username}:{password}".encode("utf-8"))