From d0dc40c25a5caa0ffbc335fa89f6d8d93292874c Mon Sep 17 00:00:00 2001 From: g97iulio1609 Date: Sat, 28 Feb 2026 14:15:11 +0100 Subject: [PATCH] fix(proxy): preserve master_key set by initialize() during lifespan startup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit proxy_startup_event() unconditionally overwrote master_key with the value of the LITELLM_MASTER_KEY env var, discarding a valid master_key already configured by initialize() from the YAML config. When neither CONFIG_FILE_PATH nor WORKER_CONFIG env vars are set (e.g. programmatic startup or test harnesses), the config is not reloaded during lifespan, so master_key stayed None. This caused user_api_key_auth() to return INTERNAL_USER for every request — including the admin key — resulting in 403 errors for MCP tool calls and list_tools. Only overwrite master_key from the env var when it has a truthy value, preserving any previously configured value. Fixes #22330 --- litellm/proxy/proxy_server.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index bd5b5309e0fb..b67ab8287901 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -765,7 +765,13 @@ async def proxy_startup_event(app: FastAPI): # noqa: PLR0915 premium_user = _license_check.is_premium() ## CHECK MASTER KEY IN ENVIRONMENT ## - master_key = get_secret_str("LITELLM_MASTER_KEY") + # Only overwrite master_key from env var if it is actually set. + # initialize() may have already configured master_key from a config file; + # unconditionally assigning None here would discard that value when the + # LITELLM_MASTER_KEY env var is absent (e.g. programmatic startup, tests). + _env_master_key = get_secret_str("LITELLM_MASTER_KEY") + if _env_master_key: + master_key = _env_master_key ### LOAD CONFIG ### worker_config: Optional[Union[str, dict]] = get_secret("WORKER_CONFIG") # type: ignore env_config_yaml: Optional[str] = get_secret_str("CONFIG_FILE_PATH")