fix(proxy): treat an empty allowed_ips list as no allowlist

Deleting the last entry via POST /delete/allowed_ip stored
general_settings.allowed_ips = [] in the LiteLLM_Config row. An empty list is
not None, so the IP check read it as "an allowlist is configured and nothing
matches" and answered 403 to every caller from every address, including the
/add/allowed_ip call needed to put an IP back. Recovery needed direct database
access.

Fixed at both ends. The IP check now treats a falsy allowlist the same as an
unset one, which also heals deployments already holding a stored []. The delete
endpoint drops the key entirely once the list empties, so the trap is not
re-armed. The boot-time enterprise-license gate had the same blind spot and
would refuse to start a non-premium proxy over an empty list.
This commit is contained in:
Yuneng Jiang 2026-09-18 10:19:47 -07:00
parent 4e2117832a
commit cfcf0b0997
No known key found for this signature in database
5 changed files with 184 additions and 13 deletions

View file

@ -93,9 +93,13 @@ def _check_valid_ip(
use_x_forwarded_for: bool | None = False,
) -> tuple[bool, str | None]:
"""
Returns if ip is allowed or not
Returns if ip is allowed or not.
An empty allowlist means "no allowlist configured", the same as an unset
one. Treating it as "allow nothing" would lock every caller out of the
proxy, including the endpoints needed to add an IP back.
"""
if allowed_ips is None: # if not set, assume true
if not allowed_ips:
return True, None
# if general_settings.get("use_x_forwarded_for") is True then use x-forwarded-for

View file

@ -6207,7 +6207,7 @@ class ProxyConfig:
ui_access_mode = general_settings.get("ui_access_mode", "all") # can be either ["admin_only" or "all"]
### ALLOWED IP ###
allowed_ips: Final = general_settings.get("allowed_ips", None)
if allowed_ips is not None and premium_user is False:
if allowed_ips and premium_user is False:
raise ValueError(
"allowed_ips is an Enterprise Feature. Please add a valid LITELLM_LICENSE to your envionment."
)

View file

@ -568,11 +568,14 @@ async def delete_allowed_ip(
)
_allowed_ips: Final[list] = general_settings.get("allowed_ips", [])
if ip_address.ip in _allowed_ips:
_allowed_ips.remove(ip_address.ip)
if ip_address.ip not in _allowed_ips:
raise HTTPException(status_code=404, detail="IP address not found")
_allowed_ips.remove(ip_address.ip)
if _allowed_ips:
general_settings["allowed_ips"] = _allowed_ips
else:
raise HTTPException(status_code=404, detail="IP address not found")
general_settings.pop("allowed_ips", None)
# Load existing config
config: Final = await proxy_config.get_config()
@ -580,12 +583,12 @@ async def delete_allowed_ip(
if "general_settings" not in config:
config["general_settings"] = {}
if "allowed_ips" not in config["general_settings"]:
config["general_settings"]["allowed_ips"] = []
before_allowed_ips: Final = list(config["general_settings"]["allowed_ips"])
if ip_address.ip in config["general_settings"]["allowed_ips"]:
config["general_settings"]["allowed_ips"].remove(ip_address.ip)
before_allowed_ips: Final = list(config["general_settings"].get("allowed_ips", []))
after_allowed_ips: Final = [ip for ip in before_allowed_ips if ip != ip_address.ip]
if after_allowed_ips:
config["general_settings"]["allowed_ips"] = after_allowed_ips
else:
config["general_settings"].pop("allowed_ips", None)
await proxy_config.save_config(new_config=config)
@ -594,7 +597,7 @@ async def delete_allowed_ip(
param_name="general_settings",
action="deleted",
before_value={"allowed_ips": before_allowed_ips},
after_value={"allowed_ips": config["general_settings"]["allowed_ips"]},
after_value={"allowed_ips": after_allowed_ips},
user_api_key_dict=user_api_key_dict,
)
)

View file

@ -3918,3 +3918,71 @@ class TestIsRequestBodySafeBlocksAwsIdentitySelectors:
)
is True
)
class TestAllowedIpsEmptyListIsNotAnAllowlist:
"""Removing the last entry via ``/delete/allowed_ip`` used to leave
``general_settings.allowed_ips == []`` behind, which the IP check read as
"an allowlist is configured and nothing matches" and answered with 403 for
every caller from every address, including the endpoint needed to add an IP
back. An empty allowlist has to mean the same thing as an unset one."""
@staticmethod
def _request(client_host: str) -> Request:
return Request(
scope={
"type": "http",
"method": "POST",
"path": "/v1/chat/completions",
"headers": [],
"client": (client_host, 51234),
}
)
async def _status_for(self, monkeypatch, general_settings: dict, client_host: str) -> int:
from fastapi import HTTPException
import litellm.proxy.proxy_server as proxy_server_module
from litellm.proxy.auth.auth_utils import pre_db_read_auth_checks
monkeypatch.setattr(proxy_server_module, "general_settings", general_settings)
try:
await pre_db_read_auth_checks(
request=self._request(client_host),
request_data={"model": "gpt-4o"},
route="/v1/chat/completions",
)
except HTTPException as exc:
return exc.status_code
return 200
@pytest.mark.asyncio
@pytest.mark.parametrize("stored_allowed_ips", [[], None])
async def test_any_ip_is_allowed_when_allowlist_is_empty_or_unset(self, monkeypatch, stored_allowed_ips):
status_code = await self._status_for(
monkeypatch,
general_settings={"allowed_ips": stored_allowed_ips},
client_host="203.0.113.9",
)
assert status_code == 200
@pytest.mark.asyncio
async def test_unlisted_ip_is_still_rejected_when_allowlist_is_populated(self, monkeypatch):
status_code = await self._status_for(
monkeypatch,
general_settings={"allowed_ips": ["198.51.100.4"]},
client_host="203.0.113.9",
)
assert status_code == 403
@pytest.mark.asyncio
async def test_listed_ip_is_allowed_when_allowlist_is_populated(self, monkeypatch):
status_code = await self._status_for(
monkeypatch,
general_settings={"allowed_ips": ["203.0.113.9"]},
client_host="203.0.113.9",
)
assert status_code == 200

View file

@ -2,6 +2,7 @@ import json
import os
import pytest
from fastapi import Request
from fastapi.testclient import TestClient
@ -3467,3 +3468,98 @@ class TestSyncUiSettingsToGeneralSettings:
assert general_settings["forward_client_headers_to_llm_api"] is False
assert general_settings.source("forward_client_headers_to_llm_api") == "config"
class TestDeleteAllowedIpClearsTheKeyWhenTheListEmpties:
"""Leaving ``allowed_ips: []`` behind after the last entry is removed bricks
the proxy: the IP check reads an empty allowlist as "configured and nothing
matches" and 403s everyone, including the call that would add an IP back.
The delete endpoint has to drop the key, not store an empty list."""
@staticmethod
def _harness(monkeypatch, stored_allowed_ips: list[str]):
from unittest.mock import AsyncMock, MagicMock
import litellm
import litellm.proxy.proxy_server as proxy_server_module
from litellm.proxy.config_resolvers import SettingsStore
saved: dict = {}
config = {"general_settings": {"allowed_ips": list(stored_allowed_ips)}}
async def _get_config():
return config
async def _save_config(new_config=None):
saved["config"] = new_config
return new_config
general_settings = SettingsStore("general_settings")
general_settings.apply_db_row("general_settings", {"allowed_ips": list(stored_allowed_ips)})
monkeypatch.setattr(proxy_server_module, "prisma_client", MagicMock())
monkeypatch.setattr(proxy_server_module, "premium_user", True)
monkeypatch.setattr(proxy_server_module, "general_settings", general_settings)
monkeypatch.setattr(litellm, "store_audit_logs", False)
monkeypatch.setattr(proxy_server_module.proxy_config, "get_config", _get_config)
monkeypatch.setattr(proxy_server_module.proxy_config, "save_config", _save_config)
return general_settings, saved
@staticmethod
def _delete(ip: str):
from litellm.proxy._types import UserAPIKeyAuth
from litellm.proxy.auth.user_api_key_auth import user_api_key_auth
async def _admin_auth():
return UserAPIKeyAuth(
user_id="allowed-ip-admin",
api_key="hashed-admin-key",
user_role=LitellmUserRoles.PROXY_ADMIN,
)
app.dependency_overrides[user_api_key_auth] = _admin_auth
try:
return client.post("/delete/allowed_ip", json={"ip": ip})
finally:
app.dependency_overrides.pop(user_api_key_auth, None)
def test_removing_the_last_ip_persists_no_allowlist_at_all(self, monkeypatch):
general_settings, saved = self._harness(monkeypatch, ["203.0.113.77"])
resp = self._delete("203.0.113.77")
assert resp.status_code == 200, resp.text
assert "allowed_ips" not in saved["config"]["general_settings"]
assert general_settings.get("allowed_ips") is None
def test_removing_the_last_ip_leaves_the_ip_check_open(self, monkeypatch):
from litellm.proxy.auth.auth_utils import _check_valid_ip
general_settings, saved = self._harness(monkeypatch, ["203.0.113.77"])
assert self._delete("203.0.113.77").status_code == 200
request = Request(
scope={
"type": "http",
"method": "POST",
"path": "/v1/chat/completions",
"headers": [],
"client": ("198.51.100.23", 51234),
}
)
is_valid, _ = _check_valid_ip(
allowed_ips=saved["config"]["general_settings"].get("allowed_ips"),
request=request,
)
assert is_valid is True
def test_removing_one_of_several_ips_keeps_the_allowlist(self, monkeypatch):
general_settings, saved = self._harness(monkeypatch, ["203.0.113.77", "198.51.100.1"])
assert self._delete("203.0.113.77").status_code == 200
assert saved["config"]["general_settings"]["allowed_ips"] == ["198.51.100.1"]
assert general_settings["allowed_ips"] == ["198.51.100.1"]