The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
Fix: Prevent LiteLLM API key leakage on /health endpoint failures (#18133)
Alexsander Hamire96b3016420b
diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.pyindex f2f6a785969..ba516c1b78b 100644--- a/litellm/litellm_core_utils/litellm_logging.py+++ b/litellm/litellm_core_utils/litellm_logging.py@@ -917,9 +917,11 @@ class Logging(LiteLLMLoggingBaseClass):raw_request_body=self._get_raw_request_body(additional_args.get("complete_input_dict", {})),+ # NOTE: setting ignore_sensitive_headers to True will cause+ # the Authorization header to be leaked when calls to the health+ # endpoint are made and fail.raw_request_headers=self._get_masked_headers(additional_args.get("headers", {}) or {},- ignore_sensitive_headers=True,),error=None,)diff --git a/tests/test_litellm/litellm_core_utils/test_health_check_helpers.py b/tests/test_litellm/litellm_core_utils/test_health_check_helpers.pyindex 49be7f39a18..867ab675943 100644--- a/tests/test_litellm/litellm_core_utils/test_health_check_helpers.py+++ b/tests/test_litellm/litellm_core_utils/test_health_check_helpers.py@@ -11,6 +11,7 @@ sys.path.insert(from litellm.constants import LITTELM_INTERNAL_HEALTH_SERVICE_ACCOUNT_NAMEfrom litellm.litellm_core_utils.health_check_helpers import HealthCheckHelpers+from litellm.main import ahealth_checkfrom litellm.proxy._types import UserAPIKeyAuth@@ -78,4 +79,59 @@ def test_get_litellm_internal_health_check_user_api_key_auth():assert result.api_key == LITTELM_INTERNAL_HEALTH_SERVICE_ACCOUNT_NAMEassert result.team_id == LITTELM_INTERNAL_HEALTH_SERVICE_ACCOUNT_NAMEassert result.key_alias == LITTELM_INTERNAL_HEALTH_SERVICE_ACCOUNT_NAME- assert result.team_alias == LITTELM_INTERNAL_HEALTH_SERVICE_ACCOUNT_NAME\ No newline at end of file+ assert result.team_alias == LITTELM_INTERNAL_HEALTH_SERVICE_ACCOUNT_NAME+++async def test_ahealth_check_failure_masks_raw_request_headers():+ """+ Security test: Verify that when ahealth_check() fails, the raw_request_headers+ in raw_request_typed_dict are properly masked to prevent API key leaks.++ This tests the fix for the security vulnerability where Authorization headers+ were being exposed in health check error responses.+ """+ # Use a model configuration that will fail (invalid endpoint)+ test_api_key = "dapi-test-key-1234567890abcdef"+ test_headers = {+ "Authorization": f"Bearer {test_api_key}",+ "Content-Type": "application/json",+ }++ response = await ahealth_check(+ model_params={+ "model": "databricks/dbrx-instruct",+ "api_base": "https://invalid-endpoint-that-will-fail.com/",+ "api_key": test_api_key,+ "headers": test_headers,+ },+ mode="chat",+ )++ # Should have error and raw_request_typed_dict+ assert "error" in response+ assert "raw_request_typed_dict" in response++ raw_request_dict = response["raw_request_typed_dict"]+ assert raw_request_dict is not None+ assert isinstance(raw_request_dict, dict)+ assert "raw_request_headers" in raw_request_dict++ headers = raw_request_dict["raw_request_headers"]+ assert headers is not None++ # Security check: Authorization header should be masked, not show full key+ if "Authorization" in headers:+ auth_header = headers["Authorization"]+ # Should be masked (e.g., "Be****90" or similar)+ assert auth_header != f"Bearer {test_api_key}", "Authorization header must be masked"+ assert auth_header != test_api_key, "API key must not appear in Authorization header"+ # Masked headers typically have asterisks or are truncated+ assert "*" in auth_header or len(auth_header) < len(f"Bearer {test_api_key}"), \+ f"Authorization header should be masked but got: {auth_header}"++ # Content-Type should remain unmasked (not sensitive)+ if "Content-Type" in headers:+ assert headers["Content-Type"] == "application/json"++ print(f"Masked Authorization header: {headers.get('Authorization', 'NOT FOUND')}")\ No newline at end of file