@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 251 lines 8.99 KB
Raw
1 """Tests for the auto-upload directory allowlist (parity with TS sdk)."""
2
3 from __future__ import annotations
4
5 import os
6 import sys
7 from pathlib import Path
8
9 import pytest
10
11 from composio.core.models.base import allow_tracking
12 from composio.exceptions import (
13 FileUploadPathNotAllowedError,
14 SDKFileNotFoundError,
15 )
16 from composio.utils.upload_dir_allowlist import (
17 assert_path_inside_upload_dirs,
18 get_default_upload_dir,
19 resolve_effective_upload_allowlist,
20 )
21
22
23 @pytest.fixture(autouse=True)
24 def disable_telemetry():
25 token = allow_tracking.set(False)
26 yield
27 allow_tracking.reset(token)
28
29
30 class TestResolveEffectiveUploadAllowlist:
31 def test_none_returns_default(self):
32 result = resolve_effective_upload_allowlist(None)
33 default = get_default_upload_dir()
34 assert default is not None, "Expected a default upload dir in this env."
35 assert result == [default]
36
37 def test_empty_list_replaces_default(self):
38 assert resolve_effective_upload_allowlist([]) == []
39
40 def test_false_means_no_local_paths(self):
41 # Explicit "reject all local paths during auto-upload". Equivalent to []
42 # but reads better at call sites.
43 assert resolve_effective_upload_allowlist(False) == []
44
45 def test_explicit_list_replaces_default(self, tmp_path: Path):
46 result = resolve_effective_upload_allowlist([str(tmp_path)])
47 assert result == [tmp_path.resolve()]
48
49 def test_tilde_is_expanded(self):
50 result = resolve_effective_upload_allowlist(["~"])
51 assert result == [Path.home().resolve()]
52
53 def test_skips_blank_and_non_string_entries(self, tmp_path: Path):
54 result = resolve_effective_upload_allowlist([str(tmp_path), "", " "])
55 assert result == [tmp_path.resolve()]
56
57 def test_deduplicates_entries(self, tmp_path: Path):
58 result = resolve_effective_upload_allowlist([str(tmp_path), str(tmp_path)])
59 assert result == [tmp_path.resolve()]
60
61
62 class TestAssertPathInsideUploadDirs:
63 def test_accepts_direct_child(self, tmp_path: Path):
64 allowed = tmp_path
65 f = allowed / "a.txt"
66 f.write_text("hi")
67 assert_path_inside_upload_dirs(str(f), [allowed])
68
69 def test_accepts_nested_child(self, tmp_path: Path):
70 allowed = tmp_path
71 nested = allowed / "sub" / "deeper"
72 nested.mkdir(parents=True)
73 f = nested / "a.txt"
74 f.write_text("hi")
75 assert_path_inside_upload_dirs(str(f), [allowed])
76
77 def test_rejects_path_outside(self, tmp_path: Path):
78 allowed = tmp_path / "inside"
79 outside = tmp_path / "outside"
80 allowed.mkdir()
81 outside.mkdir()
82 f = outside / "a.txt"
83 f.write_text("hi")
84 with pytest.raises(FileUploadPathNotAllowedError):
85 assert_path_inside_upload_dirs(str(f), [allowed])
86
87 def test_enforces_component_boundary(self, tmp_path: Path):
88 # /tmp/foo must NOT accept /tmp/foo-bar/x.
89 allowed = tmp_path / "foo"
90 sibling = tmp_path / "foo-bar"
91 allowed.mkdir()
92 sibling.mkdir()
93 f = sibling / "x.txt"
94 f.write_text("hi")
95 with pytest.raises(FileUploadPathNotAllowedError):
96 assert_path_inside_upload_dirs(str(f), [allowed])
97
98 def test_raises_not_found_when_missing(self, tmp_path: Path):
99 allowed = tmp_path
100 missing = tmp_path / "does_not_exist.txt"
101 with pytest.raises(SDKFileNotFoundError) as excinfo:
102 assert_path_inside_upload_dirs(str(missing), [allowed])
103 assert "does not exist on disk" in str(excinfo.value)
104 assert "file_upload_dirs" in str(excinfo.value)
105
106 def test_empty_allowlist_fails_closed(self, tmp_path: Path):
107 f = tmp_path / "a.txt"
108 f.write_text("hi")
109 with pytest.raises(FileUploadPathNotAllowedError) as excinfo:
110 assert_path_inside_upload_dirs(str(f), [])
111 msg = str(excinfo.value)
112 assert "no upload directories are configured" in msg
113 assert "dangerously_allow_auto_upload_download_files" in msg
114
115 def test_error_message_lists_allowlist(self, tmp_path: Path):
116 allowed = tmp_path / "inside"
117 outside = tmp_path / "outside"
118 allowed.mkdir()
119 outside.mkdir()
120 f = outside / "a.txt"
121 f.write_text("hi")
122 with pytest.raises(FileUploadPathNotAllowedError) as excinfo:
123 assert_path_inside_upload_dirs(str(f), [allowed])
124 msg = str(excinfo.value)
125 assert str(allowed) in msg
126 assert "file_upload_dirs" in msg
127 assert "dangerously_allow_auto_upload_download_files" in msg
128
129 @pytest.mark.skipif(sys.platform == "win32", reason="POSIX-only symlink semantics")
130 def test_rejects_symlink_pointing_outside(self, tmp_path: Path):
131 allowed = tmp_path / "inside"
132 outside = tmp_path / "outside"
133 allowed.mkdir()
134 outside.mkdir()
135 secret = outside / "secret.txt"
136 secret.write_text("pw")
137 link = allowed / "link.txt"
138 os.symlink(secret, link)
139
140 with pytest.raises(FileUploadPathNotAllowedError):
141 assert_path_inside_upload_dirs(str(link), [allowed])
142
143
144 class TestToolsWiresAllowlistCorrectly:
145 """Verifies ``Tools`` forwards the right allowlist to ``FileHelper`` given
146 the full flag/value matrix."""
147
148 @staticmethod
149 def _make_tools(
150 dangerously_allow_auto_upload_download_files: bool,
151 file_upload_dirs,
152 ):
153 from composio.core.models.tools import Tools
154 from unittest.mock import Mock
155
156 return Tools(
157 client=Mock(),
158 provider=Mock(name="provider"),
159 dangerously_allow_auto_upload_download_files=dangerously_allow_auto_upload_download_files,
160 file_upload_dirs=file_upload_dirs,
161 )
162
163 def test_flag_off_means_no_allowlist_is_ever_built(self):
164 # Flag off ⇒ auto-upload code path is never run. We pass ``None`` to
165 # FileHelper so manual/no-op paths don't accidentally enforce anything.
166 t = self._make_tools(False, None)
167 assert t._file_helper._file_upload_allowlist is None # noqa: SLF001
168
169 def test_flag_off_ignores_file_upload_dirs_entirely(self):
170 t = self._make_tools(False, ["/tmp/whatever"])
171 assert t._file_helper._file_upload_allowlist is None # noqa: SLF001
172
173 t = self._make_tools(False, False)
174 assert t._file_helper._file_upload_allowlist is None # noqa: SLF001
175
176 t = self._make_tools(False, [])
177 assert t._file_helper._file_upload_allowlist is None # noqa: SLF001
178
179 def test_flag_on_none_uses_default(self):
180 t = self._make_tools(True, None)
181 assert t._file_helper._file_upload_allowlist == [ # noqa: SLF001
182 get_default_upload_dir()
183 ]
184
185 def test_flag_on_false_means_empty_allowlist(self):
186 t = self._make_tools(True, False)
187 assert t._file_helper._file_upload_allowlist == [] # noqa: SLF001
188
189 def test_flag_on_empty_list_means_empty_allowlist(self):
190 t = self._make_tools(True, [])
191 assert t._file_helper._file_upload_allowlist == [] # noqa: SLF001
192
193 def test_flag_on_explicit_dirs_replace_default(self, tmp_path: Path):
194 t = self._make_tools(True, [str(tmp_path)])
195 assert t._file_helper._file_upload_allowlist == [ # noqa: SLF001
196 tmp_path.resolve()
197 ]
198
199
200 class TestFileUploadableAllowlistWiring:
201 """Sanity: FileUploadable.from_path must honor the allowlist param."""
202
203 def test_from_path_with_allowlist_blocks_outside(self, tmp_path: Path):
204 from composio.core.models._files import FileUploadable
205 from unittest.mock import Mock
206
207 inside = tmp_path / "inside"
208 outside = tmp_path / "outside"
209 inside.mkdir()
210 outside.mkdir()
211 f = outside / "a.txt"
212 f.write_text("hi")
213
214 with pytest.raises(FileUploadPathNotAllowedError):
215 FileUploadable.from_path(
216 client=Mock(),
217 file=str(f),
218 tool="T",
219 toolkit="tk",
220 file_upload_allowlist=[inside],
221 )
222
223 def test_from_path_without_allowlist_skips_check(
224 self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
225 ):
226 """Passing ``file_upload_allowlist=None`` must NOT trigger the check.
227
228 This simulates manual upload paths and the no-auto-upload default.
229 """
230 from composio.core.models import _files as files_mod
231 from composio.core.models._files import FileUploadable
232 from unittest.mock import Mock
233
234 f = tmp_path / "a.txt"
235 f.write_text("hi")
236
237 # Short-circuit the network-bound parts.
238 mock_client = Mock()
239 mock_client.files.create_presigned_url.return_value = Mock(
240 new_presigned_url="https://example/upload", key="k"
241 )
242 monkeypatch.setattr(files_mod, "upload", lambda url, file, mimetype=None: True)
243
244 result = FileUploadable.from_path(
245 client=mock_client,
246 file=str(f),
247 tool="T",
248 toolkit="tk",
249 file_upload_allowlist=None,
250 )
251 assert result.s3key == "k"