@devries-cameron20 / composio-1 / commits / dd40a9bb6
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): bound file transfer requests with timeouts (#3563)

## Summary - add bounded `(connect, read)` timeouts to Python SDK session file downloads and uploads - add the same timeout coverage to presigned S3 upload/download paths used by file helpers - convert timeout/request failures into the SDK's existing file errors and cover them with tests Fixes #3560 Fixes #3561 Fixes #3562 ## Changes - `RemoteFile.buffer()` now uses the existing file transfer timeout constants and preserves file context on request failures - `ToolRouterSessionFilesMount.upload()` and S3 upload helpers now bound presigned `PUT` requests - `FileDownloadable.download()` now bounds streaming `GET` requests, wraps streaming read failures, and closes the response - added regression tests for request timeouts and timeout arguments - added a changeset for the published SDK change ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactor/Chore - [ ] Documentation - [ ] Breaking change ## How Has This Been Tested? - [x] `D:\anaconda3\envs\yolov12\python.exe -m ruff check python\composio\core\models\tool_router_session_files.py python\composio\core\models\_files.py python\tests\test_tool_router_session_files.py python\tests\test_files.py` - [x] `D:\anaconda3\envs\yolov12\python.exe -m ruff format --check python\composio\core\models\tool_router_session_files.py python\composio\core\models\_files.py python\tests\test_tool_router_session_files.py python\tests\test_files.py` - [x] `COMPOSIO_CACHE_DIR=<repo>\.tmp-composio-cache D:\anaconda3\envs\yolov12\python.exe -m pytest python\tests\test_tool_router_session_files.py python\tests\test_files.py::TestUploadBytesToS3 python\tests\test_files.py::TestFileDownloadablePathTraversal::test_safe_filename_passes_through python\tests\test_files.py::TestFileDownloadablePathTraversal::test_download_uses_timeout python\tests\test_files.py::TestFileDownloadablePathTraversal::test_download_timeout_raises_error python\tests\test_files.py::TestFileDownloadablePathTraversal::test_download_stream_timeout_raises_error` Note: I also ran the full `python\tests\test_tool_router_session_files.py python\tests\test_files.py` pair locally. The new and related tests passed, but the full run has one pre-existing Windows-specific failure in `TestFileDownloadablePathTraversal::test_empty_name_safe_fails_at_write_time`: opening a directory for writing raises `PermissionError` on Windows instead of the test's expected `IsADirectoryError`. ## Screenshots (if applicable) N/A ## Checklist - [x] I have read the Code of Conduct and this PR adheres to it - [x] I ran linters/tests locally and they passed - [ ] I updated documentation as needed - [x] I added tests or explain why not applicable - [x] I added a changeset if this change affects published packages ## Additional context This keeps the existing `(5s connect, 60s read)` timeout convention already used by the Python SDK's URL fetch helpers. --------- Co-authored-by: jkomyno <alberto@composio.dev>

Xiaoju committed Jun 25, 2026 at 03:00 UTC dd40a9bb6f163c76dfcedec8dc4fa0b44fc118fe
4 files changed +234 -19
python/composio/core/models/_files.py
+55 -11
@@ -1,6 +1,7 @@
1 from __future__ import annotations
2
3 import hashlib
4 +import logging
5 import os
6 import typing as t
7 from pathlib import Path
@@ -50,6 +51,8 @@ Maximum response size in bytes when fetching files from URLs.
51 Prevents memory exhaustion attacks from malicious URLs pointing to large files.
52 """
53
54 +_logger = logging.getLogger(__name__)
55 +
56 _CONNECT_TIMEOUT = 5 # seconds
57 _READ_TIMEOUT = 60 # seconds
58 """
@@ -145,7 +148,19 @@ def upload(url: str, file: Path) -> bool:
148 True if upload succeeded (HTTP 200), False otherwise
149 """
150 with file.open("rb") as data:
148 - response = requests.put(url=url, data=data)
151 + try:
152 + response = requests.put(
153 + url=url,
154 + data=data,
155 + timeout=(_CONNECT_TIMEOUT, _READ_TIMEOUT),
156 + )
157 + except requests.exceptions.RequestException as e:
158 + _logger.debug(
159 + "Upload to %s failed: %s",
160 + _sanitize_url_for_logging(url),
161 + type(e).__name__,
162 + )
163 + return False
164 return response.status_code == 200
165
166
@@ -378,11 +393,19 @@ def _upload_bytes_to_s3(
393 )
394
395 # Upload the content directly to S3
381 - upload_response = requests.put(
382 - url=s3meta.new_presigned_url,
383 - data=content,
384 - headers={"Content-Type": mimetype},
385 - )
396 + try:
397 + upload_response = requests.put(
398 + url=s3meta.new_presigned_url,
399 + data=content,
400 + headers={"Content-Type": mimetype},
401 + timeout=(_CONNECT_TIMEOUT, _READ_TIMEOUT),
402 + )
403 + except requests.exceptions.RequestException as e:
404 + raise ErrorUploadingFile(
405 + "Failed to upload to S3: "
406 + f"{_sanitize_url_for_logging(s3meta.new_presigned_url)}. "
407 + f"Error: {type(e).__name__}"
408 + ) from e
409
410 if upload_response.status_code != 200:
411 raise ErrorUploadingFile(
@@ -567,13 +590,34 @@ class FileDownloadable(BaseModel):
590 "outside the intended output directory."
591 )
592 outdir.mkdir(exist_ok=True, parents=True)
570 - response = requests.get(url=self.s3url, stream=True)
593 + try:
594 + response = requests.get(
595 + url=self.s3url,
596 + stream=True,
597 + timeout=(_CONNECT_TIMEOUT, _READ_TIMEOUT),
598 + )
599 + except requests.exceptions.RequestException as e:
600 + raise ErrorDownloadingFile(
601 + "Error downloading file: "
602 + f"{_sanitize_url_for_logging(self.s3url)}. Error: {type(e).__name__}"
603 + ) from e
604 if response.status_code != 200:
572 - raise ErrorDownloadingFile(f"Error downloading file: {self.s3url}")
605 + response.close()
606 + raise ErrorDownloadingFile(
607 + f"Error downloading file: {_sanitize_url_for_logging(self.s3url)}"
608 + )
609
574 - with outfile.open("wb") as fd:
575 - for chunk in response.iter_content(chunk_size=chunk_size):
576 - fd.write(chunk)
610 + try:
611 + with outfile.open("wb") as fd:
612 + for chunk in response.iter_content(chunk_size=chunk_size):
613 + fd.write(chunk)
614 + except requests.exceptions.RequestException as e:
615 + raise ErrorDownloadingFile(
616 + "Error downloading file: "
617 + f"{_sanitize_url_for_logging(self.s3url)}. Error: {type(e).__name__}"
618 + ) from e
619 + finally:
620 + response.close()
621 return outfile
622
623
python/composio/core/models/tool_router_session_files.py
+21 -6
@@ -118,7 +118,18 @@ class RemoteFile:
118
119 def buffer(self) -> bytes:
120 """Fetch the file content as bytes."""
121 - response = requests.get(self.download_url)
121 + try:
122 + response = requests.get(
123 + self.download_url,
124 + timeout=(_CONNECT_TIMEOUT, _READ_TIMEOUT),
125 + )
126 + except requests.exceptions.RequestException as e:
127 + raise RemoteFileDownloadError(
128 + f"Failed to download file: {type(e).__name__}",
129 + download_url=self.download_url,
130 + mount_relative_path=self.mount_relative_path,
131 + filename=self.filename,
132 + ) from e
133
134 if not response.ok:
135 raise RemoteFileDownloadError(
@@ -287,11 +298,15 @@ class ToolRouterSessionFilesMount:
298 mimetype=mime,
299 )
300
290 - upload_resp = requests.put(
291 - create_resp.upload_url,
292 - data=content,
293 - headers={"Content-Type": mime},
294 - )
301 + try:
302 + upload_resp = requests.put(
303 + create_resp.upload_url,
304 + data=content,
305 + headers={"Content-Type": mime},
306 + timeout=(_CONNECT_TIMEOUT, _READ_TIMEOUT),
307 + )
308 + except requests.exceptions.RequestException as e:
309 + raise ValidationError(f"Failed to upload file: {type(e).__name__}") from e
310
311 if not upload_resp.ok:
312 raise ValidationError(
python/tests/test_files.py
+122 -2
@@ -7,6 +7,7 @@ that use anyOf, oneOf, allOf, or $ref instead of direct 'type' properties.
7 from unittest.mock import Mock, patch, MagicMock
8
9 import pytest
10 +import requests
11
12 from composio.client.types import Tool, tool_list_response
13 from composio.core.models._files import (
@@ -23,7 +24,11 @@ from composio.core.models._files import (
24 _MAX_FILENAME_LENGTH,
25 )
26 from composio.core.models.base import allow_tracking
26 -from composio.exceptions import ErrorUploadingFile, ResponseTooLargeError
27 +from composio.exceptions import (
28 + ErrorDownloadingFile,
29 + ErrorUploadingFile,
30 + ResponseTooLargeError,
31 +)
32
33
34 @pytest.fixture(autouse=True)
@@ -1518,7 +1523,12 @@ class TestUploadBytesToS3:
1523
1524 assert result == "s3-key-123"
1525 mock_client.post.assert_called_once()
1521 - mock_put.assert_called_once()
1526 + mock_put.assert_called_once_with(
1527 + url="https://s3.example.com/upload",
1528 + data=b"file content",
1529 + headers={"Content-Type": "image/jpeg"},
1530 + timeout=(5, 60),
1531 + )
1532
1533 @patch("composio.core.models._files.requests.put")
1534 def test_upload_bytes_to_s3_failure(self, mock_put):
@@ -1545,6 +1555,34 @@ class TestUploadBytesToS3:
1555
1556 assert "Failed to upload to S3" in str(exc_info.value)
1557
1558 + @patch("composio.core.models._files.requests.put")
1559 + def test_upload_bytes_to_s3_timeout(self, mock_put):
1560 + """Test request timeouts are reported as upload errors."""
1561 + mock_client = MagicMock()
1562 + mock_s3_response = MagicMock()
1563 + mock_s3_response.key = "s3-key-123"
1564 + mock_s3_response.new_presigned_url = "https://s3.example.com/upload?token=abc"
1565 + mock_client.post.return_value = mock_s3_response
1566 + # The exception text itself carries the presigned URL (incl. token), as
1567 + # real urllib3 errors do — the SDK must not surface it in the message.
1568 + mock_put.side_effect = requests.exceptions.Timeout(
1569 + "HTTPSConnectionPool(host='s3.example.com', port=443): "
1570 + "Max retries exceeded with url: /upload?token=abc"
1571 + )
1572 +
1573 + with pytest.raises(ErrorUploadingFile) as exc_info:
1574 + _upload_bytes_to_s3(
1575 + client=mock_client,
1576 + filename="test.jpg",
1577 + content=b"file content",
1578 + mimetype="image/jpeg",
1579 + tool="TEST_TOOL",
1580 + toolkit="test_toolkit",
1581 + )
1582 +
1583 + assert "Failed to upload to S3" in str(exc_info.value)
1584 + assert "token=abc" not in str(exc_info.value)
1585 +
1586
1587 class TestFileUploadableFromUrl:
1588 """Test cases for FileUploadable.from_url and from_path with URLs."""
@@ -2285,6 +2323,7 @@ class TestFileDownloadablePathTraversal:
2323 response = MagicMock()
2324 response.status_code = 200
2325 response.iter_content = lambda chunk_size: [content]
2326 + response.close = MagicMock()
2327 return response
2328
2329 def test_relative_traversal_is_neutralized(self, tmp_path):
@@ -2360,6 +2399,87 @@ class TestFileDownloadablePathTraversal:
2399 assert written == outdir / "report.pdf"
2400 assert written.read_bytes() == b"%PDF-1.4"
2401
2402 + def test_download_uses_timeout(self, tmp_path):
2403 + outdir = tmp_path / "safe"
2404 + f = FileDownloadable(
2405 + name="report.pdf",
2406 + mimetype="application/pdf",
2407 + s3url="https://example.com/file",
2408 + )
2409 + with patch(
2410 + "composio.core.models._files.requests.get",
2411 + return_value=self._mock_response(b"%PDF-1.4"),
2412 + ) as mock_get:
2413 + f.download(outdir)
2414 +
2415 + mock_get.assert_called_once_with(
2416 + url="https://example.com/file",
2417 + stream=True,
2418 + timeout=(5, 60),
2419 + )
2420 +
2421 + def test_download_timeout_raises_error(self, tmp_path):
2422 + outdir = tmp_path / "safe"
2423 + f = FileDownloadable(
2424 + name="report.pdf",
2425 + mimetype="application/pdf",
2426 + s3url="https://example.com/file?token=abc",
2427 + )
2428 + with patch(
2429 + "composio.core.models._files.requests.get",
2430 + side_effect=requests.exceptions.Timeout(
2431 + "Max retries exceeded with url: /file?token=abc"
2432 + ),
2433 + ):
2434 + with pytest.raises(ErrorDownloadingFile) as exc_info:
2435 + f.download(outdir)
2436 +
2437 + assert "Error downloading file" in str(exc_info.value)
2438 + assert "token=abc" not in str(exc_info.value)
2439 +
2440 + def test_download_stream_timeout_raises_error(self, tmp_path):
2441 + outdir = tmp_path / "safe"
2442 + response = self._mock_response()
2443 + response.iter_content = MagicMock(
2444 + side_effect=requests.exceptions.ReadTimeout(
2445 + "Max retries exceeded with url: /file?token=abc"
2446 + )
2447 + )
2448 + f = FileDownloadable(
2449 + name="report.pdf",
2450 + mimetype="application/pdf",
2451 + s3url="https://example.com/file?token=abc",
2452 + )
2453 + with patch(
2454 + "composio.core.models._files.requests.get",
2455 + return_value=response,
2456 + ):
2457 + with pytest.raises(ErrorDownloadingFile) as exc_info:
2458 + f.download(outdir)
2459 +
2460 + assert "Error downloading file" in str(exc_info.value)
2461 + assert "token=abc" not in str(exc_info.value)
2462 + response.close.assert_called_once()
2463 +
2464 + def test_download_non_200_redacts_url_and_closes(self, tmp_path):
2465 + outdir = tmp_path / "safe"
2466 + response = self._mock_response()
2467 + response.status_code = 403
2468 + f = FileDownloadable(
2469 + name="report.pdf",
2470 + mimetype="application/pdf",
2471 + s3url="https://example.com/file?token=abc",
2472 + )
2473 + with patch(
2474 + "composio.core.models._files.requests.get",
2475 + return_value=response,
2476 + ):
2477 + with pytest.raises(ErrorDownloadingFile) as exc_info:
2478 + f.download(outdir)
2479 +
2480 + assert "token=abc" not in str(exc_info.value)
2481 + response.close.assert_called_once()
2482 +
2483 def test_basename_collapse_through_subdir_is_rejected(self, tmp_path):
2484 """`Path('foo/..').name == '..'` — the basename strip of a name that
2485 traverses *through* a subdir collapses to `..`, then the
python/tests/test_tool_router_session_files.py
+36
@@ -4,6 +4,7 @@ from pathlib import Path
4 from unittest.mock import MagicMock, patch
5
6 import pytest
7 +import requests
8
9 from composio.core.models.tool_router_session_files import (
10 RemoteFile,
@@ -101,9 +102,25 @@ class TestToolRouterSessionFilesMount:
102
103 assert isinstance(result, RemoteFile)
104 assert result.mount_relative_path == "output/test.txt"
105 + mock_put.assert_called_once_with(
106 + "https://s3.example.com/upload",
107 + data=b"hello world",
108 + headers={"Content-Type": "text/plain"},
109 + timeout=(5, 60),
110 + )
111 mock_client.tool_router.session.files.create_upload_url.assert_called_once()
112 mock_client.tool_router.session.files.create_download_url.assert_called_once()
113
114 + def test_upload_raises_validation_error_on_timeout(self, files_mount):
115 + """Test upload converts request timeouts to ValidationError."""
116 + with patch("requests.put", side_effect=requests.exceptions.Timeout("timeout")):
117 + with pytest.raises(ValidationError, match="Failed to upload file"):
118 + files_mount.upload(
119 + b"hello world",
120 + remote_path="data.txt",
121 + mimetype="text/plain",
122 + )
123 +
124 def test_upload_from_local_file(self, files_mount, mock_client, tmp_path):
125 """Test upload from local file path."""
126 test_file = tmp_path / "report.pdf"
@@ -174,6 +191,10 @@ class TestRemoteFile:
191
192 result = rf.buffer()
193 assert result == b"file content"
194 + mock_get.assert_called_once_with(
195 + "https://example.com/file",
196 + timeout=(5, 60),
197 + )
198
199 def test_buffer_failure_raises_remote_file_download_error(self):
200 """Test buffer() raises RemoteFileDownloadError on HTTP error."""
@@ -194,6 +215,21 @@ class TestRemoteFile:
215 assert exc_info.value.status_code == 404
216 assert exc_info.value.filename == "test.txt"
217
218 + def test_buffer_timeout_raises_remote_file_download_error(self):
219 + """Test buffer() converts request timeouts to RemoteFileDownloadError."""
220 + rf = RemoteFile(
221 + expires_at="2026-01-01",
222 + mount_relative_path="test.txt",
223 + sandbox_mount_prefix="/mnt/files",
224 + download_url="https://example.com/file",
225 + )
226 + with patch("requests.get", side_effect=requests.exceptions.Timeout("timeout")):
227 + with pytest.raises(RemoteFileDownloadError) as exc_info:
228 + rf.buffer()
229 +
230 + assert exc_info.value.filename == "test.txt"
231 + assert exc_info.value.download_url == "https://example.com/file"
232 +
233 def test_text(self):
234 """Test text() decodes UTF-8."""
235 rf = RemoteFile(