@devries-cameron20 / composio-1 / commits / 6ba9179b4
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(files): validate URLs from API responses before fetching them (#4146)

This PR: - builds on top of https://github.com/ComposioHQ/composio/pull/4144 - routes every fetch whose URL comes from an API response through the SSRF guards that already existed — ten sinks, five per SDK: the tool-execution download, both S3 presigned uploads, `RemoteFile.buffer()`/`blob()`, and the Tool Router session file upload - adds `safe_request()` (Python), which follows redirects itself and re-validates each hop, so a validated URL cannot 302 into private space and an S3 307 region redirect still works - makes `RemoteFile.buffer()` (Python) share `_fetch_url_bytes` with the user-supplied-URL path instead of duplicating it — it previously read `response.content` with no size cap, no redirect control, and no target validation - adds `ssrfSafeFetchWhereSupported` (TypeScript): the full guard on Node, a plain `fetch` on workerd, so Tool Router session file transfers keep working in edge runtimes rather than failing closed - documents DNS rebinding as a known residual in both guards - adds tests that assert the guard *runs* — blocked URL, sink never called, nothing written — rather than that a transfer succeeds ## Context `python/AGENTS.md` states the trust boundary: every field of an API response is untrusted input, because the backend may be compromised or the connection MITM'd. Both SDKs enforced that for URLs a *user* passes in (`composio.utils.url_safety`, `ssrfGuard.node.ts`) and left the URLs an API *response* supplies unguarded — backwards relative to the stated model. A response naming an internal address turned the SDK into a request proxy for it, and the fetched bytes were written to disk or returned to the caller, typically into an LLM context. `RemoteFile.buffer()` was the weakest of the ten: four lines above it, `_fetch_from_url` validated its target, refused redirects, and streamed against a 100 MiB cap; `buffer()` did none of the three. The only difference between them was which side of the trust boundary the URL arrived from. Three decisions worth review: - **The guard is unconditional.** Presigned URLs are public, so nothing legitimate should resolve to private space. There is no escape hatch, and no new configuration surface. - **The tool-execution download stays uncapped.** It streams straight to disk, so `_MAX_RESPONSE_SIZE` — a memory-exhaustion bound — does not apply, and tool attachments legitimately exceed it. `RemoteFile.buffer()` *is* capped, because it buffers in memory. - **Uploads follow redirects with re-validation rather than refusing them**, since S3 can answer a PUT with a 307 region redirect. Downloads keep `allow_redirects=False`, matching the existing fetch paths. Python now matches the TypeScript guard's per-hop re-validation, which was the better of the two implementations. Verified locally: `make chk` and `make tst` clean on the Python side; `pnpm -C packages/core test` 1095 passed, typecheck and lint clean.

Alberto Schiabel committed Aug 18, 2026 at 13:25 UTC 6ba9179b488c27557838b94295b35591ba82fa0c
16 files changed +811 -113
.changeset/guard-response-derived-fetches.md new
+5
@@ -0,0 +1,5 @@
1 +---
2 +'@composio/core': patch
3 +---
4 +
5 +Validate the URLs that come from API responses before fetching them. Tool-execution downloads (`s3Url`), S3 presigned uploads (`new_presigned_url`), Tool Router session file downloads (`RemoteFile.buffer()` / `blob()` / `text()` / `save()`) and session file uploads (`upload_url`) now go through the same SSRF guard that already covered user-supplied URLs, so a response naming a private, loopback, or link-local address is refused instead of fetched. Redirect hops are re-validated. Edge runtimes keep their current behavior: session file transfers are not blocked there, since a Worker cannot resolve DNS to check and its `fetch` does not originate inside the caller's network.
python/composio/core/models/_files.py
+9 -5
@@ -27,7 +27,7 @@ from composio.exceptions import (
27 from composio.utils import mimetypes
28 from composio.utils.json_schema import dereference_json_schema
29 from composio.utils.safe_path import secure_basename_join, secure_join
30 -from composio.utils.url_safety import assert_safe_fetch_target
30 +from composio.utils.url_safety import assert_safe_fetch_target, safe_request
31 from composio.utils.sensitive_file_upload_paths import (
32 assert_safe_local_file_upload_path,
33 )
@@ -153,8 +153,9 @@ def upload(url: str, file: Path) -> bool:
153 """
154 with file.open("rb") as data:
155 try:
156 - response = requests.put(
157 - url=url,
156 + response = safe_request(
157 + "PUT",
158 + url,
159 data=data,
160 timeout=(_CONNECT_TIMEOUT, _READ_TIMEOUT),
161 )
@@ -400,8 +401,9 @@ def _upload_bytes_to_s3(
401
402 # Upload the content directly to S3
403 try:
403 - upload_response = requests.put(
404 - url=s3meta.new_presigned_url,
404 + upload_response = safe_request(
405 + "PUT",
406 + s3meta.new_presigned_url,
407 data=content,
408 headers={"Content-Type": mimetype},
409 timeout=(_CONNECT_TIMEOUT, _READ_TIMEOUT),
@@ -607,11 +609,13 @@ class FileDownloadable(BaseModel):
609 outfile = secure_basename_join(outdir, self.name, root=root)
610 except UnsafePathComponentError as e:
611 raise ErrorDownloadingFile(str(e)) from e
612 + assert_safe_fetch_target(self.s3url)
613 outdir.mkdir(exist_ok=True, parents=True)
614 try:
615 response = requests.get(
616 url=self.s3url,
617 stream=True,
618 + allow_redirects=False,
619 timeout=(_CONNECT_TIMEOUT, _READ_TIMEOUT),
620 )
621 except requests.exceptions.RequestException as e:
python/composio/core/models/tool_router_session_files.py
+98 -33
@@ -29,7 +29,7 @@ from composio.exceptions import (
29 )
30 from composio.utils.mimetypes import get_extension_from_mime_type
31 from composio.utils.safe_path import secure_basename_join
32 -from composio.utils.url_safety import assert_safe_fetch_target
32 +from composio.utils.url_safety import assert_safe_fetch_target, safe_request
33 from composio.utils.uuid import generate_short_id
34
35 DEFAULT_TOOL_ROUTER_SESSION_FILES_MOUNT_ID = "files"
@@ -50,8 +50,42 @@ def _is_url(value: str) -> bool:
50 return False
51
52
53 -def _fetch_from_url(url: str) -> t.Tuple[bytes, str]:
54 - """Fetch file content from URL. Returns (content, mimetype)."""
53 +class _UrlFetchError(Exception):
54 + """Internal failure raised by :func:`_fetch_url_bytes`.
55 +
56 + Carries the facts rather than a message, so the two callers can keep their
57 + own public error type and wording while sharing one network body. Neither
58 + the type nor the instance escapes this module.
59 + """
60 +
61 + def __init__(
62 + self,
63 + *,
64 + cause: t.Optional[BaseException] = None,
65 + status_code: t.Optional[int] = None,
66 + status_text: t.Optional[str] = None,
67 + redirected: bool = False,
68 + size_detail: t.Optional[str] = None,
69 + ) -> None:
70 + super().__init__(size_detail or "Failed to fetch URL")
71 + self.cause = cause
72 + self.status_code = status_code
73 + self.status_text = status_text
74 + self.redirected = redirected
75 + self.size_detail = size_detail
76 +
77 +
78 +def _fetch_url_bytes(url: str) -> t.Tuple[bytes, str]:
79 + """Fetch a URL into memory under the SDK's fetch protections.
80 +
81 + The protections are the point of sharing this: the target is validated
82 + before connecting, redirects are refused rather than followed, and the body
83 + is streamed with the size accounted against ``_MAX_RESPONSE_SIZE`` so a
84 + dishonest ``Content-Length`` cannot exhaust memory. Every caller needs all
85 + four, whether the URL came from the user or from an API response.
86 +
87 + Returns (content, mimetype). Raises :class:`_UrlFetchError`.
88 + """
89 assert_safe_fetch_target(url)
90 try:
91 response = requests.get(
@@ -61,26 +95,26 @@ def _fetch_from_url(url: str) -> t.Tuple[bytes, str]:
95 timeout=(_CONNECT_TIMEOUT, _READ_TIMEOUT),
96 )
97 except requests.exceptions.RequestException as e:
64 - raise ValidationError(f"Failed to fetch file from URL: {e}") from e
98 + raise _UrlFetchError(cause=e) from e
99
100 if response.status_code in (301, 302, 303, 307, 308):
101 response.close()
68 - raise ValidationError(
69 - "URL returned redirect. Please provide a direct URL to the file."
70 - )
102 + raise _UrlFetchError(redirected=True)
103
104 if not response.ok:
105 response.close()
74 - raise ValidationError(
75 - f"Failed to fetch file from URL: {response.status_code} {response.reason}"
106 + raise _UrlFetchError(
107 + status_code=response.status_code, status_text=response.reason
108 )
109
110 content_length = response.headers.get("Content-Length")
111 if content_length and int(content_length) > _MAX_RESPONSE_SIZE:
112 response.close()
81 - raise ValidationError(
82 - f"File size ({int(content_length)} bytes) exceeds maximum allowed "
83 - f"size ({_MAX_RESPONSE_SIZE} bytes)"
113 + raise _UrlFetchError(
114 + size_detail=(
115 + f"File size ({int(content_length)} bytes) exceeds maximum allowed "
116 + f"size ({_MAX_RESPONSE_SIZE} bytes)"
117 + )
118 )
119
120 chunks: t.List[bytes] = []
@@ -90,7 +124,9 @@ def _fetch_from_url(url: str) -> t.Tuple[bytes, str]:
124 total_bytes += len(chunk)
125 if total_bytes > _MAX_RESPONSE_SIZE:
126 response.close()
93 - raise ValidationError("Response size exceeds maximum allowed size")
127 + raise _UrlFetchError(
128 + size_detail="Response size exceeds maximum allowed size"
129 + )
130 chunks.append(chunk)
131 response.close()
132
@@ -99,6 +135,24 @@ def _fetch_from_url(url: str) -> t.Tuple[bytes, str]:
135 return b"".join(chunks), mimetype
136
137
138 +def _fetch_from_url(url: str) -> t.Tuple[bytes, str]:
139 + """Fetch file content from a user-supplied URL. Returns (content, mimetype)."""
140 + try:
141 + return _fetch_url_bytes(url)
142 + except _UrlFetchError as e:
143 + if e.cause is not None:
144 + raise ValidationError(f"Failed to fetch file from URL: {e.cause}") from e
145 + if e.redirected:
146 + raise ValidationError(
147 + "URL returned redirect. Please provide a direct URL to the file."
148 + ) from e
149 + if e.status_code is not None:
150 + raise ValidationError(
151 + f"Failed to fetch file from URL: {e.status_code} {e.status_text}"
152 + ) from e
153 + raise ValidationError(str(e)) from e
154 +
155 +
156 class RemoteFile:
157 """Represents a file stored in a tool router session's file mount.
158
@@ -134,30 +188,37 @@ class RemoteFile:
188 return PureWindowsPath(self.mount_relative_path).name
189
190 def buffer(self) -> bytes:
137 - """Fetch the file content as bytes."""
191 + """Fetch the file content as bytes.
192 +
193 + ``self.download_url`` is set by :meth:`from_api_response`, so it is
194 + untrusted input like every other response field. It goes through
195 + :func:`_fetch_url_bytes` — the same body that serves user-supplied
196 + URLs — rather than a bare ``requests.get``: which side of the trust
197 + boundary the URL arrived from does not change what the fetch needs to
198 + defend against.
199 + """
200 try:
139 - response = requests.get(
140 - self.download_url,
141 - timeout=(_CONNECT_TIMEOUT, _READ_TIMEOUT),
142 - )
143 - except requests.exceptions.RequestException as e:
144 - raise RemoteFileDownloadError(
145 - f"Failed to download file: {type(e).__name__}",
146 - download_url=self.download_url,
147 - mount_relative_path=self.mount_relative_path,
148 - filename=self.filename,
149 - ) from e
150 -
151 - if not response.ok:
201 + content, _ = _fetch_url_bytes(self.download_url)
202 + except _UrlFetchError as e:
203 + if e.redirected:
204 + message = (
205 + "Failed to download file: the download URL returned a redirect"
206 + )
207 + elif e.status_code is not None:
208 + message = f"Failed to download file: {e.status_code} {e.status_text}"
209 + elif e.cause is not None:
210 + message = f"Failed to download file: {type(e.cause).__name__}"
211 + else:
212 + message = f"Failed to download file: {e}"
213 raise RemoteFileDownloadError(
153 - f"Failed to download file: {response.status_code} {response.reason}",
154 - status_code=response.status_code,
155 - status_text=response.reason,
214 + message,
215 + status_code=e.status_code,
216 + status_text=e.status_text,
217 download_url=self.download_url,
218 mount_relative_path=self.mount_relative_path,
219 filename=self.filename,
159 - )
160 - return response.content
220 + ) from (e.cause or e)
221 + return content
222
223 def text(self) -> str:
224 """Fetch the file content as UTF-8 text."""
@@ -319,8 +380,12 @@ class ToolRouterSessionFilesMount:
380 mimetype=mime,
381 )
382
383 + # `upload_url` comes from the API response, so it is validated before
384 + # the file's bytes are sent to it, and re-validated on every redirect
385 + # hop. See `composio.utils.url_safety`.
386 try:
323 - upload_resp = requests.put(
387 + upload_resp = safe_request(
388 + "PUT",
389 create_resp.upload_url,
390 data=content,
391 headers={"Content-Type": mime},
python/composio/utils/url_safety.py
+73 -2
@@ -1,15 +1,36 @@
1 -"""SSRF protections for user-supplied URL file inputs."""
1 +"""SSRF protections for URLs the SDK fetches.
2 +
3 +Applies to URLs the caller supplies *and* to URLs an API response supplies:
4 +under the trust boundary documented in ``python/AGENTS.md`` the backend may be
5 +compromised or the connection MITM'd, so a presigned URL in a response is no
6 +more trusted than one typed by a user. Without a guard, either can point the
7 +SDK at loopback, RFC1918 space, or a link-local cloud-metadata endpoint
8 +(``169.254.169.254``) and turn it into a request proxy for internal
9 +infrastructure.
10 +
11 +Known residual — DNS rebinding: the host is resolved here with
12 +``getaddrinfo``, and the HTTP client resolves it again independently when it
13 +connects. A short-TTL record that alternates a public and a private answer can
14 +pass this check and still connect to the private address. Closing that window
15 +requires pinning the validated address and connecting to it directly (a custom
16 +``requests`` transport adapter), which is out of scope for this module; the
17 +TypeScript guard carries the same limitation.
18 +"""
19
20 from __future__ import annotations
21
22 import ipaddress
23 import socket
7 -from urllib.parse import urlparse
24 +import typing as t
25 +from urllib.parse import urljoin, urlparse
26
27 import requests
28
29 from composio.exceptions import BlockedInternalUrlError
30
31 +_REDIRECT_STATUS_CODES = frozenset({301, 302, 303, 307, 308})
32 +_MAX_REDIRECTS = 5
33 +
34
35 def is_blocked_ip(value: str) -> bool:
36 """Return whether an address is non-publicly-routable."""
@@ -68,3 +89,53 @@ def assert_safe_fetch_target(url: str) -> None:
89 raise BlockedInternalUrlError(
90 f'Refusing to fetch "{parsed.hostname}" because it resolves to a non-public address'
91 )
92 +
93 +
94 +def safe_request(
95 + method: str,
96 + url: str,
97 + *,
98 + max_redirects: int = _MAX_REDIRECTS,
99 + **kwargs: t.Any,
100 +) -> requests.Response:
101 + """Send a request, validating the target before *every* hop.
102 +
103 + Redirects are followed manually so each new location is validated too.
104 + Validating only the first URL is not enough: a target that passes the check
105 + and then answers ``302 Location: http://169.254.169.254/`` would have the
106 + redirect followed by ``requests`` with no further validation. Mirrors
107 + ``ssrfSafeFetch`` in the TypeScript SDK.
108 +
109 + Use this where redirects are legitimate (S3 can answer a PUT with a 307
110 + region redirect). Call sites that require a direct URL should instead call
111 + :func:`assert_safe_fetch_target` and pass ``allow_redirects=False``.
112 +
113 + :param max_redirects: Hops to follow before giving up.
114 + :raises BlockedInternalUrlError: If any hop fails validation, or the
115 + redirect chain is longer than ``max_redirects``.
116 + """
117 + body = kwargs.get("data")
118 + current_url = url
119 +
120 + for _ in range(max_redirects + 1):
121 + assert_safe_fetch_target(current_url)
122 + response = requests.request(
123 + method, current_url, allow_redirects=False, **kwargs
124 + )
125 +
126 + location = response.headers.get("Location")
127 + if response.status_code not in _REDIRECT_STATUS_CODES or location is None:
128 + return response
129 +
130 + response.close()
131 + current_url = urljoin(current_url, location)
132 +
133 + # `requests` rewinds the body itself when it follows a redirect; doing
134 + # it manually means doing that too, or a retried upload sends nothing.
135 + seek = getattr(body, "seek", None)
136 + if callable(seek):
137 + seek(0)
138 +
139 + raise BlockedInternalUrlError(
140 + f"Refusing to fetch: too many redirects (max {max_redirects})"
141 + )
python/tests/test_files.py
+160 -28
@@ -16,6 +16,7 @@ from composio.core.models._files import (
16 FileDownloadable,
17 FileHelper,
18 FileUploadable,
19 + upload,
20 _is_url,
21 _get_extension_from_mimetype,
22 _generate_timestamped_filename,
@@ -27,6 +28,7 @@ from composio.core.models._files import (
28 )
29 from composio.core.models.base import allow_tracking
30 from composio.exceptions import (
31 + BlockedInternalUrlError,
32 ErrorDownloadingFile,
33 ErrorUploadingFile,
34 ResponseTooLargeError,
@@ -1704,8 +1706,8 @@ class TestFetchFileFromUrl:
1706 class TestUploadBytesToS3:
1707 """Test cases for _upload_bytes_to_s3 function."""
1708
1707 - @patch("composio.core.models._files.requests.put")
1708 - def test_upload_bytes_to_s3_success(self, mock_put):
1709 + @patch("composio.core.models._files.safe_request")
1710 + def test_upload_bytes_to_s3_success(self, mock_safe_request):
1711 """Test successful upload to S3."""
1712 mock_client = MagicMock()
1713 mock_s3_response = MagicMock()
@@ -1715,7 +1717,7 @@ class TestUploadBytesToS3:
1717
1718 mock_put_response = MagicMock()
1719 mock_put_response.status_code = 200
1718 - mock_put.return_value = mock_put_response
1720 + mock_safe_request.return_value = mock_put_response
1721
1722 result = _upload_bytes_to_s3(
1723 client=mock_client,
@@ -1728,15 +1730,16 @@ class TestUploadBytesToS3:
1730
1731 assert result == "s3-key-123"
1732 mock_client.post.assert_called_once()
1731 - mock_put.assert_called_once_with(
1732 - url="https://s3.example.com/upload",
1733 + mock_safe_request.assert_called_once_with(
1734 + "PUT",
1735 + "https://s3.example.com/upload",
1736 data=b"file content",
1737 headers={"Content-Type": "image/jpeg"},
1738 timeout=(5, 60),
1739 )
1740
1738 - @patch("composio.core.models._files.requests.put")
1739 - def test_upload_bytes_to_s3_failure(self, mock_put):
1741 + @patch("composio.core.models._files.safe_request")
1742 + def test_upload_bytes_to_s3_failure(self, mock_safe_request):
1743 """Test error handling when S3 upload fails."""
1744 mock_client = MagicMock()
1745 mock_s3_response = MagicMock()
@@ -1746,7 +1749,7 @@ class TestUploadBytesToS3:
1749
1750 mock_put_response = MagicMock()
1751 mock_put_response.status_code = 500
1749 - mock_put.return_value = mock_put_response
1752 + mock_safe_request.return_value = mock_put_response
1753
1754 with pytest.raises(ErrorUploadingFile) as exc_info:
1755 _upload_bytes_to_s3(
@@ -1760,8 +1763,8 @@ class TestUploadBytesToS3:
1763
1764 assert "Failed to upload to S3" in str(exc_info.value)
1765
1763 - @patch("composio.core.models._files.requests.put")
1764 - def test_upload_bytes_to_s3_timeout(self, mock_put):
1766 + @patch("composio.core.models._files.safe_request")
1767 + def test_upload_bytes_to_s3_timeout(self, mock_safe_request):
1768 """Test request timeouts are reported as upload errors."""
1769 mock_client = MagicMock()
1770 mock_s3_response = MagicMock()
@@ -1770,7 +1773,7 @@ class TestUploadBytesToS3:
1773 mock_client.post.return_value = mock_s3_response
1774 # The exception text itself carries the presigned URL (incl. token), as
1775 # real urllib3 errors do — the SDK must not surface it in the message.
1773 - mock_put.side_effect = requests.exceptions.Timeout(
1776 + mock_safe_request.side_effect = requests.exceptions.Timeout(
1777 "HTTPSConnectionPool(host='s3.example.com', port=443): "
1778 "Max retries exceeded with url: /upload?token=abc"
1779 )
@@ -2424,8 +2427,8 @@ class TestRedirectHandling:
2427 class TestS3UploadErrorHandling:
2428 """Test S3 upload error handling."""
2429
2427 - @patch("composio.core.models._files.requests.put")
2428 - def test_403_is_treated_as_error(self, mock_put):
2430 + @patch("composio.core.models._files.safe_request")
2431 + def test_403_is_treated_as_error(self, mock_safe_request):
2432 """HTTP 403 should be treated as upload failure."""
2433 mock_client = MagicMock()
2434 mock_s3_response = MagicMock()
@@ -2435,7 +2438,7 @@ class TestS3UploadErrorHandling:
2438
2439 mock_put_response = MagicMock()
2440 mock_put_response.status_code = 403
2438 - mock_put.return_value = mock_put_response
2441 + mock_safe_request.return_value = mock_put_response
2442
2443 with pytest.raises(ErrorUploadingFile, match="403"):
2444 _upload_bytes_to_s3(
@@ -2447,8 +2450,8 @@ class TestS3UploadErrorHandling:
2450 toolkit="test",
2451 )
2452
2450 - @patch("composio.core.models._files.requests.put")
2451 - def test_200_is_success(self, mock_put):
2453 + @patch("composio.core.models._files.safe_request")
2454 + def test_200_is_success(self, mock_safe_request):
2455 """HTTP 200 should be treated as success."""
2456 mock_client = MagicMock()
2457 mock_s3_response = MagicMock()
@@ -2458,7 +2461,7 @@ class TestS3UploadErrorHandling:
2461
2462 mock_put_response = MagicMock()
2463 mock_put_response.status_code = 200
2461 - mock_put.return_value = mock_put_response
2464 + mock_safe_request.return_value = mock_put_response
2465
2466 result = _upload_bytes_to_s3(
2467 client=mock_client,
@@ -2470,8 +2473,8 @@ class TestS3UploadErrorHandling:
2473 )
2474 assert result == "s3-key"
2475
2473 - @patch("composio.core.models._files.requests.put")
2474 - def test_500_is_treated_as_error(self, mock_put):
2476 + @patch("composio.core.models._files.safe_request")
2477 + def test_500_is_treated_as_error(self, mock_safe_request):
2478 """HTTP 500 should be treated as upload failure."""
2479 mock_client = MagicMock()
2480 mock_s3_response = MagicMock()
@@ -2481,7 +2484,7 @@ class TestS3UploadErrorHandling:
2484
2485 mock_put_response = MagicMock()
2486 mock_put_response.status_code = 500
2484 - mock_put.return_value = mock_put_response
2487 + mock_safe_request.return_value = mock_put_response
2488
2489 with pytest.raises(ErrorUploadingFile, match="500"):
2490 _upload_bytes_to_s3(
@@ -2528,6 +2531,11 @@ class TestUrlSanitization:
2531 class TestFileDownloadablePathTraversal:
2532 """SEC-316: server-controlled `name` must not escape the output dir."""
2533
2534 + @pytest.fixture(autouse=True)
2535 + def _allow_fetch_target(self):
2536 + with patch("composio.core.models._files.assert_safe_fetch_target"):
2537 + yield
2538 +
2539 def _mock_response(self, content: bytes = b"data") -> MagicMock:
2540 response = MagicMock()
2541 response.status_code = 200
@@ -2624,6 +2632,7 @@ class TestFileDownloadablePathTraversal:
2632 mock_get.assert_called_once_with(
2633 url="https://example.com/file",
2634 stream=True,
2635 + allow_redirects=False,
2636 timeout=(5, 60),
2637 )
2638
@@ -3017,12 +3026,6 @@ class TestFromPathSensitiveGuard:
3026 mock_client.post.assert_not_called()
3027
3028 def test_from_path_opt_out_disables_guard(self, mock_client):
3020 - """`sensitive_file_upload_protection=False` restores the legacy
3021 - (unguarded) behavior; the denylist no longer raises for that path."""
3022 - # A non-existent basename under `.ssh`: the `.ssh` segment still trips the
3023 - # denylist when protection is on, but the file never exists, so with
3024 - # protection off we deterministically hit the missing-file path (no real
3025 - # key read, no network) rather than the block error.
3029 p = Path.home() / ".ssh" / "composio-does-not-exist-guard-test"
3030 with pytest.raises(Exception) as exc_info:
3031 FileUploadable.from_path(
@@ -3033,6 +3036,135 @@ class TestFromPathSensitiveGuard:
3036 sensitive_file_upload_protection=False,
3037 )
3038 assert not isinstance(exc_info.value, SensitiveFilePathBlockedError)
3036 - # Guard skipped, so the failure is downstream (missing file), before any
3037 - # presigned-URL request.
3039 mock_client.post.assert_not_called()
3040 +
3041 +
3042 +class TestResponseDerivedUrlsAreGuarded:
3043 + def _download_response(self) -> MagicMock:
3044 + response = MagicMock()
3045 + response.status_code = 200
3046 + response.iter_content = lambda chunk_size: [b"%PDF-1.4"]
3047 + response.close = MagicMock()
3048 + return response
3049 +
3050 + def _s3_client(self, presigned_url: str) -> MagicMock:
3051 + client = MagicMock()
3052 + s3meta = MagicMock()
3053 + s3meta.key = "s3-key"
3054 + s3meta.new_presigned_url = presigned_url
3055 + client.post.return_value = s3meta
3056 + return client
3057 +
3058 + def test_download_validates_s3url(self, tmp_path):
3059 + f = FileDownloadable(
3060 + name="report.pdf",
3061 + mimetype="application/pdf",
3062 + s3url="https://s3.example.com/file",
3063 + )
3064 + with patch(
3065 + "composio.core.models._files.assert_safe_fetch_target"
3066 + ) as mock_assert:
3067 + with patch(
3068 + "composio.core.models._files.requests.get",
3069 + return_value=self._download_response(),
3070 + ):
3071 + f.download(tmp_path / "out", root=tmp_path / "out")
3072 +
3073 + mock_assert.assert_called_once_with("https://s3.example.com/file")
3074 +
3075 + def test_download_blocked_url_never_reaches_the_network(self, tmp_path):
3076 + outdir = tmp_path / "out"
3077 + f = FileDownloadable(
3078 + name="report.pdf",
3079 + mimetype="application/pdf",
3080 + s3url="http://169.254.169.254/latest/meta-data",
3081 + )
3082 + with patch(
3083 + "composio.core.models._files.assert_safe_fetch_target",
3084 + side_effect=BlockedInternalUrlError("blocked"),
3085 + ):
3086 + with patch("composio.core.models._files.requests.get") as mock_get:
3087 + with pytest.raises(BlockedInternalUrlError):
3088 + f.download(outdir, root=outdir)
3089 +
3090 + mock_get.assert_not_called()
3091 + assert not outdir.exists()
3092 +
3093 + def test_download_refuses_to_follow_redirects(self, tmp_path):
3094 + f = FileDownloadable(
3095 + name="report.pdf",
3096 + mimetype="application/pdf",
3097 + s3url="https://s3.example.com/file",
3098 + )
3099 + with patch("composio.core.models._files.assert_safe_fetch_target"):
3100 + with patch(
3101 + "composio.core.models._files.requests.get",
3102 + return_value=self._download_response(),
3103 + ) as mock_get:
3104 + f.download(tmp_path / "out", root=tmp_path / "out")
3105 +
3106 + assert mock_get.call_args.kwargs["allow_redirects"] is False
3107 +
3108 + def test_upload_bytes_to_s3_goes_through_safe_request(self):
3109 + client = self._s3_client("https://s3.example.com/upload")
3110 + with patch("composio.core.models._files.safe_request") as mock_safe_request:
3111 + mock_safe_request.return_value.status_code = 200
3112 + _upload_bytes_to_s3(
3113 + client=client,
3114 + filename="test.jpg",
3115 + content=b"data",
3116 + mimetype="image/jpeg",
3117 + tool="TEST",
3118 + toolkit="test",
3119 + )
3120 +
3121 + assert mock_safe_request.call_args.args == (
3122 + "PUT",
3123 + "https://s3.example.com/upload",
3124 + )
3125 +
3126 + def test_upload_bytes_to_s3_blocked_url_sends_nothing(self):
3127 + client = self._s3_client("http://169.254.169.254/upload")
3128 + with patch(
3129 + "composio.utils.url_safety.assert_safe_fetch_target",
3130 + side_effect=BlockedInternalUrlError("blocked"),
3131 + ):
3132 + with patch("composio.utils.url_safety.requests.request") as mock_request:
3133 + with pytest.raises(BlockedInternalUrlError):
3134 + _upload_bytes_to_s3(
3135 + client=client,
3136 + filename="test.jpg",
3137 + content=b"data",
3138 + mimetype="image/jpeg",
3139 + tool="TEST",
3140 + toolkit="test",
3141 + )
3142 +
3143 + mock_request.assert_not_called()
3144 +
3145 + def test_file_upload_goes_through_safe_request(self, tmp_path):
3146 + source = tmp_path / "report.pdf"
3147 + source.write_bytes(b"%PDF-1.4")
3148 +
3149 + with patch("composio.core.models._files.safe_request") as mock_safe_request:
3150 + mock_safe_request.return_value.status_code = 200
3151 + assert upload(url="https://s3.example.com/upload", file=source) is True
3152 +
3153 + assert mock_safe_request.call_args.args == (
3154 + "PUT",
3155 + "https://s3.example.com/upload",
3156 + )
3157 +
3158 + def test_file_upload_blocked_url_sends_nothing(self, tmp_path):
3159 + source = tmp_path / "report.pdf"
3160 + source.write_bytes(b"%PDF-1.4")
3161 +
3162 + with patch(
3163 + "composio.utils.url_safety.assert_safe_fetch_target",
3164 + side_effect=BlockedInternalUrlError("blocked"),
3165 + ):
3166 + with patch("composio.utils.url_safety.requests.request") as mock_request:
3167 + with pytest.raises(BlockedInternalUrlError):
3168 + upload(url="http://127.0.0.1:9000/upload", file=source)
3169 +
3170 + mock_request.assert_not_called()
python/tests/test_tool_router_session_files.py
+159 -28
@@ -10,7 +10,32 @@ from composio.core.models.tool_router_session_files import (
10 RemoteFile,
11 ToolRouterSessionFilesMount,
12 )
13 -from composio.exceptions import RemoteFileDownloadError, ValidationError
13 +from composio.exceptions import (
14 + BlockedInternalUrlError,
15 + RemoteFileDownloadError,
16 + ValidationError,
17 +)
18 +
19 +MODULE = "composio.core.models.tool_router_session_files"
20 +SAFE_REQUEST = f"{MODULE}.safe_request"
21 +ASSERT_SAFE_FETCH_TARGET = f"{MODULE}.assert_safe_fetch_target"
22 +
23 +
24 +def mock_stream_response(
25 + content: bytes = b"file content",
26 + *,
27 + status_code: int = 200,
28 + content_type: str = "text/plain",
29 +) -> MagicMock:
30 + """A streaming `requests` response double, as `_fetch_url_bytes` reads it."""
31 + response = MagicMock()
32 + response.status_code = status_code
33 + response.ok = 200 <= status_code < 300
34 + response.reason = "OK" if response.ok else "Not Found"
35 + response.headers = {"content-type": content_type}
36 + response.iter_content = lambda chunk_size: [content]
37 + response.close = MagicMock()
38 + return response
39
40
41 @pytest.fixture
@@ -90,9 +115,9 @@ class TestToolRouterSessionFilesMount:
115
116 def test_upload_from_bytes_with_remote_path(self, files_mount, mock_client):
117 """Test upload from bytes with remote_path."""
93 - with patch("requests.put") as mock_put:
94 - mock_put.return_value.status_code = 200
95 - mock_put.return_value.ok = True
118 + with patch(SAFE_REQUEST) as mock_safe_request:
119 + mock_safe_request.return_value.status_code = 200
120 + mock_safe_request.return_value.ok = True
121
122 result = files_mount.upload(
123 b"hello world",
@@ -102,7 +127,11 @@ class TestToolRouterSessionFilesMount:
127
128 assert isinstance(result, RemoteFile)
129 assert result.mount_relative_path == "output/test.txt"
105 - mock_put.assert_called_once_with(
130 + # Routed through `safe_request`, not a bare `requests.put`:
131 + # `upload_url` is a response field, so its target is validated
132 + # before the bytes are sent, and on every redirect hop after.
133 + mock_safe_request.assert_called_once_with(
134 + "PUT",
135 "https://s3.example.com/upload",
136 data=b"hello world",
137 headers={"Content-Type": "text/plain"},
@@ -113,7 +142,7 @@ class TestToolRouterSessionFilesMount:
142
143 def test_upload_raises_validation_error_on_timeout(self, files_mount):
144 """Test upload converts request timeouts to ValidationError."""
116 - with patch("requests.put", side_effect=requests.exceptions.Timeout("timeout")):
145 + with patch(SAFE_REQUEST, side_effect=requests.exceptions.Timeout("timeout")):
146 with pytest.raises(ValidationError, match="Failed to upload file"):
147 files_mount.upload(
148 b"hello world",
@@ -126,9 +155,9 @@ class TestToolRouterSessionFilesMount:
155 test_file = tmp_path / "report.pdf"
156 test_file.write_bytes(b"pdf content")
157
129 - with patch("requests.put") as mock_put:
130 - mock_put.return_value.status_code = 200
131 - mock_put.return_value.ok = True
158 + with patch(SAFE_REQUEST) as mock_safe_request:
159 + mock_safe_request.return_value.status_code = 200
160 + mock_safe_request.return_value.ok = True
161
162 result = files_mount.upload(str(test_file))
163
@@ -184,15 +213,15 @@ class TestRemoteFile:
213 sandbox_mount_prefix="/mnt/files",
214 download_url="https://example.com/file",
215 )
187 - with patch("requests.get") as mock_get:
188 - mock_get.return_value.status_code = 200
189 - mock_get.return_value.ok = True
190 - mock_get.return_value.content = b"file content"
216 + with patch(ASSERT_SAFE_FETCH_TARGET):
217 + with patch("requests.get", return_value=mock_stream_response()) as mock_get:
218 + result = rf.buffer()
219
192 - result = rf.buffer()
220 assert result == b"file content"
221 mock_get.assert_called_once_with(
222 "https://example.com/file",
223 + stream=True,
224 + allow_redirects=False,
225 timeout=(5, 60),
226 )
227
@@ -204,16 +233,15 @@ class TestRemoteFile:
233 sandbox_mount_prefix="/mnt/files",
234 download_url="https://example.com/file",
235 )
207 - with patch("requests.get") as mock_get:
208 - mock_get.return_value.status_code = 404
209 - mock_get.return_value.ok = False
210 - mock_get.return_value.reason = "Not Found"
211 -
212 - with pytest.raises(RemoteFileDownloadError) as exc_info:
213 - rf.buffer()
236 + with patch(ASSERT_SAFE_FETCH_TARGET):
237 + with patch(
238 + "requests.get", return_value=mock_stream_response(status_code=404)
239 + ):
240 + with pytest.raises(RemoteFileDownloadError) as exc_info:
241 + rf.buffer()
242
215 - assert exc_info.value.status_code == 404
216 - assert exc_info.value.filename == "test.txt"
243 + assert exc_info.value.status_code == 404
244 + assert exc_info.value.filename == "test.txt"
245
246 def test_buffer_timeout_raises_remote_file_download_error(self):
247 """Test buffer() converts request timeouts to RemoteFileDownloadError."""
@@ -223,12 +251,15 @@ class TestRemoteFile:
251 sandbox_mount_prefix="/mnt/files",
252 download_url="https://example.com/file",
253 )
226 - with patch("requests.get", side_effect=requests.exceptions.Timeout("timeout")):
227 - with pytest.raises(RemoteFileDownloadError) as exc_info:
228 - rf.buffer()
254 + with patch(ASSERT_SAFE_FETCH_TARGET):
255 + with patch(
256 + "requests.get", side_effect=requests.exceptions.Timeout("timeout")
257 + ):
258 + with pytest.raises(RemoteFileDownloadError) as exc_info:
259 + rf.buffer()
260
230 - assert exc_info.value.filename == "test.txt"
231 - assert exc_info.value.download_url == "https://example.com/file"
261 + assert exc_info.value.filename == "test.txt"
262 + assert exc_info.value.download_url == "https://example.com/file"
263
264 def test_text(self):
265 """Test text() decodes UTF-8."""
@@ -290,3 +321,103 @@ class TestRemoteFile:
321
322 # The check raises before mkdir/write, so nothing was written under tmp_path.
323 assert not (tmp_path / ".composio").exists()
324 +
325 +
326 +class TestResponseDerivedUrlsAreGuarded:
327 + """`download_url` and `upload_url` are response fields, so they are guarded.
328 +
329 + `RemoteFile.buffer()` previously called `requests.get` directly: no target
330 + validation, no redirect control, and `response.content` read the whole body
331 + into memory with no cap — while the sibling `_fetch_from_url`, four lines
332 + up, had all three. The only difference between them was which side of the
333 + trust boundary the URL came from.
334 + """
335 +
336 + def _remote_file(self, download_url: str) -> RemoteFile:
337 + return RemoteFile(
338 + expires_at="2026-01-01",
339 + mount_relative_path="test.txt",
340 + sandbox_mount_prefix="/mnt/files",
341 + download_url=download_url,
342 + )
343 +
344 + def test_buffer_validates_download_url(self):
345 + rf = self._remote_file("https://s3.example.com/download")
346 +
347 + with patch(ASSERT_SAFE_FETCH_TARGET) as mock_assert:
348 + with patch("requests.get", return_value=mock_stream_response()):
349 + rf.buffer()
350 +
351 + mock_assert.assert_called_once_with("https://s3.example.com/download")
352 +
353 + def test_buffer_blocked_url_never_reaches_the_network(self):
354 + rf = self._remote_file("http://169.254.169.254/latest/meta-data")
355 +
356 + with patch(
357 + ASSERT_SAFE_FETCH_TARGET,
358 + side_effect=BlockedInternalUrlError("blocked"),
359 + ):
360 + with patch("requests.get") as mock_get:
361 + with pytest.raises(BlockedInternalUrlError):
362 + rf.buffer()
363 +
364 + mock_get.assert_not_called()
365 +
366 + def test_buffer_rejects_redirects(self):
367 + """A validated URL must not be able to bounce the fetch elsewhere."""
368 + rf = self._remote_file("https://s3.example.com/download")
369 +
370 + with patch(ASSERT_SAFE_FETCH_TARGET):
371 + with patch(
372 + "requests.get", return_value=mock_stream_response(status_code=302)
373 + ) as mock_get:
374 + with pytest.raises(RemoteFileDownloadError, match="redirect"):
375 + rf.buffer()
376 +
377 + assert mock_get.call_args.kwargs["allow_redirects"] is False
378 +
379 + def test_buffer_caps_response_size(self):
380 + """The body is streamed against a cap rather than read whole."""
381 + rf = self._remote_file("https://s3.example.com/download")
382 + oversized = mock_stream_response()
383 + oversized.headers = {
384 + "content-type": "text/plain",
385 + "Content-Length": str(200 * 1024 * 1024),
386 + }
387 +
388 + with patch(ASSERT_SAFE_FETCH_TARGET):
389 + with patch("requests.get", return_value=oversized):
390 + with pytest.raises(RemoteFileDownloadError, match="exceeds maximum"):
391 + rf.buffer()
392 +
393 + def test_text_and_save_inherit_the_guard(self, tmp_path):
394 + """`text()` and `save()` read through `buffer()`, so they are covered."""
395 + rf = self._remote_file("http://127.0.0.1:9000/download")
396 +
397 + with patch(
398 + ASSERT_SAFE_FETCH_TARGET,
399 + side_effect=BlockedInternalUrlError("blocked"),
400 + ):
401 + with patch("requests.get") as mock_get:
402 + with pytest.raises(BlockedInternalUrlError):
403 + rf.text()
404 + with pytest.raises(BlockedInternalUrlError):
405 + rf.save(str(tmp_path / "out.txt"))
406 +
407 + mock_get.assert_not_called()
408 + assert not (tmp_path / "out.txt").exists()
409 +
410 + def test_upload_blocked_url_sends_nothing(self, files_mount):
411 + with patch(
412 + "composio.utils.url_safety.assert_safe_fetch_target",
413 + side_effect=BlockedInternalUrlError("blocked"),
414 + ):
415 + with patch("composio.utils.url_safety.requests.request") as mock_request:
416 + with pytest.raises(BlockedInternalUrlError):
417 + files_mount.upload(
418 + b"hello world",
419 + remote_path="data.txt",
420 + mimetype="text/plain",
421 + )
422 +
423 + mock_request.assert_not_called()
python/tests/test_url_safety.py
+104 -2
@@ -2,13 +2,18 @@
2
3 from __future__ import annotations
4
5 +import io
6 import socket
6 -from unittest.mock import patch
7 +from unittest.mock import MagicMock, call, patch
8
9 import pytest
10
11 from composio.exceptions import BlockedInternalUrlError
11 -from composio.utils.url_safety import assert_safe_fetch_target, is_blocked_ip
12 +from composio.utils.url_safety import (
13 + assert_safe_fetch_target,
14 + is_blocked_ip,
15 + safe_request,
16 +)
17
18
19 @pytest.mark.parametrize(
@@ -94,3 +99,100 @@ def test_allows_public_dns_answers(mock_getaddrinfo) -> None:
99 ]
100
101 assert_safe_fetch_target("https://example.com/file.pdf")
102 +
103 +
104 +def _response(status_code: int, location: str | None = None) -> MagicMock:
105 + response = MagicMock()
106 + response.status_code = status_code
107 + response.headers = {"Location": location} if location else {}
108 + response.close = MagicMock()
109 + return response
110 +
111 +
112 +@patch("composio.utils.url_safety.requests.request")
113 +@patch("composio.utils.url_safety.assert_safe_fetch_target")
114 +def test_safe_request_validates_before_sending(mock_assert, mock_request) -> None:
115 + mock_assert.side_effect = BlockedInternalUrlError("blocked")
116 +
117 + with pytest.raises(BlockedInternalUrlError):
118 + safe_request("PUT", "https://s3.example.com/upload", data=b"payload")
119 +
120 + # The point of the guard: a rejected target must never reach the network.
121 + mock_request.assert_not_called()
122 +
123 +
124 +@patch("composio.utils.url_safety.requests.request")
125 +@patch("composio.utils.url_safety.assert_safe_fetch_target")
126 +def test_safe_request_disables_automatic_redirects(mock_assert, mock_request) -> None:
127 + mock_request.return_value = _response(200)
128 +
129 + response = safe_request("PUT", "https://s3.example.com/upload", data=b"payload")
130 +
131 + assert response.status_code == 200
132 + mock_assert.assert_called_once_with("https://s3.example.com/upload")
133 + # `requests` must not follow redirects on its own, or hops after the first
134 + # would be fetched without ever being validated.
135 + assert mock_request.call_args.kwargs["allow_redirects"] is False
136 +
137 +
138 +@patch("composio.utils.url_safety.requests.request")
139 +@patch("composio.utils.url_safety.assert_safe_fetch_target")
140 +def test_safe_request_revalidates_each_redirect_hop(mock_assert, mock_request) -> None:
141 + """A public URL that redirects into private space must be caught at the hop."""
142 + mock_assert.side_effect = [None, BlockedInternalUrlError("blocked")]
143 + mock_request.return_value = _response(
144 + 307, "http://169.254.169.254/latest/meta-data"
145 + )
146 +
147 + with pytest.raises(BlockedInternalUrlError):
148 + safe_request("PUT", "https://s3.example.com/upload", data=b"payload")
149 +
150 + assert mock_assert.call_args_list == [
151 + call("https://s3.example.com/upload"),
152 + call("http://169.254.169.254/latest/meta-data"),
153 + ]
154 + # Only the first hop was sent; the redirect target never was.
155 + assert mock_request.call_count == 1
156 +
157 +
158 +@patch("composio.utils.url_safety.requests.request")
159 +@patch("composio.utils.url_safety.assert_safe_fetch_target")
160 +def test_safe_request_follows_validated_redirect(mock_assert, mock_request) -> None:
161 + """S3 can answer a PUT with a 307 region redirect; that must still work."""
162 + mock_request.side_effect = [
163 + _response(307, "https://s3.eu-west-1.example.com/upload"),
164 + _response(200),
165 + ]
166 + body = io.BytesIO(b"payload")
167 +
168 + response = safe_request("PUT", "https://s3.example.com/upload", data=body)
169 +
170 + assert response.status_code == 200
171 + assert mock_assert.call_args_list == [
172 + call("https://s3.example.com/upload"),
173 + call("https://s3.eu-west-1.example.com/upload"),
174 + ]
175 + # The body was rewound, so the retried hop sends the payload rather than
176 + # an already-exhausted stream.
177 + assert body.read() == b"payload"
178 +
179 +
180 +@patch("composio.utils.url_safety.requests.request")
181 +@patch("composio.utils.url_safety.assert_safe_fetch_target")
182 +def test_safe_request_relative_redirect_is_resolved(mock_assert, mock_request) -> None:
183 + mock_request.side_effect = [_response(302, "/elsewhere"), _response(200)]
184 +
185 + safe_request("GET", "https://files.example.com/a/b")
186 +
187 + assert mock_assert.call_args_list[1] == call("https://files.example.com/elsewhere")
188 +
189 +
190 +@patch("composio.utils.url_safety.requests.request")
191 +@patch("composio.utils.url_safety.assert_safe_fetch_target")
192 +def test_safe_request_rejects_endless_redirects(mock_assert, mock_request) -> None:
193 + mock_request.return_value = _response(302, "https://s3.example.com/upload")
194 +
195 + with pytest.raises(BlockedInternalUrlError, match="too many redirects"):
196 + safe_request("GET", "https://s3.example.com/upload", max_redirects=2)
197 +
198 + assert mock_request.call_count == 3
ts/packages/core/src/models/RemoteFile.ts
+7 -2
@@ -1,4 +1,5 @@
1 import { platform } from '#platform';
2 +import { ssrfSafeFetchWhereSupported } from '#ssrf_guard';
3 import { COMPOSIO_DIR, TEMP_FILES_DIRECTORY_NAME } from '../utils/constants';
4
5 function getParentDir(filePath: string): string {
@@ -82,7 +83,10 @@ export class RemoteFile {
83 * @throws RemoteFileDownloadError if the fetch fails
84 */
85 async buffer(): Promise<Uint8Array> {
85 - const response = await fetch(this.downloadUrl);
86 + // SSRF guard: `downloadUrl` is set from an API response, so it is untrusted
87 + // input like every other response field, and its bytes are handed straight
88 + // back to the caller. See ssrfGuard.node.ts.
89 + const response = await ssrfSafeFetchWhereSupported(this.downloadUrl);
90 if (!response.ok) {
91 throw new RemoteFileDownloadError(
92 `Failed to download file: ${response.status} ${response.statusText}`,
@@ -116,7 +120,8 @@ export class RemoteFile {
120 * @throws RemoteFileDownloadError if the fetch fails
121 */
122 async blob(): Promise<Blob> {
119 - const response = await fetch(this.downloadUrl);
123 + // SSRF guard: see `buffer()`.
124 + const response = await ssrfSafeFetchWhereSupported(this.downloadUrl);
125 if (!response.ok) {
126 throw new RemoteFileDownloadError(
127 `Failed to download file: ${response.status} ${response.statusText}`,
ts/packages/core/src/models/ToolRouterSessionFileMount.ts
+14 -8
@@ -18,7 +18,7 @@ import {
18 import { RemoteFile } from './RemoteFile';
19 import { ValidationError } from '../errors';
20 import { platform } from '#platform';
21 -import { ssrfSafeFetch } from '#ssrf_guard';
21 +import { ssrfSafeFetch, ssrfSafeFetchWhereSupported } from '#ssrf_guard';
22 import { getExtensionFromMimeType } from '../utils/mime';
23 import { readResponseBodyWithLimit } from '../utils/readResponseBody';
24 import { getRandomShortId } from '../utils/uuid';
@@ -240,13 +240,19 @@ export class ToolRouterSessionFilesMount {
240 ? (createUploadURLResponse as { body: unknown }).body
241 : createUploadURLResponse;
242
243 - const uploadResponse = await fetch((uploadURLData as { upload_url: string }).upload_url, {
244 - method: 'PUT',
245 - body: await fileToUpload.arrayBuffer(),
246 - headers: {
247 - 'Content-Type': mimetype,
248 - },
249 - });
243 + // SSRF guard: `upload_url` comes from the API response, so the target is
244 + // validated before the file's bytes are sent to it — the same treatment the
245 + // user-supplied URL above already gets. See ssrfGuard.node.ts.
246 + const uploadResponse = await ssrfSafeFetchWhereSupported(
247 + (uploadURLData as { upload_url: string }).upload_url,
248 + {
249 + method: 'PUT',
250 + body: await fileToUpload.arrayBuffer(),
251 + headers: {
252 + 'Content-Type': mimetype,
253 + },
254 + }
255 + );
256
257 if (!uploadResponse.ok) {
258 throw new Error(`Failed to upload file: ${uploadResponse.statusText}`);
ts/packages/core/src/utils/fileUtils.node.ts
+8 -2
@@ -246,7 +246,10 @@ const uploadFileToS3 = async (
246 // `InvalidArgumentError: invalid content-length header`, which surfaces as
247 // `TypeError: fetch failed` and breaks every `file_uploadable` connector tool
248 // on Node 22+.
249 - const uploadResponse = await fetch(signedURL, {
249 + // SSRF guard: `new_presigned_url` comes from the API response, which the SDK
250 + // treats as untrusted — a response naming an internal address would otherwise
251 + // have the file's bytes PUT to it. See ssrfGuard.node.ts.
252 + const uploadResponse = await ssrfSafeFetch(signedURL, {
253 method: 'PUT',
254 body: uploadBuffer,
255 signal,
@@ -348,7 +351,10 @@ export const downloadFileFromS3 = async ({
351 fileDownloadDir?: string;
352 signal?: AbortSignal;
353 }): Promise<FileDownloadData> => {
351 - const response = await fetch(s3Url, { signal });
354 + // SSRF guard: `s3Url` is a field of the tool-execution response, so it is no
355 + // more trusted than a user-supplied URL — and the bytes it returns are
356 + // written to disk. See ssrfGuard.node.ts.
357 + const response = await ssrfSafeFetch(s3Url, { signal });
358 if (!response.ok) {
359 throw new Error(`Failed to download file: ${response.statusText}`);
360 }
ts/packages/core/src/utils/ssrfGuard.node.ts
+11
@@ -204,3 +204,14 @@ export const ssrfSafeFetch = async (
204 { url: rawUrl }
205 );
206 };
207 +
208 +/**
209 + * {@link ssrfSafeFetch} for call sites that must keep working on every runtime.
210 + *
211 + * `ssrfSafeFetch` fails closed in edge runtimes, which is right for a URL the
212 + * caller chose to upload — the alternative is fetching it unvalidated — but not
213 + * for Tool Router session file transfers, where refusing would remove a working
214 + * feature from Workers rather than close a hole reachable there. On Node this
215 + * is the full guard; only the edge build differs.
216 + */
217 +export const ssrfSafeFetchWhereSupported = ssrfSafeFetch;
ts/packages/core/src/utils/ssrfGuard.workerd.ts
+16
@@ -17,3 +17,19 @@ export const ssrfSafeFetch = async (rawUrl: string): Promise<Response> => {
17 }
18 );
19 };
20 +
21 +/**
22 + * Unguarded `fetch`, for the call sites that must keep working here.
23 + *
24 + * Failing closed is the right default for a URL the caller chose to upload, but
25 + * applying it to Tool Router session file transfers would take a working
26 + * feature away from Workers instead of closing a hole reachable there. The
27 + * guard exists to stop a URL from reaching private address space the SDK's host
28 + * can see; an edge worker has no such vantage point — it cannot resolve DNS to
29 + * check, and its `fetch` does not originate inside the caller's network. The
30 + * Node build applies the full guard.
31 + */
32 +export const ssrfSafeFetchWhereSupported = async (
33 + rawUrl: string,
34 + init: RequestInit = {}
35 +): Promise<Response> => fetch(rawUrl, init);
ts/packages/core/test/models/RemoteFile.test.ts
+63 -2
@@ -1,6 +1,22 @@
1 import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2 import { RemoteFile } from '../../src/models/RemoteFile';
3 -import { RemoteFileDownloadError, ValidationError } from '../../src/errors';
3 +import {
4 + ComposioBlockedInternalUrlError,
5 + RemoteFileDownloadError,
6 + ValidationError,
7 +} from '../../src/errors';
8 +
9 +// `downloadUrl` comes from an API response, so downloads run through the SSRF
10 +// guard, which resolves the host before connecting. Resolve as public by
11 +// default; the guard's own tests cover the blocking behavior.
12 +vi.mock('node:dns/promises', () => ({
13 + lookup: vi.fn(),
14 +}));
15 +
16 +// eslint-disable-next-line no-restricted-imports
17 +import { lookup } from 'node:dns/promises';
18 +
19 +const mockLookup = vi.mocked(lookup);
20
21 describe('RemoteFile', () => {
22 const validCamelCaseData = {
@@ -21,6 +37,8 @@ describe('RemoteFile', () => {
37
38 beforeEach(() => {
39 originalFetch = globalThis.fetch;
40 + mockLookup.mockReset();
41 + mockLookup.mockResolvedValue([{ address: '93.184.216.34', family: 4 }] as never);
42 });
43
44 afterEach(() => {
@@ -79,7 +97,11 @@ describe('RemoteFile', () => {
97 const file = new RemoteFile(validCamelCaseData);
98 const result = await file.buffer();
99
82 - expect(globalThis.fetch).toHaveBeenCalledWith(validCamelCaseData.downloadUrl);
100 + // `redirect: 'manual'` is the guard following redirects itself so it can
101 + // re-validate each hop, rather than letting fetch follow them unchecked.
102 + expect(globalThis.fetch).toHaveBeenCalledWith(validCamelCaseData.downloadUrl, {
103 + redirect: 'manual',
104 + });
105 expect(result).toBeInstanceOf(Uint8Array);
106 expect(result).toEqual(content);
107 });
@@ -143,6 +165,45 @@ describe('RemoteFile', () => {
165 });
166 });
167
168 + describe('SSRF guard', () => {
169 + // `downloadUrl` is set from an API response. Under the SDK's trust boundary
170 + // that is untrusted input, so a response naming an internal address must
171 + // not be fetched — the bytes go straight back to the caller, typically into
172 + // an LLM context.
173 + it.each([
174 + ['loopback', 'http://127.0.0.1/secret', '127.0.0.1'],
175 + ['cloud metadata', 'http://169.254.169.254/latest/meta-data/', '169.254.169.254'],
176 + ])('should block a %s downloadUrl before fetching', async (_label, downloadUrl, address) => {
177 + mockLookup.mockResolvedValue([{ address, family: 4 }] as never);
178 + globalThis.fetch = vi.fn();
179 +
180 + const file = new RemoteFile({ ...validCamelCaseData, downloadUrl });
181 +
182 + await expect(file.buffer()).rejects.toBeInstanceOf(ComposioBlockedInternalUrlError);
183 + await expect(file.blob()).rejects.toBeInstanceOf(ComposioBlockedInternalUrlError);
184 + await expect(file.text()).rejects.toBeInstanceOf(ComposioBlockedInternalUrlError);
185 + expect(globalThis.fetch).not.toHaveBeenCalled();
186 + });
187 +
188 + it('should block a downloadUrl that redirects into private space', async () => {
189 + mockLookup
190 + .mockResolvedValueOnce([{ address: '93.184.216.34', family: 4 }] as never)
191 + .mockResolvedValueOnce([{ address: '169.254.169.254', family: 4 }] as never);
192 + globalThis.fetch = vi.fn().mockResolvedValue(
193 + new Response(null, {
194 + status: 302,
195 + headers: { location: 'http://169.254.169.254/latest/meta-data/' },
196 + })
197 + );
198 +
199 + const file = new RemoteFile(validCamelCaseData);
200 +
201 + await expect(file.buffer()).rejects.toBeInstanceOf(ComposioBlockedInternalUrlError);
202 + // The first hop was fetched; the redirect target never was.
203 + expect(globalThis.fetch).toHaveBeenCalledTimes(1);
204 + });
205 + });
206 +
207 describe('save', () => {
208 it('should save file to specified path when platform supports file system', async () => {
209 const content = new Uint8Array([1, 2, 3]);
ts/packages/core/test/models/ToolRouterSessionFilesMount.test.ts
+22
@@ -35,6 +35,10 @@ describe('ToolRouterSessionFilesMount', () => {
35 beforeEach(() => {
36 vi.clearAllMocks();
37 mockLookup.mockReset();
38 + // Uploads PUT to a response-supplied `upload_url`, which is now validated
39 + // like any other fetch target; resolve test hosts as public by default so
40 + // only the tests that care about the guard have to say anything about DNS.
41 + mockLookup.mockResolvedValue([{ address: '93.184.216.34', family: 4 }] as never);
42 mockClient = createMockClient();
43 filesMount = new ToolRouterSessionFilesMount(mockClient, sessionId);
44 });
@@ -366,6 +370,24 @@ describe('ToolRouterSessionFilesMount', () => {
370 expect(result.downloadUrl).toBe('https://s3.example.com/presigned');
371 });
372
373 + it('should block a response-supplied upload_url that resolves internally', async () => {
374 + // The user-supplied URL branch above was already guarded; `upload_url`
375 + // comes back from the API and was not. Both are untrusted here, and this
376 + // one receives the file's bytes.
377 + mockClient.toolRouter.session.files.createUploadURL.mockResolvedValueOnce({
378 + ...createUploadURLResponse,
379 + upload_url: 'http://metadata.example/upload',
380 + });
381 + mockLookup.mockResolvedValue([{ address: '169.254.169.254', family: 4 }] as never);
382 +
383 + await expect(
384 + filesMount.upload(new File(['data'], 'report.txt', { type: 'text/plain' }))
385 + ).rejects.toBeInstanceOf(ComposioBlockedInternalUrlError);
386 +
387 + expect(fetch).not.toHaveBeenCalled();
388 + expect(mockClient.toolRouter.session.files.createDownloadURL).not.toHaveBeenCalled();
389 + });
390 +
391 it('should handle upload response body wrapper', async () => {
392 const { platform } = await import('../../src/platform/node');
393 const { join } = await import('node:path');
ts/packages/core/test/utils/fileUtils.test.ts
+45
@@ -49,6 +49,12 @@ vi.mock('node:dns/promises', () => ({
49 lookup: vi.fn().mockResolvedValue([{ address: '93.184.216.34', family: 4 }]),
50 }));
51
52 +// eslint-disable-next-line no-restricted-imports
53 +import { lookup } from 'node:dns/promises';
54 +import { ComposioBlockedInternalUrlError } from '../../src/errors';
55 +
56 +const mockLookup = vi.mocked(lookup);
57 +
58 // Mock global fetch
59 const mockFetch = vi.fn();
60 global.fetch = mockFetch;
@@ -515,4 +521,43 @@ describe('fileUtils', () => {
521 ).rejects.toThrow('Invalid file type');
522 });
523 });
524 + describe('SSRF guard on response-supplied URLs', () => {
525 + // The S3 URLs below are API response fields, not caller input. The SDK
526 + // treats a response as untrusted, so both directions are validated: a
527 + // download would otherwise fetch an internal address and write the bytes
528 + // to disk, and an upload would PUT the user's file to one.
529 + it('should block a download whose s3Url resolves internally', async () => {
530 + mockLookup.mockResolvedValueOnce([{ address: '169.254.169.254', family: 4 }] as never);
531 +
532 + await expect(
533 + downloadFileFromS3({
534 + toolSlug: 'github',
535 + s3Url: 'http://metadata.example/latest/meta-data/',
536 + mimeType: 'text/plain',
537 + })
538 + ).rejects.toBeInstanceOf(ComposioBlockedInternalUrlError);
539 +
540 + expect(mockFetch).not.toHaveBeenCalled();
541 + });
542 +
543 + it('should block an upload whose presigned URL resolves internally', async () => {
544 + vi.mocked(mockClient.files.createPresignedURL).mockResolvedValue({
545 + key: 'test-key',
546 + type: 'new',
547 + new_presigned_url: 'http://127.0.0.1:9000/upload',
548 + } as never);
549 + // The file content is read first, then the presigned URL is validated.
550 + mockLookup.mockResolvedValueOnce([{ address: '127.0.0.1', family: 4 }] as never);
551 +
552 + await expect(
553 + getFileDataAfterUploadingToS3(new File(['data'], 'report.txt', { type: 'text/plain' }), {
554 + toolSlug: 'test-tool',
555 + toolkitSlug: 'test-toolkit',
556 + client: mockClient,
557 + })
558 + ).rejects.toBeInstanceOf(ComposioBlockedInternalUrlError);
559 +
560 + expect(mockFetch).not.toHaveBeenCalled();
561 + });
562 + });
563 });
ts/packages/core/test/utils/ssrfGuard.workerd.test.ts
+17 -1
@@ -1,6 +1,6 @@
1 import { afterEach, describe, expect, it, vi } from 'vitest';
2 import { ComposioBlockedInternalUrlError } from '../../src/errors/SsrfErrors';
3 -import { ssrfSafeFetch } from '../../src/utils/ssrfGuard.workerd';
3 +import { ssrfSafeFetch, ssrfSafeFetchWhereSupported } from '../../src/utils/ssrfGuard.workerd';
4
5 describe('ssrfSafeFetch (workerd)', () => {
6 afterEach(() => vi.unstubAllGlobals());
@@ -14,4 +14,20 @@ describe('ssrfSafeFetch (workerd)', () => {
14 );
15 expect(fetchMock).not.toHaveBeenCalled();
16 });
17 +
18 + it('lets session file transfers through, since the guard cannot run here', async () => {
19 + // Failing closed would remove a working feature from Workers rather than
20 + // close a hole reachable there: an edge worker has no DNS API to validate
21 + // with, and its fetch does not originate inside the caller's network.
22 + const response = new Response('ok');
23 + const fetchMock = vi.fn().mockResolvedValue(response);
24 + vi.stubGlobal('fetch', fetchMock);
25 +
26 + await expect(
27 + ssrfSafeFetchWhereSupported('https://s3.example.com/presigned', { method: 'PUT' })
28 + ).resolves.toBe(response);
29 + expect(fetchMock).toHaveBeenCalledWith('https://s3.example.com/presigned', {
30 + method: 'PUT',
31 + });
32 + });
33 });