Skip to content

The agent that grows with you

fix(url-safety): metadata floor accepts templated URL literals

unsupportedpastelsad55e8a66ba9
diff --git a/tests/tools/test_url_safety_authority_floor.py b/tests/tools/test_url_safety_authority_floor.py
new file mode 100644
index 00000000000..ae1912e057b
--- /dev/null
+++ b/tests/tools/test_url_safety_authority_floor.py
@@ -0,0 +1,86 @@
+"""What the metadata floor refuses, and host forms WHATWG clients normalise away.
+
+``is_always_blocked_url`` guards local browser backends that skip ``is_safe_url``, so it refuses
+only what can hide a different host. A templated port (``{port}``, ``%d``, ``${port}``) leaves the
+host as written. Chromium and Node drop a trailing C0 control and map IDNA dots (``。``, ``.``,
+``。``) to ``.``, so both checks must judge the host those clients dial, even when DNS fails.
+"""
+
+import socket
+from unittest.mock import patch
+
+import pytest
+
+from tools import browser_session_cloud, browser_url_policy, browser_use_cli
+from tools.url_safety import is_always_blocked_url, is_safe_url
+
+_LOCALHOST = [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("127.0.0.1", 0))]
+
+
+def _localhost_only(host, *args, **kwargs):
+ """``localhost`` resolves to loopback; every other name fails like an unresolvable name."""
+ if host == "localhost":
+ return _LOCALHOST
+ raise socket.gaierror(socket.EAI_NONAME, "Name or service not known")
+
+
[email protected](autouse=True)
+def _dns():
+ with patch("socket.getaddrinfo", side_effect=_localhost_only):
+ yield
+
+
+def local_browser(monkeypatch):
+ """A local Chromium backend: ``is_safe_url`` is skipped, only the metadata floor applies."""
+ monkeypatch.setattr(browser_session_cloud, "is_local_backend", lambda: True)
+ monkeypatch.setattr(browser_session_cloud, "allow_private_urls", lambda: False)
+ monkeypatch.setattr(browser_url_policy, "hybrid_routes_locally", lambda url: False)
+ monkeypatch.setattr(browser_url_policy, "check_website_access", lambda url: None)
+
+
+def proxy(monkeypatch):
+ """A configured proxy: ``is_safe_url`` hands a DNS failure on a name to the proxy."""
+ monkeypatch.setenv("HTTPS_PROXY", "http://proxy.invalid:3128")
+
+
+TEMPLATED_CODE = [
+ 'port = 8000\nnew_tab(f"http://localhost:{port}/")',
+ 'port = 8000\nnew_tab("http://localhost:%d/" % port)',
+ 'js("fetch(`http://localhost:${port}/api`)")',
+]
+
+class TestFloorAcceptsTemplatedUrlLiterals:
+ @pytest.mark.parametrize("code", TEMPLATED_CODE)
+ def test_local_browser_runs_templated_dev_server_urls(self, local_browser, code):
+ assert browser_use_cli._code_url_verdict(code).blocked is None
+
+ @pytest.mark.parametrize("url", [
+ "http://localhost:{port}/", "http://localhost:%d/", "http://localhost:${port}/api",
+ "http://{host}:8080/", "http://%s/", "http://user:pass@localhost:{port}/",
+ ])
+ def test_floor_ignores_unparseable_port_and_host_placeholders(self, url):
+ assert is_always_blocked_url(url) is False
+
+ @pytest.mark.parametrize("url", ["http://localhost:{port}/", "http://localhost:%d/"])
+ def test_is_safe_url_keeps_refusing_them(self, url):
+ assert is_safe_url(url) is False
+
+
+class TestFloorJudgesPercentEncodedHostDecoded:
+ @pytest.mark.parametrize("url", [
+ "http://%31%36%39.254.169.254/",
+ "HTTP://%31%36%39.254.169.254:80/latest",
+ "http://metadata.google.%69nternal/computeMetadata/v1/",
+ ])
+ def test_decoded_metadata_host_blocked(self, url):
+ assert is_always_blocked_url(url) is True
+
+ def test_decoded_metadata_host_blocked_through_browser_scan(self, local_browser):
+ verdict = browser_use_cli._code_url_verdict('new_tab("http://%31%36%39.254.169.254/")')
+ assert verdict.blocked is not None and "cloud metadata" in verdict.blocked
+
+ @pytest.mark.parametrize("url", ["http://%6c%6f%63%61%6c%68%6f%73%74/", "http://exa%20mple.com/"])
+ def test_decoded_ordinary_host_not_blocked(self, url):
+ assert is_always_blocked_url(url) is False
diff --git a/tools/url_safety.py b/tools/url_safety.py
index 3fabfed6654..8a7ef15d974 100644
--- a/tools/url_safety.py
+++ b/tools/url_safety.py
@@ -234,10 +234,13 @@ def _normalize_hostname(host: Optional[str]) -> str:
_C0_CONTROL_OR_SPACE = "".join(chr(c) for c in range(0x21))
-def _authority_ambiguity(url: str, *, allow_userinfo: bool = False) -> Optional[str]:
+def _authority_ambiguity(url: str, *, floor: bool = False) -> Optional[str]:
"""Why another client could read a different host or port from an http(s) *url* than urlsplit
does, else None. ``\\`` and ``@`` after the authority (path, query, fragment) are accepted: every
- parser has fixed the host by then, and WHATWG's ``\\`` -> ``/`` rewrite only changes the path."""
+ parser has fixed the host by then, and WHATWG's ``\\`` -> ``/`` rewrite only changes the path.
+ ``floor`` keeps only what can move the host itself (controls, no ``//``, ``\\`` in the authority):
+ userinfo and an unparseable port (templated ``{port}``/``%d`` literals) leave the host as is, and
+ the floor checks a percent-encoded host after decoding it."""
url = url.strip(_C0_CONTROL_OR_SPACE)
parts = urlsplit(url)
if parts.scheme.lower() not in _HTTP_SCHEMES:
@@ -248,8 +251,10 @@ def _authority_ambiguity(url: str, *, allow_userinfo: bool = False) -> Optional[
return "authority not introduced by //"
if "\\" in parts.netloc:
return "backslash in authority"
+ if floor:
+ return None
_, at, host = parts.netloc.rpartition("@")
- if at and not allow_userinfo:
+ if at:
return "userinfo in authority"
if "%" in host:
return "percent-encoding in host"
@@ -324,13 +329,14 @@ def is_always_blocked_url(url: str) -> bool:
deliberately bypass the full check (e.g. hybrid cloud browser routing private URLs to a local
sidecar) but must still enforce the floor. False for ordinary private/loopback URLs, DNS
failures and parse errors (the caller's ordinary fail-closed path handles those); True for an
- http(s) URL whose host another client could read differently, which may hide a metadata target."""
+ http(s) URL whose host another client could read differently, which may hide a metadata target.
+ A percent-encoded host is judged decoded, as WHATWG clients read it."""
try:
- reason = _authority_ambiguity(url, allow_userinfo=True)
+ reason = _authority_ambiguity(url, floor=True)
if reason is not None:
logger.warning("Blocked request — ambiguous URL authority (always-blocked floor): %s", reason)
return True
- hostname = _normalize_hostname(urlparse(url).hostname)
+ hostname = _normalize_hostname(unquote(urlparse(url).hostname or ""))
if not hostname:
return False
if hostname in _BLOCKED_HOSTNAMES: