mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
fix(keys): authorize caller before project lookup on /key/update
This commit is contained in:
parent
6a2e7645aa
commit
728cd7f178
2 changed files with 96 additions and 6 deletions
|
|
@ -3750,12 +3750,6 @@ async def _validate_update_key_data(
|
|||
user_api_key_dict=user_api_key_dict,
|
||||
)
|
||||
|
||||
await _validate_project_assignment(
|
||||
data=data,
|
||||
existing_key_row=existing_key_row,
|
||||
prisma_client=checked_prisma_client,
|
||||
user_api_key_cache=user_api_key_cache,
|
||||
)
|
||||
is_project_change: Final = "project_id" in data.model_fields_set and data.project_id != existing_key_row.project_id
|
||||
|
||||
acting_as_team_admin: Final = await _acting_as_team_admin_for_key_update(
|
||||
|
|
@ -3887,6 +3881,12 @@ async def _validate_update_key_data(
|
|||
user_api_key_cache=user_api_key_cache,
|
||||
route=("/key/update (max_budget/spend)" if _is_budget_change else "/key/update"),
|
||||
)
|
||||
await _validate_project_assignment(
|
||||
data=data,
|
||||
existing_key_row=existing_key_row,
|
||||
prisma_client=checked_prisma_client,
|
||||
user_api_key_cache=user_api_key_cache,
|
||||
)
|
||||
check_denied_passthrough_routes_caller_permission(
|
||||
data,
|
||||
user_api_key_dict,
|
||||
|
|
|
|||
|
|
@ -21453,6 +21453,61 @@ async def test_project_assignment_write_matches_null_models_column():
|
|||
assert result["data"]["project_id"] == "project-orbit"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_project_assignment_write_rejects_when_max_budget_changed():
|
||||
stored: Final = {
|
||||
"token": _hash_token_if_needed("sk-assign"),
|
||||
"project_id": None,
|
||||
"team_id": "team-lit-5823",
|
||||
"models": ["model-orbit"],
|
||||
"max_budget": 50.0,
|
||||
"object_permission_id": None,
|
||||
}
|
||||
database: Final = _project_assignment_prisma(stored_row=stored)
|
||||
existing: Final = LiteLLM_VerificationToken(
|
||||
token=stored["token"], project_id=None, team_id="team-lit-5823",
|
||||
models=["model-orbit"], max_budget=100.0,
|
||||
)
|
||||
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
await _update_key_row_assigning_project(
|
||||
prisma_client=database,
|
||||
key="sk-assign",
|
||||
non_default_values={"project_id": "project-orbit"},
|
||||
existing_key_row=existing,
|
||||
)
|
||||
|
||||
assert exc.value.status_code == 400
|
||||
assert "concurrently" in str(exc.value.detail)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_project_assignment_write_rejects_when_object_permission_changed():
|
||||
stored: Final = {
|
||||
"token": _hash_token_if_needed("sk-assign"),
|
||||
"project_id": None,
|
||||
"team_id": "team-lit-5823",
|
||||
"models": ["model-orbit"],
|
||||
"max_budget": None,
|
||||
"object_permission_id": "perm-new",
|
||||
}
|
||||
database: Final = _project_assignment_prisma(stored_row=stored)
|
||||
existing: Final = LiteLLM_VerificationToken(
|
||||
token=stored["token"], project_id=None, team_id="team-lit-5823", models=["model-orbit"]
|
||||
)
|
||||
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
await _update_key_row_assigning_project(
|
||||
prisma_client=database,
|
||||
key="sk-assign",
|
||||
non_default_values={"project_id": "project-orbit"},
|
||||
existing_key_row=existing,
|
||||
)
|
||||
|
||||
assert exc.value.status_code == 400
|
||||
assert "concurrently" in str(exc.value.detail)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_key_creator_cannot_detach_project_without_admin_access():
|
||||
existing: Final = LiteLLM_VerificationToken(
|
||||
|
|
@ -21470,6 +21525,41 @@ async def test_key_creator_cannot_detach_project_without_admin_access():
|
|||
assert "Only proxy admins, team admins, or org admins" in str(exc.value.detail)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_unauthorized_caller_gets_403_not_project_lookup_result():
|
||||
existing: Final = LiteLLM_VerificationToken(
|
||||
token="project-assign-token", project_id=None, team_id="team-lit-5823",
|
||||
user_id="user-orbit", created_by="user-orbit",
|
||||
)
|
||||
database: Final = _project_assignment_prisma()
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
await _validate_update_key_data(
|
||||
UpdateKeyRequest(key=existing.token, project_id="project-missing"), existing,
|
||||
UserAPIKeyAuth(user_id="user-other", user_role=LitellmUserRoles.INTERNAL_USER),
|
||||
None, False, database, UserApiKeyCache(),
|
||||
)
|
||||
assert exc.value.status_code == 403
|
||||
assert "Project not found" not in str(exc.value.detail)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_unauthorized_caller_gets_403_not_project_team_leak():
|
||||
existing: Final = LiteLLM_VerificationToken(
|
||||
token="project-assign-token", project_id=None, team_id="team-lit-5823",
|
||||
user_id="user-orbit", created_by="user-orbit",
|
||||
)
|
||||
cache: Final = await _cache_with_project("project-orbit", [], team_id="team-other")
|
||||
database: Final = _project_assignment_prisma()
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
await _validate_update_key_data(
|
||||
UpdateKeyRequest(key=existing.token, project_id="project-orbit"), existing,
|
||||
UserAPIKeyAuth(user_id="user-other", user_role=LitellmUserRoles.INTERNAL_USER),
|
||||
None, False, database, cache,
|
||||
)
|
||||
assert exc.value.status_code == 403
|
||||
assert "belongs to team" not in str(exc.value.detail)
|
||||
|
||||
|
||||
_OWNED_PROJECT: Final = "proj-owned-1"
|
||||
_OWNERSHIP_PROJECT_TEAM: Final = "ownership-project-team"
|
||||
_OWNERSHIP_KEY_TEAM: Final = "ownership-key-team"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue