Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(files): enforce require_managed_files on file retrieve, content and delete

Devin AIc5c5a2767905
diff --git a/litellm/proxy/openai_files_endpoints/common_utils.py b/litellm/proxy/openai_files_endpoints/common_utils.py
index 87514b46dbd..3eef3868c94 100644
--- a/litellm/proxy/openai_files_endpoints/common_utils.py
+++ b/litellm/proxy/openai_files_endpoints/common_utils.py
@@ -866,6 +866,36 @@ def validate_managed_files_requirement(
)
+def validate_managed_file_id_requirement(file_id: str) -> None:
+ """
+ Enforce proxy-level managed files on the file read/delete routes when
+ ``litellm.require_managed_files`` is enabled.
+
+ Ownership is only recorded for LiteLLM managed files, so a raw provider file id sent to
+ retrieve/content/delete is forwarded to the provider under shared credentials without any
+ tenant check; knowing another tenant's provider file id would be enough to read or delete it.
+
+ Raises:
+ HTTPException: 400 if ``file_id`` is not a LiteLLM managed file id.
+ """
+ import litellm
+ from fastapi import HTTPException
+
+ if litellm.require_managed_files is not True:
+ return
+
+ if _is_base64_encoded_unified_file_id(file_id):
+ return
+
+ raise HTTPException(
+ status_code=400,
+ detail=(
+ "Raw provider file ids cannot be used when require_managed_files is enabled in "
+ "litellm_settings. Use the LiteLLM managed file id returned when the file was created."
+ ),
+ )
+
+
def _extract_model_param(request: "Request", request_body: dict) -> str | None:
"""
Extract model parameter from request.
diff --git a/litellm/proxy/openai_files_endpoints/files_endpoints.py b/litellm/proxy/openai_files_endpoints/files_endpoints.py
index 4e4718272bd..37f1ced6996 100644
--- a/litellm/proxy/openai_files_endpoints/files_endpoints.py
+++ b/litellm/proxy/openai_files_endpoints/files_endpoints.py
@@ -49,6 +49,7 @@ from litellm.proxy.openai_files_endpoints.common_utils import (
get_credentials_for_model,
handle_model_based_routing,
prepare_data_with_credentials,
+ validate_managed_file_id_requirement,
validate_managed_files_requirement,
)
from litellm.proxy.utils import ProxyLogging, is_known_model
@@ -612,6 +613,8 @@ async def get_file_content(
data: dict = {"file_id": file_id}
try:
+ validate_managed_file_id_requirement(file_id=file_id)
+
# Include original request and headers in the data
base_llm_response_processor = ProxyBaseLLMRequestProcessing(data=data)
(
@@ -908,6 +911,8 @@ async def get_file(
data: dict = {"file_id": file_id}
try:
+ validate_managed_file_id_requirement(file_id=file_id)
+
custom_llm_provider = (
provider
or get_custom_llm_provider_from_request_headers(request=request)
@@ -1098,6 +1103,8 @@ async def delete_file(
data: dict = {"file_id": file_id}
try:
+ validate_managed_file_id_requirement(file_id=file_id)
+
custom_llm_provider = (
provider
or get_custom_llm_provider_from_request_headers(request=request)
diff --git a/tests/test_litellm/proxy/openai_files_endpoint/test_files_endpoint.py b/tests/test_litellm/proxy/openai_files_endpoint/test_files_endpoint.py
index ac01c6ae1d1..24b814bae1f 100644
--- a/tests/test_litellm/proxy/openai_files_endpoint/test_files_endpoint.py
+++ b/tests/test_litellm/proxy/openai_files_endpoint/test_files_endpoint.py
@@ -3051,3 +3051,119 @@ def test_list_files_key_allowed_openai_model_still_resolves_team_credentials(
mocker, monkeypatch, _team_openai_plus_global_anthropic_router(), ["team-gpt"]
)
assert captured_kwargs.get("api_key") == "team-openai-key"
+
+
+ "http_method, url, patched_litellm_call",
+ [
+ ("get", "/v1/files/file-victim-abc123", "litellm.afile_retrieve"),
+ ("get", "/v1/files/file-victim-abc123/content", "litellm.afile_content"),
+ ("delete", "/v1/files/file-victim-abc123", "litellm.afile_delete"),
+ ],
+)
+def test_require_managed_files_rejects_raw_provider_file_id(
+ mocker: MockerFixture,
+ monkeypatch,
+ llm_router: Router,
+ http_method: str,
+ url: str,
+ patched_litellm_call: str,
+):
+ import litellm.proxy.proxy_server as ps
+ from litellm.proxy._types import LitellmUserRoles
+
+ monkeypatch.setattr("litellm.require_managed_files", True)
+ monkeypatch.setattr("litellm.proxy.proxy_server.master_key", None)
+ monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", None)
+ monkeypatch.setattr("litellm.proxy.proxy_server.llm_router", llm_router)
+ setup_proxy_logging_object(monkeypatch, llm_router)
+
+ mock_call = mocker.patch(patched_litellm_call, new=mocker.AsyncMock())
+
+ app.dependency_overrides[ps.user_api_key_auth] = lambda: UserAPIKeyAuth(
+ user_role=LitellmUserRoles.INTERNAL_USER, user_id="attacker-user"
+ )
+
+ try:
+ response = getattr(client, http_method)(
+ url, headers={"Authorization": "Bearer test-key"}
+ )
+ finally:
+ app.dependency_overrides.pop(ps.user_api_key_auth, None)
+ monkeypatch.setattr("litellm.require_managed_files", False)
+
+ assert response.status_code == 400, response.text
+ mock_call.assert_not_called()
+
+
+def _unified_managed_file_id() -> str:
+ import base64
+
+ from litellm.types.utils import SpecialEnums
+
+ unified_id = SpecialEnums.LITELLM_MANAGED_FILE_COMPLETE_STR.value.format(
+ "application/json", "victim-unified-id", "gpt-3.5-turbo", "file-victim-abc123", "gpt-3.5-turbo-id"
+ )
+ return base64.urlsafe_b64encode(unified_id.encode()).decode().rstrip("=")
+
+
+def test_require_managed_files_allows_unified_managed_file_id(monkeypatch):
+ from litellm.proxy.openai_files_endpoints.common_utils import (
+ validate_managed_file_id_requirement,
+ )
+
+ monkeypatch.setattr("litellm.require_managed_files", True)
+
+ validate_managed_file_id_requirement(file_id=_unified_managed_file_id())
+
+
+def test_managed_file_id_requirement_is_opt_in(monkeypatch):
+ from litellm.proxy.openai_files_endpoints.common_utils import (
+ validate_managed_file_id_requirement,
+ )
+
+ monkeypatch.setattr("litellm.require_managed_files", False)
+
+ validate_managed_file_id_requirement(file_id="file-victim-abc123")
+
+
+def test_raw_provider_file_id_retrieve_allowed_when_managed_files_not_required(
+ mocker: MockerFixture, monkeypatch, llm_router: Router
+):
+ import litellm.proxy.proxy_server as ps
+ from litellm.proxy._types import LitellmUserRoles
+
+ monkeypatch.setattr("litellm.require_managed_files", False)
+ monkeypatch.setattr("litellm.proxy.proxy_server.master_key", None)
+ monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", None)
+ monkeypatch.setattr("litellm.proxy.proxy_server.llm_router", llm_router)
+ setup_proxy_logging_object(monkeypatch, llm_router)
+
+ mock_retrieve = mocker.patch(
+ "litellm.afile_retrieve",
+ new=mocker.AsyncMock(
+ return_value=OpenAIFileObject(
+ id="file-victim-abc123",
+ object="file",
+ bytes=3,
+ created_at=1234567890,
+ filename="test.txt",
+ purpose="user_data",
+ status="uploaded",
+ )
+ ),
+ )
+
+ app.dependency_overrides[ps.user_api_key_auth] = lambda: UserAPIKeyAuth(
+ user_role=LitellmUserRoles.INTERNAL_USER, user_id="some-user"
+ )
+
+ try:
+ response = client.get(
+ "/v1/files/file-victim-abc123", headers={"Authorization": "Bearer test-key"}
+ )
+ finally:
+ app.dependency_overrides.pop(ps.user_api_key_auth, None)
+
+ assert response.status_code == 200, response.text
+ mock_retrieve.assert_called_once()