Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | """Regression tests for file upload and URL fetch robustness. |
| 2 | |
| 3 | Covers https://github.com/ComposioHQ/composio/issues/4153: a malformed |
| 4 | ``Content-Length`` response header must not crash the URL fetch helpers with |
| 5 | a raw ``ValueError``, and the local-file presigned PUT must send the |
| 6 | ``Content-Type`` it was signed with. The shared helpers under test keep the |
| 7 | fetch and upload paths from drifting apart again. |
| 8 | """ |
| 9 | |
| 10 | import typing as t |
| 11 | from pathlib import Path |
| 12 | from unittest.mock import MagicMock, patch |
| 13 | |
| 14 | import pytest |
| 15 | import requests |
| 16 | |
| 17 | from composio.core.models._files import ( |
| 18 | FileUploadable, |
| 19 | _fetch_file_from_url, |
| 20 | upload, |
| 21 | ) |
| 22 | from composio.core.models.base import allow_tracking |
| 23 | from composio.exceptions import ErrorUploadingFile, ResponseTooLargeError |
| 24 | from composio.utils import mimetypes |
| 25 | from composio.utils.url_safety import parse_content_length |
| 26 | |
| 27 | |
| 28 | @pytest.fixture(autouse=True) |
| 29 | def disable_telemetry(): |
| 30 | """Disable telemetry for all tests to prevent thread issues.""" |
| 31 | token = allow_tracking.set(False) |
| 32 | yield |
| 33 | allow_tracking.reset(token) |
| 34 | |
| 35 | |
| 36 | def _stream_response( |
| 37 | headers: t.Dict[str, str], |
| 38 | chunks: t.Optional[t.List[bytes]] = None, |
| 39 | ) -> MagicMock: |
| 40 | """A streaming `requests` response double, as `_fetch_file_from_url` reads it.""" |
| 41 | response = MagicMock() |
| 42 | response.ok = True |
| 43 | response.status_code = 200 |
| 44 | response.headers = headers |
| 45 | response.iter_content.return_value = chunks if chunks is not None else [b"payload"] |
| 46 | response.close = MagicMock() |
| 47 | return response |
| 48 | |
| 49 | |
| 50 | def _s3_client(presigned_url: str = "https://s3.example.com/upload") -> MagicMock: |
| 51 | """A mock HTTP client whose presign POST answers a fresh upload URL.""" |
| 52 | client = MagicMock() |
| 53 | s3meta = MagicMock() |
| 54 | s3meta.key = "s3-key-123" |
| 55 | s3meta.new_presigned_url = presigned_url |
| 56 | client.files.create_presigned_url.return_value = s3meta |
| 57 | return client |
| 58 | |
| 59 | |
| 60 | class TestPresignedUploadContentType: |
| 61 | """The PUT must send the content type the presigned URL was signed with.""" |
| 62 | |
| 63 | @patch("composio.core.models._files.safe_request") |
| 64 | def test_upload_sends_explicit_mimetype( |
| 65 | self, mock_safe_request: MagicMock, tmp_path: Path |
| 66 | ): |
| 67 | mock_safe_request.return_value = MagicMock(status_code=200) |
| 68 | source = tmp_path / "report.pdf" |
| 69 | source.write_bytes(b"%PDF-1.4") |
| 70 | |
| 71 | assert upload( |
| 72 | url="https://s3.example.com/upload", |
| 73 | file=source, |
| 74 | mimetype="application/pdf", |
| 75 | ) |
| 76 | |
| 77 | assert mock_safe_request.call_args.args == ( |
| 78 | "PUT", |
| 79 | "https://s3.example.com/upload", |
| 80 | ) |
| 81 | assert mock_safe_request.call_args.kwargs["headers"] == { |
| 82 | "Content-Type": "application/pdf" |
| 83 | } |
| 84 | assert mock_safe_request.call_args.kwargs["timeout"] == (5, 60) |
| 85 | |
| 86 | @patch("composio.core.models._files.safe_request") |
| 87 | def test_upload_guesses_mimetype_when_omitted( |
| 88 | self, mock_safe_request: MagicMock, tmp_path: Path |
| 89 | ): |
| 90 | """Back-compat: two-argument callers still send a Content-Type.""" |
| 91 | mock_safe_request.return_value = MagicMock(status_code=200) |
| 92 | source = tmp_path / "notes.txt" |
| 93 | source.write_text("hello") |
| 94 | |
| 95 | assert upload(url="https://s3.example.com/upload", file=source) |
| 96 | |
| 97 | assert mock_safe_request.call_args.kwargs["headers"] == { |
| 98 | "Content-Type": mimetypes.guess(file=source) |
| 99 | } |
| 100 | |
| 101 | @patch("composio.core.models._files.safe_request") |
| 102 | def test_from_path_put_matches_presigned_mimetype( |
| 103 | self, mock_safe_request: MagicMock, tmp_path: Path |
| 104 | ): |
| 105 | """The PUT content type must match the mimetype used to mint the URL. |
| 106 | |
| 107 | S3 answers ``403 SignatureDoesNotMatch`` when a presigned URL is |
| 108 | signed over a content type the subsequent PUT does not send, which |
| 109 | made the local-file path fail where the bytes path succeeded. |
| 110 | """ |
| 111 | mock_safe_request.return_value = MagicMock(status_code=200) |
| 112 | client = _s3_client() |
| 113 | source = tmp_path / "photo.jpg" |
| 114 | source.write_bytes(b"jpeg bytes") |
| 115 | |
| 116 | result = FileUploadable.from_path( |
| 117 | client=client, |
| 118 | file=source, |
| 119 | tool="TEST_TOOL", |
| 120 | toolkit="test_toolkit", |
| 121 | ) |
| 122 | |
| 123 | presigned_mimetype = client.files.create_presigned_url.call_args.kwargs[ |
| 124 | "mimetype" |
| 125 | ] |
| 126 | assert presigned_mimetype == mimetypes.guess(file=source) |
| 127 | assert mock_safe_request.call_args.kwargs["headers"] == { |
| 128 | "Content-Type": presigned_mimetype |
| 129 | } |
| 130 | assert result.mimetype == presigned_mimetype |
| 131 | assert result.s3key == "s3-key-123" |
| 132 | |
| 133 | @patch("composio.core.models._files.safe_request") |
| 134 | def test_upload_surfaces_http_status( |
| 135 | self, mock_safe_request: MagicMock, tmp_path: Path |
| 136 | ): |
| 137 | """A rejected PUT raises with the status instead of returning False.""" |
| 138 | mock_safe_request.return_value = MagicMock(status_code=403) |
| 139 | source = tmp_path / "report.pdf" |
| 140 | source.write_bytes(b"%PDF-1.4") |
| 141 | |
| 142 | with pytest.raises(ErrorUploadingFile, match="403"): |
| 143 | upload(url="https://s3.example.com/upload", file=source) |
| 144 | |
| 145 | @patch("composio.core.models._files.safe_request") |
| 146 | def test_upload_wraps_transport_errors_without_leaking_the_url( |
| 147 | self, mock_safe_request: MagicMock, tmp_path: Path |
| 148 | ): |
| 149 | mock_safe_request.side_effect = requests.exceptions.Timeout( |
| 150 | "HTTPSConnectionPool(host='s3.example.com', port=443): " |
| 151 | "Max retries exceeded with url: /upload?token=abc" |
| 152 | ) |
| 153 | source = tmp_path / "report.pdf" |
| 154 | source.write_bytes(b"%PDF-1.4") |
| 155 | |
| 156 | with pytest.raises(ErrorUploadingFile) as exc_info: |
| 157 | upload(url="https://s3.example.com/upload?token=abc", file=source) |
| 158 | |
| 159 | assert "Failed to upload to S3" in str(exc_info.value) |
| 160 | assert "token=abc" not in str(exc_info.value) |
| 161 | |
| 162 | |
| 163 | class TestParseContentLength: |
| 164 | """``Content-Length`` is remote-controlled input and must not crash a fetch.""" |
| 165 | |
| 166 | @pytest.mark.parametrize( |
| 167 | "value, expected", |
| 168 | [ |
| 169 | ("0", 0), |
| 170 | ("1024", 1024), |
| 171 | (" 2048 ", 2048), |
| 172 | ], |
| 173 | ) |
| 174 | def test_accepts_valid_sizes(self, value: str, expected: int): |
| 175 | assert parse_content_length(value) == expected |
| 176 | |
| 177 | @pytest.mark.parametrize( |
| 178 | "value", |
| 179 | [None, "", " ", "abc", "12.5", "1,024", "1e3", "0x10", "100 200", "-1"], |
| 180 | ) |
| 181 | def test_untrustworthy_values_mean_unknown_size(self, value: t.Optional[str]): |
| 182 | assert parse_content_length(value) is None |
| 183 | |
| 184 | |
| 185 | class TestMalformedContentLength: |
| 186 | """A malformed header degrades to unknown size under the streaming cap.""" |
| 187 | |
| 188 | @pytest.mark.parametrize( |
| 189 | "header", ["abc", "12.5", "1,024", "1e3", "0x10", "100 200", ""] |
| 190 | ) |
| 191 | @patch("composio.core.models._files.safe_get") |
| 192 | def test_malformed_header_does_not_raise(self, mock_get: MagicMock, header: str): |
| 193 | """A malformed header must not surface a raw ValueError to the caller.""" |
| 194 | mock_get.return_value = _stream_response( |
| 195 | {"content-type": "image/jpeg", "Content-Length": header} |
| 196 | ) |
| 197 | |
| 198 | filename, content, mimetype = _fetch_file_from_url( |
| 199 | "https://example.com/image.jpg" |
| 200 | ) |
| 201 | |
| 202 | assert filename == "image.jpg" |
| 203 | assert content == b"payload" |
| 204 | assert mimetype == "image/jpeg" |
| 205 | |
| 206 | @patch("composio.core.models._files.safe_get") |
| 207 | def test_negative_header_still_enforced_while_streaming(self, mock_get: MagicMock): |
| 208 | """A negative header means unknown size, not a trusted "small" value.""" |
| 209 | mock_get.return_value = _stream_response( |
| 210 | {"Content-Length": "-1"}, |
| 211 | [b"x" * 1024 * 1024 for _ in range(20)], |
| 212 | ) |
| 213 | |
| 214 | with pytest.raises(ResponseTooLargeError): |
| 215 | _fetch_file_from_url( |
| 216 | "https://example.com/large.zip", max_size=10 * 1024 * 1024 |
| 217 | ) |