Skip to content

The agent that grows with you

fix(url-safety): compare the IDNA-encoded host against metadata addresses

unsupportedpastelsc5d9f2025e3b
diff --git a/tests/tools/test_url_safety_authority_floor.py b/tests/tools/test_url_safety_authority_floor.py
index 883c9dc3f87..0c2f97c2a25 100644
--- a/tests/tools/test_url_safety_authority_floor.py
+++ b/tests/tools/test_url_safety_authority_floor.py
@@ -54,6 +54,16 @@ TEMPLATED_CODE = [
# Trailing C0 controls (and DEL) that urlsplit keeps in the host and WHATWG clients drop.
TRAILING_CONTROLS = ["\x00", "\x01", "\x08", "\x1b", "\x1f", "\x7f"]
+# The metadata address with a trailing dot form that IDNA maps to ".".
+IDNA_DOT_METADATA = [
+ "http://169.254.169.254。/", # ideographic full stop
+ "http://169.254.169.254./latest/meta-data/", # fullwidth full stop
+ "http://169.254.169.254。/", # halfwidth ideographic full stop
+ "http://169。254。169。254/", # every dot
+ "http://169.254.169.254。/", # fullwidth digits too
+]
+
+
class TestFloorAcceptsTemplatedUrlLiterals:
@pytest.mark.parametrize("code", TEMPLATED_CODE)
def test_local_browser_runs_templated_dev_server_urls(self, local_browser, code):
@@ -106,3 +116,17 @@ class TestTrailingControlCannotHideMetadataHost:
def test_surrounding_whitespace_still_accepted(self, proxy, url):
assert is_always_blocked_url(url) is False
assert is_safe_url(url) is True
+
+
+class TestIdnaDotFormsCannotHideMetadataHost:
+ @pytest.mark.parametrize("url", IDNA_DOT_METADATA)
+ def test_floor_blocks(self, url):
+ assert is_always_blocked_url(url) is True
+
+ @pytest.mark.parametrize("url", IDNA_DOT_METADATA)
+ def test_is_safe_url_refuses_with_proxy_configured(self, proxy, url):
+ assert is_safe_url(url) is False
+
+ def test_public_name_with_ideographic_dots_still_reaches_proxy(self, proxy):
+ assert is_always_blocked_url("https://example。com。/") is False
+ assert is_safe_url("https://example。com。/") is True
diff --git a/tools/url_safety.py b/tools/url_safety.py
index 98f24b985da..6a51b8e8330 100644
--- a/tools/url_safety.py
+++ b/tools/url_safety.py
@@ -233,14 +233,20 @@ def _normalize_hostname(host: Optional[str]) -> str:
# URLs are refused rather than guessed at. WHATWG strips every C0 control from the ends, but urlsplit
# keeps a trailing one in the host, so only ordinary whitespace is stripped before looking for controls.
_URL_EDGE_WHITESPACE = " \t\n\r\x0b\x0c"
+# Dots IDNA maps to "." (ideographic, fullwidth, halfwidth ideographic); used when IDNA encoding fails.
+_IDNA_DOTS = str.maketrans(dict.fromkeys("。.。", "."))
def _url_hostname(url: str, *, percent_decode: bool = False) -> str:
- """The host of *url* as every check compares it: the same edge stripping as the ambiguity check,
- lowercased, trailing dot dropped, optionally percent-decoded first."""
+ """The host of *url* in the form a WHATWG client dials it: IDNA-encoded (so ``169.254.169.254。``
+ is ``169.254.169.254``), lowercased, trailing dot dropped, optionally percent-decoded first."""
host = urlsplit(url.strip(_URL_EDGE_WHITESPACE)).hostname or ""
if percent_decode:
host = unquote(host)
+ try:
+ host = host.encode("idna").decode("ascii")
+ except UnicodeError:
+ host = host.translate(_IDNA_DOTS)
return _normalize_hostname(host)