@devries-cameron20 / composio Mirror · read-only
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.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 03:37 UTC

next
py 579 lines 22.9 KB
Raw
1 """Tests for ToolRouterSessionFilesMount and RemoteFile."""
2
3 from pathlib import Path
4 from unittest.mock import MagicMock, patch
5
6 import pytest
7 import requests
8 from composio_client import ProjectAuth
9
10 from composio.core.models.tool_router_session_files import (
11 RemoteFile,
12 ToolRouterSessionFilesMount,
13 )
14 from composio.exceptions import (
15 BlockedInternalUrlError,
16 RemoteFileDownloadError,
17 ValidationError,
18 )
19
20 MODULE = "composio.core.models.tool_router_session_files"
21 SAFE_REQUEST = f"{MODULE}.safe_request"
22 SAFE_GET = f"{MODULE}.safe_get"
23 ASSERT_SAFE_FETCH_TARGET = "composio.utils.url_safety.assert_safe_fetch_target"
24 SESSION_REQUEST = "composio.utils.url_safety.requests.Session.request"
25
26
27 def mock_stream_response(
28 content: bytes = b"file content",
29 *,
30 status_code: int = 200,
31 content_type: str = "text/plain",
32 ) -> MagicMock:
33 """A streaming `requests` response double, as `_fetch_url_bytes` reads it."""
34 response = MagicMock()
35 response.status_code = status_code
36 response.ok = 200 <= status_code < 300
37 response.reason = "OK" if response.ok else "Not Found"
38 response.headers = {"content-type": content_type}
39 response.iter_content = lambda chunk_size: [content]
40 response.close = MagicMock()
41 return response
42
43
44 @pytest.fixture
45 def mock_client():
46 """Create a mock HTTP client with files API."""
47 client = MagicMock()
48 client.sdk_auth = ProjectAuth("test-api-key")
49
50 # Mock files.list
51 mock_list_response = MagicMock()
52 mock_list_response.items = []
53 mock_list_response.next_cursor = None
54 client.tool_router.session.files.list.return_value = mock_list_response
55
56 # Mock files.create_upload_url
57 mock_upload_url_response = MagicMock()
58 mock_upload_url_response.upload_url = "https://s3.example.com/upload"
59 mock_upload_url_response.mount_relative_path = "test.txt"
60 mock_upload_url_response.expires_at = "2026-01-01T00:00:00Z"
61 mock_upload_url_response.sandbox_mount_prefix = "/mnt/files"
62 client.tool_router.session.files.create_upload_url.return_value = (
63 mock_upload_url_response
64 )
65
66 # Mock files.create_download_url
67 mock_download_response = MagicMock()
68 mock_download_response.download_url = "https://s3.example.com/download"
69 mock_download_response.expires_at = "2026-01-01T00:00:00Z"
70 mock_download_response.mount_relative_path = "output/test.txt"
71 mock_download_response.sandbox_mount_prefix = "/mnt/files"
72 client.tool_router.session.files.create_download_url.return_value = (
73 mock_download_response
74 )
75
76 # Mock files.delete
77 mock_delete_response = MagicMock()
78 mock_delete_response.mount_relative_path = "deleted.txt"
79 mock_delete_response.sandbox_mount_prefix = "/mnt/files"
80 client.tool_router.session.files.delete.return_value = mock_delete_response
81
82 return client
83
84
85 @pytest.fixture
86 def files_mount(mock_client):
87 """Create ToolRouterSessionFilesMount with mocked client."""
88 return ToolRouterSessionFilesMount(mock_client, "session_123")
89
90
91 class TestToolRouterSessionFilesMount:
92 """Test ToolRouterSessionFilesMount."""
93
94 def test_list_root(self, files_mount, mock_client):
95 """Test listing root directory."""
96 result = files_mount.list()
97
98 mock_client.tool_router.session.files.list.assert_called_once()
99 call_args = mock_client.tool_router.session.files.list.call_args
100 assert call_args[0][0] == "files" # mount_id positional
101 assert call_args[1]["session_id"] == "session_123"
102 assert result.items == []
103 assert result.next_cursor is None
104
105 def test_list_with_path_and_pagination(self, files_mount, mock_client):
106 """Test list with path and pagination params."""
107 files_mount.list(path="/documents", cursor="c123", limit=10)
108
109 call_kwargs = mock_client.tool_router.session.files.list.call_args[1]
110 assert call_kwargs.get("mount_relative_prefix") == "documents"
111 assert call_kwargs.get("cursor") == "c123"
112 assert call_kwargs.get("limit") == 10.0
113
114 def test_upload_from_bytes_requires_mimetype_or_remote_path(self, files_mount):
115 """Test that buffer upload requires mimetype or remote_path."""
116 with pytest.raises(ValidationError, match="mimetype or remote_path"):
117 files_mount.upload(b"content")
118
119 def test_upload_from_bytes_with_remote_path(self, files_mount, mock_client):
120 """Test upload from bytes with remote_path."""
121 with patch(SAFE_REQUEST) as mock_safe_request:
122 mock_safe_request.return_value.status_code = 200
123 mock_safe_request.return_value.ok = True
124
125 result = files_mount.upload(
126 b"hello world",
127 remote_path="data.txt",
128 mimetype="text/plain",
129 )
130
131 assert isinstance(result, RemoteFile)
132 assert result.mount_relative_path == "output/test.txt"
133 # Routed through `safe_request`, not a bare `requests.put`:
134 # `upload_url` is a response field, so its target is validated
135 # before the bytes are sent, and on every redirect hop after.
136 mock_safe_request.assert_called_once_with(
137 "PUT",
138 "https://s3.example.com/upload",
139 data=b"hello world",
140 headers={"Content-Type": "text/plain"},
141 timeout=(5, 60),
142 )
143 mock_client.tool_router.session.files.create_upload_url.assert_called_once()
144 mock_client.tool_router.session.files.create_download_url.assert_called_once()
145
146 def test_upload_raises_validation_error_on_timeout(self, files_mount):
147 """Test upload converts request timeouts to ValidationError."""
148 with patch(SAFE_REQUEST, side_effect=requests.exceptions.Timeout("timeout")):
149 with pytest.raises(ValidationError, match="Failed to upload file"):
150 files_mount.upload(
151 b"hello world",
152 remote_path="data.txt",
153 mimetype="text/plain",
154 )
155
156 def test_upload_from_local_file(self, files_mount, mock_client, tmp_path):
157 """Test upload from local file path."""
158 test_file = tmp_path / "report.pdf"
159 test_file.write_bytes(b"pdf content")
160
161 with patch(SAFE_REQUEST) as mock_safe_request:
162 mock_safe_request.return_value.status_code = 200
163 mock_safe_request.return_value.ok = True
164
165 result = files_mount.upload(str(test_file))
166
167 assert isinstance(result, RemoteFile)
168 call_kwargs = (
169 mock_client.tool_router.session.files.create_upload_url.call_args[1]
170 )
171 assert call_kwargs["mount_relative_path"] == "report.pdf"
172
173 def test_download(self, files_mount, mock_client):
174 """Test download returns RemoteFile."""
175 result = files_mount.download("/output/report.pdf")
176
177 assert isinstance(result, RemoteFile)
178 assert result.download_url == "https://s3.example.com/download"
179 assert result.mount_relative_path == "output/test.txt"
180 mock_client.tool_router.session.files.create_download_url.assert_called_once_with(
181 "files",
182 session_id="session_123",
183 mount_relative_path="/output/report.pdf",
184 )
185
186 def test_delete(self, files_mount, mock_client):
187 """Test delete calls API."""
188 result = files_mount.delete("/temp/cache.json")
189
190 assert result.mount_relative_path == "deleted.txt"
191 mock_client.tool_router.session.files.delete.assert_called_once_with(
192 "files",
193 session_id="session_123",
194 mount_relative_path="/temp/cache.json",
195 )
196
197
198 class TestRemoteFile:
199 """Test RemoteFile."""
200
201 def test_filename_property(self):
202 """Test filename extracted from mount path."""
203 rf = RemoteFile(
204 expires_at="2026-01-01",
205 mount_relative_path="output/report.pdf",
206 sandbox_mount_prefix="/mnt/files",
207 download_url="https://example.com/file",
208 )
209 assert rf.filename == "report.pdf"
210
211 def test_buffer_success(self):
212 """Test buffer() fetches content."""
213 rf = RemoteFile(
214 expires_at="2026-01-01",
215 mount_relative_path="test.txt",
216 sandbox_mount_prefix="/mnt/files",
217 download_url="https://example.com/file",
218 )
219 with patch(ASSERT_SAFE_FETCH_TARGET):
220 with patch(SAFE_GET, return_value=mock_stream_response()) as mock_get:
221 result = rf.buffer()
222
223 assert result == b"file content"
224 mock_get.assert_called_once_with(
225 "https://example.com/file",
226 stream=True,
227 timeout=(5, 60),
228 )
229
230 def test_buffer_failure_raises_remote_file_download_error(self):
231 """Test buffer() raises RemoteFileDownloadError on HTTP error."""
232 rf = RemoteFile(
233 expires_at="2026-01-01",
234 mount_relative_path="test.txt",
235 sandbox_mount_prefix="/mnt/files",
236 download_url="https://example.com/file",
237 )
238 with patch(ASSERT_SAFE_FETCH_TARGET):
239 with patch(SAFE_GET, return_value=mock_stream_response(status_code=404)):
240 with pytest.raises(RemoteFileDownloadError) as exc_info:
241 rf.buffer()
242
243 assert exc_info.value.status_code == 404
244 assert exc_info.value.filename == "test.txt"
245
246 def test_buffer_timeout_raises_remote_file_download_error(self):
247 """Test buffer() converts request timeouts to RemoteFileDownloadError."""
248 rf = RemoteFile(
249 expires_at="2026-01-01",
250 mount_relative_path="test.txt",
251 sandbox_mount_prefix="/mnt/files",
252 download_url="https://example.com/file",
253 )
254 with patch(ASSERT_SAFE_FETCH_TARGET):
255 with patch(SAFE_GET, side_effect=requests.exceptions.Timeout("timeout")):
256 with pytest.raises(RemoteFileDownloadError) as exc_info:
257 rf.buffer()
258
259 assert exc_info.value.filename == "test.txt"
260 assert exc_info.value.download_url == "https://example.com/file"
261
262 def test_text(self):
263 """Test text() decodes UTF-8."""
264 rf = RemoteFile(
265 expires_at="2026-01-01",
266 mount_relative_path="test.txt",
267 sandbox_mount_prefix="/mnt/files",
268 download_url="https://example.com/file",
269 )
270 with patch.object(rf, "buffer", return_value=b"hello world"):
271 assert rf.text() == "hello world"
272
273 def test_save_to_path(self, tmp_path):
274 """Test save() writes to specified path."""
275 rf = RemoteFile(
276 expires_at="2026-01-01",
277 mount_relative_path="test.txt",
278 sandbox_mount_prefix="/mnt/files",
279 download_url="https://example.com/file",
280 )
281 with patch.object(rf, "buffer", return_value=b"saved content"):
282 out_path = rf.save(str(tmp_path / "output.txt"))
283
284 assert Path(out_path).read_bytes() == b"saved content"
285 assert out_path.endswith("output.txt")
286
287 @pytest.mark.parametrize(
288 ("mount_relative_path", "expected_name"),
289 [
290 ("report.pdf", "report.pdf"),
291 ("C:report.txt", "report.txt"),
292 ("\ufeffreport.txt", "\ufeffreport.txt"),
293 ("\u0085report.txt\u0085", "report.txt"),
294 ],
295 )
296 def test_save_default_location(self, tmp_path, mount_relative_path, expected_name):
297 """Test save() without path uses default directory."""
298 rf = RemoteFile(
299 expires_at="2026-01-01",
300 mount_relative_path=mount_relative_path,
301 sandbox_mount_prefix="/mnt/files",
302 download_url="https://example.com/file",
303 )
304 with patch.object(rf, "buffer", return_value=b"pdf content"):
305 with patch("pathlib.Path.home", return_value=tmp_path):
306 out_path = rf.save()
307
308 expected = tmp_path / ".composio" / "files" / expected_name
309 assert Path(out_path) == expected
310 assert expected.read_bytes() == b"pdf content"
311
312 def test_save_default_location_rejects_dotdot_filename(self, tmp_path):
313 """SEC-316 defense-in-depth: a server-controlled ``mount_relative_path``
314 whose basename is ``..`` (e.g. ``"foo/.."``) must be rejected before
315 any bytes touch the disk, not silently fail with ``IsADirectoryError``."""
316 rf = RemoteFile(
317 expires_at="2026-01-01",
318 mount_relative_path="foo/..",
319 sandbox_mount_prefix="/mnt/files",
320 download_url="https://example.com/file",
321 )
322 assert rf.filename == ".." # `Path("foo/..").name == ".."`
323
324 with patch.object(rf, "buffer", return_value=b"should not be written"):
325 with patch("pathlib.Path.home", return_value=tmp_path):
326 with pytest.raises(ValidationError, match="Path traversal detected"):
327 rf.save()
328
329 # The check raises before mkdir/write, so nothing was written under tmp_path.
330 assert not (tmp_path / ".composio").exists()
331
332 @pytest.mark.parametrize(
333 "mount_relative_path", ["", ".", "\u00a0.\u00a0", "\u2007..\u2007"]
334 )
335 def test_save_default_location_rejects_unusable_basename(
336 self, tmp_path, mount_relative_path
337 ):
338 """A mount path that strips down to ``.`` or ``..`` would make the
339 save path equal the download directory or its parent; each must be a
340 ``ValidationError`` raised before anything touches the disk, not an
341 ``IsADirectoryError`` from ``write_bytes``."""
342 rf = RemoteFile(
343 expires_at="2026-01-01",
344 mount_relative_path=mount_relative_path,
345 sandbox_mount_prefix="/mnt/files",
346 download_url="https://example.com/file",
347 )
348
349 with patch.object(rf, "buffer", return_value=b"should not be written"):
350 with patch("pathlib.Path.home", return_value=tmp_path):
351 with pytest.raises(ValidationError, match="Path traversal detected"):
352 rf.save()
353
354 assert not (tmp_path / ".composio").exists()
355
356 @pytest.mark.parametrize(
357 ("mount_relative_path", "expected"),
358 [
359 ("report.\u00a0", "report-11aada8ba3168adf"),
360 (
361 "out/report_2026-09-29T10:30:00.csv",
362 "report_2026-09-29T10_30_00-d7211bb25cb815fe.csv",
363 ),
364 ("What is this?.png", "What is this_-9c68adf2da8b6e8d.png"),
365 ],
366 )
367 def test_save_makes_unportable_names_portable(
368 self, tmp_path, mount_relative_path, expected
369 ):
370 rf = RemoteFile(
371 expires_at="2026-01-01",
372 mount_relative_path=mount_relative_path,
373 sandbox_mount_prefix="/mnt/files",
374 download_url="https://example.com/file",
375 )
376 with patch.object(rf, "buffer", return_value=b"content"):
377 with patch("pathlib.Path.home", return_value=tmp_path):
378 saved = Path(rf.save())
379 assert saved.name == expected
380 assert saved.read_bytes() == b"content"
381
382 @pytest.mark.parametrize(
383 "names",
384 [
385 ["report?.png", "report_-05fcb95aa5b918e9.png"],
386 ["report_-05fcb95aa5b918e9.png", "report?.png"],
387 ["請" * 41 + ".pdf"] * 3,
388 ],
389 )
390 def test_default_save_preserves_files_with_colliding_names(self, names, tmp_path):
391 paths = []
392 with patch("pathlib.Path.home", return_value=tmp_path):
393 for index, name in enumerate(names):
394 file = RemoteFile(
395 expires_at="2026-01-01",
396 mount_relative_path=name,
397 sandbox_mount_prefix="/mnt/files",
398 download_url="https://example.com/file",
399 )
400 with patch.object(file, "buffer", return_value=bytes([index])):
401 paths.append(Path(file.save()))
402
403 assert len(set(paths)) == len(names)
404 assert [path.read_bytes() for path in paths] == [
405 bytes([i]) for i in range(len(names))
406 ]
407 assert all(len(path.name.encode()) <= 128 for path in paths)
408 assert all(path.suffix == Path(names[0]).suffix for path in paths)
409
410 def test_failed_default_save_leaves_no_file_behind(self, tmp_path):
411 file = RemoteFile(
412 expires_at="2026-01-01",
413 mount_relative_path="report.pdf",
414 sandbox_mount_prefix="/mnt/files",
415 download_url="https://example.com/file",
416 )
417 with patch("pathlib.Path.home", return_value=tmp_path):
418 # Writing `str` to the binary file fails after the path is claimed.
419 with patch.object(file, "buffer", return_value="not bytes"):
420 with pytest.raises(TypeError):
421 file.save()
422 with patch.object(file, "buffer", return_value=b"content"):
423 saved = Path(file.save())
424
425 assert saved.name == "report.pdf"
426 assert [path.name for path in saved.parent.iterdir()] == ["report.pdf"]
427
428
429 class TestResponseDerivedUrlsAreGuarded:
430 """`download_url` and `upload_url` are response fields, so they are guarded.
431
432 `RemoteFile.buffer()` previously called `requests.get` directly: no target
433 validation, no redirect control, and `response.content` read the whole body
434 into memory with no cap — while the sibling `_fetch_from_url`, four lines
435 up, had all three. The only difference between them was which side of the
436 trust boundary the URL came from.
437 """
438
439 def _remote_file(self, download_url: str) -> RemoteFile:
440 return RemoteFile(
441 expires_at="2026-01-01",
442 mount_relative_path="test.txt",
443 sandbox_mount_prefix="/mnt/files",
444 download_url=download_url,
445 )
446
447 def test_buffer_validates_download_url(self):
448 rf = self._remote_file("https://s3.example.com/download")
449
450 with patch(SAFE_GET, return_value=mock_stream_response()) as mock_get:
451 rf.buffer()
452
453 # `safe_get` is the guard: it validates the target and then connects to
454 # the address it validated instead of re-resolving the hostname.
455 assert mock_get.call_args.args == ("https://s3.example.com/download",)
456
457 def test_buffer_blocked_url_never_reaches_the_network(self):
458 rf = self._remote_file("http://169.254.169.254/latest/meta-data")
459
460 with patch(
461 ASSERT_SAFE_FETCH_TARGET,
462 side_effect=BlockedInternalUrlError("blocked"),
463 ):
464 with patch(SESSION_REQUEST) as mock_send:
465 with pytest.raises(BlockedInternalUrlError):
466 rf.buffer()
467
468 mock_send.assert_not_called()
469
470 def test_buffer_rejects_redirects(self):
471 """A validated URL must not be able to bounce the fetch elsewhere."""
472 rf = self._remote_file("https://s3.example.com/download")
473
474 with patch(ASSERT_SAFE_FETCH_TARGET):
475 with patch(
476 SAFE_GET, return_value=mock_stream_response(status_code=302)
477 ) as mock_get:
478 with pytest.raises(RemoteFileDownloadError, match="redirect"):
479 rf.buffer()
480
481 # `safe_get` never follows redirects; passing `allow_redirects` through
482 # to it would be a way to turn that off.
483 assert "allow_redirects" not in mock_get.call_args.kwargs
484
485 def test_buffer_tolerates_malformed_content_length(self):
486 """A malformed `Content-Length` means unknown size, not a crash.
487
488 The header is remote-controlled; `_fetch_url_bytes` must fall through
489 to the streamed byte count instead of raising `ValueError` out of
490 `int()` (the crash class issue #4153 fixed for `_files.py`).
491 """
492 rf = self._remote_file("https://s3.example.com/download")
493 malformed = mock_stream_response()
494 malformed.headers = {
495 "content-type": "text/plain",
496 "Content-Length": "1,024",
497 }
498
499 with patch(ASSERT_SAFE_FETCH_TARGET):
500 with patch(SAFE_GET, return_value=malformed):
501 assert rf.buffer() == b"file content"
502
503 def test_buffer_caps_response_size(self):
504 """The body is streamed against a cap rather than read whole."""
505 rf = self._remote_file("https://s3.example.com/download")
506 oversized = mock_stream_response()
507 oversized.headers = {
508 "content-type": "text/plain",
509 "Content-Length": str(200 * 1024 * 1024),
510 }
511
512 with patch(ASSERT_SAFE_FETCH_TARGET):
513 with patch(SAFE_GET, return_value=oversized):
514 with pytest.raises(RemoteFileDownloadError, match="exceeds maximum"):
515 rf.buffer()
516
517 def test_buffer_maps_midstream_failure_and_closes_response(self):
518 rf = self._remote_file("https://s3.example.com/download")
519 response = mock_stream_response()
520
521 def failing_stream(chunk_size=None):
522 yield b"partial"
523 raise requests.exceptions.ConnectionError("peer reset mid-stream")
524
525 response.iter_content = failing_stream
526
527 with patch(SAFE_GET, return_value=response):
528 with pytest.raises(RemoteFileDownloadError) as exc_info:
529 rf.buffer()
530
531 assert isinstance(exc_info.value.__cause__, requests.exceptions.ConnectionError)
532 response.close.assert_called_once()
533
534 def test_url_upload_maps_midstream_failure_and_closes_response(self, files_mount):
535 response = mock_stream_response()
536
537 def failing_stream(chunk_size=None):
538 yield b"partial"
539 raise requests.exceptions.ConnectionError("peer reset mid-stream")
540
541 response.iter_content = failing_stream
542
543 with patch(SAFE_GET, return_value=response):
544 with pytest.raises(ValidationError, match="peer reset mid-stream"):
545 files_mount.upload("https://files.example/input.txt")
546
547 response.close.assert_called_once()
548
549 def test_text_and_save_inherit_the_guard(self, tmp_path):
550 """`text()` and `save()` read through `buffer()`, so they are covered."""
551 rf = self._remote_file("http://127.0.0.1:9000/download")
552
553 with patch(
554 ASSERT_SAFE_FETCH_TARGET,
555 side_effect=BlockedInternalUrlError("blocked"),
556 ):
557 with patch(SESSION_REQUEST) as mock_send:
558 with pytest.raises(BlockedInternalUrlError):
559 rf.text()
560 with pytest.raises(BlockedInternalUrlError):
561 rf.save(str(tmp_path / "out.txt"))
562
563 mock_send.assert_not_called()
564 assert not (tmp_path / "out.txt").exists()
565
566 def test_upload_blocked_url_sends_nothing(self, files_mount):
567 with patch(
568 "composio.utils.url_safety.assert_safe_fetch_target",
569 side_effect=BlockedInternalUrlError("blocked"),
570 ):
571 with patch(SESSION_REQUEST) as mock_request:
572 with pytest.raises(BlockedInternalUrlError):
573 files_mount.upload(
574 b"hello world",
575 remote_path="data.txt",
576 mimetype="text/plain",
577 )
578
579 mock_request.assert_not_called()