Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
fix(py): validate canonical URL targets (#3962)
Alberto Schiabel026c104fe8b3
diff --git a/python/composio/utils/url_safety.py b/python/composio/utils/url_safety.pyindex 0d68476d5..cbceb96d8 100644--- a/python/composio/utils/url_safety.py+++ b/python/composio/utils/url_safety.py@@ -6,6 +6,8 @@ import ipaddressimport socketfrom urllib.parse import urlparse+import requests+from composio.exceptions import BlockedInternalUrlError@@ -30,10 +32,22 @@ def is_blocked_ip(value: str) -> bool:def assert_safe_fetch_target(url: str) -> None:- """Refuse non-HTTP(S) URLs and hosts that resolve to internal addresses."""- parsed = urlparse(url)- if parsed.scheme not in {"http", "https"} or not parsed.hostname:- raise BlockedInternalUrlError("Refusing to fetch a non-http(s) URL")+ """Refuse non-HTTP(S) URLs and hosts that resolve to internal addresses.++ Parse the URL after Requests prepares it so validation uses the same+ canonical hostname that the eventual connection will use.+ """+ try:+ prepared_url = requests.Request(method="GET", url=url).prepare().url+ if prepared_url is None:+ raise ValueError("Prepared URL is missing")+ parsed = urlparse(prepared_url)+ if parsed.scheme not in {"http", "https"} or not parsed.hostname:+ raise ValueError("URL must use HTTP(S) and include a hostname")+ except (requests.exceptions.RequestException, ValueError):+ raise BlockedInternalUrlError(+ "Refusing to fetch a malformed or non-http(s) URL"+ ) from Nonetry:addresses: set[str] = set()diff --git a/python/tests/test_url_safety.py b/python/tests/test_url_safety.pyindex 17eef45d3..90fc88494 100644--- a/python/tests/test_url_safety.py+++ b/python/tests/test_url_safety.py@@ -47,13 +47,35 @@ def test_allows_public_addresses(address: str) -> None:@pytest.mark.parametrize(- "url", ["file:///etc/passwd", "ftp://example.com/file", "not a url"]+ "url",+ [+ "file:///etc/passwd",+ "ftp://example.com/file",+ "not a url",+ "http://example.com:invalid/file",+ ],)-def test_rejects_non_http_urls(url: str) -> None:+def test_rejects_malformed_or_non_http_urls(url: str) -> None:with pytest.raises(BlockedInternalUrlError):assert_safe_fetch_target(url)+@patch("composio.utils.url_safety.socket.getaddrinfo")+def test_validates_requests_canonicalized_hostname(mock_getaddrinfo) -> None:+ def resolve(host: str, _port: int | None):+ address = "127.0.0.1" if host == "127.0.0.1" else "93.184.216.34"+ return [+ (socket.AF_INET, socket.SOCK_STREAM, 6, "", (address, 0)),+ ]++ mock_getaddrinfo.side_effect = resolve++ with pytest.raises(BlockedInternalUrlError):+ assert_safe_fetch_target(r"http://127.0.0.1\@example.com/file.pdf")++ mock_getaddrinfo.assert_called_once_with("127.0.0.1", None)++@patch("composio.utils.url_safety.socket.getaddrinfo")def test_rejects_internal_dns_answers(mock_getaddrinfo) -> None:mock_getaddrinfo.return_value = [