From 4fea8fb17b9dea40058668b4cd6007e26a4fd71a Mon Sep 17 00:00:00 2001 From: hatchedagent Date: Sun, 20 Sep 2026 15:12:06 +0000 Subject: [PATCH 1/2] fix: return 401 auth_error for wrong key when no DB is connected With a master key configured and no database connected, a request using any other key raised 400 no_db_connection ("No connected db."). Without a DB the master key is the only valid credential, so a non-matching key is an authentication failure. Raise 401 auth_error instead. Adds regression tests driving the real auth builder with prisma_client=None. Fixes #12273. --- litellm/proxy/auth/user_api_key_auth.py | 14 +++-- .../proxy/auth/test_user_api_key_auth.py | 59 +++++++++++++++++++ 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/litellm/proxy/auth/user_api_key_auth.py b/litellm/proxy/auth/user_api_key_auth.py index de0131772bc..a724461b227 100644 --- a/litellm/proxy/auth/user_api_key_auth.py +++ b/litellm/proxy/auth/user_api_key_auth.py @@ -2084,13 +2084,15 @@ async def _user_api_key_auth_builder( ## Check DB - if ( - prisma_client is None - ): # if both master key + user key submitted, and user key != master key, and no db connected, raise an error + # No database is connected, so the master key is the only valid + # credential. A key that does not match it is an authentication + # failure, not a database connectivity problem. + if prisma_client is None: raise ProxyException( - message="No connected db.", - type=ProxyErrorTypes.no_db_connection, - code=400, + message="Authentication Error, Invalid proxy server token passed. " + "No database is connected, so only the master key is accepted.", + type=ProxyErrorTypes.auth_error, + code=401, 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 8593be751fa..473f2f949ff 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 @@ -6734,6 +6734,65 @@ async def test_builder_returns_401_when_db_lookup_reports_missing_key(): assert int(exc_info.value.code) == status.HTTP_401_UNAUTHORIZED +async def _run_builder_without_db(api_key: str): + """Drive the real auth builder with no database connected, using the + given bearer key. Returns the builder result.""" + 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_attrs_for_db_lookup(), "prisma_client": None} + originals = {a: getattr(_proxy_server_mod, a, None) for a 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 patch( + "litellm.proxy.auth.auth_exception_handler.seed_request_identity", + ): + return await _user_api_key_auth_builder( + request=request, + api_key=api_key, + azure_api_key_header="", + anthropic_api_key_header=None, + google_ai_studio_api_key_header=None, + azure_apim_header=None, + request_data={}, + ) + finally: + for k, v in originals.items(): + setattr(_proxy_server_mod, k, v) + + +@pytest.mark.asyncio +async def test_builder_returns_401_auth_error_for_wrong_key_without_db(): + """Regression test for https://github.com/BerriAI/litellm/issues/12273. + + With a master key configured and no database connected, a request using + any other key must fail as an authentication error (401), not as + "No connected db." (400 no_db_connection). Without a DB the master key + is the only valid credential, so a non-matching key is a wrong key. + """ + with pytest.raises(ProxyException) as exc_info: + await _run_builder_without_db(api_key="Bearer ") + + assert int(exc_info.value.code) == status.HTTP_401_UNAUTHORIZED + assert exc_info.value.type == ProxyErrorTypes.auth_error + assert "No connected db" not in exc_info.value.message + + +@pytest.mark.asyncio +async def test_builder_accepts_master_key_without_db(): + """Control: the master key itself still authenticates when no DB is + connected, proving the 401 above only fires for non-matching keys.""" + result = await _run_builder_without_db(api_key="Bearer sk-test-master") + + assert isinstance(result, UserAPIKeyAuth) + + @pytest.mark.asyncio async def test_builder_succeeds_when_db_lookup_returns_valid_token(): """Regression guard: a valid key still authenticates. Proves the 503 From b18c0d6b12266a545f07397cf95f52377d62b1f1 Mon Sep 17 00:00:00 2001 From: hatchedagent Date: Sun, 20 Sep 2026 15:32:43 +0000 Subject: [PATCH 2/2] chore: remove redundant comment restating control flow --- litellm/proxy/auth/user_api_key_auth.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/litellm/proxy/auth/user_api_key_auth.py b/litellm/proxy/auth/user_api_key_auth.py index a724461b227..151f4643538 100644 --- a/litellm/proxy/auth/user_api_key_auth.py +++ b/litellm/proxy/auth/user_api_key_auth.py @@ -2084,9 +2084,6 @@ async def _user_api_key_auth_builder( ## Check DB - # No database is connected, so the master key is the only valid - # credential. A key that does not match it is an authentication - # failure, not a database connectivity problem. if prisma_client is None: raise ProxyException( message="Authentication Error, Invalid proxy server token passed. "