@devries-cameron20 / composio Mirror · read-only / commits / 85996c4a1
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, 13:30 UTC

fix(sdk): harden pusher auth and cross-origin redirect headers (#4406)

This PR: - wraps `pysher.Pusher` in `_ComposioPusher`, whose channel-auth POST carries a `(5, 15)` connect/read timeout and raises `TriggerSubscriptionAuthError` (a `TriggerSubscriptionError`) on a transport failure, a non-200, or a response without an `auth` token — pysher 1.0.8 sent it with no timeout and turned a non-200 into a bare `AssertionError` on the websocket thread, on every (re)subscribe - keeps that POST a plain `requests.post(..., timeout=...)` rather than routing it through `safe_request`: the endpoint is built from the configured Composio API base URL, a fixed trusted host, not a value from a response, and the SSRF guard would refuse a local dev base URL - validates `pusher_cluster` against `^[a-z0-9-]+$` (non-empty, at most 64 chars) before pysher formats it into `ws-{cluster}.pusher.com`, raising `InvalidPusherClusterError` that names the shape violation without echoing the value - replaces the `unittest.mock.MagicMock` stand-in for pysher's connection logger with a dedicated `logging.Logger` (`NullHandler`, `propagate=False`, disabled), so `unittest` leaves the runtime import graph while raw frames stay out of user logs; a test asserts the module source no longer mentions `unittest` - strips `Authorization`, `Proxy-Authorization`, and `Cookie` from the next hop when `ssrfSafeFetch` or `safe_request` follows a redirect to a different origin; same-origin hops keep them. Manual redirect following bypasses both `fetch`'s cross-origin rule and `requests`' `rebuild_auth`, so neither guard applied it before — the gap #4387 left out - `@composio/slim` has no mirrored source (its build copies `core/dist`), so the changeset covers `@composio/core` and `@composio/slim` as patches Verified with `pytest tests/test_triggers.py tests/test_url_safety.py tests/test_path_join_guardrail.py` (192 passed), `ruff check` / `ruff format --check` on the changed files, `mypy --config-file config/mypy.ini` on the three changed modules with the noxfile's stub pins (no issues), `vitest run test/utils/ssrfGuard.test.ts` in `@composio/core` (42 passed), `pnpm typecheck` at the root (14 tasks successful), and `oxlint` + `prettier --check` on the changed TypeScript files. https://claude.ai/code/session_016ZuBv7JhVdSYTLYcTy2VJr

Alberto Schiabel committed Sep 9, 2026 at 21:29 UTC 85996c4a1d7cfffa8ec7b94c21dfb98d5a616614
8 files changed +605 -13
.changeset/strip-credentials-cross-origin-redirect.md new
+6
@@ -0,0 +1,6 @@
1 +---
2 +'@composio/core': patch
3 +'@composio/slim': patch
4 +---
5 +
6 +Drop `Authorization`, `Proxy-Authorization`, and `Cookie` from the request headers when the SSRF guard follows a redirect to a different origin, as the Fetch standard does for automatic redirects. Same-origin redirects keep them.
python/composio/core/models/triggers.py
+126 -5
@@ -5,6 +5,8 @@ import functools
5 import hashlib
6 import hmac
7 import json
8 +import logging
9 +import re
10 import threading
11 import time
12 import traceback
@@ -12,8 +14,8 @@ import typing as t
14 import uuid
15 from concurrent.futures import ThreadPoolExecutor
16 from enum import Enum
15 -from unittest import mock
17
18 +import requests
19 import typing_extensions as te
20 from composio_client import APIStatusError, Omit, omit
21 from composio_client.types import TriggersTypeRetrieveResponse
@@ -33,6 +35,22 @@ from composio.utils.pydantic import none_to_omit
35
36 PUSHER_AUTH_URL = "{base_url}/api/v3/internal/sdk/realtime/auth?source=python"
37
38 +# `(connect, read)` seconds for the channel-auth POST pysher makes on the
39 +# websocket thread. pysher itself sends it with no timeout, so an auth endpoint
40 +# that accepts the connection and never answers would hang every (re)subscribe
41 +# for the life of the process.
42 +PUSHER_AUTH_TIMEOUT: t.Tuple[float, float] = (5.0, 15.0)
43 +
44 +# Pusher cluster names are short lowercase labels such as ``mt1`` or
45 +# ``ap-southeast-1``. pysher formats the value straight into the websocket
46 +# host (``ws-{cluster}.pusher.com``), and it arrives in an API response, which
47 +# `python/AGENTS.md` treats as untrusted: anything outside this shape could
48 +# steer the socket to a host of the response author's choosing.
49 +_PUSHER_CLUSTER_PATTERN = re.compile(r"^[a-z0-9-]+$")
50 +_PUSHER_CLUSTER_MAX_LENGTH = 64
51 +
52 +_PYSHER_LOGGER_NAME = "composio.core.models.triggers.pysher"
53 +
54 """
55 export type _TriggerData = {
56 appName: string;
@@ -836,6 +854,109 @@ class TriggerSubscription(Resource):
854 self._connection._connect() # pylint: disable=protected-access
855
856
857 +def _silent_pysher_logger() -> logging.Logger:
858 + """A logger that swallows pysher's connection chatter.
859 +
860 + pysher logs every raw websocket frame at ``INFO``, trigger payloads
861 + included. Handing its connection a logger that neither emits nor propagates
862 + keeps those frames out of the user's log output; the SDK logs what it needs
863 + itself from ``TriggerSubscription``.
864 + """
865 + logger = logging.getLogger(_PYSHER_LOGGER_NAME)
866 + if not any(isinstance(handler, logging.NullHandler) for handler in logger.handlers):
867 + logger.addHandler(logging.NullHandler())
868 + logger.propagate = False
869 + logger.setLevel(logging.CRITICAL)
870 + logger.disabled = True
871 + return logger
872 +
873 +
874 +def _validate_pusher_cluster(cluster: object) -> str:
875 + """Return ``cluster`` once it is known to be a plain cluster label.
876 +
877 + :raises InvalidPusherClusterError: naming the shape violation, never the
878 + value itself, since the response that carried it is untrusted.
879 + """
880 + if not isinstance(cluster, str) or not cluster:
881 + raise exceptions.InvalidPusherClusterError(
882 + "Realtime credentials did not include a pusher cluster"
883 + )
884 + if len(cluster) > _PUSHER_CLUSTER_MAX_LENGTH:
885 + raise exceptions.InvalidPusherClusterError(
886 + "Realtime credentials returned a pusher cluster longer than "
887 + f"{_PUSHER_CLUSTER_MAX_LENGTH} characters"
888 + )
889 + if _PUSHER_CLUSTER_PATTERN.match(cluster) is None:
890 + raise exceptions.InvalidPusherClusterError(
891 + "Realtime credentials returned a pusher cluster with characters "
892 + "outside [a-z0-9-]"
893 + )
894 + return cluster
895 +
896 +
897 +class _ComposioPusher(Pusher):
898 + """``pysher.Pusher`` with a bounded, typed channel-auth request.
899 +
900 + pysher's own ``_generate_auth_token``/``_generate_presence_token`` POST to
901 + the auth endpoint with no timeout and turn a non-200 into a bare
902 + ``AssertionError``, on the websocket thread, on every (re)subscribe. Both
903 + are replaced here: the request carries :data:`PUSHER_AUTH_TIMEOUT`, and any
904 + failure surfaces as :class:`~composio.exceptions.TriggerSubscriptionAuthError`.
905 +
906 + The endpoint is the Composio API base URL the client was configured with,
907 + a fixed trusted host rather than a value from a response, so the request
908 + is a plain ``requests.post`` instead of going through the SSRF guard.
909 + """
910 +
911 + def _generate_auth_token(self, channel_name: str) -> str:
912 + if self.secret or not self.auth_endpoint:
913 + return t.cast(str, super()._generate_auth_token(channel_name))
914 + return self._request_channel_auth(
915 + {"channel_name": channel_name, "socket_id": self.connection.socket_id}
916 + )
917 +
918 + def _generate_presence_token(self, channel_name: str) -> str:
919 + if self.secret or not self.auth_endpoint:
920 + return t.cast(str, super()._generate_presence_token(channel_name))
921 + return self._request_channel_auth(
922 + {
923 + "channel_name": channel_name,
924 + "socket_id": self.connection.socket_id,
925 + "user_data": self.user_data,
926 + }
927 + )
928 +
929 + def _request_channel_auth(self, request_data: t.Dict[str, t.Any]) -> str:
930 + try:
931 + response = requests.post(
932 + self.auth_endpoint,
933 + data=request_data,
934 + headers=self.auth_endpoint_headers,
935 + timeout=PUSHER_AUTH_TIMEOUT,
936 + )
937 + except requests.RequestException as error:
938 + raise exceptions.TriggerSubscriptionAuthError(
939 + f"Could not reach the realtime auth endpoint: {type(error).__name__}"
940 + ) from error
941 +
942 + if response.status_code != 200:
943 + raise exceptions.TriggerSubscriptionAuthError(
944 + f"Realtime channel auth failed with HTTP {response.status_code}"
945 + )
946 +
947 + try:
948 + auth = response.json()["auth"]
949 + except (ValueError, KeyError, TypeError) as error:
950 + raise exceptions.TriggerSubscriptionAuthError(
951 + "Realtime channel auth response did not carry an `auth` token"
952 + ) from error
953 + if not isinstance(auth, str) or not auth:
954 + raise exceptions.TriggerSubscriptionAuthError(
955 + "Realtime channel auth response did not carry an `auth` token"
956 + )
957 + return auth
958 +
959 +
960 class _SubcriptionBuilder(WithLogger):
961 """Pusher client for Composio SDK."""
962
@@ -879,9 +1000,9 @@ class _SubcriptionBuilder(WithLogger):
1000
1001 def _get_pusher_instance(self, key: str, cluster: str) -> Pusher:
1002 """Get a pusher instance."""
882 - return Pusher(
1003 + return _ComposioPusher(
1004 key=key,
884 - cluster=cluster,
1005 + cluster=_validate_pusher_cluster(cluster),
1006 auth_endpoint=PUSHER_AUTH_URL.format(base_url=self._client.base_url),
1007 auth_endpoint_headers={
1008 "x-api-key": self._client.api_key,
@@ -899,8 +1020,8 @@ class _SubcriptionBuilder(WithLogger):
1020 cluster=project_info.pusher_cluster,
1021 )
1022
902 - # Patch pusher logger
903 - pusher.connection.logger = mock.MagicMock() # type: ignore
1023 + # pysher logs every raw frame at INFO; keep them out of user logs.
1024 + pusher.connection.logger = _silent_pysher_logger()
1025 pusher.connection.bind(
1026 "pusher:connection_established",
1027 self._get_connection_handler(
python/composio/exceptions.py
+16
@@ -292,6 +292,22 @@ class InvalidTriggerFilters(TriggerSubscriptionError):
292 pass
293
294
295 +class TriggerSubscriptionAuthError(TriggerSubscriptionError):
296 + """Raised when the realtime channel-auth request fails.
297 +
298 + Covers a transport error, a timeout, a non-200 response, and a response
299 + without an ``auth`` token.
300 + """
301 +
302 + pass
303 +
304 +
305 +class InvalidPusherClusterError(TriggerSubscriptionError, ValidationError):
306 + """Raised when the realtime credentials carry a malformed pusher cluster."""
307 +
308 + pass
309 +
310 +
311 class ApiKeyError(ComposioClientError):
312 pass
313
python/composio/utils/url_safety.py
+54 -1
@@ -72,9 +72,44 @@ _BODY_HEADERS = frozenset(
72 "transfer-encoding",
73 }
74 )
75 +# Headers that carry a credential for the origin the request was addressed
76 +# to, so they must not follow a redirect to a different origin. The Fetch
77 +# standard strips ``Authorization`` on a cross-origin redirect; ``Cookie`` and
78 +# ``Proxy-Authorization`` go with it the way ``requests``' own ``rebuild_auth``
79 +# and curl drop them on a host change. Following redirects by hand bypasses
80 +# ``rebuild_auth``, so the rule is applied here. The TypeScript guard drops the
81 +# same three (``CREDENTIAL_HEADERS``).
82 +_CREDENTIAL_HEADERS = frozenset({"authorization", "cookie", "proxy-authorization"})
83 _MAX_REDIRECTS = 5
84
85
86 +def _origin(url: str) -> t.Optional[t.Tuple[str, str, int]]:
87 + """The ``(scheme, host, port)`` triple two URLs must share to be same-origin.
88 +
89 + ``None`` when the URL cannot be parsed: ``urlparse`` raises ``ValueError``
90 + for a broken IPv6 literal, and ``.port`` for an out-of-range port. A
91 + redirect ``Location`` is remote input, so that is not an error here: the
92 + hop is treated as leaving the origin, and :func:`assert_safe_fetch_target`
93 + rejects the URL before anything is sent.
94 + """
95 + try:
96 + parsed = urlparse(url)
97 + scheme = parsed.scheme.lower()
98 + host = (parsed.hostname or "").lower()
99 + port = parsed.port
100 + except ValueError:
101 + return None
102 + if port is None:
103 + port = 443 if scheme == "https" else 80
104 + return scheme, host, port
105 +
106 +
107 +def _same_origin(previous_url: str, next_url: str) -> bool:
108 + """Whether a redirect hop stays on the origin the caller addressed."""
109 + previous_origin = _origin(previous_url)
110 + return previous_origin is not None and previous_origin == _origin(next_url)
111 +
112 +
113 def is_blocked_ip(value: str) -> bool:
114 """Return whether an address is non-publicly-routable."""
115 try:
@@ -233,7 +268,15 @@ def safe_request(
268 return response
269
270 response.close()
236 - current_url = urljoin(current_url, location)
271 + previous_url = current_url
272 + try:
273 + current_url = urljoin(current_url, location)
274 + except ValueError:
275 + # `urljoin` refuses a broken IPv6 literal. `Location` is remote
276 + # input, so this is a rejected hop, not a crash.
277 + raise BlockedInternalUrlError(
278 + "Refusing to follow a malformed redirect Location"
279 + ) from None
280
281 # The next hop is whatever `Location` says, query string included, so
282 # `params` must not be appended to it a second time — that is how
@@ -241,6 +284,16 @@ def safe_request(
284 # credential from being handed to a target that never asked for one.
285 kwargs.pop("params", None)
286
287 + # A credential header was addressed to the origin the caller named, so
288 + # a hop that leaves that origin must not carry it along.
289 + if not _same_origin(previous_url, current_url):
290 + if headers := kwargs.get("headers"):
291 + kwargs["headers"] = {
292 + name: value
293 + for name, value in headers.items()
294 + if name.lower() not in _CREDENTIAL_HEADERS
295 + }
296 +
297 rewritten_method = _redirect_rewrite(response.status_code, method)
298 if rewritten_method is not None:
299 method = rewritten_method
python/tests/test_triggers.py
+187
@@ -4,6 +4,8 @@ import base64
4 import hashlib
5 import hmac
6 import json
7 +import logging
8 +import pathlib
9 import threading
10 import time
11 from datetime import datetime, timezone
@@ -11,9 +13,11 @@ from unittest.mock import Mock, patch
13
14 import httpx
15 import pytest
16 +import requests
17 from composio_client import NotFoundError, omit
18
19 from composio import exceptions
20 +from composio.core.models import triggers as triggers_module
21 from composio.core.models.triggers import (
22 _MAX_LOGGED_FRAME_CHARS,
23 ComposioSDKTimeoutError,
@@ -1789,3 +1793,186 @@ class TestSubscriptionBuilderConnectTimeout:
1793
1794 assert result is builder.subscription
1795 pusher.disconnect.assert_not_called()
1796 +
1797 +
1798 +class TestPusherChannelAuth:
1799 + """Tests for the hardened pysher channel-auth request."""
1800 +
1801 + @staticmethod
1802 + def _pusher() -> triggers_module._ComposioPusher:
1803 + return triggers_module._ComposioPusher(
1804 + key="app-key",
1805 + cluster="mt1",
1806 + auth_endpoint="https://api.example.com/api/v3/internal/sdk/realtime/auth",
1807 + auth_endpoint_headers={"x-api-key": "sk-secret-value"},
1808 + )
1809 +
1810 + def test_auth_request_carries_a_timeout(self):
1811 + """pysher sends the auth POST with no timeout; the subclass bounds it."""
1812 + pusher = self._pusher()
1813 + pusher.connection.socket_id = "123.456"
1814 + response = Mock(status_code=200)
1815 + response.json.return_value = {"auth": "app-key:signature"}
1816 +
1817 + with patch.object(
1818 + triggers_module.requests, "post", return_value=response
1819 + ) as post:
1820 + token = pusher._generate_auth_token("private-project_triggers")
1821 +
1822 + assert token == "app-key:signature"
1823 + post.assert_called_once_with(
1824 + "https://api.example.com/api/v3/internal/sdk/realtime/auth",
1825 + data={"channel_name": "private-project_triggers", "socket_id": "123.456"},
1826 + headers={"x-api-key": "sk-secret-value"},
1827 + timeout=triggers_module.PUSHER_AUTH_TIMEOUT,
1828 + )
1829 + assert triggers_module.PUSHER_AUTH_TIMEOUT == (5.0, 15.0)
1830 +
1831 + def test_presence_request_carries_a_timeout(self):
1832 + pusher = self._pusher()
1833 + pusher.connection.socket_id = "123.456"
1834 + response = Mock(status_code=200)
1835 + response.json.return_value = {"auth": "app-key:signature"}
1836 +
1837 + with patch.object(
1838 + triggers_module.requests, "post", return_value=response
1839 + ) as post:
1840 + pusher._generate_presence_token("presence-room")
1841 +
1842 + assert post.call_args.kwargs["timeout"] == triggers_module.PUSHER_AUTH_TIMEOUT
1843 + assert post.call_args.kwargs["data"]["user_data"] == {}
1844 +
1845 + @pytest.mark.parametrize("status_code", [401, 403, 500])
1846 + def test_non_200_raises_typed_error_without_the_api_key(self, status_code):
1847 + """A non-200 used to trip a bare ``assert`` on the websocket thread."""
1848 + pusher = self._pusher()
1849 + response = Mock(status_code=status_code)
1850 +
1851 + with (
1852 + patch.object(triggers_module.requests, "post", return_value=response),
1853 + pytest.raises(exceptions.TriggerSubscriptionAuthError) as info,
1854 + ):
1855 + pusher._generate_auth_token("private-project_triggers")
1856 +
1857 + assert isinstance(info.value, exceptions.TriggerSubscriptionError)
1858 + assert f"HTTP {status_code}" in str(info.value)
1859 + assert "sk-secret-value" not in str(info.value)
1860 +
1861 + def test_transport_failure_raises_typed_error(self):
1862 + with (
1863 + patch.object(
1864 + triggers_module.requests,
1865 + "post",
1866 + side_effect=requests.ConnectTimeout("slow"),
1867 + ),
1868 + pytest.raises(
1869 + exceptions.TriggerSubscriptionAuthError, match="ConnectTimeout"
1870 + ),
1871 + ):
1872 + self._pusher()._generate_auth_token("private-project_triggers")
1873 +
1874 + @pytest.mark.parametrize("body", [{}, {"auth": ""}, {"auth": 1}, []])
1875 + def test_missing_auth_token_raises_typed_error(self, body):
1876 + response = Mock(status_code=200)
1877 + response.json.return_value = body
1878 +
1879 + with (
1880 + patch.object(triggers_module.requests, "post", return_value=response),
1881 + pytest.raises(
1882 + exceptions.TriggerSubscriptionAuthError, match="`auth` token"
1883 + ),
1884 + ):
1885 + self._pusher()._generate_auth_token("private-project_triggers")
1886 +
1887 +
1888 +class TestPusherClusterValidation:
1889 + """The cluster comes from an API response and lands in the websocket host."""
1890 +
1891 + @staticmethod
1892 + def _builder() -> _SubcriptionBuilder:
1893 + client = Mock()
1894 + client.base_url = "https://api.example.com"
1895 + client.api_key = "sk-secret-value"
1896 + return _SubcriptionBuilder(client=client)
1897 +
1898 + @pytest.mark.parametrize("cluster", ["mt1", "eu", "ap-southeast-1", "us3"])
1899 + def test_valid_cluster_builds_the_expected_host(self, cluster):
1900 + pusher = self._builder()._get_pusher_instance(key="app-key", cluster=cluster)
1901 +
1902 + assert isinstance(pusher, triggers_module._ComposioPusher)
1903 + assert pusher.host == f"ws-{cluster}.pusher.com"
1904 + assert pusher.auth_endpoint_headers["x-api-key"] == "sk-secret-value"
1905 +
1906 + @pytest.mark.parametrize(
1907 + ("cluster", "reason"),
1908 + [
1909 + ("", "did not include"),
1910 + (None, "did not include"),
1911 + ("MT1", "outside"),
1912 + ("mt1.evil.example", "outside"),
1913 + ("mt1/../evil", "outside"),
1914 + ("mt1 ", "outside"),
1915 + ("mt1:8080", "outside"),
1916 + ("a" * 65, "longer than 64"),
1917 + ],
1918 + )
1919 + def test_invalid_cluster_is_rejected_before_building_pusher(self, cluster, reason):
1920 + with (
1921 + patch.object(triggers_module, "_ComposioPusher") as pusher_cls,
1922 + pytest.raises(exceptions.InvalidPusherClusterError, match=reason) as info,
1923 + ):
1924 + self._builder()._get_pusher_instance(key="app-key", cluster=cluster)
1925 +
1926 + pusher_cls.assert_not_called()
1927 + assert isinstance(info.value, exceptions.TriggerSubscriptionError)
1928 + assert isinstance(info.value, exceptions.ValidationError)
1929 + # The offending value is never echoed; the response is untrusted.
1930 + if cluster:
1931 + assert cluster not in str(info.value)
1932 +
1933 +
1934 +class TestPysherLogger:
1935 + def test_module_does_not_import_unittest(self):
1936 + """``unittest.mock`` used to stand in for a logger at runtime."""
1937 + source = pathlib.Path(triggers_module.__file__).read_text()
1938 +
1939 + assert "unittest" not in source
1940 + assert not hasattr(triggers_module, "mock")
1941 +
1942 + def test_silent_logger_emits_nothing(self, caplog):
1943 + logger = triggers_module._silent_pysher_logger()
1944 +
1945 + assert isinstance(logger, logging.Logger)
1946 + assert logger.propagate is False
1947 + assert any(isinstance(h, logging.NullHandler) for h in logger.handlers)
1948 + with caplog.at_level(logging.DEBUG):
1949 + logger.info("Connection: Message - %s", '{"data": "raw frame"}')
1950 + logger.error("Connection: Error")
1951 + assert caplog.records == []
1952 +
1953 + def test_silent_logger_is_idempotent(self):
1954 + first = triggers_module._silent_pysher_logger()
1955 + second = triggers_module._silent_pysher_logger()
1956 +
1957 + assert first is second
1958 + assert sum(isinstance(h, logging.NullHandler) for h in first.handlers) == 1
1959 +
1960 + def test_connect_installs_the_silent_logger(self):
1961 + client = Mock()
1962 + client.base_url = "https://api.example.com"
1963 + pusher = Mock()
1964 + builder = _SubcriptionBuilder(client=client)
1965 + builder.subscription = Mock()
1966 + builder.subscription.is_alive.return_value = True
1967 + builder.internal = Mock()
1968 + builder.internal.get_sdk_realtime_credentials.return_value = Mock(
1969 + project_id="p", pusher_key="k", pusher_cluster="mt1"
1970 + )
1971 +
1972 + with patch.object(
1973 + _SubcriptionBuilder, "_get_pusher_instance", return_value=pusher
1974 + ):
1975 + builder.connect(timeout=2.0)
1976 +
1977 + assert isinstance(pusher.connection.logger, logging.Logger)
1978 + assert pusher.connection.logger.name == triggers_module._PYSHER_LOGGER_NAME
python/tests/test_url_safety.py
+116
@@ -292,6 +292,122 @@ def test_safe_request_does_not_reapply_params_to_the_redirect_target(
292 )
293
294
295 +_CREDENTIALED: t.Dict[str, str] = {
296 + "Authorization": "Bearer token",
297 + "Proxy-Authorization": "Basic cHJveHk=",
298 + "Cookie": "session=abc",
299 + "X-Test": "kept",
300 +}
301 +
302 +
303 +@pytest.mark.parametrize(
304 + "location",
305 + [
306 + "https://other.example.com/elsewhere", # different host
307 + "http://example.com/elsewhere", # different scheme
308 + "https://example.com:8443/elsewhere", # different port
309 + ],
310 +)
311 +@patch("composio.utils.url_safety.requests.Session.request")
312 +@patch("composio.utils.url_safety.assert_safe_fetch_target")
313 +def test_safe_request_drops_credentials_on_cross_origin_redirect(
314 + mock_assert, mock_request, location: str
315 +) -> None:
316 + """Manual redirects bypass `requests`' `rebuild_auth`, so strip here.
317 +
318 + A credential header is addressed to the origin the caller named; replaying
319 + it to another origin hands the bearer token to whoever answered the
320 + redirect. Mirrors the Fetch rule `ssrfSafeFetch` applies.
321 + """
322 + mock_request.side_effect = [_response(307, location), _response(200)]
323 +
324 + safe_request("GET", "https://example.com/download", headers=dict(_CREDENTIALED))
325 +
326 + assert mock_request.call_args_list[1] == call(
327 + "GET",
328 + location,
329 + allow_redirects=False,
330 + headers={"X-Test": "kept"},
331 + )
332 +
333 +
334 +@patch("composio.utils.url_safety.requests.Session.request")
335 +@patch("composio.utils.url_safety.assert_safe_fetch_target")
336 +def test_safe_request_keeps_credentials_on_same_origin_redirect(
337 + mock_assert, mock_request
338 +) -> None:
339 + mock_request.side_effect = [
340 + _response(307, "https://example.com:443/moved"),
341 + _response(200),
342 + ]
343 +
344 + safe_request("GET", "https://example.com/download", headers=dict(_CREDENTIALED))
345 +
346 + assert mock_request.call_args_list[1] == call(
347 + "GET",
348 + "https://example.com:443/moved",
349 + allow_redirects=False,
350 + headers=_CREDENTIALED,
351 + )
352 +
353 +
354 +@patch("composio.utils.url_safety.requests.Session.request")
355 +@patch("composio.utils.url_safety.assert_safe_fetch_target")
356 +def test_safe_request_strips_credentials_case_insensitively(
357 + mock_assert, mock_request
358 +) -> None:
359 + mock_request.side_effect = [
360 + _response(302, "https://other.example.com/elsewhere"),
361 + _response(200),
362 + ]
363 +
364 + safe_request(
365 + "GET",
366 + "https://example.com/download",
367 + headers={"AUTHORIZATION": "Bearer token", "cookie": "a=b", "X-Test": "kept"},
368 + )
369 +
370 + assert mock_request.call_args_list[1].kwargs["headers"] == {"X-Test": "kept"}
371 +
372 +
373 +@pytest.mark.parametrize(
374 + "location",
375 + [
376 + "https://example.com:99999/elsewhere", # port out of range
377 + "https://[::1/elsewhere", # broken IPv6 literal
378 + ],
379 +)
380 +@patch("composio.utils.url_safety.requests.Session.request")
381 +@patch("composio.utils.url_safety.assert_safe_fetch_target")
382 +def test_safe_request_unparseable_redirect_is_blocked_not_a_crash(
383 + mock_assert, mock_request, location: str
384 +) -> None:
385 + """`urljoin` / `urlparse(...).port` raise `ValueError` on these.
386 +
387 + Both run on the `Location` before the target is validated, so they must
388 + not turn remote input into a stray `ValueError` that call sites catching
389 + only `RequestException` would let escape. The hop is rejected the way any
390 + malformed URL is, and never sent.
391 + """
392 + mock_assert.side_effect = [["93.184.216.34"], BlockedInternalUrlError("blocked")]
393 + mock_request.return_value = _response(307, location)
394 +
395 + with pytest.raises(BlockedInternalUrlError):
396 + safe_request("GET", "https://example.com/download", headers=dict(_CREDENTIALED))
397 +
398 + assert mock_request.call_count == 1
399 +
400 +
401 +def test_origin_is_none_for_unparseable_urls() -> None:
402 + from composio.utils.url_safety import _origin, _same_origin
403 +
404 + assert _origin("https://example.com:99999/x") is None
405 + assert _origin("https://[::1/x") is None
406 + assert _origin("https://example.com/x") == ("https", "example.com", 443)
407 + assert not _same_origin("https://example.com/a", "https://example.com:99999/b")
408 + assert _same_origin("https://example.com/a", "https://example.com:443/b")
409 +
410 +
411 @patch("composio.utils.url_safety.requests.Session.request")
412 @patch("composio.utils.url_safety.assert_safe_fetch_target")
413 def test_safe_request_relative_redirect_is_resolved(mock_assert, mock_request) -> None:
ts/packages/core/src/utils/ssrfGuard.node.ts
+26 -7
@@ -81,12 +81,30 @@ const redirectRewrite = (status: number, method: string): string | null => {
81 return null;
82 };
83
84 -const applyRedirectSemantics = (init: RequestInit, status: number): RequestInit => {
84 +/**
85 + * Headers that carry a credential for the origin the request was addressed
86 + * to, so they must not follow a redirect to a different origin. The Fetch
87 + * standard strips `Authorization` on a cross-origin redirect; `Cookie` and
88 + * `Proxy-Authorization` go with it the way `requests` and curl drop them on a
89 + * host change. `redirect: 'manual'` means `fetch` never applies that rule for
90 + * us either. The Python guard drops the same three (`_CREDENTIAL_HEADERS`).
91 + */
92 +const CREDENTIAL_HEADERS = ['authorization', 'cookie', 'proxy-authorization'];
93 +
94 +const applyRedirectSemantics = (
95 + init: RequestInit,
96 + status: number,
97 + fromUrl: string,
98 + toUrl: string
99 +): RequestInit => {
100 const method = redirectRewrite(status, (init.method ?? 'GET').toUpperCase());
86 - if (method === null) return init;
101 + const crossOrigin = new URL(fromUrl).origin !== new URL(toUrl).origin;
102 + if (method === null && !crossOrigin) return init;
103
104 const headers = new Headers(init.headers);
89 - for (const name of BODY_HEADERS) headers.delete(name);
105 + if (method !== null) for (const name of BODY_HEADERS) headers.delete(name);
106 + if (crossOrigin) for (const name of CREDENTIAL_HEADERS) headers.delete(name);
107 + if (method === null) return { ...init, headers };
108 return { ...init, method, body: undefined, headers };
109 };
110
@@ -284,8 +302,8 @@ export const assertSafeFetchTarget = async (rawUrl: string): Promise<string[]> =
302 * connects to the address it validated, and re-validates and re-pins every
303 * redirect hop (redirects are followed manually up to {@link MAX_REDIRECTS}).
304 * Intermediate redirect bodies are cancelled; non-redirect responses are
287 - * returned unchanged. Each hop carries the method and body the Fetch standard
288 - * says it should — see {@link applyRedirectSemantics}.
305 + * returned unchanged. Each hop carries the method, body, and credential headers
306 + * the Fetch standard says it should — see {@link applyRedirectSemantics}.
307 *
308 * A hop whose effective dispatcher is a configured route (caller-supplied
309 * `init.dispatcher`, non-stock global dispatcher, env-proxy mode) is *not*
@@ -357,8 +375,9 @@ export const ssrfSafeFetch = async (
375 // than leaving it to the garbage collector (mirrors `readResponseBodyWithLimit`).
376 await response.body?.cancel().catch(() => undefined);
377
360 - currentUrl = new URL(response.headers.get('location')!, currentUrl).toString();
361 - currentInit = applyRedirectSemantics(currentInit, response.status);
378 + const nextUrl = new URL(response.headers.get('location')!, currentUrl).toString();
379 + currentInit = applyRedirectSemantics(currentInit, response.status, currentUrl, nextUrl);
380 + currentUrl = nextUrl;
381 }
382
383 throw new ComposioBlockedInternalUrlError(
ts/packages/core/test/utils/ssrfGuard.test.ts
+74
@@ -437,6 +437,80 @@ describe('ssrfSafeFetch', () => {
437 expect(mockFetch.mock.calls[2][1].body).toBeUndefined();
438 });
439
440 + // A credential header is addressed to the origin the caller named, so a hop
441 + // that leaves that origin must not carry it: `redirect: 'manual'` means
442 + // `fetch` never strips it for us. The Python guard applies the same rule.
443 + const credentialed = {
444 + Authorization: 'Bearer token',
445 + 'Proxy-Authorization': 'Basic cHJveHk=',
446 + Cookie: 'session=abc',
447 + 'X-Test': 'kept',
448 + };
449 +
450 + it.each([
451 + { location: 'https://other.example.com/elsewhere', differs: 'host' },
452 + { location: 'http://example.com/elsewhere', differs: 'scheme' },
453 + { location: 'https://example.com:8443/elsewhere', differs: 'port' },
454 + ])('drops credential headers on a redirect to a different $differs', async ({ location }) => {
455 + resolvesTo('93.184.216.34');
456 + mockFetch
457 + .mockResolvedValueOnce(new Response(null, { status: 307, headers: { location } }))
458 + .mockResolvedValueOnce(new Response('data', { status: 200 }));
459 +
460 + await ssrfSafeFetch('https://example.com/download', { headers: credentialed });
461 +
462 + const [url, init] = mockFetch.mock.calls[1];
463 + expect(url).toBe(location);
464 + const headers = new Headers(init.headers);
465 + expect(headers.get('authorization')).toBeNull();
466 + expect(headers.get('proxy-authorization')).toBeNull();
467 + expect(headers.get('cookie')).toBeNull();
468 + expect(headers.get('x-test')).toBe('kept');
469 + });
470 +
471 + it('keeps credential headers on a same-origin redirect', async () => {
472 + resolvesTo('93.184.216.34');
473 + mockFetch
474 + .mockResolvedValueOnce(
475 + new Response(null, { status: 307, headers: { location: 'https://example.com:443/moved' } })
476 + )
477 + .mockResolvedValueOnce(new Response('data', { status: 200 }));
478 +
479 + await ssrfSafeFetch('https://example.com/download', { headers: credentialed });
480 +
481 + const headers = new Headers(mockFetch.mock.calls[1][1].headers);
482 + expect(headers.get('authorization')).toBe('Bearer token');
483 + expect(headers.get('proxy-authorization')).toBe('Basic cHJveHk=');
484 + expect(headers.get('cookie')).toBe('session=abc');
485 + expect(headers.get('x-test')).toBe('kept');
486 + });
487 +
488 + it('drops credential headers together with the body on a cross-origin 303', async () => {
489 + resolvesTo('93.184.216.34');
490 + mockFetch
491 + .mockResolvedValueOnce(
492 + new Response(null, {
493 + status: 303,
494 + headers: { location: 'https://other.example.com/result' },
495 + })
496 + )
497 + .mockResolvedValueOnce(new Response('data', { status: 200 }));
498 +
499 + await ssrfSafeFetch('https://example.com/create', {
500 + method: 'POST',
501 + body: 'payload',
502 + headers: { ...credentialed, 'Content-Type': 'application/octet-stream' },
503 + });
504 +
505 + const init = mockFetch.mock.calls[1][1];
506 + expect(init.method).toBe('GET');
507 + expect(init.body).toBeUndefined();
508 + const headers = new Headers(init.headers);
509 + expect(headers.get('content-type')).toBeNull();
510 + expect(headers.get('authorization')).toBeNull();
511 + expect(headers.get('x-test')).toBe('kept');
512 + });
513 +
514 it.each([300, 304, 305, 306])(
515 'returns a %i without following its location',
516 async (status: number) => {