litellm/tests/unit/test_router
joshua-berri 5feca58a3b
fix(router): preserve IAM auth on Anthropic to Bedrock fallbacks (#54209)
* fix(router): keep a caller's Anthropic key and forwarded headers off Bedrock fallbacks

A router fallback from an Anthropic deployment onto bedrock or bedrock_mantle inherited the
caller's api_key, so AWS received it as a bearer token instead of a SigV4 signature (#45309).
The fallback hop now leaves that key behind and signs with the deployment's own credentials.
The origin is the provider of the requested group's first attempt, or the group's sole provider
when no attempt reached it. Same-provider fallbacks, Bedrock and Mantle bearer sharing, dict
fallback targets that set api_key, and callers that send their own api_base keep today's behavior.

The proxy now stamps the names of the client headers it forwards. On a fallback hop, the router
re-checks model_group_settings.forward_client_headers_to_llm_api for the fallback group and drops
only those stamped headers when the group is out of scope, plus the forwarded x-api-key copy when
the key is left behind.

* fix(router): keep blank keys, target base URLs and hook-set headers on Bedrock fallbacks

Review follow-up for #45309. A blank caller api_key already turns off the env bearer and signs
with SigV4, so it is no longer treated as a key to leave behind. A dict fallback target that sets
api_base or base_url without its own api_key keeps the caller key, as a caller redirect does.

The proxy now stamps a keyed digest of each forwarded header value instead of only its name, so
a fallback removes a forwarded header only while it still holds that value. A pre-call hook that
replaced it keeps its value, and the logged stamp reveals no header value.

When every deployment of the requested group is Anthropic, that group decides the key owner on
every attempt. A stream recovered in a new walk is built from the same request, so it cannot
bring the caller key back onto Bedrock or Mantle.

* fix(router): leave the caller key behind only on IAM-signed Bedrock fallbacks

Narrows #45309 to its reported setup. A bedrock or bedrock_mantle fallback deployment that
authenticates with a bearer of its own, its api_key or the provider's env bearer, still gets the
caller's key as before, so shared gateways keep working. The key is dropped only when that
provider's own bearer resolver (bedrock_bearer_token, resolve_mantle_bearer_token) finds no
bearer, which keeps each provider's empty and missing api_key rules.

* test(router): let the mid-stream continuation reach a prefill-capable Mantle deployment

The continuation prefill check only routes a continuation to deployments that support assistant
prefill, and the Mantle gpt-oss entry does not declare it, so the backup was filtered out. The
backup now declares supports_assistant_prefill in model_info, and the test also asserts the
second Mantle request carries the assistant prefill.

* fix(router): keep the caller key on AWS fallbacks that set an explicit Authorization header

On an AWS deployment the caller's api_key outranks an Authorization set in headers or
extra_headers, because the bearer path writes its own Authorization. Dropping the key handed the
call to that header instead, which changed an accepted precedence. The key is now kept whenever
the request, the dict fallback target or the deployment sets an explicit Authorization header.
The proxy-forwarded path never carries one, so the reported leak stays fixed.

* fix(router): keep the caller key on AWS fallbacks behind a custom endpoint

Scopes #45309 to the public AWS endpoint setup it reports. A bedrock or bedrock_mantle
deployment that overrides its destination (api_base, base_url, aws_bedrock_runtime_endpoint, or
the AWS_BEDROCK_RUNTIME_ENDPOINT / BEDROCK_MANTLE_API_BASE env vars) may be a gateway that
takes the caller's bearer, and its auth intent cannot be inferred, so it keeps today's behavior.

* fix(router): preserve request and fallback runtime endpoint credentials

* fix(router): type the forwarded-header stamp and key mixed groups by membership

The proxy reads the request through the existing typed request seam and builds the forwarded
header stamp in a pure helper, so it adds no mutable parameter and no Unknown diagnostics, and
writes it the same way caller_credential_fields is written. The not-a-dict branch is gone, since
proxy_server_request is always the dict the proxy just built.

In a requested group whose deployments mix providers, the caller key is kept on the group's own
deployments (its order chain and retries) and left behind only on a deployment outside the
group, whatever depth the walk reports. Tests cover the mixed group, a malformed SDK stamp, and a
forwarding scope that is no longer configured.

* fix(router): avoid exported stamp constants and cover unnamed default routes

* test: remove unsupported unnamed caller-key route scenario

* style: remove trailing blank line from fallback tests

* fix(router): preserve fallback auth across defaults and stream recovery

* test(router): preserve Claude Platform fallback authentication

---------

Co-authored-by: Joshua Valluru <326636767+joshua-berri@users.noreply.github.com>
2026-11-24 12:42:27 -08:00
..
core feat(proxy): skip blocked deployments in background health checks (#52761) 2026-11-24 07:53:23 -08:00
__init__.py test: add __init__.py to every tests/unit directory phase 16 touches 2026-09-20 11:52:38 +00:00
test_enforce_model_rate_limits.py refactor(router): count the whole router_usage family on the native store (#50862) 2026-11-13 03:22:28 +00:00
test_io_token_rate_limits.py fix(router): count a Responses WebSocket turn's input against deployment ITPM (#53594) 2026-11-24 04:26:36 -08:00
test_router.py fix(router): preserve provider PDF preflight compatibility (#54208) 2026-11-24 12:36:47 -08:00
test_router_callback_hook_sequence.py test: replace 61 live logging and otel tests with offline unit and integration coverage (#45362) 2026-10-08 15:55:57 -07:00
test_router_endpoints.py fix(proxy): resolve admin oidc api_key references on batch cancel, fine-tuning listing, OCR, /v1/messages and cache prediction (#53678) 2026-11-23 10:11:10 -08:00
test_router_entry_points.py feat(proxy): mixed Codex model picker and LiteLLM routing on native codex routes (#49408) 2026-11-06 11:14:47 -08:00
test_router_facade.py refactor(router): delete the python router core, the rust core is the only core (#50178) 2026-11-05 10:40:46 -08:00
test_router_fallbacks.py fix(router): preserve IAM auth on Anthropic to Bedrock fallbacks (#54209) 2026-11-24 12:42:27 -08:00
test_router_fuzz_regressions_rust_only.py ci(unit): cut unit check time with cheaper per-test resets and fewer non-unit tests (#51864) 2026-11-14 15:41:30 -08:00
test_router_get_deployments.py refactor(router): delete the python router core, the rust core is the only core (#50178) 2026-11-05 10:40:46 -08:00
test_router_helper_utils.py fix(router): count a Responses WebSocket turn's input against deployment ITPM (#53594) 2026-11-24 04:26:36 -08:00
test_router_host.py ci(unit): cut unit check time with cheaper per-test resets and fewer non-unit tests (#51864) 2026-11-14 15:41:30 -08:00
test_router_max_parallel_requests.py feat(tts)!: stream audio from the provider as it arrives on /v1/audio/speech (#46222) 2026-11-17 15:03:12 +00:00
test_router_provider_endpoints.py feat(redis): serve the proxy's common Redis call with Rust stores (#50170) 2026-11-06 06:01:11 +00:00
test_router_retries.py refactor(router): delete the python router core, the rust core is the only core (#50178) 2026-11-05 10:40:46 -08:00
test_router_surface_check.py chore: move workspace packages under litellm (#51000) 2026-11-12 04:19:57 +00:00
test_router_timeout.py feat(messages): serve bedrock invoke messages on rust only (#51330) 2026-11-15 16:58:31 -08:00
test_router_vector_store_endpoints.py test: migrate SDK and proxy-booting legacy CircleCI tests to their tiers (#45758) 2026-10-11 00:17:21 -07:00