1
-"""Security tests for the legacy ``CustomTool`` / ``CustomTools`` API.
2
-
3
-Regression tests for SEC-365 (CWE-639, cross-tenant credential access).
4
-
5
-Before the fix, ``CustomTool.__call__`` extracted ``user_id`` from the same
6
-``**kwargs`` dict that carried LLM-supplied tool arguments, and
7
-``CustomTools.execute`` happily forwarded that LLM-controlled dict via
8
-``custom_tool(**request, user_id=user_id)``. An LLM (or prompt-injection
9
-input) could therefore set ``user_id`` to another tenant's identifier and
10
-have ``CustomTool.__get_auth_credentials`` look up that victim's OAuth
11
-tokens.
12
-
13
-After the fix:
14
- * ``CustomTools.execute`` filters the LLM-supplied ``request`` through
15
- an *allowlist* of fields declared on the tool's Pydantic
16
- ``request_model`` — ``user_id`` and any other unexpected key are
17
- dropped before they can influence credential lookup or reach the
18
- tool's execute function.
19
- * ``CustomTool.invoke_trusted`` is the structurally separate trusted
20
- entry point; ``user_id`` cannot be smuggled inside ``request_kwargs``.
21
- * ``CustomTool.__call__`` raises ``TypeError`` if ``user_id`` appears
22
- in ``kwargs`` rather than silently using a fallback.
23
-"""
24
-
25
-from unittest.mock import MagicMock
26
-
27
-import pytest
28
-from pydantic import BaseModel, Field
29
-
30
-from composio.core.models.custom_tools import CustomTool, CustomTools
31
-from composio.exceptions import NotFoundError
32
-
33
-
34
-class _IssueInput(BaseModel):
35
- issue_number: int = Field(description="GitHub issue number")
36
-
37
-
38
-@pytest.fixture
39
-def mock_http_client() -> MagicMock:
40
- """Mock HttpClient that returns a single connected account on lookup."""
41
- state = MagicMock()
42
- state.val.model_dump.return_value = {"access_token": "trusted-token"}
43
-
44
- account = MagicMock()
45
- account.created_at = "2026-01-01T00:00:00Z"
46
- account.state = state
47
-
48
- response = MagicMock()
49
- response.items = [account]
50
-
51
- client = MagicMock()
52
- client.connected_accounts.list.return_value = response
53
- client.tools.proxy = MagicMock()
54
- return client
55
-
56
-
57
-@pytest.fixture
58
-def github_tool(mock_http_client: MagicMock) -> CustomTool:
59
- """A registered ``github`` custom tool whose handler echoes the auth token."""
60
-
61
- def github_tool(request: _IssueInput, execute_request, auth_credentials):
62
- """Fetch issue info."""
63
- return {
64
- "issue_number": request.issue_number,
65
- "token": auth_credentials["access_token"],
66
- }
67
-
68
- return CustomTool(f=github_tool, client=mock_http_client, toolkit="github")
69
-
70
-
71
-@pytest.fixture
72
-def custom_tools(mock_http_client: MagicMock, github_tool: CustomTool) -> CustomTools:
73
- """A ``CustomTools`` registry pre-loaded with ``github_tool``."""
74
- tools = CustomTools(client=mock_http_client)
75
- tools.custom_tools_registry[github_tool.slug] = github_tool
76
- return tools
77
-
78
-
79
-def test_execute_strips_user_id_from_llm_request(
80
- mock_http_client: MagicMock, custom_tools: CustomTools, github_tool: CustomTool
81
-) -> None:
82
- """``CustomTools.execute`` MUST drop ``user_id`` from the request dict."""
83
- result = custom_tools.execute(
84
- slug=github_tool.slug,
85
- request={"issue_number": 1, "user_id": "victim-user"},
86
- user_id="trusted-user",
87
- )
88
-
89
- assert result == {"issue_number": 1, "token": "trusted-token"}
90
- mock_http_client.connected_accounts.list.assert_called_once_with(
91
- toolkit_slugs=["github"],
92
- user_ids=["trusted-user"],
93
- )
94
-
95
-
96
-def test_execute_strips_unexpected_fields_via_allowlist(
97
- mock_http_client: MagicMock, custom_tools: CustomTools, github_tool: CustomTool
98
-) -> None:
99
- """The allowlist is durable: any non-declared key is dropped, not just user_id.
100
-
101
- Pins the allowlist behaviour so a future identity-bearing key in the
102
- auth path (``tenant_id``, ``org_id``, ``connected_account_id``, …)
103
- cannot re-introduce CWE-639 by being smuggled through ``request``.
104
- """
105
- captured: dict = {}
106
-
107
- def echo_tool(request: _IssueInput, execute_request, auth_credentials):
108
- """Echo back the validated request."""
109
- captured["fields"] = request.model_dump()
110
- return {"ok": True}
111
-
112
- tools = CustomTools(client=mock_http_client)
113
- tool = CustomTool(f=echo_tool, client=mock_http_client, toolkit="github")
114
- tools.custom_tools_registry[tool.slug] = tool
115
-
116
- tools.execute(
117
- slug=tool.slug,
118
- request={
119
- "issue_number": 1,
120
- "user_id": "victim-user",
121
- "tenant_id": "evil-tenant",
122
- "connected_account_id": "ca_evil",
123
- "extra_garbage": "xyz",
124
- },
125
- user_id="trusted-user",
126
- )
127
-
128
- assert captured["fields"] == {"issue_number": 1}
129
- mock_http_client.connected_accounts.list.assert_called_once_with(
130
- toolkit_slugs=["github"],
131
- user_ids=["trusted-user"],
132
- )
133
-
134
-
135
-def test_execute_keeps_aliases_declared_on_request_model(
136
- mock_http_client: MagicMock,
137
-) -> None:
138
- """Aliases declared on the request model are honoured by the allowlist."""
139
-
140
- class _AliasedInput(BaseModel):
141
- issue_number: int = Field(alias="issueNumber")
142
-
143
- captured: dict = {}
144
-
145
- def aliased_tool(request: _AliasedInput, execute_request, auth_credentials):
146
- """Echo back the validated request."""
147
- captured["fields"] = request.model_dump()
148
- return {"ok": True}
149
-
150
- tools = CustomTools(client=mock_http_client)
151
- tool = CustomTool(f=aliased_tool, client=mock_http_client, toolkit="github")
152
- tools.custom_tools_registry[tool.slug] = tool
153
-
154
- tools.execute(
155
- slug=tool.slug,
156
- request={"issueNumber": 5, "user_id": "victim"},
157
- user_id="trusted-user",
158
- )
159
-
160
- assert captured["fields"] == {"issue_number": 5}
161
-
162
-
163
-def test_execute_falls_back_to_default_when_user_id_missing(
164
- mock_http_client: MagicMock, custom_tools: CustomTools, github_tool: CustomTool
165
-) -> None:
166
- """If no trusted ``user_id`` is provided, ``execute`` MUST use ``"default"``.
167
-
168
- Specifically, ``execute`` MUST NOT silently use the LLM-supplied
169
- ``user_id`` from ``request`` as a fallback — that would re-introduce
170
- SEC-365 under a different code path.
171
- """
172
- custom_tools.execute(
173
- slug=github_tool.slug,
174
- request={"issue_number": 1, "user_id": "victim-user"},
175
- user_id=None,
176
- )
177
-
178
- mock_http_client.connected_accounts.list.assert_called_once_with(
179
- toolkit_slugs=["github"],
180
- user_ids=["default"],
181
- )
182
-
183
-
184
-def test_execute_does_not_mutate_caller_request_dict(
185
- custom_tools: CustomTools, github_tool: CustomTool
186
-) -> None:
187
- """Sanitization MUST NOT mutate the caller's ``request`` dict in place."""
188
- request_dict = {"issue_number": 1, "user_id": "victim-user"}
189
- custom_tools.execute(
190
- slug=github_tool.slug,
191
- request=request_dict,
192
- user_id="trusted-user",
193
- )
194
-
195
- assert request_dict == {"issue_number": 1, "user_id": "victim-user"}
196
-
197
-
198
-def test_call_raises_typeerror_when_user_id_smuggled_in_kwargs(
199
- github_tool: CustomTool,
200
-) -> None:
201
- """``CustomTool.__call__`` MUST refuse a ``user_id`` kwarg loudly.
202
-
203
- Silent fallbacks expand blast radius: a tool that quietly transacts
204
- against ``"default"`` could end up using a real tenant who happens to
205
- be registered under that id. Failing fast also surfaces prompt-injection
206
- attempts instead of swallowing them.
207
- """
208
- smuggled_kwargs = {"issue_number": 1, "user_id": "victim-user"}
209
-
210
- with pytest.raises(TypeError, match="user_id"):
211
- github_tool(**smuggled_kwargs)
212
-
213
-
214
-def test_invoke_trusted_uses_explicit_user_id_over_smuggled_one(
215
- mock_http_client: MagicMock, github_tool: CustomTool
216
-) -> None:
217
- """``invoke_trusted``'s explicit ``user_id`` MUST win over a smuggled key.
218
-
219
- The auth-path argument is structurally separate from ``request_kwargs``,
220
- so an LLM-controlled ``user_id`` inside ``request_kwargs`` cannot
221
- override the trusted parameter. This pins the docstring contract.
222
- """
223
- github_tool.invoke_trusted(
224
- user_id="trusted-user",
225
- request_kwargs={"issue_number": 7, "user_id": "victim-user"},
226
- )
227
-
228
- mock_http_client.connected_accounts.list.assert_called_once_with(
229
- toolkit_slugs=["github"],
230
- user_ids=["trusted-user"],
231
- )
232
-
233
-
234
-def test_execute_unknown_slug_raises(custom_tools: CustomTools) -> None:
235
- """Sanity check: unknown slugs still surface ``NotFoundError``."""
236
- with pytest.raises(NotFoundError):
237
- custom_tools.execute(
238
- slug="DOES_NOT_EXIST",
239
- request={"foo": "bar"},
240
- user_id="trusted-user",
241
- )
242
-
243
-
244
-def test_tools_execute_e2e_strips_user_id_through_full_stack(
245
- mock_http_client: MagicMock, custom_tools: CustomTools, github_tool: CustomTool
246
-) -> None:
247
- """End-to-end check through ``Tools.execute`` (the public SDK entry point).
248
-
249
- ``Tools._execute_tool`` routes custom-tool calls into
250
- ``CustomTools.execute``. The modifier-hook block can also overwrite
251
- ``arguments`` and ``user_id`` from a ``processed_params`` dict, so we
252
- pin that the sanitization still applies after that path.
253
- """
254
- from composio.core.models.tools import Tools
255
-
256
- provider = MagicMock()
257
- provider.name = "test"
258
-
259
- tools = Tools(client=mock_http_client, provider=provider)
260
- tools._custom_tools = custom_tools
261
-
262
- response = tools.execute(
263
- slug=github_tool.slug,
264
- arguments={"issue_number": 1, "user_id": "victim-user"},
265
- user_id="trusted-user",
266
- )
267
-
268
- assert response["successful"] is True
269
- assert response["data"]["issue_number"] == 1
270
- assert response["data"]["token"] == "trusted-token"
271
- mock_http_client.connected_accounts.list.assert_called_once_with(
272
- toolkit_slugs=["github"],
273
- user_ids=["trusted-user"],
274
- )