From a0fb762624447fb9c511f53c40e793ba2bf9a1c2 Mon Sep 17 00:00:00 2001 From: sinksilk <785976238@qq.com> Date: Tue, 22 Sep 2026 02:04:32 +0800 Subject: [PATCH] fix(proxy/auth): clarify wrong master key error when no DB is connected Keep ProxyErrorTypes.no_db_connection so allow_requests_on_db_unavailable HA fallback still works; only replace the misleading "No connected db." message so operators can tell an invalid bearer from a missing database. Fixes #12273 --- litellm/proxy/auth/user_api_key_auth.py | 11 ++- .../proxy/auth/test_user_api_key_auth.py | 82 +++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/auth/user_api_key_auth.py b/litellm/proxy/auth/user_api_key_auth.py index 4371ce4fda8..9b4241b09c5 100644 --- a/litellm/proxy/auth/user_api_key_auth.py +++ b/litellm/proxy/auth/user_api_key_auth.py @@ -2097,8 +2097,17 @@ async def _user_api_key_auth_builder( if ( prisma_client is None ): # if both master key + user key submitted, and user key != master key, and no db connected, raise an error + # Keep type=no_db_connection: auth_exception_handler / + # allow_requests_on_db_unavailable keys off this type for HA + # fallback. Only the operator-facing message is corrected here + # (issue #12273 — "No connected db." misled master-key-only setups). raise ProxyException( - message="No connected db.", + message=( + "Authentication Error, Invalid proxy server token passed. " + "Token does not match the configured master key, and no " + "database is connected to look up additional API keys. " + "Set DATABASE_URL to enable virtual keys." + ), type=ProxyErrorTypes.no_db_connection, code=400, param=None, diff --git a/tests/test_litellm/proxy/auth/test_user_api_key_auth.py b/tests/test_litellm/proxy/auth/test_user_api_key_auth.py index da36071a5b4..01e95c5bf7f 100644 --- a/tests/test_litellm/proxy/auth/test_user_api_key_auth.py +++ b/tests/test_litellm/proxy/auth/test_user_api_key_auth.py @@ -6297,6 +6297,88 @@ async def test_master_key_auth_substitutes_alias_for_api_key(): setattr(_proxy_server_mod, k, v) +@pytest.mark.asyncio +async def test_wrong_master_key_without_db_returns_clear_auth_error(): + """Regression for https://github.com/BerriAI/litellm/issues/12273. + + Wrong bearer + no DB previously returned ``\"No connected db.\"``, which + sends operators chasing Postgres on master-key-only deployments. The + message must state the token is invalid / only the master key works. + """ + from fastapi import Request + from starlette.datastructures import URL + + import litellm.proxy.proxy_server as _proxy_server_mod + from litellm.proxy.auth.user_api_key_auth import _user_api_key_auth_builder + + attrs = _proxy_server_attrs_for_custom_auth(user_custom_auth=None) + attrs["prisma_client"] = None + _orig = {k: getattr(_proxy_server_mod, k, None) for k in attrs} + try: + for k, v in attrs.items(): + setattr(_proxy_server_mod, k, v) + + request = Request(scope={"type": "http"}) + request._url = URL(url="/chat/completions") + + with pytest.raises(ProxyException) as exc_info: + await _user_api_key_auth_builder( + request=request, + api_key="Bearer sk-not-the-master-key", + azure_api_key_header="", + anthropic_api_key_header=None, + google_ai_studio_api_key_header=None, + azure_apim_header=None, + request_data={}, + ) + + message = str(exc_info.value.message) + assert "No connected db" not in message + assert "master key" in message.lower() + finally: + for k, v in _orig.items(): + setattr(_proxy_server_mod, k, v) + + +@pytest.mark.asyncio +async def test_wrong_master_key_without_db_preserves_no_db_connection_type(): + """Pin ``ProxyErrorTypes.no_db_connection`` for the wrong-key / no-DB + path. ``allow_requests_on_db_unavailable`` keys off this type; switching + it to ``auth_error`` breaks HA fallback (see closed PRs #20837/#28389). + """ + from fastapi import Request + from starlette.datastructures import URL + + import litellm.proxy.proxy_server as _proxy_server_mod + from litellm.proxy.auth.user_api_key_auth import _user_api_key_auth_builder + + attrs = _proxy_server_attrs_for_custom_auth(user_custom_auth=None) + attrs["prisma_client"] = None + _orig = {k: getattr(_proxy_server_mod, k, None) for k in attrs} + try: + for k, v in attrs.items(): + setattr(_proxy_server_mod, k, v) + + request = Request(scope={"type": "http"}) + request._url = URL(url="/chat/completions") + + with pytest.raises(ProxyException) as exc_info: + await _user_api_key_auth_builder( + request=request, + api_key="Bearer sk-not-the-master-key", + azure_api_key_header="", + anthropic_api_key_header=None, + google_ai_studio_api_key_header=None, + azure_apim_header=None, + request_data={}, + ) + + assert exc_info.value.type == ProxyErrorTypes.no_db_connection + finally: + for k, v in _orig.items(): + setattr(_proxy_server_mod, k, v) + + @pytest.mark.asyncio async def test_user_api_key_auth_sets_end_user_id_when_builder_skips_it(): """Defense-in-depth: ``_user_api_key_auth_builder`` has multiple