Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions litellm/proxy/auth/ip_address_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Comment thread
mateo-berri marked this conversation as resolved.
Resolved
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)
70 changes: 59 additions & 11 deletions tests/test_litellm/proxy/auth/test_mcp_ip_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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()
Expand Down Expand Up @@ -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."""
Expand Down
Loading