Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions pkgs/lab-ops-hub/container/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -732,13 +732,21 @@ 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")
Expand Down Expand Up @@ -813,10 +821,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:
Expand Down Expand Up @@ -846,7 +855,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
Expand Down Expand Up @@ -936,10 +951,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)
Expand Down Expand Up @@ -1007,7 +1026,9 @@ 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"))
Expand Down
40 changes: 40 additions & 0 deletions pkgs/lab-ops-hub/test/test_container_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading