litellm/tests/test_litellm/proxy/auth
yuneng-jiang 697a90ea77
fix: end user logs (#27758) (#28290)
* fix: end user logs

* fix(auth): address PR review feedback on end-user id validation

- Gate DB validation behind litellm.validate_end_user_id_in_db (default
  False) so arbitrary client-supplied identifiers still pass through.
- Reuse get_end_user_object / get_user_object / _get_fuzzy_user_object
  instead of issuing raw Prisma queries in the auth hot path.
- Consolidate: builder does the resolution once and stores it on the
  auth obj; centralized checks reuse it, the outer user_api_key_auth
  copy is removed.
- Preserve end_user_id when litellm.max_end_user_budget_id is set so
  the default end-user budget can still apply to new customers.

* fix(auth): gate JSON-blob user-id rejection behind validate_end_user_id_in_db

Addresses PR review feedback: the JSON-encoded dict/list rejection in
_coerce_user_id_to_str was unconditionally applied, which would silently
stop tracking spend for deployments passing JSON-encoded user identifiers
on upgrade. Per the backwards-compatibility rule, default-path behavior
changes must be opt-in.

Now only strings that decode to a JSON object/array are dropped when
litellm.validate_end_user_id_in_db is True. Non-string dict/list/tuple
values are still always dropped, since stringifying them produces
unusable "{'device_id': ...}"-shaped spend-log rows.

* fix(auth): route email end-user lookup through get_user_object cache

The email-shaped end-user id branch called _get_fuzzy_user_object directly,
bypassing get_user_object's _should_check_db throttle and user_api_key_cache.
Every unique email would hit an unbudgeted raw Prisma query on the critical
auth path. Collapsing the two calls into one get_user_object invocation
with user_email=end_user_id routes through the cached helper per PR review
feedback.

* fix(auth): keep end-user safety net at user_api_key_auth tail

Krrish flagged that removing the tail-of-user_api_key_auth assignment
was a regression risk: ``_user_api_key_auth_builder`` has multiple
early-return paths (master_key=None, /user/auth, JWT short-circuits)
that bypass the end-user resolution block, so dropping the safety net
silently strips end-user attribution from those paths.

Restore the assignment but route it through resolve_and_validate_end_user_id
so the same validation rules apply. Skip the second pass when the builder
already set an id.

Adds two tests pinning the behaviour: one for the early-return safety
net and one verifying we don't double-resolve when the builder set the id.

Co-authored-by: Dennis Henry <dennis.henry@okta.com>
2026-05-20 23:37:19 -07:00
..
test_admin_viewer_handler_access.py fix(rbac): restore admin-viewer read parity for Logs page + settings reads 2026-04-29 19:21:41 -07:00
test_auth_checks.py fix: end user logs (#27758) (#28290) 2026-05-20 23:37:19 -07:00
test_auth_exception_handler.py Fix proxy auth status code tests (#27555) 2026-05-09 14:47:48 -07:00
test_auth_hot_path_network_requests.py
test_auth_utils.py fix: end user logs (#27758) (#28290) 2026-05-20 23:37:19 -07:00
test_banned_params_extra_body.py chore(tests): drop redundant membership check; trim test comment 2026-05-14 03:39:15 +00:00
test_cli_auth.py cover cli sso start validation 2026-04-29 17:25:32 -07:00
test_custom_auth_end_user_budget.py fix(auth): centralize common_checks to close authorization bypass 2026-04-23 00:04:42 +00:00
test_handle_jwt.py fix(auth): harden JWT routing wildcard iss and merge list team_id claims 2026-05-11 11:35:56 +05:30
test_info_routes.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_litellm_license.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_login_utils.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_mcp_ip_filtering.py
test_model_checks_fallbacks.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_model_checks.py fix(proxy): hydrate wildcard discovery credentials (#28284) (#28419) 2026-05-20 20:03:05 -07:00
test_multi_budget_windows.py
test_oauth2_proxy_hook.py chore(auth): require trusted proxy for header identity auth 2026-04-29 21:20:21 -07:00
test_object_permission_loading.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_onboarding.py chore(auth): address onboarding review follow-ups 2026-04-29 19:10:30 -07:00
test_organization_budget_enforcement.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_password_hashing.py
test_route_checks.py fix: harden /key/update authorization checks (#27878) 2026-05-14 04:16:04 +00:00
test_router_override_fallback_auth.py [Fix] Proxy: Repair Merge Fallout In Router-Override Fallback Auth 2026-05-01 17:48:51 -07:00
test_team_member_budget.py Fix team member budget enforcement without user row (#27273) 2026-05-06 11:42:29 -07:00
test_unmapped_model_budget_enforcement.py perf: eliminate per-request callback scanning on proxy hot path (#27858) 2026-05-14 09:28:31 -07:00
test_user_api_key_auth.py fix: end user logs (#27758) (#28290) 2026-05-20 23:37:19 -07:00