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): bound file transfer requests with timeouts (#3563)
Xiaojudd40a9bb6f16
diff --git a/python/composio/core/models/_files.py b/python/composio/core/models/_files.pyindex 96ca58574..57f1d0ae7 100644--- a/python/composio/core/models/_files.py+++ b/python/composio/core/models/_files.py@@ -1,6 +1,7 @@from __future__ import annotationsimport hashlib+import loggingimport osimport typing as tfrom pathlib import Path@@ -50,6 +51,8 @@ Maximum response size in bytes when fetching files from URLs.Prevents memory exhaustion attacks from malicious URLs pointing to large files."""+_logger = logging.getLogger(__name__)+_CONNECT_TIMEOUT = 5 # seconds_READ_TIMEOUT = 60 # seconds"""@@ -145,7 +148,19 @@ def upload(url: str, file: Path) -> bool:True if upload succeeded (HTTP 200), False otherwise"""with file.open("rb") as data:- response = requests.put(url=url, data=data)+ try:+ response = requests.put(+ url=url,+ data=data,+ timeout=(_CONNECT_TIMEOUT, _READ_TIMEOUT),+ )+ except requests.exceptions.RequestException as e:+ _logger.debug(+ "Upload to %s failed: %s",+ _sanitize_url_for_logging(url),+ type(e).__name__,+ )+ return Falsereturn response.status_code == 200@@ -378,11 +393,19 @@ def _upload_bytes_to_s3()# Upload the content directly to S3- upload_response = requests.put(- url=s3meta.new_presigned_url,- data=content,- headers={"Content-Type": mimetype},- )+ try:+ upload_response = requests.put(+ url=s3meta.new_presigned_url,+ data=content,+ headers={"Content-Type": mimetype},+ timeout=(_CONNECT_TIMEOUT, _READ_TIMEOUT),+ )+ except requests.exceptions.RequestException as e:+ raise ErrorUploadingFile(+ "Failed to upload to S3: "+ f"{_sanitize_url_for_logging(s3meta.new_presigned_url)}. "+ f"Error: {type(e).__name__}"+ ) from eif upload_response.status_code != 200:raise ErrorUploadingFile(@@ -567,13 +590,34 @@ class FileDownloadable(BaseModel):"outside the intended output directory.")outdir.mkdir(exist_ok=True, parents=True)- response = requests.get(url=self.s3url, stream=True)+ try:+ response = requests.get(+ url=self.s3url,+ stream=True,+ timeout=(_CONNECT_TIMEOUT, _READ_TIMEOUT),+ )+ except requests.exceptions.RequestException as e:+ raise ErrorDownloadingFile(+ "Error downloading file: "+ f"{_sanitize_url_for_logging(self.s3url)}. Error: {type(e).__name__}"+ ) from eif response.status_code != 200:- raise ErrorDownloadingFile(f"Error downloading file: {self.s3url}")+ response.close()+ raise ErrorDownloadingFile(+ f"Error downloading file: {_sanitize_url_for_logging(self.s3url)}"+ )- with outfile.open("wb") as fd:- for chunk in response.iter_content(chunk_size=chunk_size):- fd.write(chunk)+ try:+ with outfile.open("wb") as fd:+ for chunk in response.iter_content(chunk_size=chunk_size):+ fd.write(chunk)+ except requests.exceptions.RequestException as e:+ raise ErrorDownloadingFile(+ "Error downloading file: "+ f"{_sanitize_url_for_logging(self.s3url)}. Error: {type(e).__name__}"+ ) from e+ finally:+ response.close()return outfilediff --git a/python/composio/core/models/tool_router_session_files.py b/python/composio/core/models/tool_router_session_files.pyindex 0d9f3c584..2558f029d 100644--- a/python/composio/core/models/tool_router_session_files.py+++ b/python/composio/core/models/tool_router_session_files.py@@ -118,7 +118,18 @@ class RemoteFile:def buffer(self) -> bytes:"""Fetch the file content as bytes."""- response = requests.get(self.download_url)+ try:+ response = requests.get(+ self.download_url,+ timeout=(_CONNECT_TIMEOUT, _READ_TIMEOUT),+ )+ except requests.exceptions.RequestException as e:+ raise RemoteFileDownloadError(+ f"Failed to download file: {type(e).__name__}",+ download_url=self.download_url,+ mount_relative_path=self.mount_relative_path,+ filename=self.filename,+ ) from eif not response.ok:raise RemoteFileDownloadError(@@ -287,11 +298,15 @@ class ToolRouterSessionFilesMount:mimetype=mime,)- upload_resp = requests.put(- create_resp.upload_url,- data=content,- headers={"Content-Type": mime},- )+ try:+ upload_resp = requests.put(+ create_resp.upload_url,+ data=content,+ headers={"Content-Type": mime},+ timeout=(_CONNECT_TIMEOUT, _READ_TIMEOUT),+ )+ except requests.exceptions.RequestException as e:+ raise ValidationError(f"Failed to upload file: {type(e).__name__}") from eif not upload_resp.ok:raise ValidationError(diff --git a/python/tests/test_files.py b/python/tests/test_files.pyindex 80297035e..15b399b47 100644--- a/python/tests/test_files.py+++ b/python/tests/test_files.py@@ -7,6 +7,7 @@ that use anyOf, oneOf, allOf, or $ref instead of direct 'type' properties.from unittest.mock import Mock, patch, MagicMockimport pytest+import requestsfrom composio.client.types import Tool, tool_list_responsefrom composio.core.models._files import (@@ -23,7 +24,11 @@ from composio.core.models._files import (_MAX_FILENAME_LENGTH,)from composio.core.models.base import allow_tracking-from composio.exceptions import ErrorUploadingFile, ResponseTooLargeError+from composio.exceptions import (+ ErrorDownloadingFile,+ ErrorUploadingFile,+ ResponseTooLargeError,+)@pytest.fixture(autouse=True)@@ -1518,7 +1523,12 @@ class TestUploadBytesToS3:assert result == "s3-key-123"mock_client.post.assert_called_once()- mock_put.assert_called_once()+ mock_put.assert_called_once_with(+ url="https://s3.example.com/upload",+ data=b"file content",+ headers={"Content-Type": "image/jpeg"},+ timeout=(5, 60),+ )@patch("composio.core.models._files.requests.put")def test_upload_bytes_to_s3_failure(self, mock_put):@@ -1545,6 +1555,34 @@ class TestUploadBytesToS3:assert "Failed to upload to S3" in str(exc_info.value)+ @patch("composio.core.models._files.requests.put")+ def test_upload_bytes_to_s3_timeout(self, mock_put):+ """Test request timeouts are reported as upload errors."""+ mock_client = MagicMock()+ mock_s3_response = MagicMock()+ mock_s3_response.key = "s3-key-123"+ mock_s3_response.new_presigned_url = "https://s3.example.com/upload?token=abc"+ mock_client.post.return_value = mock_s3_response+ # The exception text itself carries the presigned URL (incl. token), as+ # real urllib3 errors do — the SDK must not surface it in the message.+ mock_put.side_effect = requests.exceptions.Timeout(+ "HTTPSConnectionPool(host='s3.example.com', port=443): "+ "Max retries exceeded with url: /upload?token=abc"+ )++ with pytest.raises(ErrorUploadingFile) as exc_info:+ _upload_bytes_to_s3(+ client=mock_client,+ filename="test.jpg",+ content=b"file content",+ mimetype="image/jpeg",+ tool="TEST_TOOL",+ toolkit="test_toolkit",+ )++ assert "Failed to upload to S3" in str(exc_info.value)+ assert "token=abc" not in str(exc_info.value)+class TestFileUploadableFromUrl:"""Test cases for FileUploadable.from_url and from_path with URLs."""@@ -2285,6 +2323,7 @@ class TestFileDownloadablePathTraversal:response = MagicMock()response.status_code = 200response.iter_content = lambda chunk_size: [content]+ response.close = MagicMock()return responsedef test_relative_traversal_is_neutralized(self, tmp_path):@@ -2360,6 +2399,87 @@ class TestFileDownloadablePathTraversal:assert written == outdir / "report.pdf"assert written.read_bytes() == b"%PDF-1.4"+ def test_download_uses_timeout(self, tmp_path):+ outdir = tmp_path / "safe"+ f = FileDownloadable(+ name="report.pdf",+ mimetype="application/pdf",+ s3url="https://example.com/file",+ )+ with patch(+ "composio.core.models._files.requests.get",+ return_value=self._mock_response(b"%PDF-1.4"),+ ) as mock_get:+ f.download(outdir)++ mock_get.assert_called_once_with(+ url="https://example.com/file",+ stream=True,+ timeout=(5, 60),+ )++ def test_download_timeout_raises_error(self, tmp_path):+ outdir = tmp_path / "safe"+ f = FileDownloadable(+ name="report.pdf",+ mimetype="application/pdf",+ s3url="https://example.com/file?token=abc",+ )+ with patch(+ "composio.core.models._files.requests.get",+ side_effect=requests.exceptions.Timeout(+ "Max retries exceeded with url: /file?token=abc"+ ),+ ):+ with pytest.raises(ErrorDownloadingFile) as exc_info:+ f.download(outdir)++ assert "Error downloading file" in str(exc_info.value)+ assert "token=abc" not in str(exc_info.value)++ def test_download_stream_timeout_raises_error(self, tmp_path):+ outdir = tmp_path / "safe"+ response = self._mock_response()+ response.iter_content = MagicMock(+ side_effect=requests.exceptions.ReadTimeout(+ "Max retries exceeded with url: /file?token=abc"+ )+ )+ f = FileDownloadable(+ name="report.pdf",+ mimetype="application/pdf",+ s3url="https://example.com/file?token=abc",+ )+ with patch(+ "composio.core.models._files.requests.get",+ return_value=response,+ ):+ with pytest.raises(ErrorDownloadingFile) as exc_info:+ f.download(outdir)++ assert "Error downloading file" in str(exc_info.value)+ assert "token=abc" not in str(exc_info.value)+ response.close.assert_called_once()++ def test_download_non_200_redacts_url_and_closes(self, tmp_path):+ outdir = tmp_path / "safe"+ response = self._mock_response()+ response.status_code = 403+ f = FileDownloadable(+ name="report.pdf",+ mimetype="application/pdf",+ s3url="https://example.com/file?token=abc",+ )+ with patch(+ "composio.core.models._files.requests.get",+ return_value=response,+ ):+ with pytest.raises(ErrorDownloadingFile) as exc_info:+ f.download(outdir)++ assert "token=abc" not in str(exc_info.value)+ response.close.assert_called_once()+def test_basename_collapse_through_subdir_is_rejected(self, tmp_path):"""`Path('foo/..').name == '..'` — the basename strip of a name thattraverses *through* a subdir collapses to `..`, then thediff --git a/python/tests/test_tool_router_session_files.py b/python/tests/test_tool_router_session_files.pyindex 8f00d4927..48ca520f0 100644--- a/python/tests/test_tool_router_session_files.py+++ b/python/tests/test_tool_router_session_files.py@@ -4,6 +4,7 @@ from pathlib import Pathfrom unittest.mock import MagicMock, patchimport pytest+import requestsfrom composio.core.models.tool_router_session_files import (RemoteFile,@@ -101,9 +102,25 @@ class TestToolRouterSessionFilesMount:assert isinstance(result, RemoteFile)assert result.mount_relative_path == "output/test.txt"+ mock_put.assert_called_once_with(+ "https://s3.example.com/upload",+ data=b"hello world",+ headers={"Content-Type": "text/plain"},+ timeout=(5, 60),+ )mock_client.tool_router.session.files.create_upload_url.assert_called_once()mock_client.tool_router.session.files.create_download_url.assert_called_once()+ def test_upload_raises_validation_error_on_timeout(self, files_mount):+ """Test upload converts request timeouts to ValidationError."""+ with patch("requests.put", side_effect=requests.exceptions.Timeout("timeout")):+ with pytest.raises(ValidationError, match="Failed to upload file"):+ files_mount.upload(+ b"hello world",+ remote_path="data.txt",+ mimetype="text/plain",+ )+def test_upload_from_local_file(self, files_mount, mock_client, tmp_path):"""Test upload from local file path."""test_file = tmp_path / "report.pdf"@@ -174,6 +191,10 @@ class TestRemoteFile:result = rf.buffer()assert result == b"file content"+ mock_get.assert_called_once_with(+ "https://example.com/file",+ timeout=(5, 60),+ )def test_buffer_failure_raises_remote_file_download_error(self):"""Test buffer() raises RemoteFileDownloadError on HTTP error."""@@ -194,6 +215,21 @@ class TestRemoteFile:assert exc_info.value.status_code == 404assert exc_info.value.filename == "test.txt"+ def test_buffer_timeout_raises_remote_file_download_error(self):+ """Test buffer() converts request timeouts to RemoteFileDownloadError."""+ rf = RemoteFile(+ expires_at="2026-01-01",+ mount_relative_path="test.txt",+ sandbox_mount_prefix="/mnt/files",+ download_url="https://example.com/file",+ )+ with patch("requests.get", side_effect=requests.exceptions.Timeout("timeout")):+ with pytest.raises(RemoteFileDownloadError) as exc_info:+ rf.buffer()++ assert exc_info.value.filename == "test.txt"+ assert exc_info.value.download_url == "https://example.com/file"+def test_text(self):"""Test text() decodes UTF-8."""rf = RemoteFile(