@devries-cameron20 / composio Mirror · read-only / commits / 026c104fe
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 18:30 UTC

fix(py): validate canonical URL targets (#3962)

This PR: - builds on top of https://github.com/ComposioHQ/composio/pull/3823 - carries forward the Requests canonicalization hardening proposed in https://github.com/ComposioHQ/composio/pull/3810 - resolves and validates the prepared hostname that Requests will actually connect to - blocks backslash parser differentials and rejects malformed ports before DNS resolution - adds focused URL-safety coverage while preserving both existing upload entry points Validation: - `uv run --frozen pytest tests/test_url_safety.py tests/test_files.py tests/test_tool_router_session_files.py -q` (177 passed) - `make chk`

Alberto Schiabel committed Jul 28, 2026 at 14:03 UTC 026c104fe8b31cb1854426f3c68b820f4ea9bf1f
2 files changed +42 -6
python/composio/utils/url_safety.py
+18 -4
@@ -6,6 +6,8 @@ import ipaddress
6 import socket
7 from urllib.parse import urlparse
8
9 +import requests
10 +
11 from composio.exceptions import BlockedInternalUrlError
12
13
@@ -30,10 +32,22 @@ def is_blocked_ip(value: str) -> bool:
32
33
34 def assert_safe_fetch_target(url: str) -> None:
33 - """Refuse non-HTTP(S) URLs and hosts that resolve to internal addresses."""
34 - parsed = urlparse(url)
35 - if parsed.scheme not in {"http", "https"} or not parsed.hostname:
36 - raise BlockedInternalUrlError("Refusing to fetch a non-http(s) URL")
35 + """Refuse non-HTTP(S) URLs and hosts that resolve to internal addresses.
36 +
37 + Parse the URL after Requests prepares it so validation uses the same
38 + canonical hostname that the eventual connection will use.
39 + """
40 + try:
41 + prepared_url = requests.Request(method="GET", url=url).prepare().url
42 + if prepared_url is None:
43 + raise ValueError("Prepared URL is missing")
44 + parsed = urlparse(prepared_url)
45 + if parsed.scheme not in {"http", "https"} or not parsed.hostname:
46 + raise ValueError("URL must use HTTP(S) and include a hostname")
47 + except (requests.exceptions.RequestException, ValueError):
48 + raise BlockedInternalUrlError(
49 + "Refusing to fetch a malformed or non-http(s) URL"
50 + ) from None
51
52 try:
53 addresses: set[str] = set()
python/tests/test_url_safety.py
+24 -2
@@ -47,13 +47,35 @@ def test_allows_public_addresses(address: str) -> None:
47
48
49 @pytest.mark.parametrize(
50 - "url", ["file:///etc/passwd", "ftp://example.com/file", "not a url"]
50 + "url",
51 + [
52 + "file:///etc/passwd",
53 + "ftp://example.com/file",
54 + "not a url",
55 + "http://example.com:invalid/file",
56 + ],
57 )
52 -def test_rejects_non_http_urls(url: str) -> None:
58 +def test_rejects_malformed_or_non_http_urls(url: str) -> None:
59 with pytest.raises(BlockedInternalUrlError):
60 assert_safe_fetch_target(url)
61
62
63 +@patch("composio.utils.url_safety.socket.getaddrinfo")
64 +def test_validates_requests_canonicalized_hostname(mock_getaddrinfo) -> None:
65 + def resolve(host: str, _port: int | None):
66 + address = "127.0.0.1" if host == "127.0.0.1" else "93.184.216.34"
67 + return [
68 + (socket.AF_INET, socket.SOCK_STREAM, 6, "", (address, 0)),
69 + ]
70 +
71 + mock_getaddrinfo.side_effect = resolve
72 +
73 + with pytest.raises(BlockedInternalUrlError):
74 + assert_safe_fetch_target(r"http://127.0.0.1\@example.com/file.pdf")
75 +
76 + mock_getaddrinfo.assert_called_once_with("127.0.0.1", None)
77 +
78 +
79 @patch("composio.utils.url_safety.socket.getaddrinfo")
80 def test_rejects_internal_dns_answers(mock_getaddrinfo) -> None:
81 mock_getaddrinfo.return_value = [