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
refactor(router): scrub retry-breadcrumb credentials by pattern, not a denylist
mateo-berri721227e9eee3
diff --git a/litellm/router.py b/litellm/router.pyindex 132ff6671a7..6ee474730c9 100644--- a/litellm/router.py+++ b/litellm/router.py@@ -80,6 +80,7 @@ from litellm.litellm_core_utils.request_timeout_resolver import (from litellm.litellm_core_utils.secret_redaction import redact_stringfrom litellm.litellm_core_utils.sensitive_data_masker import (SensitiveDataMasker,+ mask_credentials_in_payload,mask_sensitive_structure,)from litellm.llms.openai_like.json_loader import JSONProviderRegistry@@ -375,18 +376,15 @@ def _replay_live_router_model_cost() -> None:set_live_deployment_replay(_replay_live_router_model_cost)-# Kwargs that log_retry must not copy into a retry breadcrumb. The breadcrumbs reach spend-# logs and logging callbacks, so they must never carry the request payload, router-internal-# walk state, or transport credentials: provider_specific_header / headers / api_key can hold a-# client's forwarded Authorization or a provider key, none of which identify the failed attempt.+# Kwargs that carry no signal about the failed attempt, so log_retry drops them from a+# breadcrumb entirely: the request payload and the router-internal walk state. Credentials are+# handled separately by mask_credentials_in_payload, which scrubs credential-named values from+# whatever kwargs remain rather than trying to enumerate every credential-bearing key here.RETRY_BREADCRUMB_EXCLUDED_KWARGS: Final = frozenset(("messages","original_function","attempted_targets",- "provider_specific_header",- "headers",- "api_key",))@@ -7357,7 +7355,8 @@ class Router:if len(self.previous_models) > 3:self.previous_models.pop(0)- self.previous_models.append(previous_model)+ scrubbed_previous_model: Final = mask_credentials_in_payload(previous_model)+ self.previous_models.append(scrubbed_previous_model)kwargs[_metadata_var]["previous_models"] = self.previous_modelsreturn kwargsexcept Exception as e:diff --git a/tests/test_litellm/test_router.py b/tests/test_litellm/test_router.pyindex 7e6cc010834..910b874c2ac 100644--- a/tests/test_litellm/test_router.py+++ b/tests/test_litellm/test_router.py@@ -8565,28 +8565,47 @@ async def test_retry_breadcrumbs_do_not_carry_the_walk_state():assert "attempted_targets" not in breadcrumb+_BREADCRUMB_CREDENTIAL_CANARY = "Bearer sk-ant-oat01-RETRY-BREADCRUMB-CANARY-doNotShip"+++ "container_key, request_kwargs",+ [+ (+ "provider_specific_header",+ {+ "provider_specific_header": {+ "custom_llm_provider": "openai",+ "extra_headers": {"authorization": _BREADCRUMB_CREDENTIAL_CANARY},+ }+ },+ ),+ (+ "extra_headers",+ {"extra_headers": {"authorization": _BREADCRUMB_CREDENTIAL_CANARY}},+ ),+ (+ "api_key",+ {"api_key": _BREADCRUMB_CREDENTIAL_CANARY},+ ),+ ],+)@pytest.mark.asyncio-async def test_retry_breadcrumbs_drop_forwarded_client_credentials():- """log_retry copies kwargs verbatim into previous_models, which reaches spend logs and logging- callbacks. provider_specific_header can carry a client's forwarded Authorization token, and a- breadcrumb has no diagnostic use for it, so the raw credential must never land in the breadcrumb."""- canary = "Bearer sk-ant-oat01-RETRY-BREADCRUMB-CANARY-doNotShip"+async def test_retry_breadcrumbs_never_carry_a_forwarded_credential(container_key, request_kwargs):+ """log_retry copies kwargs into previous_models, which reaches spend logs and logging callbacks.+ Any of these kwargs can carry a client's forwarded Authorization token or a provider key, and a+ breadcrumb has no diagnostic use for the raw secret. A denylist of key names is always one new+ credential kwarg behind, so log_retry scrubs credential-named values by pattern instead: the+ container still reaches the breadcrumb, but the raw secret never does, whatever key holds it."""router = _cyclic_fallback_router(num_retries=1)capture = _LogCapture(logging.ERROR)- await _drive_cyclic_fallback(- router,- capture,- provider_specific_header={- "custom_llm_provider": "openai",- "extra_headers": {"authorization": canary},- },- )+ await _drive_cyclic_fallback(router, capture, **request_kwargs)assert router.previous_models, "no retry breadcrumbs were recorded"- for breadcrumb in router.previous_models:- assert "provider_specific_header" not in breadcrumb- assert canary not in json.dumps(router.previous_models, default=str)+ dumped = json.dumps(router.previous_models, default=str)+ assert container_key in dumped, "the credential-bearing kwarg never reached the breadcrumb, so this test cannot see the leak"+ assert _BREADCRUMB_CREDENTIAL_CANARY not in dumped@pytest.mark.asyncio