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
24 changes: 24 additions & 0 deletions pkgs/lab-ops-hub/container/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,30 @@ def validate_crawl_url(raw):
host = unicodedata.normalize("NFKC", host).lower()
host = host.translate({0x3002: ".", 0xFF0E: ".", 0xFF61: "."})
host = host.translate({cp: None for cp in _UTS46_IGNORED})
# NFKC mapping can itself CREATE a host delimiter the earlier
# checks never saw: _percent_decode_host refuses delimiters in
# the decoded form, but a percent-encoded compatibility character
# decodes to an innocent-looking codepoint whose NFKC mapping is
# a delimiter — U+FF0F -> /, U+FF1A -> :, U+FF20 -> @, U+FF03 ->
# #, U+FF1F -> ?, U+FF3C -> \, U+FF3B/U+FF3D -> [ ], U+FF05 -> %,
# and expansions like U+2100 -> "a/c" and U+2105 -> "c/o" (#567).
# Raw spellings cannot smuggle these past urlparse (its NFKC
# netloc guard raises), but the percent-encoded spelling is pure
# ASCII in the raw netloc, so that guard never fires — and the
# spliced returned URL would make the fetcher connect to a host
# that was never judged ("localhost%ef%bc%8fexample.com" came
# back as "localhost/example.com"). Re-check the structural
# characters on the fully mapped form: refuse any mapped
# delimiter, including a mapped % (the decode step guarantees no
# % survives, and mapping must not reintroduce one). A colon is
# only legitimate inside a bracketed IPv6 literal; an unbracketed
# mapped colon is a delimiter, not an address.
if any(c in "%#/?@[\\]" for c in host) or (
":" in host and not u.netloc.rsplit("@", 1)[-1].startswith("[")
):
raise ValueError(
f"refusing to crawl host with a forbidden character once mapped {host!r}"
)
# A single trailing dot is the DNS root dot (the FQDN spelling of the
# same host: "localhost." is "localhost", "127.0.0.1." is 127.0.0.1);
# strip it before the checks so the dotted spelling cannot evade
Expand Down
33 changes: 33 additions & 0 deletions pkgs/lab-ops-hub/test/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,39 @@ test("validateCrawlUrl judges percent-encoded hosts in their decoded form (#529)
}
});

test("validateCrawlUrl refuses percent-encoded hosts whose NFKC mapping is a delimiter (#567)", () => {
// Parity battery for the container fix: a percent-encoded
// compatibility character decodes to a codepoint whose NFKC
// mapping is a host delimiter (or expands to one). The Worker's
// WHATWG parser refuses every spelling outright; the container
// originally spliced the mapped delimiter into the returned URL
// and fetched an unjudged host (localhost / private addresses).
const blocked = [
"http://localhost%ef%bc%8fexample.com/", // U+FF0F -> /
"http://127.0.0.1%ef%bc%8fexample.com/", // U+FF0F -> /
"http://192.168.0.1%ef%bc%8fadmin/", // U+FF0F -> /
"http://exam%ef%bc%8fple.com/", // U+FF0F -> /
"http://exam%ef%bc%9aple.com/", // U+FF1A -> :
"http://exam%ef%bc%a0ple.com/", // U+FF20 -> @
"http://exam%ef%bc%83ple.com/", // U+FF03 -> #
"http://exam%ef%bc%9fple.com/", // U+FF1F -> ?
"http://exam%ef%bc%bcple.com/", // U+FF3C -> backslash
"http://exam%ef%bc%bbple.com/", // U+FF3B -> [
"http://exam%ef%bc%bdple.com/", // U+FF3D -> ]
"http://example%ef%bc%85com/", // U+FF05 -> %
"http://%e2%84%80.com/", // U+2100 expands to a/c
"http://%e2%84%85.com/", // U+2105 expands to c/o
];
for (const u of blocked) {
assert.equal(validateCrawlUrl(u).ok, false, u);
}
// Control: U+FF5E maps to ~, not a delimiter — still accepted,
// and the returned URL re-parses to the judged host.
const tilde = validateCrawlUrl("http://exam%ef%bd%9eple.com/");
assert.equal(tilde.ok, true);
if (tilde.ok) assert.equal(tilde.url, "http://exam~ple.com/");
});

test("fetchFollowingValidatedRedirects follows a percent-encoded public redirect target (#529)", async () => {
const { fetchFn, seen } = stubFetcher({
"https://example.com/start": { status: 302, location: "http://%65xample.org/final" },
Expand Down
59 changes: 59 additions & 0 deletions pkgs/lab-ops-hub/test/test_container_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,65 @@ def test_refuses_percent_decoded_blocked_and_invalid_hosts(self):
):
self.assertRefused(url)

def test_refuses_percent_encoded_nfkc_mapped_delimiters(self):
# The decode step (#529) refuses delimiters in the decoded
# form, but a percent-encoded compatibility character decodes
# to a codepoint whose NFKC mapping IS a delimiter, and the
# mapped form is what gets spliced into the returned URL:
# "localhost%ef%bc%8fexample.com" came back ALLOW as
# "http://localhost/example.com/" — the fetcher connected to
# localhost, a host that was never judged (#567). The Worker
# refuses every one of these outright.
for url in (
"http://localhost%ef%bc%8fexample.com/", # U+FF0F -> /
"http://127.0.0.1%ef%bc%8fexample.com/", # U+FF0F -> /
"http://192.168.0.1%ef%bc%8fadmin/", # U+FF0F -> /
"http://exam%ef%bc%8fple.com/", # U+FF0F -> /
"http://exam%ef%bc%9aple.com/", # U+FF1A -> :
"http://exam%ef%bc%a0ple.com/", # U+FF20 -> @
"http://exam%ef%bc%83ple.com/", # U+FF03 -> #
"http://exam%ef%bc%9fple.com/", # U+FF1F -> ?
"http://exam%ef%bc%bcple.com/", # U+FF3C -> backslash
"http://exam%ef%bc%bbple.com/", # U+FF3B -> [
"http://exam%ef%bc%bdple.com/", # U+FF3D -> ]
"http://example%ef%bc%85com/", # U+FF05 -> %
"http://%e2%84%80.com/", # U+2100 expands to a/c
"http://%e2%84%85.com/", # U+2105 expands to c/o
):
self.assertRefused(url)

def test_accepted_percent_urls_reparse_to_the_judged_host(self):
# For every accepted percent-encoded URL, re-parsing the
# returned URL must yield exactly the host that was judged —
# no delimiter may be spliced in by decoding or mapping
# (#567 acceptance criterion).
from urllib.parse import urlparse

for url, expected_host in (
("http://%65xample.com/", "example.com"),
("http://example%2ecom/", "example.com"),
("http://8%2e8%2e8%2e8/", "8.8.8.8"),
("http://%65xample.com:8080/page", "example.com"),
# U+FF5E maps to ~, not a delimiter: the mapped form is
# judged and returned, and re-parses to itself.
("http://exam%ef%bd%9eple.com/", "exam~ple.com"),
):
returned = validate_crawl_url(url)
self.assertEqual(urlparse(returned).hostname, expected_host, url)

def test_redirect_handler_refuses_nfkc_mapped_delimiter_target(self):
# Redirect targets are validated raw (#529/#567): a Location
# whose percent-encoded host NFKC-maps into a private host
# plus delimiter must be refused, not followed.
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://192.168.0.1%ef%bc%8fadmin/"
)

def test_redirect_handler_follows_the_decoded_target(self):
# Redirect targets are validated raw, with no Worker
# normalisation in between (#529): the handler must fetch the
Expand Down
Loading