@devries-cameron20 / composio-1 / commits / c4071feab
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.

fix(py): guard URL uploads and redact telemetry (#3823)

This PR: - ports the TypeScript SDK's public-network guard to Python URL file uploads - rejects non-HTTP(S), private, loopback, link-local, reserved, and mixed DNS targets before a request is made - applies the guard to both `FileUploadable.from_url()` and Tool Router session-file URL uploads - redacts URL queries, Authorization credentials, and secret-like key/value pairs before error telemetry leaves the process - adds focused SSRF and redaction regressions alongside the existing file-upload coverage Validation: - `uv run --frozen pytest tests/test_url_safety.py tests/test_redaction.py tests/test_files.py -q` - `uv run --frozen ruff check …` - `uv run --frozen ruff format --check …`

Alberto Schiabel committed Jul 14, 2026 at 14:32 UTC c4071feab4d781e1f0981d7edccedf0b55b81b3d
9 files changed +183 -4
python/composio/core/models/_files.py
+3
@@ -25,6 +25,7 @@ from composio.exceptions import (
25 )
26 from composio.utils import mimetypes
27 from composio.utils.json_schema import dereference_json_schema
28 +from composio.utils.url_safety import assert_safe_fetch_target
29 from composio.utils.sensitive_file_upload_paths import (
30 assert_safe_local_file_upload_path,
31 )
@@ -281,6 +282,8 @@ def _fetch_file_from_url(
282 ResponseTooLargeError: If response exceeds max_size
283 ErrorUploadingFile: If fetch fails for other reasons
284 """
285 + assert_safe_fetch_target(url)
286 +
287 # Make request without following redirects
288 try:
289 response = requests.get(
python/composio/core/models/base.py
+3 -2
@@ -11,6 +11,7 @@ import typing as t
11
12 from composio.__version__ import __version__
13 from composio.client import HttpClient
14 +from composio.utils.redaction import redact_sensitive_text
15 from composio.utils.logging import WithLogger
16
17 from ._telemetry import Event, create_event, push_event
@@ -56,8 +57,8 @@ def trace_method(method: t.Callable, name: str) -> t.Callable:
57 _, payload = event
58 payload["error"] = {
59 "name": e.__class__.__name__,
59 - "message": str(e),
60 - "stack": traceback.format_exc(),
60 + "message": redact_sensitive_text(str(e)),
61 + "stack": redact_sensitive_text(traceback.format_exc()),
62 }
63 event = ("error", payload)
64 raise e
python/composio/core/models/tool_router_session_files.py
+2
@@ -24,6 +24,7 @@ from composio_client.types.tool_router.session.file_list_response import (
24 from composio.client import HttpClient
25 from composio.exceptions import RemoteFileDownloadError, ValidationError
26 from composio.utils.mimetypes import get_extension_from_mime_type
27 +from composio.utils.url_safety import assert_safe_fetch_target
28 from composio.utils.uuid import generate_short_id
29
30 DEFAULT_TOOL_ROUTER_SESSION_FILES_MOUNT_ID = "files"
@@ -46,6 +47,7 @@ def _is_url(value: str) -> bool:
47
48 def _fetch_from_url(url: str) -> t.Tuple[bytes, str]:
49 """Fetch file content from URL. Returns (content, mimetype)."""
50 + assert_safe_fetch_target(url)
51 try:
52 response = requests.get(
53 url,
python/composio/exceptions.py
+4
@@ -187,6 +187,10 @@ class ErrorUploadingFile(FileError):
187 pass
188
189
190 +class BlockedInternalUrlError(FileError):
191 + """Raised when a URL file input resolves to a non-public network address."""
192 +
193 +
194 class SensitiveFilePathBlockedError(FileError):
195 """Raised when a local file path is refused before upload (sensitive directory or credential-like name)."""
196
python/composio/utils/redaction.py new
+20
@@ -0,0 +1,20 @@
1 +"""Best-effort secret redaction for free-form telemetry text."""
2 +
3 +from __future__ import annotations
4 +
5 +import re
6 +
7 +_REDACTED = "[REDACTED]"
8 +_URL_QUERY = re.compile(r"(\bhttps?://[^\s?#'\"]+)\?[^\s'\"]*", re.IGNORECASE)
9 +_AUTHORIZATION = re.compile(r"\b(bearer|basic)\s+[A-Za-z0-9._~+/=-]+", re.IGNORECASE)
10 +_SECRET_PAIR = re.compile(
11 + r"\b(authorization|api[-_]?key|apikey|x-api-key|access[-_]?token|refresh[-_]?token|client[-_]?secret|secret|password|passwd|pwd)\b(\s*[:=]+\s*)([\"']?)([^\s\"',}&]+)\3",
12 + re.IGNORECASE,
13 +)
14 +
15 +
16 +def redact_sensitive_text(value: str) -> str:
17 + """Remove common URL, authorization, and key-value secret shapes."""
18 + value = _URL_QUERY.sub(rf"\1?{_REDACTED}", value)
19 + value = _AUTHORIZATION.sub(rf"\1 {_REDACTED}", value)
20 + return _SECRET_PAIR.sub(rf"\1\2\3{_REDACTED}\3", value)
python/composio/utils/url_safety.py new
+56
@@ -0,0 +1,56 @@
1 +"""SSRF protections for user-supplied URL file inputs."""
2 +
3 +from __future__ import annotations
4 +
5 +import ipaddress
6 +import socket
7 +from urllib.parse import urlparse
8 +
9 +from composio.exceptions import BlockedInternalUrlError
10 +
11 +
12 +def is_blocked_ip(value: str) -> bool:
13 + """Return whether an address is non-publicly-routable."""
14 + try:
15 + address = ipaddress.ip_address(value)
16 + except ValueError:
17 + return True
18 +
19 + if isinstance(address, ipaddress.IPv6Address):
20 + embedded_ipv4 = address.ipv4_mapped
21 + if embedded_ipv4 is None and address.packed[:12] in {
22 + b"\x00" * 12,
23 + b"\x00d\xff\x9b" + b"\x00" * 8,
24 + }:
25 + embedded_ipv4 = ipaddress.IPv4Address(address.packed[-4:])
26 + if embedded_ipv4 is not None:
27 + return is_blocked_ip(str(embedded_ipv4))
28 +
29 + return not address.is_global
30 +
31 +
32 +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")
37 +
38 + try:
39 + addresses: set[str] = set()
40 + for result in socket.getaddrinfo(parsed.hostname, None):
41 + address = result[4][0]
42 + if not isinstance(address, str):
43 + raise BlockedInternalUrlError(
44 + f'Could not resolve host "{parsed.hostname}"'
45 + )
46 + addresses.add(address)
47 + except socket.gaierror as error:
48 + raise BlockedInternalUrlError(
49 + f'Could not resolve host "{parsed.hostname}"'
50 + ) from error
51 +
52 + for address in addresses:
53 + if is_blocked_ip(address):
54 + raise BlockedInternalUrlError(
55 + f'Refusing to fetch "{parsed.hostname}" because it resolves to a non-public address'
56 + )
python/tests/test_files.py
+6 -2
@@ -2204,8 +2204,9 @@ class TestTruncateFilename:
2204 class TestFetchFileFromUrlWithTruncation:
2205 """Test cases for _fetch_file_from_url with filename truncation."""
2206
2207 + @patch("composio.core.models._files.assert_safe_fetch_target")
2208 @patch("composio.core.models._files.requests.get")
2208 - def test_fetch_truncates_long_filename(self, mock_get):
2209 + def test_fetch_truncates_long_filename(self, mock_get, _mock_safe_fetch_target):
2210 """Long filenames from URLs should be truncated."""
2211 mock_response = MagicMock()
2212 mock_response.ok = True
@@ -2244,8 +2245,11 @@ class TestFetchFileFromUrlWithTruncation:
2245
2246 assert filename == "photo.jpg"
2247
2248 + @patch("composio.core.models._files.assert_safe_fetch_target")
2249 @patch("composio.core.models._files.requests.get")
2248 - def test_fetch_truncates_after_adding_extension(self, mock_get):
2250 + def test_fetch_truncates_after_adding_extension(
2251 + self, mock_get, _mock_safe_fetch_target
2252 + ):
2253 """Truncation should happen after extension is appended."""
2254 mock_response = MagicMock()
2255 mock_response.ok = True
python/tests/test_redaction.py new
+15
@@ -0,0 +1,15 @@
1 +"""Tests for telemetry secret redaction."""
2 +
3 +from composio.utils.redaction import redact_sensitive_text
4 +
5 +
6 +def test_redacts_url_queries_authorization_and_secret_pairs() -> None:
7 + redacted = redact_sensitive_text(
8 + "https://example.com/file?signature=secret Bearer token123 api_key=abc password: hunter2"
9 + )
10 +
11 + assert "signature=secret" not in redacted
12 + assert "token123" not in redacted
13 + assert "api_key=abc" not in redacted
14 + assert "password: hunter2" not in redacted
15 + assert redacted.count("[REDACTED]") == 4
python/tests/test_url_safety.py new
+74
@@ -0,0 +1,74 @@
1 +"""Regression tests for URL file-upload SSRF protections."""
2 +
3 +from __future__ import annotations
4 +
5 +import socket
6 +from unittest.mock import patch
7 +
8 +import pytest
9 +
10 +from composio.exceptions import BlockedInternalUrlError
11 +from composio.utils.url_safety import assert_safe_fetch_target, is_blocked_ip
12 +
13 +
14 +@pytest.mark.parametrize(
15 + "address",
16 + [
17 + "127.0.0.1",
18 + "10.0.0.5",
19 + "169.254.169.254",
20 + "100.64.0.1",
21 + "::1",
22 + "fc00::1",
23 + "::ffff:127.0.0.1",
24 + "::127.0.0.1",
25 + "::7f00:1",
26 + "::169.254.169.254",
27 + "64:ff9b::7f00:1",
28 + "64:ff9b::a9fe:a9fe",
29 + ],
30 +)
31 +def test_blocks_non_public_addresses(address: str) -> None:
32 + assert is_blocked_ip(address) is True
33 +
34 +
35 +@pytest.mark.parametrize(
36 + "address",
37 + [
38 + "8.8.8.8",
39 + "93.184.216.34",
40 + "2606:4700:4700::1111",
41 + "::8.8.8.8",
42 + "64:ff9b::8.8.8.8",
43 + ],
44 +)
45 +def test_allows_public_addresses(address: str) -> None:
46 + assert is_blocked_ip(address) is False
47 +
48 +
49 +@pytest.mark.parametrize(
50 + "url", ["file:///etc/passwd", "ftp://example.com/file", "not a url"]
51 +)
52 +def test_rejects_non_http_urls(url: str) -> None:
53 + with pytest.raises(BlockedInternalUrlError):
54 + assert_safe_fetch_target(url)
55 +
56 +
57 +@patch("composio.utils.url_safety.socket.getaddrinfo")
58 +def test_rejects_internal_dns_answers(mock_getaddrinfo) -> None:
59 + mock_getaddrinfo.return_value = [
60 + (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 0)),
61 + (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("127.0.0.1", 0)),
62 + ]
63 +
64 + with pytest.raises(BlockedInternalUrlError):
65 + assert_safe_fetch_target("https://example.com/file.pdf")
66 +
67 +
68 +@patch("composio.utils.url_safety.socket.getaddrinfo")
69 +def test_allows_public_dns_answers(mock_getaddrinfo) -> None:
70 + mock_getaddrinfo.return_value = [
71 + (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 0)),
72 + ]
73 +
74 + assert_safe_fetch_target("https://example.com/file.pdf")