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 —
10
-either getting it forwarded into ``CustomTool.__call__`` directly or
11
-relying on Python's duplicate-kwarg ``TypeError`` to leak whose tokens
12
-were nearly looked up.
13
-
14
-The fix:
15
- * ``CustomTools.execute`` strips ``user_id`` from the LLM-supplied
16
- ``request`` dict at the trust boundary, so only the trusted, explicit
17
- ``user_id`` parameter reaches the credential lookup.
18
- * ``CustomTool.__call__`` exposes ``user_id`` as a keyword-only
19
- parameter and drops any ``user_id`` left in ``kwargs`` before the
20
- request body is validated.
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
35
issue_number: int = Field(description="GitHub issue number")
36
37
36
-def _make_mock_client() -> MagicMock:
37
- """Build a mock HttpClient suitable for CustomTool.__get_auth_credentials."""
38
-
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
54
return client
55
56
55
-def test_execute_strips_user_id_from_llm_request() -> None:
56
- """``CustomTools.execute`` MUST strip ``user_id`` from the request dict."""
57
- client = _make_mock_client()
58
- tools = CustomTools(client=client)
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."""
62
- return {"token": auth_credentials["access_token"]}
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
64
- tools.register(toolkit="github")(github_tool)
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
66
- result = tools.execute(
67
- slug="GITHUB_GITHUB_TOOL",
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
72
- assert result == {"token": "trusted-token"}
73
- # Only the trusted, explicit user_id reaches the credential lookup.
74
- client.connected_accounts.list.assert_called_once_with(
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
80
-def test_execute_falls_back_to_default_when_user_id_missing() -> None:
81
- """If the caller forgets a trusted ``user_id``, fall back to ``"default"``.
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
83
- Specifically, ``execute`` must NOT silently use the LLM-supplied
84
- ``user_id`` from ``request`` as a fallback — that would re-introduce
85
- the SEC-365 vulnerability under a different code path.
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
"""
87
- client = _make_mock_client()
88
- tools = CustomTools(client=client)
105
+ captured: dict = {}
106
90
- def github_tool(request: _IssueInput, execute_request, auth_credentials):
91
- """Fetch issue info."""
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
94
- tools.register(toolkit="github")(github_tool)
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(
97
- slug="GITHUB_GITHUB_TOOL",
98
- request={"issue_number": 1, "user_id": "victim-user"},
99
- user_id=None,
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
102
- client.connected_accounts.list.assert_called_once_with(
128
+ assert captured["fields"] == {"issue_number": 1}
129
+ mock_http_client.connected_accounts.list.assert_called_once_with(
130
toolkit_slugs=["github"],
104
- user_ids=["default"],
131
+ user_ids=["trusted-user"],
132
)
133
134
108
-def test_execute_does_not_mutate_caller_request_dict() -> None:
109
- """Sanitization MUST NOT mutate the caller's ``request`` dict in place."""
110
- client = _make_mock_client()
111
- tools = CustomTools(client=client)
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
113
- def github_tool(request: _IssueInput, execute_request, auth_credentials):
114
- """Fetch issue info."""
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
117
- tools.register(toolkit="github")(github_tool)
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
119
- request_dict = {"issue_number": 1, "user_id": "victim-user"}
154
tools.execute(
121
- slug="GITHUB_GITHUB_TOOL",
122
- request=request_dict,
155
+ slug=tool.slug,
156
+ request={"issueNumber": 5, "user_id": "victim"},
157
user_id="trusted-user",
158
)
159
126
- # The original dict is untouched (the SDK should not surprise the caller).
127
- assert request_dict == {"issue_number": 1, "user_id": "victim-user"}
160
+ assert captured["fields"] == {"issue_number": 5}
161
162
130
-def test_call_drops_user_id_smuggled_in_kwargs_dict() -> None:
131
- """``CustomTool.__call__`` MUST ignore any ``user_id`` smuggled in kwargs.
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
133
- A caller might unpack a partially-trusted dict like ``tool(**llm_dict)``
134
- without realizing that the LLM controls every key in it. Before the fix,
135
- a ``user_id`` key in that dict was popped and used to look up OAuth
136
- credentials — letting an LLM read another tenant's tokens. After the fix,
137
- ``__call__`` always uses the safe ``"default"`` user_id and the smuggled
138
- value is dropped before request validation.
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
"""
140
- client = _make_mock_client()
141
-
142
- def github_tool(request: _IssueInput, execute_request, auth_credentials):
143
- """Fetch issue info."""
144
- return {"ok": True}
145
-
146
- tool = CustomTool(f=github_tool, client=client, toolkit="github")
147
-
148
- smuggled_kwargs = {"issue_number": 1, "user_id": "victim-user"}
149
- tool(**smuggled_kwargs)
172
+ custom_tools.execute(
173
+ slug=github_tool.slug,
174
+ request={"issue_number": 1, "user_id": "victim-user"},
175
+ user_id=None,
176
+ )
177
151
- client.connected_accounts.list.assert_called_once_with(
178
+ mock_http_client.connected_accounts.list.assert_called_once_with(
179
toolkit_slugs=["github"],
180
user_ids=["default"],
181
)
182
183
157
-def test_invoke_uses_trusted_user_id_separate_from_request() -> None:
158
- """``_invoke`` takes ``user_id`` separately from the request dict.
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
160
- This is the trusted internal entry point used by ``CustomTools.execute``.
161
- Even if the LLM-controlled ``request_kwargs`` happens to contain a
162
- ``user_id`` key (and somehow gets past the sanitization in ``execute``),
163
- the ``user_id`` argument here is structurally separate, so the trusted
164
- value is what reaches the credential lookup.
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
"""
166
- client = _make_mock_client()
208
+ smuggled_kwargs = {"issue_number": 1, "user_id": "victim-user"}
209
168
- captured: dict = {}
210
+ with pytest.raises(TypeError, match="user_id"):
211
+ github_tool(**smuggled_kwargs)
212
170
- def github_tool(request: _IssueInput, execute_request, auth_credentials):
171
- """Fetch issue info."""
172
- captured["request"] = request
173
- return {"ok": True}
213
175
- tool = CustomTool(f=github_tool, client=client, toolkit="github")
176
- tool._invoke(user_id="trusted-user", request_kwargs={"issue_number": 7})
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
178
- client.connected_accounts.list.assert_called_once_with(
228
+ mock_http_client.connected_accounts.list.assert_called_once_with(
229
toolkit_slugs=["github"],
230
user_ids=["trusted-user"],
231
)
182
- assert captured["request"].issue_number == 7
232
233
185
-def test_execute_unknown_slug_raises() -> None:
234
+def test_execute_unknown_slug_raises(custom_tools: CustomTools) -> None:
235
"""Sanity check: unknown slugs still surface ``NotFoundError``."""
187
- client = _make_mock_client()
188
- tools = CustomTools(client=client)
189
-
236
with pytest.raises(NotFoundError):
191
- tools.execute(
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
+ )