Repository navigation
fix(proxy): reject team-scoped object_permission on personal keys for non-admins - #31471
yuneng-berri merged 4 commits into
Conversation
Greptile SummaryThis PR hardens virtual-key management for teamless keys. The main changes are:
Confidence Score: 5/5The authorization changes are narrowly scoped to personal-key grant assignment and are covered across helper and endpoint paths. The changes add focused validation for the affected grant fields while preserving documented admin and team-scoped behavior, with regression tests covering generation, update, regenerate, and helper-level cases.
What T-Rex did
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
4fdd76e to
330ce52
Compare
330ce52 to
5ad78dc
Compare
|
@greptileai @Veria @cursor latest commit at |
a99f878 to
fddd032
Compare
|
@greptileai @Veria @cursor latest commit |
There was a problem hiding this comment.
Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.
|
@greptileai PR body updated with live-proxy traces for org_admin (bob: internal_user globally, ORG_ADMIN of acme-org). No code changes since HEAD |
There was a problem hiding this comment.
Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.
|
@greptileai PR body updated with org-admin live traces (Trace A1-A4 personal key denied, Trace B clean personal key allowed, Trace C team-key allowed). No code change in this update; please re-review against HEAD if needed |
There was a problem hiding this comment.
Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.
06c2769 to
c8e417a
Compare
PR overviewAll previously flagged issues have been addressed. No open security concerns remain on this pull request. Security reviewNo open security issues remain on this pull request. Fixed/addressed: 3 · PR risk: 0/10 |
Mirrors the ObjectPermissionDict pattern introduced in PR #31471. The new TypedDict names the keys the proxy actually reads off the permissions field (allow_pii_controls, get_spend_routes) so callers and readers share one source of truth. total=False keeps forward- compat keys passing without a schema bump. Drops the bare-dict annotation on _check_permissions_caller_permission flagged in mock review.
Audit of every annotation that holds the permissions payload:
Already typed (prior commit):
_check_permissions_caller_permission -> Optional[PermissionsDict]
Now typed:
generate_key_helper_fn(permissions=) Optional[PermissionsDict]
VerificationTokenRepository.{three helpers} Optional[PermissionsDict]
Intentionally left loose:
GenerateRequestBase.permissions (wire-Pydantic model field, kept
as Optional[dict] = {} to match the precedent #31471 set for
LiteLLM_ObjectPermissionBase vs ObjectPermissionDict).
LiteLLM_VerificationToken.permissions (DB roundtrip carries
arbitrary JSON; same reason).
Docstrings on /key/generate, /key/service-account/generate,
/key/update, /key/regenerate, and the /user/* endpoints are
free text and stay as-is.
|
@greptileai @cursor latest HEAD is
Please re-review against HEAD. e2e against a local proxy re-run is in progress and will be appended |
|
e2e re-run on HEAD |
5607340 to
515324d
Compare
Mirrors the ObjectPermissionDict pattern introduced in PR #31471. The new TypedDict names the keys the proxy actually reads off the permissions field (allow_pii_controls, get_spend_routes) so callers and readers share one source of truth. total=False keeps forward- compat keys passing without a schema bump. Drops the bare-dict annotation on _check_permissions_caller_permission flagged in mock review.
Audit of every annotation that holds the permissions payload:
Already typed (prior commit):
_check_permissions_caller_permission -> Optional[PermissionsDict]
Now typed:
generate_key_helper_fn(permissions=) Optional[PermissionsDict]
VerificationTokenRepository.{three helpers} Optional[PermissionsDict]
Intentionally left loose:
GenerateRequestBase.permissions (wire-Pydantic model field, kept
as Optional[dict] = {} to match the precedent #31471 set for
LiteLLM_ObjectPermissionBase vs ObjectPermissionDict).
LiteLLM_VerificationToken.permissions (DB roundtrip carries
arbitrary JSON; same reason).
Docstrings on /key/generate, /key/service-account/generate,
/key/update, /key/regenerate, and the /user/* endpoints are
free text and stay as-is.
515324d to
24a0db4
Compare
|
@greptileai @Veria rebased on latest |
|
bugbot run |
|
Live e2e on HEAD |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
There are 2 total unresolved issues (including 1 from previous review).
Autofix Details
Bugbot Autofix prepared a fix for the issue found in the latest run.
- ✅ Fixed: Regenerate ignores request team_id
- Regenerate now resolves the effective team from the request team_id before falling back to the existing key team_id, with regression coverage for object-permission validation.
You can send follow-ups to the cloud agent here.
|
@greptileai @Veria addressed Bugbot's latest finding (Medium: "Regenerate ignores request team_id") at HEAD with a two-line tweak: regenerate now reads |
2686f19 to
f2ee615
Compare
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
1 issue from previous review remains unresolved.
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit f2ee6154214c1b6c3718f0d24ee8872c77e6132a. Configure here.
|
@greptileai @Veria HEAD is now |
… non-admins Non-admin callers could create or update a personal key (no team_id) with arbitrary access_group_ids, mcp_toolsets, vector_stores, or search_tools in object_permission. The server persisted the values without ownership validation; runtime authorization then trusted the IDs because they were stored on the key, allowing cross-tenant access to other teams' restricted models, MCP toolsets, and vector stores. The personal-key gate now mirrors the team-key path. enforce_member_can_assign_access_groups raises 403 for non-admin teamless callers. validate_key_mcp_servers_against_team rejects non-empty mcp_toolsets on personal non-admin keys. A new validate_key_vector_stores_against_team enforces the same rule for vector_stores. validate_key_search_tools_against_team gains the same gate for search_tools. The four validators are wired into /key/generate, /key/update, and /key/regenerate. Proxy admins keep their existing carve-out across all fields; team keys are unaffected. Endpoint-level regression coverage lives in tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py (six new parametrised cases through generate_key_fn and _validate_update_key_data) and helper-level coverage in tests/test_litellm/proxy/management_helpers/. Deleting any of the validator calls in _common_key_generation_helper or unmoving the enforce gate in _validate_update_key_data breaks the suite.
Replace bare Optional[dict] on the object_permission validator surfaces with a typed TypedDict mirror of LiteLLM_ObjectPermissionBase. The TypedDict shape matches the Pydantic model field-for-field and supports .get() and item assignment, so the mutation in _rewrite_object_permission_mcp_identifiers continues to work at runtime (TypedDict is a plain dict). Propagated through the surfaces this PR touches: _object_permission_to_dict, _validate_mcp_servers_for_key_update, validate_key_mcp_servers_against_team, validate_key_search_tools_against_team, validate_key_vector_stores_against _team, the five _extract_requested_* helpers, and the two _rewrite_object_permission_mcp_* mutators. attach_object_permission_to_dict, handle_update_object_permission_common, and _set_object_permission keep their wider dict typing because they handle the full key/team data_json, which is a superset of ObjectPermissionDict and pre-dates this PR. No behavior change. 373 tests pass; ruff strict + type discipline gates green.
|
Reverted the regen one-line tweak at HEAD; Veria correctly pointed out my mirror of Back to: |
f2ee615 to
022fad1
Compare
|
The reasoning is sound. Looking at the current diff for if data is not None and (data.access_group_ids or data.object_permission is not None):
regenerate_team_table: Optional[LiteLLM_TeamTableCachedObj] = None
if _key_in_db.team_id is not None:
regenerate_team_table = await get_team_object(...)The revert is the right call. Using The Bugbot Medium ("Regenerate ignores request team_id") is an accurate description of the asymmetry, but the deferred handling is the correct tradeoff — closing that gap requires either adding a team-membership check to regenerate (scope of its own) or making Risk on the regenerate path as merged: 0/10. Agreed. |
|
@greptileai @Veria HEAD is now |
|
bugbot run |
|
@greptileai per request, please re-confirm against HEAD |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
There are 2 total unresolved issues (including 1 from previous review).
Bugbot Autofix prepared a fix for the issue found in the latest run.
- ✅ Fixed: Regenerate drops MCP normalization
- Regenerate now writes the normalized MCP object_permission back before key update preparation and includes a regression test.
You can send follow-ups to the cloud agent here.
Reviewed by Cursor Bugbot for commit 022fad1. Configure here.
| object_permission=_regen_object_permission_dict, | ||
| team_obj=regenerate_team_table, | ||
| is_proxy_admin=_regen_is_proxy_admin, | ||
| ) |
There was a problem hiding this comment.
Regenerate drops MCP normalization
Medium Severity
On /key/regenerate, validate_key_mcp_servers_against_team runs against a detached dict from _object_permission_to_dict, but its normalized return value is never applied to data.object_permission before prepare_key_update_data. Generate and update both persist the normalized payload; regenerate can store the pre-validation object_permission instead.
Reviewed by Cursor Bugbot for commit 022fad1. Configure here.
c319b52 to
0e82276
Compare
…reuse Defines the TypedDict mirror of LiteLLM_ObjectPermissionBase under litellm/types/object_permission.py so SDK-side modules can adopt the type without violating the SDK-must-not-import-from-proxy layering rule. litellm/proxy/_types.py re-exports it for existing callers. Supports the validator-surface retypes in the preceding proxy refactor commit.
0e82276 to
5b029ec
Compare
This comment was marked as duplicate.
This comment was marked as duplicate.
|
@greptileai Reverted the AgentResponse retype at HEAD 5b029ec. Empirically confirmed the reviewer's finding: AgentResponse.object_permission: Optional[ObjectPermissionDict] was stripping object_permission_id plus seven Prisma back-relation fields (teams, projects, verification_tokens, organizations, users, end_users, agents_table) from every agent CRUD response, because the validator-side TypedDict only mirrors the request-shape Pydantic, not the DB-row shape. AgentResponse.object_permission is back to Optional[Dict[str, Any]]. The TypedDict file (litellm/types/object_permission.py) and the proxy validator surfaces still use ObjectPermissionDict; only the agent-response field that touched the DB row shape was reverted. Commit message on the refactor(types) commit updated to reflect the narrowed scope. 415 tests pass; ruff format + strict + type-discipline gates green |
|
Good call on the revert. The boundary is now correctly drawn:
The scoped approach (TypedDict on the write/validation path, bare dict on the read/response path) avoids silent field-stripping while still getting type-safety where it matters most for this security fix. The 415-passing / lint-green state looks ready for maintainer review. |


Relevant issues
Linear ticket
Resolves LIT-4069
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer reviewDelays 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
Live proxy on
localhost:4000, Postgres backed, with a non-admin internal_user "alice".Before the fix, Alice could call
/key/generatewithout ateam_idand stuff arbitrary team-private resource IDs into her own key; the server accepted them and the runtime auth check at use-time then trusted the IDs because they were "on the key".Before the fix, all four returned 200 OK and persisted the IDs onto Alice's new personal key. After the fix, all four return 403:
Legitimate paths still work:
Type
🐛 Bug Fix
Changes
The security invariant enforced here is: a key with no team_id must not retain team-scoped grants.
This PR closes the create/update path: non-admin callers can no longer create or update a personal key with team-private grants in
access_group_ids,object_permission.mcp_toolsets,object_permission.vector_stores, orobject_permission.search_tools. The fix adds the personal-key gate that mirrors the team-key gate already in_team_key_generation_check, wires the four validators into_personal_key_generation_check,_validate_update_key_data, andregenerate_key_fn, and routes the validators through a typedObjectPermissionDictso the dict shape is no longer a baredict.A separate path of the same invariant; clearing
team_idfrom an existing team key while team-scoped grants remain on the row; is not closed in this PR. It is a follow-up; the work would either land its own helper or take the stricter alternative of makingteam_idadmin-only for all non-admin updates, which would be a broader product behavior change I did not want to bundle into this security fix.One asymmetry worth calling out.
mcp_serverson a personal non-admin key remains permitted when the server is markedallow_all_keys(existing carve-out);mcp_toolsets,vector_stores, andsearch_toolsare a blanket deny. Toolsets and vector stores have no equivalent per-row "globally allowed" flag, so there is no safe subset to allow through; search_tools follows the same pattern for consistency. Proxy admins may still assign any of these to a teamless key.Regression coverage lives at two levels. Endpoint-level cases in
tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.pydrivegenerate_key_fnand_validate_update_key_dataso that deleting any of the validator calls in_common_key_generation_helperor unmoving theenforce_member_can_assign_access_groupsmove breaks the suite (mutation-killed). Helper-level cases intests/test_litellm/proxy/management_helpers/test_object_permission_utils.pyandtest_team_member_permission_checks.pycover the non-admin-denied path, the admin-allowed path, and the empty-payload no-op for each field.Note
High Risk
Changes proxy key-management authorization for non-admin personal keys; incorrect gating could block legitimate self-serve keys or leave a path to attach team-private resource IDs.
Overview
Closes a privilege-escalation path where internal users could create or update teamless API keys with
access_group_idsandobject_permissionfields (mcp_toolsets,vector_stores,search_tools) that runtime auth trusts at use time.Authorization changes:
enforce_member_can_assign_access_groupsnow returns 403 when a non-admin assigns access groups with no team context (personal keys). New or tightened validators block the same callers from setting thoseobject_permissionlists on personal keys; proxy admins still may, and team-scoped keys keep existing team-subset rules (including MCP server normalization).Wiring: Validators run on key generate (via
_personal_key_generation_checkand_common_key_generation_helper), key update (_validate_mcp_servers_for_key_updatewith access-group checks moved outside the team-only branch), and key regenerate whenaccess_group_idsorobject_permissionis present.Typing: Introduces shared
ObjectPermissionDictinlitellm/types/object_permission.pyand uses it through permission helpers and agent response types instead of untyped dicts.Reviewed by Cursor Bugbot for commit 022fad1. Bugbot is set up for automated code reviews on this repo. Configure here.