Skip to content

fix(proxy): stop serving stale team model allowlist after /team/update - #34266

Merged
mateo-berri merged 4 commits into
litellm_internal_stagingfrom
litellm_team-model-allowlist-stale-qqg50q
Jul 29, 2026
Merged

mateo-berri merged 4 commits into
litellm_internal_stagingfrom
litellm_team-model-allowlist-stale-qqg50q

Conversation

@mateo-berri

@mateo-berri mateo-berri commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • all-team-models keys kept a stale team allowlist after /team/update
  • newly added team models returned 403 team_model_access_denied
  • removed team models stayed callable; /cache/flushall did not help

How it solves it:

  • _cache_team_object now invalidates the internal usage cache copy
  • invalidation is best-effort so cache blips cannot fail requests
  • auth no longer writes team objects back into the cache

Relevant issues

Linear ticket

Resolves LIT-4391

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • My PR passes all CI/CD checks (e.g., lint, format, unit tests)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Root cause: get_team_object reads proxy_logging_obj.internal_usage_cache before user_api_key_cache. With enable_redis_auth_cache both caches share one Redis, so ordinary reads backfill the internal cache's in-memory tier with the team object. /team/update refreshed only user_api_key_cache, so the backfilled copy kept shadowing the update; the auth builder then wrote the team object it had just read back into the cache after check 6, clobbering the fresh Redis value with the stale one. Under traffic this loop is self-sustaining and immune to /cache/flushall, which is exactly what the customer reported

Proof below is fully e2e: a live proxy on localhost:4000 backed by Postgres and Redis (enable_redis_auth_cache: true), with model-a/model-b both mapped to real gemini/gemini-3.6-flash deployments (real provider calls, real cost), following the reproduction steps from the Linear ticket

Before, captured at commit f1f0a0b (master without the fix):

$ TEAM_ID=$(curl -s -X POST $BASE/team/new -H "Authorization: Bearer $MASTER" -H "Content-Type: application/json" -d '{"team_alias":"lit-4391-repro","models":["model-a"]}' | jq -r .team_id)
-> 8eba32df-5321-4103-89d3-c52893500987

$ KEY=$(curl -s -X POST $BASE/key/generate -H "Authorization: Bearer $MASTER" -H "Content-Type: application/json" -d "{\"team_id\":\"$TEAM_ID\"}" | jq -r .key)
-> key generated

$ curl -s -X POST $BASE/key/update -H "Authorization: Bearer $MASTER" -H "Content-Type: application/json" -d "{\"key\":\"$KEY\",\"models\":[\"all-team-models\"]}" | jq .models
-> ['all-team-models']

$ for i in 1 2 3; do curl -s -o /dev/null -w "%{http_code} " $BASE/v1/chat/completions -H "Authorization: Bearer $KEY" -d '{"model":"model-a","messages":[{"role":"user","content":"say ok"}]}'; done   # prime auth cache
200 200 200

$ curl -s -X POST $BASE/team/update -H "Authorization: Bearer $MASTER" -H "Content-Type: application/json" -d "{\"team_id\":\"$TEAM_ID\",\"models\":[\"model-a\",\"model-b\"]}" | jq .data.models
-> ['model-a', 'model-b']

$ curl -s -w "\nHTTP %{http_code}\n" $BASE/v1/chat/completions -H "Authorization: Bearer $KEY" -d '{"model":"model-b","messages":[{"role":"user","content":"say ok"}]}'   # immediately after update
{"error":{"message":"team not allowed to access model. This team can only access models=['model-a']. Tried to access model-b","type":"team_model_access_denied","param":"model","code":"403"}}
HTTP 403

After, captured at commit 221b185 (this PR):

$ TEAM_ID=$(curl -s -X POST $BASE/team/new -H "Authorization: Bearer $MASTER" -H "Content-Type: application/json" -d '{"team_alias":"lit-4391-fixed","models":["model-a"]}' | jq -r .team_id)
-> 653b6782-1296-4182-93ed-107679a25757

$ KEY=$(curl -s -X POST $BASE/key/generate -H "Authorization: Bearer $MASTER" -H "Content-Type: application/json" -d "{\"team_id\":\"$TEAM_ID\"}" | jq -r .key)
-> key generated

$ curl -s -X POST $BASE/key/update -H "Authorization: Bearer $MASTER" -H "Content-Type: application/json" -d "{\"key\":\"$KEY\",\"models\":[\"all-team-models\"]}" | jq .models
-> ['all-team-models']

$ for i in 1 2 3; do curl -s -o /dev/null -w "%{http_code} " $BASE/v1/chat/completions -H "Authorization: Bearer $KEY" -d '{"model":"model-a","messages":[{"role":"user","content":"say ok"}]}'; done   # prime auth cache
200 200 200

$ curl -s -o /dev/null -w "%{http_code}\n" $BASE/v1/chat/completions -H "Authorization: Bearer $KEY" -d '{"model":"model-b","messages":[{"role":"user","content":"say ok"}]}'   # before update: correctly denied
-> HTTP 403

$ curl -s -X POST $BASE/team/update -H "Authorization: Bearer $MASTER" -H "Content-Type: application/json" -d "{\"team_id\":\"$TEAM_ID\",\"models\":[\"model-a\",\"model-b\"]}" | jq .data.models
-> ['model-a', 'model-b']

$ curl -s -w "\nHTTP %{http_code}\n" $BASE/v1/chat/completions -H "Authorization: Bearer $KEY" -d '{"model":"model-b","messages":[{"role":"user","content":"say ok"}]}'   # immediately after update
{"id":"oARhapHREZugjrEPwIDm0QU","created":1784743072,"model":"model-b","object":"chat.completion","choices":[{"finish_reason":"stop","index":0,"message":{"content":"OK","role":"assistant", ...}}],"usage":{"completion_tokens":93,"prompt_tokens":3,"total_tokens":96, ...}, ...}
HTTP 200

$ curl -s -X POST $BASE/team/update ... -d "{\"team_id\":\"$TEAM_ID\",\"models\":[\"model-a\"]}"   # revoke model-b again
-> ['model-a']

$ curl -s -o /dev/null -w "%{http_code}\n" $BASE/v1/chat/completions ... {"model":"model-b",...}   # immediately after revoke
-> HTTP 403

$ curl -s -o /dev/null -w "%{http_code}\n" $BASE/v1/chat/completions ... {"model":"model-a",...}   # model-a still allowed
-> HTTP 200

The revoke direction matters for security: before this fix a model removed from the team stayed callable until the poisoned cache finally decayed

Type

🐛 Bug Fix

Changes

litellm/proxy/auth/auth_checks.py: _cache_team_object now deletes the internal usage cache entry for team_id:{id} before writing the refreshed team to user_api_key_cache. The delete runs before the write because with a shared Redis a delete afterwards would wipe the value that was just written. This mirrors the alias key invalidation that already lived in the same function, and every team mutation endpoint benefits since they all refresh through _cache_team_object. Per Greptile review, both the internal cache delete and the alias-key invalidation are best-effort (769f434): on a cache backend error they log a warning and continue, so a Redis blip cannot turn a healthy team lookup into a 404 or a committed /team/update into a 500; this matches how DualCache.async_set_cache already swallows write errors on the same path

litellm/proxy/auth/user_api_key_auth.py: removed the post check 6 write-back that stored the fetched (or token-fallback) team object under team_id:{id} on every authenticated request. Cache-read values must never be written back into the cache; only authoritative writers (DB reads inside get_team_object and team mutations) populate it. This is the same rule the codebase already enforces for key objects (see test_auth_does_not_rewrite_cached_key_object_back_into_cache)

Tests: extended test_cache_team_object_writes_team_id_and_invalidates_team_alias to pin the new invalidation; added test_team_update_not_shadowed_by_internal_usage_cache_lit_4391, which rebuilds the shared-Redis topology with both caches over one fake Redis and fails on master; added test_auth_flow_never_persists_fallback_team_object_lit_4391, which drives the real auth builder and fails on master; added test_cache_team_object_tolerates_cache_invalidation_failures, which pins that cache backend errors do not fail the refresh and the authoritative write still runs; deleted test_auth_path_caches_team_object_under_canonical_team_id_key, which pinned the removed write-back (its underlying concern, caching under the canonical key, is handled inside get_team_object's DB path and covered by the _cache_team_object tests); gave test_update_team_org_scoped_tpm_rpm_bypasses_user_limit an awaitable mock for the new delete call. The LIT-4391 regression tests were verified to fail before the fix and pass after

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

get_team_object consults proxy_logging_obj.internal_usage_cache before
user_api_key_cache, but _cache_team_object (the refresh every team
mutation goes through) only wrote user_api_key_cache. With
enable_redis_auth_cache both caches share one Redis, so any request
backfills the internal cache's in-memory tier with the team object and
that copy keeps shadowing the freshly written team until its TTL expires.
The auth builder then wrote the team object it had just read back into
the cache after check 6, clobbering the fresh Redis value with the stale
one, which made the staleness self-sustaining under traffic: keys with
models=["all-team-models"] kept getting 403 team_model_access_denied
for models added via /team/update, and kept access to removed ones.

_cache_team_object now deletes the internal usage cache entry before
writing the refreshed team, and the auth-time write-back is removed so
only authoritative writers (DB reads and team mutations) populate the
team cache, mirroring how key objects already handle this (see
test_auth_does_not_rewrite_cached_key_object_back_into_cache).

The LIT-4000 test pinning the removed write-back is deleted; its
concern (team object cached under the canonical key) is handled by
_cache_team_object inside get_team_object's DB path and pinned by
test_cache_team_object_writes_team_id_and_invalidates_team_alias

Resolves LIT-4391
@mateo-berri
mateo-berri marked this pull request as ready for review July 22, 2026 17:59
@greptile-apps

greptile-apps Bot commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR refreshes team authorization data after team updates. The main changes are:

  • Invalidates the internal team cache before writing refreshed team data
  • Makes cache invalidation failures non-blocking
  • Removes authentication-time writes of fetched or fallback team objects
  • Adds tests for shared-cache refreshes and cache failure handling

Confidence Score: 5/5

This looks safe to merge.

  • Cache invalidation failures no longer stop the refreshed team write.
  • Request authentication no longer writes fallback team data into the canonical cache.
  • Tests cover invalidation order, shared-cache behavior, and failure handling.
  • No blocking issue remains in the changed code.

Important Files Changed

Filename Overview
litellm/proxy/auth/auth_checks.py Adds ordered, non-blocking invalidation of internal team cache entries before canonical cache refreshes.
litellm/proxy/auth/user_api_key_auth.py Removes request-time persistence of fetched or fallback team objects.
tests/test_litellm/proxy/auth/test_auth_checks.py Adds tests for shared-cache invalidation order and cache failure handling.
tests/test_litellm/proxy/auth/test_user_api_key_auth.py Adds coverage showing that fallback team data is not written into the team cache.
tests/test_litellm/proxy/management_endpoints/test_team_endpoints.py Updates the endpoint test fixture for the new asynchronous invalidation call.

Reviews (3): Last reviewed commit: "fix(proxy): suppress BLE001 for best-eff..." | Re-trigger Greptile

Comment thread litellm/proxy/auth/auth_checks.py Resolved
Comment thread litellm/proxy/auth/auth_checks.py Resolved
Greptile review: _cache_team_object runs after a successful DB fetch in
get_team_object and after every team mutation's DB write, but
DualCache.async_delete_cache propagates backend errors, so a Redis blip
during invalidation would turn a healthy team lookup into a 404 and a
committed /team/update into a 500. Both the internal usage cache delete
and the alias-key invalidation now log a warning and continue on failure,
matching how DualCache.async_set_cache already swallows write errors.
Worst case on failure is worker-local staleness bounded by the internal
cache's in-memory TTL, the same bound other workers already have
@mateo-berri

Copy link
Copy Markdown
Contributor Author

@greptileai pushed 769f434 making both cache invalidations in _cache_team_object best-effort, please re-review


Generated by Claude Code

The ruff strict gate flagged the two new blind excepts. They are
deliberate: the guards exist so that any cache backend failure, not just
an enumerable set of Redis errors, leaves the authoritative team write
and the mutation response intact
@codspeed

codspeed Bot commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_team-model-allowlist-stale-qqg50q (098cb5d) with litellm_internal_staging (0fcaadf)

Open in CodSpeed

@codecov

codecov Bot commented Jul 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@mateo-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@mateo-berri
mateo-berri merged commit c56e657 into litellm_internal_staging Jul 29, 2026
80 checks passed
@mateo-berri
mateo-berri deleted the litellm_team-model-allowlist-stale-qqg50q branch July 29, 2026 18:00
@Bytechoreographer

Bytechoreographer commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for this fix — we ran into exactly this on the v1.92.x line and it took a while to track down, so sharing the mechanism in case it helps with backport triage.

Symptom: a model added to a team through the UI never became callable — 403 team not allowed to access model. This team can only access models=[...], where the printed list was exactly the team's DB allowlist minus the newly added model, same order.

Root cause as far as we could tell: /team/update does write through (_refresh_cached_team → _cache_team_object), but with enable_redis_auth_cache and more than one replica that value gets overwritten again almost immediately — every authenticated request re-publishes the replica's own in-memory copy through the write-back in user_api_key_auth (the canonical-key change from #31418), and that call passes no ttl, so Redis gets a fresh 60s while the in-memory tier falls back to InMemoryCache's 600s default. The giveaway was a cached team object whose last_refreshed_at was days old while its TTL kept getting reset, i.e. under continuous traffic the entry can never expire and get_team_object never goes back to the DB. The key objects' denormalized team_models stayed fresh throughout, which is why only the team-level check failed.

Removing that write-back plus invalidating the internal-usage-cache copy — exactly what this PR does — matches what #33565 did for the key object, so this looks like it closes the team half of the same pattern.

Two questions, if you don't mind: is a backport to the v1.95.x line planned, or should we expect this in v1.96.0 only? (compare puts the merge commit in v1.96.0-dev.1/dev.2, but not in v1.94.0 or v1.95.0-rc.3.) And for anyone on an older tag, is there a recommended workaround short of cherry-picking this commit?

Thanks again for the quick turnaround here. cc @yassin-berriai

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants