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.post.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.post.call_args.kwargs["body"]["mimetype"]
124
+ assert presigned_mimetype == mimetypes.guess(file=source)
125
+ assert mock_safe_request.call_args.kwargs["headers"] == {
126
+ "Content-Type": presigned_mimetype
127
+ }
128
+ assert result.mimetype == presigned_mimetype
129
+ assert result.s3key == "s3-key-123"
130
+
131
+ @patch("composio.core.models._files.safe_request")
132
+ def test_upload_surfaces_http_status(
133
+ self, mock_safe_request: MagicMock, tmp_path: Path
134
+ ):
135
+ """A rejected PUT raises with the status instead of returning False."""
136
+ mock_safe_request.return_value = MagicMock(status_code=403)
137
+ source = tmp_path / "report.pdf"
138
+ source.write_bytes(b"%PDF-1.4")
139
+
140
+ with pytest.raises(ErrorUploadingFile, match="403"):
141
+ upload(url="https://s3.example.com/upload", file=source)
142
+
143
+ @patch("composio.core.models._files.safe_request")
144
+ def test_upload_wraps_transport_errors_without_leaking_the_url(
145
+ self, mock_safe_request: MagicMock, tmp_path: Path
146
+ ):
147
+ mock_safe_request.side_effect = requests.exceptions.Timeout(
148
+ "HTTPSConnectionPool(host='s3.example.com', port=443): "
149
+ "Max retries exceeded with url: /upload?token=abc"
150
+ )
151
+ source = tmp_path / "report.pdf"
152
+ source.write_bytes(b"%PDF-1.4")
153
+
154
+ with pytest.raises(ErrorUploadingFile) as exc_info:
155
+ upload(url="https://s3.example.com/upload?token=abc", file=source)
156
+
157
+ assert "Failed to upload to S3" in str(exc_info.value)
158
+ assert "token=abc" not in str(exc_info.value)
159
+
160
+
161
+class TestParseContentLength:
162
+ """``Content-Length`` is remote-controlled input and must not crash a fetch."""
163
+
164
+ @pytest.mark.parametrize(
165
+ "value, expected",
166
+ [
167
+ ("0", 0),
168
+ ("1024", 1024),
169
+ (" 2048 ", 2048),
170
+ ],
171
+ )
172
+ def test_accepts_valid_sizes(self, value: str, expected: int):
173
+ assert parse_content_length(value) == expected
174
+
175
+ @pytest.mark.parametrize(
176
+ "value",
177
+ [None, "", " ", "abc", "12.5", "1,024", "1e3", "0x10", "100 200", "-1"],
178
+ )
179
+ def test_untrustworthy_values_mean_unknown_size(self, value: t.Optional[str]):
180
+ assert parse_content_length(value) is None
181
+
182
+
183
+class TestMalformedContentLength:
184
+ """A malformed header degrades to unknown size under the streaming cap."""
185
+
186
+ @pytest.mark.parametrize(
187
+ "header", ["abc", "12.5", "1,024", "1e3", "0x10", "100 200", ""]
188
+ )
189
+ @patch("composio.core.models._files.assert_safe_fetch_target")
190
+ @patch("composio.core.models._files.requests.get")
191
+ def test_malformed_header_does_not_raise(
192
+ self, mock_get: MagicMock, _mock_assert: MagicMock, header: str
193
+ ):
194
+ """A malformed header must not surface a raw ValueError to the caller."""
195
+ mock_get.return_value = _stream_response(
196
+ {"content-type": "image/jpeg", "Content-Length": header}
197
+ )
198
+
199
+ filename, content, mimetype = _fetch_file_from_url(
200
+ "https://example.com/image.jpg"
201
+ )
202
+
203
+ assert filename == "image.jpg"
204
+ assert content == b"payload"
205
+ assert mimetype == "image/jpeg"
206
+
207
+ @patch("composio.core.models._files.assert_safe_fetch_target")
208
+ @patch("composio.core.models._files.requests.get")
209
+ def test_negative_header_still_enforced_while_streaming(
210
+ self, mock_get: MagicMock, _mock_assert: MagicMock
211
+ ):
212
+ """A negative header means unknown size, not a trusted "small" value."""
213
+ mock_get.return_value = _stream_response(
214
+ {"Content-Length": "-1"},
215
+ [b"x" * 1024 * 1024 for _ in range(20)],
216
+ )
217
+
218
+ with pytest.raises(ResponseTooLargeError):
219
+ _fetch_file_from_url(
220
+ "https://example.com/large.zip", max_size=10 * 1024 * 1024
221
+ )