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
12 changes: 12 additions & 0 deletions pkgs/lab-ops-hub/container/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Normalize redirect Locations before urllib resolves them

When an actual response contains Location: http:///127.0.0.1/admin, HTTPRedirectHandler.http_error_302 calls urljoin(req.full_url, newurl) before _ValidatingRedirectHandler.redirect_request, so this line receives http://example.com/127.0.0.1/admin and never sees or rejects the WHATWG loopback target. The added test invokes redirect_request directly and therefore misses the real flow; intercept and validate the raw Location before stdlib resolution, otherwise slash-run redirects continue to be followed on the wrong host.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Strip ignored controls before collapsing the slash run

For inputs containing WHATWG-ignored tab, CR, or LF characters in the delimiter run, such as http:/\n/example.com/x, this substitution runs before urlparse strips the control and produces http://\n/example.com/x, which then parses as hostless. The parent validator accepted this input and new URL(...) normalizes it to http://example.com/x, so direct callers can now reject URLs the Worker accepts; remove those parser-ignored characters before determining the slash run.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve backslashes that occur after the query delimiter

When this line newly admits a spelling such as http:example.com/?q=\value, the earlier global backslash replacement returns http://example.com/?q=/value, whereas Bun/WHATWG returns http://example.com/?q=\value. Backslashes are special delimiters while parsing the authority and path but are data in the query, so a direct container caller now fetches a different resource than the Worker for these newly accepted slash-run URLs; limit normalization to the pre-query URL states.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject WHATWG-invalid IDNs before admitting slash-run URLs

For a direct input such as http:x͸x.com/ containing unassigned U+0378, this new normalization accepts and returns http://x͸x.com/, while Bun's new URL and the Worker validator reject the host outright. Python's built-in IDNA codec can encode it as xn--xx-g4b.com, so the container may fetch a DNS target that was never valid under the Worker's WHATWG policy; validate newly admitted hosts with compatible UTS-46 rules before returning them.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Serialize newly accepted path characters before fetching

For a direct input such as http:example.com/café, this line now returns http://example.com/café, whereas the Worker serializes it as http://example.com/caf%C3%A9; urllib subsequently raises UnicodeEncodeError instead of crawling it. Raw spaces and C0 controls similarly pass validation and then raise InvalidURL, so these newly accepted slash-run URLs produce a container failure despite being valid to the Worker; apply WHATWG-compatible percent-encoding before returning the URL.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Normalize dot segments in newly accepted paths

When a direct caller supplies http:example.com/a/../b, this line newly accepts it as http://example.com/a/../b and urllib sends that path verbatim, while the Worker serializes the same URL as http://example.com/b. Encoded dot forms such as %2e%2e also remain unnormalized here, and servers or proxies do not handle these spellings uniformly, so the container can crawl a different resource than the Worker; apply WHATWG dot-segment normalization to the returned URL.

Useful? React with 👍 / 👎.

u = urlparse(raw)
if u.scheme not in ("http", "https"):
raise ValueError("only http: and https: URLs may be crawled")
Expand Down
46 changes: 46 additions & 0 deletions pkgs/lab-ops-hub/test/test_container_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down
Loading