Skip to content

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]

fix(proxy): keep mapped-notation trusted proxy ranges matching mapped peers

yuchengc05095373d10
diff --git a/litellm/proxy/auth/network.py b/litellm/proxy/auth/network.py
index 28466156100..8a20e207113 100644
--- a/litellm/proxy/auth/network.py
+++ b/litellm/proxy/auth/network.py
@@ -59,10 +59,11 @@ def ip_in_networks(client_ip: str | None, networks: list[TrustedProxyNetwork]) -
if not client_ip or not networks:
return False
try:
- addr: Final = _unmapped(ipaddress.ip_address(client_ip.strip()))
+ addr: Final = ipaddress.ip_address(client_ip.strip())
except ValueError:
return False
- return any(addr in network for network in networks)
+ candidates: Final = (addr, _unmapped(addr))
+ return any(candidate in network for candidate in candidates for network in networks)
def _is_valid_ip(value: str) -> bool:
diff --git a/tests/test_litellm/proxy/auth/test_network.py b/tests/test_litellm/proxy/auth/test_network.py
index 83233dc370d..e743ce8cd23 100644
--- a/tests/test_litellm/proxy/auth/test_network.py
+++ b/tests/test_litellm/proxy/auth/test_network.py
@@ -64,6 +64,14 @@ def test_ipv4_mapped_peer_and_hop_match_ipv4_trusted_ranges():
assert via_proxy is True
+def test_ipv4_mapped_peer_still_matches_mapped_notation_trusted_range():
+ config = TrustedProxyConfig(use_forwarded_for=True, trusted_proxy_cidrs=["::ffff:10.0.0.0/104"])
+ request = make_request(headers={"x-forwarded-for": "203.0.113.9"}, client=("::ffff:10.0.0.1", 1))
+ ip, via_proxy = resolve_client_ip(request, config)
+ assert ip == "203.0.113.9"
+ assert via_proxy is True
+
+
def test_spoofed_xff_from_untrusted_peer_is_ignored():
request = make_request(
headers={"x-forwarded-for": "203.0.113.9"}, client=("8.8.8.8", 1)