fix(proxy): enforce specified loopback-only forwarding trust

This commit is contained in:
Devin AI 2026-11-02 02:49:29 +00:00 • committed by Yujong Lee
parent 21b2087981
commit 48997c340c
3 changed files with 5 additions and 31 deletions

View file

@ -18,13 +18,6 @@ def gateway_binary() -> str | None:
return shutil.which(name, path=sysconfig.get_path("scripts")) or shutil.which(name)
def forwarded_allow_ips(trusted: str | None) -> str:
if trusted == "*":
return trusted
hosts: Final = tuple(host.strip() for host in (trusted or "").split(",") if host.strip())
return ",".join(dict.fromkeys(("127.0.0.1", *hosts)))
def startup_error(binary: str | None, unsupported: Mapping[str, object]) -> str | None:
flag: Final = next((name for name, value in unsupported.items() if value), None)
if flag is not None:
@ -77,7 +70,7 @@ async def _supervise(binary: str, command: Sequence[str], host: str, port: int)
"LITELLM_RUST_GATEWAY": "false",
GATEWAY_FD: str(listener.fileno()),
"_LITELLM_GATEWAY_METRICS_HOST": host,
"FORWARDED_ALLOW_IPS": forwarded_allow_ips(os.environ.get("FORWARDED_ALLOW_IPS")),
"FORWARDED_ALLOW_IPS": "127.0.0.1",
},
pass_fds=(listener.fileno(),),
start_new_session=True,

View file

@ -21,9 +21,8 @@ def ready(client: httpx.Client) -> bool:
@pytest.mark.parametrize("server_arguments", [(), ("--run_gunicorn",)])
@pytest.mark.parametrize(("trusted_proxies", "expected_ip"), [("", "127.0.0.2"), ("127.0.0.2", "198.51.100.10")])
def test_gateway_preserves_metrics_bind_and_mcp_client_ip(
gateway: Gateway, tmp_path: Path, server_arguments: tuple[str, ...], trusted_proxies: str, expected_ip: str
gateway: Gateway, tmp_path: Path, server_arguments: tuple[str, ...]
) -> None:
config: Final = tmp_path / "config.yaml"
config.write_text(
@ -41,7 +40,6 @@ def test_gateway_preserves_metrics_bind_and_mcp_client_ip(
"LITELLM_MASTER_KEY": gateway.key,
"STORE_MODEL_IN_DB": "False",
"PYTHON_DOTENV_DISABLED": "1",
"FORWARDED_ALLOW_IPS": trusted_proxies,
}
with (tmp_path / "proxy.log").open("wb") as output:
with subprocess.Popen(
@ -82,10 +80,10 @@ def test_gateway_preserves_metrics_bind_and_mcp_client_ip(
assert response.status_code == 200, response.text
client_ip: Final = external.get(
"/v1/mcp/network/client-ip",
headers={"Authorization": f"Bearer {gateway.key}", "X-Forwarded-For": "198.51.100.10"},
headers={"Authorization": f"Bearer {gateway.key}", "X-Forwarded-For": "1.2.3.4"},
)
assert client_ip.status_code == 200, client_ip.text
assert client_ip.json() == {"ip": expected_ip}
assert client_ip.json() == {"ip": "127.0.0.2"}
finally:
assert stop_root_process(supervisor), (tmp_path / "proxy.log").read_text()
assert supervisor.returncode == 0, (tmp_path / "proxy.log").read_text()

View file

@ -13,7 +13,6 @@ from litellm.proxy.proxy_cli import ProxyInitializationHelpers, run_server
from litellm.proxy.rust_gateway import (
child_argv,
cli_command,
forwarded_allow_ips,
gateway_binary,
inherited_fd,
metrics_host,
@ -54,21 +53,6 @@ def test_missing_binary_gives_install_instruction() -> None:
)
@pytest.mark.parametrize(
("trusted", "expected"),
[
(None, "127.0.0.1"),
("", "127.0.0.1"),
("192.0.2.1", "127.0.0.1,192.0.2.1"),
("127.0.0.1, 192.0.2.0/24", "127.0.0.1,192.0.2.0/24"),
("*", "*"),
("127.0.0.1,*", "127.0.0.1,*"),
],
)
def test_private_hop_preserves_configured_trusted_proxies(trusted: str | None, expected: str) -> None:
assert forwarded_allow_ips(trusted) == expected
@pytest.mark.parametrize("supervised", [False, True])
@pytest.mark.parametrize("host", ["0.0.0.0", "192.0.2.1", "::"])
def test_metrics_keep_configured_host_separate_from_private_listener(
@ -174,7 +158,6 @@ def test_supervisor_drains_before_stopping_python_and_reaps_both_children(
patch("asyncio.create_subprocess_exec", side_effect=spawn) as launch,
patch("socket.socket", return_value=listener),
patch("os.killpg", side_effect=kill_group) as kill,
patch.dict("os.environ", {"FORWARDED_ALLOW_IPS": "192.0.2.1"}),
):
assert supervise("gateway", ("python", "--rust_gateway"), "0.0.0.0", 4000) == expected
@ -184,7 +167,7 @@ def test_supervisor_drains_before_stopping_python_and_reaps_both_children(
assert python_call.args == ("python", "--host", "127.0.0.1", "--port", "45555")
assert python_call.kwargs["pass_fds"] == (17,)
assert python_call.kwargs["env"]["LITELLM_RUST_GATEWAY"] == "false"
assert python_call.kwargs["env"]["FORWARDED_ALLOW_IPS"] == "127.0.0.1,192.0.2.1"
assert python_call.kwargs["env"]["FORWARDED_ALLOW_IPS"] == "127.0.0.1"
assert rust_call.kwargs["env"]["LITELLM_FORWARD_UPSTREAM"] == "http://127.0.0.1:45555"
assert rust_call.kwargs["env"]["PORT"] == "4000"
if event == "spawn-error":