Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

fix(python-sdk): prevent cross-tenant credential access via user_id kwarg [SEC-365]

Zen Agentf6cdf7aca0f2
diff --git a/python/composio/core/models/custom_tools.py b/python/composio/core/models/custom_tools.py
index 6149720e5..7a8875b0e 100644
--- a/python/composio/core/models/custom_tools.py
+++ b/python/composio/core/models/custom_tools.py
@@ -131,9 +131,33 @@ class CustomTool:
return account.state.val.model_dump()
def __call__(self, **kwargs: t.Any) -> t.Any:
- """Call the custom tool."""
- user_id = kwargs.pop("user_id", None) or "default"
- request = self.request_model.model_validate(kwargs)
+ """Call the custom tool with the default ``user_id``.
+
+ ``kwargs`` is treated as untrusted LLM-supplied input. ``user_id`` is
+ therefore NOT extracted from ``kwargs``: allowing an LLM (or
+ prompt-injection input) to pick which tenant's ``user_id`` is used to
+ look up OAuth credentials would let it read another tenant's tokens
+ (CWE-639, see SEC-365). Any ``user_id`` key in ``kwargs`` is dropped
+ before the request body is validated.
+
+ To use a non-default ``user_id``, call through
+ ``CustomTools.execute(slug, request, user_id)`` (the canonical entry
+ point), which sanitizes the request dict and supplies the trusted
+ ``user_id`` from server-side context.
+ """
+ kwargs.pop("user_id", None)
+ return self._invoke(user_id="default", request_kwargs=kwargs)
+
+ def _invoke(self, user_id: str, request_kwargs: t.Dict[str, t.Any]) -> t.Any:
+ """Trusted internal entry point.
+
+ Called by ``CustomTools.execute`` after ``request_kwargs`` has been
+ sanitized at the LLM trust boundary. ``user_id`` is supplied here as
+ a positional / keyword argument that is structurally separate from
+ the ``request_kwargs`` dict, so an LLM-controlled key inside the
+ dict cannot be promoted to override the trusted value.
+ """
+ request = self.request_model.model_validate(request_kwargs)
if self.toolkit is None:
return t.cast(CustomToolProtocol, self.f)(request=request)
@@ -203,8 +227,21 @@ class CustomTools:
request: t.Dict,
user_id: t.Optional[str] = None,
) -> t.Dict:
- """Execute a custom tool."""
+ """Execute a custom tool.
+
+ ``request`` is the LLM-supplied argument dict and is treated as
+ untrusted: any ``user_id`` key it contains is stripped here so that
+ only the explicit ``user_id`` parameter (sourced from trusted
+ server-side context) is used to look up auth credentials. Without
+ this sanitization an LLM (or prompt-injection input) could specify
+ another tenant's ``user_id`` and read their OAuth tokens
+ (CWE-639, see SEC-365).
+ """
custom_tool = self.get(slug)
if custom_tool is None:
raise NotFoundError(f"Custom tool with slug {slug} not found")
- return custom_tool(**request, user_id=user_id)
+ sanitized_request = {k: v for k, v in request.items() if k != "user_id"}
+ return custom_tool._invoke(
+ user_id=user_id or "default",
+ request_kwargs=sanitized_request,
+ )
diff --git a/python/tests/test_custom_tools_security.py b/python/tests/test_custom_tools_security.py
new file mode 100644
index 000000000..03ed4f379
--- /dev/null
+++ b/python/tests/test_custom_tools_security.py
@@ -0,0 +1,195 @@
+"""Security tests for the legacy ``CustomTool`` / ``CustomTools`` API.
+
+Regression tests for SEC-365 (CWE-639, cross-tenant credential access).
+
+Before the fix, ``CustomTool.__call__`` extracted ``user_id`` from the same
+``**kwargs`` dict that carried LLM-supplied tool arguments, and
+``CustomTools.execute`` happily forwarded that LLM-controlled dict via
+``custom_tool(**request, user_id=user_id)``. An LLM (or prompt-injection
+input) could therefore set ``user_id`` to another tenant's identifier —
+either getting it forwarded into ``CustomTool.__call__`` directly or
+relying on Python's duplicate-kwarg ``TypeError`` to leak whose tokens
+were nearly looked up.
+
+The fix:
+ * ``CustomTools.execute`` strips ``user_id`` from the LLM-supplied
+ ``request`` dict at the trust boundary, so only the trusted, explicit
+ ``user_id`` parameter reaches the credential lookup.
+ * ``CustomTool.__call__`` exposes ``user_id`` as a keyword-only
+ parameter and drops any ``user_id`` left in ``kwargs`` before the
+ request body is validated.
+"""
+
+from unittest.mock import MagicMock
+
+import pytest
+from pydantic import BaseModel, Field
+
+from composio.core.models.custom_tools import CustomTool, CustomTools
+from composio.exceptions import NotFoundError
+
+
+class _IssueInput(BaseModel):
+ issue_number: int = Field(description="GitHub issue number")
+
+
+def _make_mock_client() -> MagicMock:
+ """Build a mock HttpClient suitable for CustomTool.__get_auth_credentials."""
+
+ state = MagicMock()
+ state.val.model_dump.return_value = {"access_token": "trusted-token"}
+
+ account = MagicMock()
+ account.created_at = "2026-01-01T00:00:00Z"
+ account.state = state
+
+ response = MagicMock()
+ response.items = [account]
+
+ client = MagicMock()
+ client.connected_accounts.list.return_value = response
+ client.tools.proxy = MagicMock()
+ return client
+
+
+def test_execute_strips_user_id_from_llm_request() -> None:
+ """``CustomTools.execute`` MUST strip ``user_id`` from the request dict."""
+ client = _make_mock_client()
+ tools = CustomTools(client=client)
+
+ def github_tool(request: _IssueInput, execute_request, auth_credentials):
+ """Fetch issue info."""
+ return {"token": auth_credentials["access_token"]}
+
+ tools.register(toolkit="github")(github_tool)
+
+ result = tools.execute(
+ slug="GITHUB_GITHUB_TOOL",
+ request={"issue_number": 1, "user_id": "victim-user"},
+ user_id="trusted-user",
+ )
+
+ assert result == {"token": "trusted-token"}
+ # Only the trusted, explicit user_id reaches the credential lookup.
+ client.connected_accounts.list.assert_called_once_with(
+ toolkit_slugs=["github"],
+ user_ids=["trusted-user"],
+ )
+
+
+def test_execute_falls_back_to_default_when_user_id_missing() -> None:
+ """If the caller forgets a trusted ``user_id``, fall back to ``"default"``.
+
+ Specifically, ``execute`` must NOT silently use the LLM-supplied
+ ``user_id`` from ``request`` as a fallback — that would re-introduce
+ the SEC-365 vulnerability under a different code path.
+ """
+ client = _make_mock_client()
+ tools = CustomTools(client=client)
+
+ def github_tool(request: _IssueInput, execute_request, auth_credentials):
+ """Fetch issue info."""
+ return {"ok": True}
+
+ tools.register(toolkit="github")(github_tool)
+
+ tools.execute(
+ slug="GITHUB_GITHUB_TOOL",
+ request={"issue_number": 1, "user_id": "victim-user"},
+ user_id=None,
+ )
+
+ client.connected_accounts.list.assert_called_once_with(
+ toolkit_slugs=["github"],
+ user_ids=["default"],
+ )
+
+
+def test_execute_does_not_mutate_caller_request_dict() -> None:
+ """Sanitization MUST NOT mutate the caller's ``request`` dict in place."""
+ client = _make_mock_client()
+ tools = CustomTools(client=client)
+
+ def github_tool(request: _IssueInput, execute_request, auth_credentials):
+ """Fetch issue info."""
+ return {"ok": True}
+
+ tools.register(toolkit="github")(github_tool)
+
+ request_dict = {"issue_number": 1, "user_id": "victim-user"}
+ tools.execute(
+ slug="GITHUB_GITHUB_TOOL",
+ request=request_dict,
+ user_id="trusted-user",
+ )
+
+ # The original dict is untouched (the SDK should not surprise the caller).
+ assert request_dict == {"issue_number": 1, "user_id": "victim-user"}
+
+
+def test_call_drops_user_id_smuggled_in_kwargs_dict() -> None:
+ """``CustomTool.__call__`` MUST ignore any ``user_id`` smuggled in kwargs.
+
+ A caller might unpack a partially-trusted dict like ``tool(**llm_dict)``
+ without realizing that the LLM controls every key in it. Before the fix,
+ a ``user_id`` key in that dict was popped and used to look up OAuth
+ credentials — letting an LLM read another tenant's tokens. After the fix,
+ ``__call__`` always uses the safe ``"default"`` user_id and the smuggled
+ value is dropped before request validation.
+ """
+ client = _make_mock_client()
+
+ def github_tool(request: _IssueInput, execute_request, auth_credentials):
+ """Fetch issue info."""
+ return {"ok": True}
+
+ tool = CustomTool(f=github_tool, client=client, toolkit="github")
+
+ smuggled_kwargs = {"issue_number": 1, "user_id": "victim-user"}
+ tool(**smuggled_kwargs)
+
+ client.connected_accounts.list.assert_called_once_with(
+ toolkit_slugs=["github"],
+ user_ids=["default"],
+ )
+
+
+def test_invoke_uses_trusted_user_id_separate_from_request() -> None:
+ """``_invoke`` takes ``user_id`` separately from the request dict.
+
+ This is the trusted internal entry point used by ``CustomTools.execute``.
+ Even if the LLM-controlled ``request_kwargs`` happens to contain a
+ ``user_id`` key (and somehow gets past the sanitization in ``execute``),
+ the ``user_id`` argument here is structurally separate, so the trusted
+ value is what reaches the credential lookup.
+ """
+ client = _make_mock_client()
+
+ captured: dict = {}
+
+ def github_tool(request: _IssueInput, execute_request, auth_credentials):
+ """Fetch issue info."""
+ captured["request"] = request
+ return {"ok": True}
+
+ tool = CustomTool(f=github_tool, client=client, toolkit="github")
+ tool._invoke(user_id="trusted-user", request_kwargs={"issue_number": 7})
+
+ client.connected_accounts.list.assert_called_once_with(
+ toolkit_slugs=["github"],
+ user_ids=["trusted-user"],
+ )
+ assert captured["request"].issue_number == 7
+
+
+def test_execute_unknown_slug_raises() -> None:
+ """Sanity check: unknown slugs still surface ``NotFoundError``."""
+ client = _make_mock_client()
+ tools = CustomTools(client=client)
+
+ with pytest.raises(NotFoundError):
+ tools.execute(
+ slug="DOES_NOT_EXIST",
+ request={"foo": "bar"},
+ user_id="trusted-user",
+ )