From 18fe69d6dc2ca9efe27d9f0a343c60b9f86f4087 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 24 Jun 2026 01:06:07 +0000 Subject: [PATCH 1/4] fix(mcp): stop 404ing internal-only MCP servers behind a load balancer get_mcp_client_ip returned "" when use_x_forwarded_for was enabled and an X-Forwarded-For header was present but mcp_trusted_proxy_ranges was not configured. That empty string flowed into the IP access-control gate, where is_internal_ip("") is False, so every available_on_public_internet=false server was filtered out and the named-server lookup raised 404. Honour X-Forwarded-For when the direct peer is itself internal (a private reverse proxy in front of the gateway); only ignore it and classify by the direct connection when the peer is a public address or falls outside the configured trusted ranges. This restores access for internal clients behind a load balancer while still blocking a public caller that spoofs an internal XFF directly against the gateway. Resolves LIT-3964 --- litellm/proxy/auth/ip_address_utils.py | 27 ++++--- .../proxy/auth/test_mcp_ip_filtering.py | 70 ++++++++++++++++--- 2 files changed, 78 insertions(+), 19 deletions(-) diff --git a/litellm/proxy/auth/ip_address_utils.py b/litellm/proxy/auth/ip_address_utils.py index be0d83dfcdc1..a73996f3e908 100644 --- a/litellm/proxy/auth/ip_address_utils.py +++ b/litellm/proxy/auth/ip_address_utils.py @@ -204,15 +204,26 @@ def get_mcp_client_ip( request, general_settings=general_settings ): direct_ip = request.client.host if request.client else None - if general_settings.get("mcp_trusted_proxy_ranges"): - # Direct connection isn't in any configured trusted CIDR. + # The X-Forwarded-For header is not trustworthy when either the + # operator configured mcp_trusted_proxy_ranges and this direct + # peer is not one of them, or no ranges are configured and the + # peer is a public address (an untrusted caller connecting to us + # directly and spoofing XFF). Classify by the direct connection. + untrusted_peer = bool( + general_settings.get("mcp_trusted_proxy_ranges") + ) or ( + direct_ip is not None + and not IPAddressUtils.is_internal_ip(direct_ip) + ) + if untrusted_peer: verbose_proxy_logger.warning( - "XFF header from untrusted IP %s, ignoring", direct_ip + "Untrusted X-Forwarded-For from %s, ignoring", direct_ip ) return direct_ip - # XFF enabled but no trusted proxy ranges configured: the direct - # peer is typically the reverse proxy's own (private) IP, so - # returning it would mis-classify external callers as internal. - # Fail closed for access control. - return "" + # No trusted ranges configured and an internal (or unknown) + # direct peer: a private reverse proxy in front of the gateway, + # so honour X-Forwarded-For (parsed below). Set + # mcp_trusted_proxy_ranges to validate the proxy explicitly. + # Failing closed here made every internal-only MCP server return + # 404 behind a load balancer (LIT-3964). return _get_request_ip_address(request, use_x_forwarded_for=use_xff) diff --git a/tests/test_litellm/proxy/auth/test_mcp_ip_filtering.py b/tests/test_litellm/proxy/auth/test_mcp_ip_filtering.py index 9444e4ebd2db..b76131ad45be 100644 --- a/tests/test_litellm/proxy/auth/test_mcp_ip_filtering.py +++ b/tests/test_litellm/proxy/auth/test_mcp_ip_filtering.py @@ -60,7 +60,11 @@ def test_fails_closed_on_bad_input(self): class TestMCPClientIPExtraction: - def test_fails_closed_when_xff_enabled_without_trusted_proxy_ranges(self): + def test_public_direct_peer_spoofing_xff_stays_external(self): + # A caller connecting directly to the gateway (public direct peer) with + # use_x_forwarded_for on but no mcp_trusted_proxy_ranges cannot spoof an + # internal X-Forwarded-For to reach available_on_public_internet=false + # servers: the forged XFF is ignored and the real public peer is used. request = MagicMock(spec=Request) request.client = MagicMock() request.client.host = "203.0.113.5" @@ -71,17 +75,14 @@ def test_fails_closed_when_xff_enabled_without_trusted_proxy_ranges(self): general_settings={"use_x_forwarded_for": True}, ) - # XFF is untrusted (no mcp_trusted_proxy_ranges) so it must be ignored, - # and we must not trust the direct peer either: fail closed so the caller - # is classified as external and is_internal_ip("") is False. - assert result == "" + assert result == "203.0.113.5" assert IPAddressUtils.is_internal_ip(result) is False - def test_private_proxy_peer_does_not_grant_internal_access(self): - # Regression: behind an internal reverse proxy with use_x_forwarded_for - # enabled but mcp_trusted_proxy_ranges unset, the direct peer is the - # proxy's private IP. Returning it would mis-classify an external caller - # as internal and expose available_on_public_internet=false servers. + def test_external_client_behind_private_proxy_stays_external(self): + # Behind an internal reverse proxy (private direct peer) with XFF on but + # no mcp_trusted_proxy_ranges, an external client (public XFF) is still + # classified external by its forwarded address, so it cannot reach + # internal-only servers. request = MagicMock(spec=Request) request.client = MagicMock() request.client.host = "10.0.0.7" @@ -92,9 +93,30 @@ def test_private_proxy_peer_does_not_grant_internal_access(self): general_settings={"use_x_forwarded_for": True}, ) - assert result == "" + assert result == "8.8.8.8" assert IPAddressUtils.is_internal_ip(result) is False + def test_internal_client_behind_private_proxy_without_trusted_ranges_is_internal( + self, + ): + # LIT-3964 regression: an internal client reaching the gateway through a + # private reverse proxy (private direct peer, internal XFF) with + # use_x_forwarded_for on but no mcp_trusted_proxy_ranges must be + # classified internal. Returning "" here (the regressed behaviour) made + # is_internal_ip("") False, so every internal-only MCP server 404'd. + request = MagicMock(spec=Request) + request.client = MagicMock() + request.client.host = "10.0.0.1" + request.headers = {"x-forwarded-for": "10.0.0.9"} + + result = IPAddressUtils.get_mcp_client_ip( + request, + general_settings={"use_x_forwarded_for": True}, + ) + + assert result == "10.0.0.9" + assert IPAddressUtils.is_internal_ip(result) is True + def test_honours_xff_from_trusted_proxy(self): request = MagicMock(spec=Request) request.client = MagicMock() @@ -162,6 +184,32 @@ def test_no_ip_means_no_filtering(self): result = manager.filter_server_ids_by_ip(["priv"], client_ip=None) assert result == ["priv"] + @patch( + "litellm.proxy.proxy_server.general_settings", + {"use_x_forwarded_for": True}, + ) + @patch("litellm.public_mcp_servers", []) + def test_internal_client_behind_proxy_can_reach_internal_only_server(self): + # LIT-3964 end-to-end: the rest/oauth paths extract the client IP via + # get_mcp_client_ip and feed it straight into filter_server_ids_by_ip. + # With use_x_forwarded_for on, no mcp_trusted_proxy_ranges, a private + # proxy peer and an internal forwarded client, the internal-only server + # must survive the filter instead of 404ing. + priv = _make_server("priv", available_on_public_internet=False) + manager = _make_manager([priv]) + + request = MagicMock(spec=Request) + request.client = MagicMock() + request.client.host = "10.0.0.1" + request.headers = {"x-forwarded-for": "10.0.0.9"} + client_ip = IPAddressUtils.get_mcp_client_ip( + request, + general_settings={"use_x_forwarded_for": True}, + ) + + result = manager.filter_server_ids_by_ip(["priv"], client_ip=client_ip) + assert result == ["priv"] + class TestFilterServerIdsByIpWithInfo: """Tests that filter_server_ids_by_ip_with_info returns accurate block counts.""" From 72c0b4ecd4496e0b88c66731c94c2922a8c94f95 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 24 Jun 2026 01:31:15 +0000 Subject: [PATCH 2/4] fix(mcp): fail closed on unknown direct peer in get_mcp_client_ip When request.client is None (Unix-socket transports, some ASGI test clients) the direct peer IP is unknown. The prior untrusted_peer check collapsed to False in that state and fell through to trust the forged X-Forwarded-For header, and the untrusted path also returned None, which means "no filtering" downstream and grants internal access. Both leak available_on_public_internet=false servers to a spoofing caller. Reframe the check as peer_is_private_proxy: honour X-Forwarded-For only when the direct peer is a private reverse proxy and no mcp_trusted_proxy_ranges are pinned. A public, out-of-range, or unknown peer is classified by its own address, with an unknown peer returning the external "" sentinel so it can never fall open to None. Update the docstring to describe the full trust model and add regression tests for the request.client is None edge case at both the unit and filter levels. --- litellm/proxy/auth/ip_address_utils.py | 44 ++++++++++--------- .../proxy/auth/test_mcp_ip_filtering.py | 42 ++++++++++++++++++ 2 files changed, 66 insertions(+), 20 deletions(-) diff --git a/litellm/proxy/auth/ip_address_utils.py b/litellm/proxy/auth/ip_address_utils.py index a73996f3e908..daf3489f84be 100644 --- a/litellm/proxy/auth/ip_address_utils.py +++ b/litellm/proxy/auth/ip_address_utils.py @@ -174,9 +174,13 @@ def get_mcp_client_ip( """ Extract client IP from a FastAPI request for MCP access control. - Security: Only trusts X-Forwarded-For if: - 1. use_x_forwarded_for is enabled in settings - 2. The direct connection is from a trusted proxy (if mcp_trusted_proxy_ranges configured) + Security: X-Forwarded-For is trusted only when use_x_forwarded_for is + enabled and the direct connection is a legitimate proxy, established by + either the direct peer falling inside mcp_trusted_proxy_ranges, or, when + no ranges are configured, the direct peer being a private address (a + reverse proxy in front of the gateway). A public, out-of-range, or + unknown direct peer is classified by its own address so a forged header + cannot grant internal access. Args: request: FastAPI request object @@ -204,26 +208,26 @@ def get_mcp_client_ip( request, general_settings=general_settings ): direct_ip = request.client.host if request.client else None - # The X-Forwarded-For header is not trustworthy when either the - # operator configured mcp_trusted_proxy_ranges and this direct - # peer is not one of them, or no ranges are configured and the - # peer is a public address (an untrusted caller connecting to us - # directly and spoofing XFF). Classify by the direct connection. - untrusted_peer = bool( - general_settings.get("mcp_trusted_proxy_ranges") - ) or ( + # X-Forwarded-For is only trustworthy when the direct peer is a + # private reverse proxy and the operator has not pinned + # mcp_trusted_proxy_ranges (a peer inside those ranges would have + # been accepted above). For a public, out-of-range, or unknown + # peer, classify by the direct connection so a forged header + # cannot reach available_on_public_internet=false servers. An + # unknown peer (request.client is None) fails closed to the + # external "" sentinel rather than leaking internal access via a + # None "no filtering" result. The private-proxy carve-out is what + # stops internal-only servers 404ing behind a load balancer + # (LIT-3964); set mcp_trusted_proxy_ranges to validate the proxy + # explicitly instead of relying on it. + peer_is_private_proxy = ( direct_ip is not None - and not IPAddressUtils.is_internal_ip(direct_ip) + and not general_settings.get("mcp_trusted_proxy_ranges") + and IPAddressUtils.is_internal_ip(direct_ip) ) - if untrusted_peer: + if not peer_is_private_proxy: verbose_proxy_logger.warning( "Untrusted X-Forwarded-For from %s, ignoring", direct_ip ) - return direct_ip - # No trusted ranges configured and an internal (or unknown) - # direct peer: a private reverse proxy in front of the gateway, - # so honour X-Forwarded-For (parsed below). Set - # mcp_trusted_proxy_ranges to validate the proxy explicitly. - # Failing closed here made every internal-only MCP server return - # 404 behind a load balancer (LIT-3964). + return direct_ip or "" return _get_request_ip_address(request, use_x_forwarded_for=use_xff) diff --git a/tests/test_litellm/proxy/auth/test_mcp_ip_filtering.py b/tests/test_litellm/proxy/auth/test_mcp_ip_filtering.py index b76131ad45be..9029878338f4 100644 --- a/tests/test_litellm/proxy/auth/test_mcp_ip_filtering.py +++ b/tests/test_litellm/proxy/auth/test_mcp_ip_filtering.py @@ -117,6 +117,24 @@ def test_internal_client_behind_private_proxy_without_trusted_ranges_is_internal assert result == "10.0.0.9" assert IPAddressUtils.is_internal_ip(result) is True + def test_unknown_direct_peer_spoofing_xff_stays_external(self): + # When the ASGI transport does not expose request.client (Unix-socket + # transports, some test clients) the direct peer is unknown. With XFF on + # and no mcp_trusted_proxy_ranges, a forged internal X-Forwarded-For must + # not be trusted. The result must be a non-None external value: returning + # None would mean "no filtering" downstream and leak internal access. + request = MagicMock(spec=Request) + request.client = None + request.headers = {"x-forwarded-for": "10.0.0.9"} + + result = IPAddressUtils.get_mcp_client_ip( + request, + general_settings={"use_x_forwarded_for": True}, + ) + + assert result is not None + assert IPAddressUtils.is_internal_ip(result) is False + def test_honours_xff_from_trusted_proxy(self): request = MagicMock(spec=Request) request.client = MagicMock() @@ -210,6 +228,30 @@ def test_internal_client_behind_proxy_can_reach_internal_only_server(self): result = manager.filter_server_ids_by_ip(["priv"], client_ip=client_ip) assert result == ["priv"] + @patch( + "litellm.proxy.proxy_server.general_settings", + {"use_x_forwarded_for": True}, + ) + @patch("litellm.public_mcp_servers", []) + def test_unknown_peer_spoofing_xff_cannot_reach_internal_only_server(self): + # Security regression: if the transport doesn't expose request.client, a + # forged internal X-Forwarded-For must not unlock internal-only servers. + # get_mcp_client_ip classifies external and the filter drops the private + # server. A None result (the fail-open bug) would leave it accessible. + priv = _make_server("priv", available_on_public_internet=False) + manager = _make_manager([priv]) + + request = MagicMock(spec=Request) + request.client = None + request.headers = {"x-forwarded-for": "10.0.0.9"} + client_ip = IPAddressUtils.get_mcp_client_ip( + request, + general_settings={"use_x_forwarded_for": True}, + ) + + result = manager.filter_server_ids_by_ip(["priv"], client_ip=client_ip) + assert result == [] + class TestFilterServerIdsByIpWithInfo: """Tests that filter_server_ids_by_ip_with_info returns accurate block counts.""" From 87f7fc77d27391018531a84c5281d4d72d4642b5 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 24 Jun 2026 01:56:43 +0000 Subject: [PATCH 3/4] fix(mcp): classify the proxy peer with mcp_internal_ip_ranges The peer_is_private_proxy carve-out classified the direct connection with the RFC1918 defaults while the access-control gate (_is_server_accessible_from_ip) classifies client IPs with the operator's mcp_internal_ip_ranges. When an operator configures a custom internal CIDR and their reverse proxy sits inside it but outside the defaults, the peer was treated as untrusted: its X-Forwarded-For was dropped and the proxy's own IP returned, which the gate then classifies internal. A public client forwarded by that proxy inherited the proxy's internal access to available_on_public_internet=false servers. Parse mcp_internal_ip_ranges and pass it to is_internal_ip so the private-proxy decision and the access-control gate share one definition of "internal". Add a unit regression asserting the forwarded client IP is honoured when the peer is internal only under a custom CIDR, and an end-to-end regression asserting a public forwarded client cannot reach an internal-only server through such a proxy. --- litellm/proxy/auth/ip_address_utils.py | 18 ++++-- .../proxy/auth/test_mcp_ip_filtering.py | 58 +++++++++++++++++++ 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/litellm/proxy/auth/ip_address_utils.py b/litellm/proxy/auth/ip_address_utils.py index daf3489f84be..a97377addd46 100644 --- a/litellm/proxy/auth/ip_address_utils.py +++ b/litellm/proxy/auth/ip_address_utils.py @@ -177,10 +177,10 @@ def get_mcp_client_ip( Security: X-Forwarded-For is trusted only when use_x_forwarded_for is enabled and the direct connection is a legitimate proxy, established by either the direct peer falling inside mcp_trusted_proxy_ranges, or, when - no ranges are configured, the direct peer being a private address (a - reverse proxy in front of the gateway). A public, out-of-range, or - unknown direct peer is classified by its own address so a forged header - cannot grant internal access. + no ranges are configured, the direct peer being internal under + mcp_internal_ip_ranges (a reverse proxy in front of the gateway). A + public, out-of-range, or unknown direct peer is classified by its own + address so a forged header cannot grant internal access. Args: request: FastAPI request object @@ -219,11 +219,17 @@ def get_mcp_client_ip( # None "no filtering" result. The private-proxy carve-out is what # stops internal-only servers 404ing behind a load balancer # (LIT-3964); set mcp_trusted_proxy_ranges to validate the proxy - # explicitly instead of relying on it. + # explicitly instead of relying on it. "Internal" here uses the + # same mcp_internal_ip_ranges the access-control gate uses, so a + # proxy internal under custom CIDRs cannot have its forwarded + # client silently inherit its internal classification. + internal_networks = IPAddressUtils.parse_internal_networks( + general_settings.get("mcp_internal_ip_ranges") + ) peer_is_private_proxy = ( direct_ip is not None and not general_settings.get("mcp_trusted_proxy_ranges") - and IPAddressUtils.is_internal_ip(direct_ip) + and IPAddressUtils.is_internal_ip(direct_ip, internal_networks) ) if not peer_is_private_proxy: verbose_proxy_logger.warning( diff --git a/tests/test_litellm/proxy/auth/test_mcp_ip_filtering.py b/tests/test_litellm/proxy/auth/test_mcp_ip_filtering.py index 9029878338f4..6911d5ea35a7 100644 --- a/tests/test_litellm/proxy/auth/test_mcp_ip_filtering.py +++ b/tests/test_litellm/proxy/auth/test_mcp_ip_filtering.py @@ -135,6 +135,29 @@ def test_unknown_direct_peer_spoofing_xff_stays_external(self): assert result is not None assert IPAddressUtils.is_internal_ip(result) is False + def test_xff_honoured_when_peer_internal_under_custom_ranges(self): + # The private-proxy carve-out must use the same internal-range + # definition as the access-control gate (mcp_internal_ip_ranges), not + # only the RFC1918 defaults. A reverse proxy internal under a custom + # CIDR (100.64.0.0/10, outside the defaults) is a private proxy, so its + # forwarded client IP must be honoured. Classifying it by defaults would + # drop the XFF and return the proxy's own custom-internal IP, letting a + # public forwarded client inherit internal access. + request = MagicMock(spec=Request) + request.client = MagicMock() + request.client.host = "100.64.0.5" + request.headers = {"x-forwarded-for": "8.8.8.8"} + + result = IPAddressUtils.get_mcp_client_ip( + request, + general_settings={ + "use_x_forwarded_for": True, + "mcp_internal_ip_ranges": ["100.64.0.0/10"], + }, + ) + + assert result == "8.8.8.8" + def test_honours_xff_from_trusted_proxy(self): request = MagicMock(spec=Request) request.client = MagicMock() @@ -252,6 +275,41 @@ def test_unknown_peer_spoofing_xff_cannot_reach_internal_only_server(self): result = manager.filter_server_ids_by_ip(["priv"], client_ip=client_ip) assert result == [] + @patch( + "litellm.proxy.proxy_server.general_settings", + { + "use_x_forwarded_for": True, + "mcp_internal_ip_ranges": ["100.64.0.0/10"], + }, + ) + @patch("litellm.public_mcp_servers", []) + def test_public_client_via_custom_internal_proxy_cannot_reach_internal_only_server( + self, + ): + # Security: with custom mcp_internal_ip_ranges, a public client forwarded + # by a proxy that is internal only under those custom ranges must still + # be blocked from internal-only servers. get_mcp_client_ip recognises the + # proxy as private and honours the public XFF, and the filter then drops + # the internal-only server. Classifying the peer by RFC1918 defaults + # would return the proxy's own IP and leak the server to the caller. + priv = _make_server("priv", available_on_public_internet=False) + manager = _make_manager([priv]) + + request = MagicMock(spec=Request) + request.client = MagicMock() + request.client.host = "100.64.0.5" + request.headers = {"x-forwarded-for": "8.8.8.8"} + client_ip = IPAddressUtils.get_mcp_client_ip( + request, + general_settings={ + "use_x_forwarded_for": True, + "mcp_internal_ip_ranges": ["100.64.0.0/10"], + }, + ) + + result = manager.filter_server_ids_by_ip(["priv"], client_ip=client_ip) + assert result == [] + class TestFilterServerIdsByIpWithInfo: """Tests that filter_server_ids_by_ip_with_info returns accurate block counts.""" From a02e05824ff542151b92eb8bf2ed2913c7a38280 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 24 Jun 2026 02:05:42 +0000 Subject: [PATCH 4/4] docs(mcp): correct the XFF-without-trusted-ranges warning text With the private-proxy carve-out, MCP access-control now honours X-Forwarded-For when the direct peer is internal, so the one-shot warning in is_request_from_trusted_proxy claiming access-control client IPs "will use the proxy's literal request values" was misleading for exactly the load-balancer operators this fix helps. Reword it to state that OAuth discovery URLs fall back to literal values while access-control honours X-Forwarded-For only when the direct peer is internal, and to point at mcp_trusted_proxy_ranges as the way to validate the proxy explicitly. --- litellm/proxy/auth/ip_address_utils.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/litellm/proxy/auth/ip_address_utils.py b/litellm/proxy/auth/ip_address_utils.py index a97377addd46..e97a36d8d8e4 100644 --- a/litellm/proxy/auth/ip_address_utils.py +++ b/litellm/proxy/auth/ip_address_utils.py @@ -152,12 +152,13 @@ def is_request_from_trusted_proxy( if not _warned_xff_without_trusted_ranges: verbose_proxy_logger.warning( "use_x_forwarded_for is enabled but mcp_trusted_proxy_ranges " - "is not configured. X-Forwarded-* headers will NOT be " - "trusted, so MCP OAuth discovery URLs and access-control " - "client IPs will use the proxy's literal request values. " - "Set mcp_trusted_proxy_ranges in " - "general_settings to your reverse-proxy CIDR(s) to allow " - "X-Forwarded-* through." + "is not configured. X-Forwarded-* headers are not validated " + "against a trusted proxy, so MCP OAuth discovery URLs fall " + "back to the proxy's literal request values and MCP " + "access-control honours X-Forwarded-For only when the direct " + "peer is an internal address. Set mcp_trusted_proxy_ranges in " + "general_settings to your reverse-proxy CIDR(s) to validate " + "the proxy explicitly." ) _warned_xff_without_trusted_ranges = True return False