Skip to content

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

Merge branch 'feat/py-use-public-urls' of github.com:ComposioHQ/composio into feat/py-use-public-urls

jkomyno242b0c4c5b75
diff --cc python/tests/test_files.py
index 20b20c193,54730734d..a9985ae06
--- a/python/tests/test_files.py
+++ b/python/tests/test_files.py
@@@ -1627,214 -1613,23 +1642,235 @@@ class TestFetchFileFromUrlWithTruncatio
assert filename.endswith(".png")
assert len(filename) < 50 # Timestamped names are short
+ @patch("composio.core.models._files.requests.get")
+ def test_fetch_long_real_world_url(self, mock_get):
+ """Long real-world URLs should be handled correctly."""
+ mock_response = MagicMock()
+ mock_response.ok = True
+ mock_response.content = b"test content"
+ mock_response.headers = {"content-type": "application/pdf"}
+ mock_get.return_value = mock_response
+
+ # Real-world long URL example (no extension, relies on mimetype)
+ long_url = (
+ "https://encrypted-tbn0.gstatic.com/images?q="
+ "tbn:ANd9GcQq0powzLhfi1bakZ0eNSIA_aJ_5UlPsCte1g&s"
+ )
+ filename, content, mimetype = _fetch_file_from_url(long_url)
+
+ assert len(filename) <= _MAX_FILENAME_LENGTH
+ assert filename.startswith("file_")
+ assert filename.endswith(".pdf")
+ assert content == b"test content"
++
+
+class TestResponseSizeLimit:
+ """Test response size limiting."""
+
+ @patch("composio.core.models._files.requests.get")
+ def test_rejects_oversized_content_length(self, mock_get):
+ """Files with Content-Length > max_size should be rejected early."""
+ mock_response = MagicMock()
+ mock_response.ok = True
+ mock_response.status_code = 200
+ mock_response.headers = {"Content-Length": "200000000"} # 200MB
+ mock_response.close = MagicMock()
+ mock_get.return_value = mock_response
+
+ with pytest.raises(ResponseTooLargeError):
+ _fetch_file_from_url(
+ "https://example.com/large.zip", max_size=100 * 1024 * 1024
+ )
+
+ @patch("composio.core.models._files.requests.get")
+ def test_rejects_oversized_during_streaming(self, mock_get):
+ """Files that exceed max_size during download should be rejected."""
+ mock_response = MagicMock()
+ mock_response.ok = True
+ mock_response.status_code = 200
+ mock_response.headers = {} # No Content-Length
+ # Return 20MB of data in chunks
+ mock_response.iter_content.return_value = [
+ b"x" * 1024 * 1024 for _ in range(20)
+ ]
+ mock_response.close = MagicMock()
+ mock_get.return_value = mock_response
+
+ with pytest.raises(ResponseTooLargeError):
+ _fetch_file_from_url(
+ "https://example.com/large.zip", max_size=10 * 1024 * 1024
+ )
+
+ @patch("composio.core.models._files.requests.get")
+ def test_accepts_file_within_limit(self, mock_get):
+ """Files within size limit should be accepted."""
+ mock_response = MagicMock()
+ mock_response.ok = True
+ mock_response.status_code = 200
+ mock_response.headers = {"content-type": "image/jpeg", "Content-Length": "1000"}
+ mock_response.iter_content.return_value = [b"x" * 1000]
+ mock_response.close = MagicMock()
+ mock_get.return_value = mock_response
+
+ filename, content, mimetype = _fetch_file_from_url(
+ "https://example.com/image.jpg", max_size=10 * 1024 * 1024
+ )
+
+ assert len(content) == 1000
+ assert mimetype == "image/jpeg"
+
+
+class TestRedirectHandling:
+ """Test redirect handling (redirects should be rejected)."""
+
+ @patch("composio.core.models._files.requests.get")
+ def test_rejects_redirect_302(self, mock_get):
+ """302 redirects should be rejected with clear error message."""
+ mock_response = MagicMock()
+ mock_response.status_code = 302
+ mock_response.headers = {"Location": "https://example.com/final.jpg"}
+ mock_response.close = MagicMock()
+ mock_get.return_value = mock_response
+
+ with pytest.raises(ErrorUploadingFile, match="redirect"):
+ _fetch_file_from_url("https://example.com/redirect")
+
+ @patch("composio.core.models._files.requests.get")
+ def test_rejects_redirect_301(self, mock_get):
+ """301 redirects should be rejected."""
+ mock_response = MagicMock()
+ mock_response.status_code = 301
+ mock_response.headers = {"Location": "https://example.com/"}
+ mock_response.close = MagicMock()
+ mock_get.return_value = mock_response
+
+ with pytest.raises(ErrorUploadingFile, match="redirect"):
+ _fetch_file_from_url("https://example.com/test")
+
+ @patch("composio.core.models._files.requests.get")
+ def test_rejects_redirect_307(self, mock_get):
+ """307 redirects should be rejected."""
+ mock_response = MagicMock()
+ mock_response.status_code = 307
+ mock_response.headers = {"Location": "https://example.com/"}
+ mock_response.close = MagicMock()
+ mock_get.return_value = mock_response
+
+ with pytest.raises(ErrorUploadingFile, match="redirect"):
+ _fetch_file_from_url("https://example.com/test")
+
+ @patch("composio.core.models._files.requests.get")
+ def test_rejects_redirect_308(self, mock_get):
+ """308 redirects should be rejected."""
+ mock_response = MagicMock()
+ mock_response.status_code = 308
+ mock_response.headers = {"Location": "https://example.com/"}
+ mock_response.close = MagicMock()
+ mock_get.return_value = mock_response
+
+ with pytest.raises(ErrorUploadingFile, match="redirect"):
+ _fetch_file_from_url("https://example.com/test")
+
+
+class TestS3UploadErrorHandling:
+ """Test S3 upload error handling."""
+
+ @patch("composio.core.models._files.requests.put")
+ def test_403_is_treated_as_error(self, mock_put):
+ """HTTP 403 should be treated as upload failure."""
+ mock_client = MagicMock()
+ mock_s3_response = MagicMock()
+ mock_s3_response.key = "s3-key"
+ mock_s3_response.new_presigned_url = "https://s3.example.com/upload"
+ mock_client.post.return_value = mock_s3_response
+
+ mock_put_response = MagicMock()
+ mock_put_response.status_code = 403
+ mock_put.return_value = mock_put_response
+
+ with pytest.raises(ErrorUploadingFile, match="403"):
+ _upload_bytes_to_s3(
+ client=mock_client,
+ filename="test.jpg",
+ content=b"data",
+ mimetype="image/jpeg",
+ tool="TEST",
+ toolkit="test",
+ )
+
+ @patch("composio.core.models._files.requests.put")
+ def test_200_is_success(self, mock_put):
+ """HTTP 200 should be treated as success."""
+ mock_client = MagicMock()
+ mock_s3_response = MagicMock()
+ mock_s3_response.key = "s3-key"
+ mock_s3_response.new_presigned_url = "https://s3.example.com/upload"
+ mock_client.post.return_value = mock_s3_response
+
+ mock_put_response = MagicMock()
+ mock_put_response.status_code = 200
+ mock_put.return_value = mock_put_response
+
+ result = _upload_bytes_to_s3(
+ client=mock_client,
+ filename="test.jpg",
+ content=b"data",
+ mimetype="image/jpeg",
+ tool="TEST",
+ toolkit="test",
+ )
+ assert result == "s3-key"
+
+ @patch("composio.core.models._files.requests.put")
+ def test_500_is_treated_as_error(self, mock_put):
+ """HTTP 500 should be treated as upload failure."""
+ mock_client = MagicMock()
+ mock_s3_response = MagicMock()
+ mock_s3_response.key = "s3-key"
+ mock_s3_response.new_presigned_url = "https://s3.example.com/upload"
+ mock_client.post.return_value = mock_s3_response
+
+ mock_put_response = MagicMock()
+ mock_put_response.status_code = 500
+ mock_put.return_value = mock_put_response
+
+ with pytest.raises(ErrorUploadingFile, match="500"):
+ _upload_bytes_to_s3(
+ client=mock_client,
+ filename="test.jpg",
+ content=b"data",
+ mimetype="image/jpeg",
+ tool="TEST",
+ toolkit="test",
+ )
+
+
+class TestUrlSanitization:
+ """Test URL sanitization for logging."""
+
+ def test_sanitizes_query_params(self):
+ """Query parameters should be redacted in logs."""
+ url = "https://example.com/file?token=secret123&key=abc"
+ sanitized = _sanitize_url_for_logging(url)
+ assert "secret123" not in sanitized
+ assert "abc" not in sanitized
+ assert "[REDACTED]" in sanitized
+
+ def test_preserves_url_without_query(self):
+ """URLs without query params should be unchanged."""
+ url = "https://example.com/path/to/file.jpg"
+ sanitized = _sanitize_url_for_logging(url)
+ assert sanitized == url
+
+ def test_preserves_path(self):
+ """Path should be preserved when redacting query params."""
+ url = "https://example.com/path/to/file.jpg?token=secret"
+ sanitized = _sanitize_url_for_logging(url)
+ assert "/path/to/file.jpg" in sanitized
+ assert "example.com" in sanitized
+
+ def test_handles_empty_query(self):
+ """URLs with empty query string should not have [REDACTED]."""
+ url = "https://example.com/file.jpg"
+ sanitized = _sanitize_url_for_logging(url)
+ assert "[REDACTED]" not in sanitized