The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(mcp): correct misleading no-trusted-proxy warning for XFF access control (#31264)
Mateo Wang257d67167f05
diff --git a/litellm/proxy/auth/ip_address_utils.py b/litellm/proxy/auth/ip_address_utils.pyindex be0d83dfcdc..6844fd340b3 100644--- a/litellm/proxy/auth/ip_address_utils.py+++ b/litellm/proxy/auth/ip_address_utils.py@@ -152,12 +152,12 @@ class IPAddressUtils: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, so X-Forwarded-* headers will NOT be trusted. "+ "MCP OAuth discovery URLs fall back to the proxy's literal request "+ "URL, and MCP access-control client-IP resolution fails closed "+ "(callers are treated as external). Set mcp_trusted_proxy_ranges in "+ "general_settings to your reverse-proxy CIDR(s) to trust "+ "X-Forwarded-*.")_warned_xff_without_trusted_ranges = Truereturn Falsediff --git a/tests/test_litellm/proxy/auth/test_mcp_ip_filtering.py b/tests/test_litellm/proxy/auth/test_mcp_ip_filtering.pyindex 9444e4ebd2d..7b6d0b12cef 100644--- a/tests/test_litellm/proxy/auth/test_mcp_ip_filtering.py+++ b/tests/test_litellm/proxy/auth/test_mcp_ip_filtering.py@@ -5,10 +5,13 @@ Tests that internal callers see all MCP servers whileexternal callers only see servers with available_on_public_internet=True."""+import loggingfrom unittest.mock import MagicMock, patchfrom fastapi import Request+import litellm.proxy.auth.ip_address_utils as ip_mod+from litellm._logging import verbose_proxy_loggerfrom litellm.proxy.auth.ip_address_utils import IPAddressUtilsfrom litellm.types.mcp_server.mcp_server_manager import MCPServer@@ -77,6 +80,44 @@ class TestMCPClientIPExtraction:assert result == ""assert IPAddressUtils.is_internal_ip(result) is False+ def test_no_trusted_ranges_warning_matches_fail_closed_behavior(self, caplog):+ ip_mod._warned_xff_without_trusted_ranges = False++ request = MagicMock(spec=Request)+ request.client = MagicMock()+ request.client.host = "203.0.113.5"+ request.headers = {"x-forwarded-for": "1.2.3.4, 10.0.0.1"}+ general_settings = {"use_x_forwarded_for": True}++ with caplog.at_level(logging.WARNING, logger=verbose_proxy_logger.name):+ assert (+ IPAddressUtils.is_request_from_trusted_proxy(+ request, general_settings=general_settings+ )+ is False+ )++ warning = next(+ (+ record.getMessage()+ for record in caplog.records+ if "mcp_trusted_proxy_ranges" in record.getMessage()+ ),+ None,+ )+ assert (+ warning is not None+ ), "Expected a warning containing 'mcp_trusted_proxy_ranges' but none was logged"++ assert "fails closed" in warning+ assert "treated as external" in warning+ assert "client IPs will use the proxy's literal request values" not in warning++ assert (+ IPAddressUtils.get_mcp_client_ip(request, general_settings=general_settings)+ == ""+ )+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