Skip to content

The agent that grows with you

fix(browser): raw CDP metadata check reads the host after userinfo

kshitijk4poor09d2e686f9b8
diff --git a/tests/tools/test_browser_eval_ssrf.py b/tests/tools/test_browser_eval_ssrf.py
index 47bc4a6b10a..0f1f85a55c1 100644
--- a/tests/tools/test_browser_eval_ssrf.py
+++ b/tests/tools/test_browser_eval_ssrf.py
@@ -180,6 +180,29 @@ def test_raw_cdp_metadata_floor_reads_scheme_relative_urls(cdp_guard):
assert cdp_guard(method="Network.loadNetworkResource", params={"frameId": "F", "url": url}) is None, url
[email protected]("local_backend", [False, True])
+def test_raw_cdp_reads_the_host_after_userinfo(cdp_guard, local_backend):
+ """``)``, ``]``, ``<`` and ``>`` are legal in userinfo, so ``http://x)@169.254.169.254/`` goes to the
+ metadata host: a literal cut at one of them inside its authority is also read past the last ``@``."""
+ for userinfo in ("1.1.1.1)", "1.1.1.1]", "1.1.1.1<", "1.1.1.1>", "1.1.1.1)@1.1.1.1]"):
+ for prefix in ("http://", "//"):
+ target = f"{prefix}{userinfo}@169.254.169.254/latest/meta-data/"
+ call = {"functionDeclaration": f"() => fetch('{target}')"}
+ assert "cloud metadata" in cdp_guard(method="Runtime.callFunctionOn", params=call), target
+ html = {"expression": "document.body.innerHTML = '<img src=\"http://1.1.1.1)@169.254.169.254/latest/\">'"}
+ assert "(http://169.254.169.254/latest/)" in cdp_guard(method="Runtime.evaluate", params=html)
+ raw = {"frameId": "F", "url": "http://1.1.1.1]@169.254.169.254/latest/", "options": {}}
+ assert "cloud metadata" in cdp_guard(method="Network.loadNetworkResource", params=raw)
+ loopback = {"functionDeclaration": "() => fetch('http://1.1.1.1)@127.0.0.1:8080/')"}
+ assert (cdp_guard(method="Runtime.callFunctionOn", params=loopback) is None) == local_backend
+ # The literal as cut is still read too: an address after an unquoted href does not hide the href.
+ href = {"functionDeclaration": "() => el.innerHTML = '<a href=http://127.0.0.1:8080>[email protected]</a>'"}
+ assert (cdp_guard(method="Runtime.callFunctionOn", params=href) is None) == local_backend
+ # An @ after the authority ended (path, query) leaves the host alone.
+ path = {"frameId": "F", "url": "https://93.184.216.34/a)@169.254.169.254/", "options": {}}
+ assert cdp_guard(method="Network.loadNetworkResource", params=path) is None
+
+
@pytest.mark.parametrize("local_backend", [False, True])
def test_raw_cdp_scheme_relative_literals_cost_no_dns_lookup(cdp_guard, monkeypatch):
"""``//word`` comments are judged by their literal host: no resolver call per comment."""
diff --git a/tools/browser_url_policy.py b/tools/browser_url_policy.py
index d436547babf..058d013a47b 100644
--- a/tools/browser_url_policy.py
+++ b/tools/browser_url_policy.py
@@ -43,6 +43,11 @@ _JS_URL_LITERAL_RE = re.compile(r"https?://" + _URL_BODY, re.IGNORECASE)
# query (``https://x/?next=//y``) is part of that literal, not a second target.
_URL_OR_SCHEME_RELATIVE_RE = re.compile(r"""(?:https?://|(?<![\w:/\\])[/\\]{2,}(?=[^/\\\s]))""" + _URL_BODY,
re.IGNORECASE)
+_URL_BODY_RE = re.compile(_URL_BODY)
+# ``)``, ``]``, ``<`` and ``>`` are legal in userinfo, so a literal cut at one inside its authority may be
+# ``http://x)@host``: the rest of the authority (up to ``/ ? # \``) through its last ``@``.
+_USERINFO_TAIL_RE = re.compile(r"""[^\s'"`/?#\\]*@""")
+_LITERAL_HEAD_RE = re.compile(r"(?:https?:)?[/\\]+", re.IGNORECASE)
_NUMERIC_HOST_RE = re.compile(r"[0-9a-fx.]+") # _url_hostname lowercases
@@ -152,10 +157,21 @@ def url_blocked(url: str) -> bool:
def url_literals(text: str, *, protocol_relative: bool = False) -> list[str]:
"""Every ``http(s)://`` literal in free text (a JS expression, an HTML snippet, a CDP param); with
- ``protocol_relative``, each scheme-relative ``//host/...`` literal too, kept scheme-less."""
+ ``protocol_relative``, each scheme-relative ``//host/...`` literal too, kept scheme-less. A literal cut
+ inside its authority with an ``@`` ahead is also returned as the host a browser reads after that ``@``
+ (``http://x)@host/`` -> ``http://host/``), next to the literal as cut."""
if not isinstance(text, str):
return []
- found = (_URL_OR_SCHEME_RELATIVE_RE if protocol_relative else _JS_URL_LITERAL_RE).findall(text)
+ found = []
+ for m in (_URL_OR_SCHEME_RELATIVE_RE if protocol_relative else _JS_URL_LITERAL_RE).finditer(text):
+ found.append(m.group())
+ head = _LITERAL_HEAD_RE.match(m.group()).group()
+ if any(c in "/?#\\" for c in m.group()[len(head):]):
+ continue # the authority ended inside the literal: its host is fixed
+ tail = _USERINFO_TAIL_RE.match(text, m.end())
+ host = tail and _URL_BODY_RE.match(text, tail.end())
+ if host:
+ found.append(head + host.group())
return [m.rstrip(".,;") for m in found]