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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
Fix deleted managed files returning 403 instead of 404
Ephrim Stanley4d87cb8fe306
diff --git a/enterprise/litellm_enterprise/proxy/hooks/managed_files.py b/enterprise/litellm_enterprise/proxy/hooks/managed_files.pyindex 32d7313aae4..9601370ef3e 100644--- a/enterprise/litellm_enterprise/proxy/hooks/managed_files.py+++ b/enterprise/litellm_enterprise/proxy/hooks/managed_files.py@@ -230,7 +230,7 @@ class _PROXY_LiteLLMManagedFiles(CustomLogger, BaseFileEndpoints):if managed_file:return managed_file.created_by == user_id- return False+ return True # allow through when record not found — downstream will return 404async def can_user_call_unified_object_id(self, unified_object_id: str, user_api_key_dict: UserAPIKeyAuthdiff --git a/tests/test_litellm/enterprise/proxy/test_deleted_file_returns_403_not_404.py b/tests/test_litellm/enterprise/proxy/test_deleted_file_returns_403_not_404.pynew file mode 100644index 00000000000..042796c9640--- /dev/null+++ b/tests/test_litellm/enterprise/proxy/test_deleted_file_returns_403_not_404.py@@ -0,0 +1,124 @@+"""+Regression test: deleted managed files should not return 403.++When a managed file's DB record has been deleted, can_user_call_unified_file_id()+returns False (record not found → treated as "access denied"). This causes+check_managed_file_id_access() to raise 403 instead of allowing the request+through so downstream code can return a proper 404.++The equivalent method for objects (can_user_call_unified_object_id) already+returns True when the record is missing.+"""++import base64++import pytest+from unittest.mock import AsyncMock, MagicMock++from fastapi import HTTPException++from litellm.proxy._types import UserAPIKeyAuth+++def _make_user_api_key_dict(user_id: str) -> UserAPIKeyAuth:+ return UserAPIKeyAuth(+ api_key="sk-test",+ user_id=user_id,+ parent_otel_span=None,+ )+++def _make_unified_file_id() -> str:+ raw = "litellm_proxy:application/octet-stream;unified_id,test-deleted-file;target_model_names,azure-gpt-4"+ return base64.b64encode(raw.encode()).decode()+++def _make_managed_files_with_no_db_record():+ """Create a _PROXY_LiteLLMManagedFiles where the DB returns None (file was deleted)."""+ from litellm_enterprise.proxy.hooks.managed_files import (+ _PROXY_LiteLLMManagedFiles,+ )++ mock_prisma = MagicMock()+ mock_prisma.db.litellm_managedfiletable.find_first = AsyncMock(return_value=None)++ return _PROXY_LiteLLMManagedFiles(+ internal_usage_cache=MagicMock(),+ prisma_client=mock_prisma,+ )+++async def test_should_not_raise_403_for_deleted_file():+ """+ When a managed file record has been deleted from the DB,+ check_managed_file_id_access should NOT raise 403.+ It should allow the request through so downstream can return 404.+ """+ unified_file_id = _make_unified_file_id()+ managed_files = _make_managed_files_with_no_db_record()+ user = _make_user_api_key_dict("any-user")+ data = {"file_id": unified_file_id}++ # This should NOT raise — deleted file should pass through access check+ result = await managed_files.check_managed_file_id_access(data, user)+ assert result is True+++async def test_should_allow_owner_access_when_record_exists():+ """Baseline: file owner can access their own file."""+ from litellm_enterprise.proxy.hooks.managed_files import (+ _PROXY_LiteLLMManagedFiles,+ )++ unified_file_id = _make_unified_file_id()++ mock_db_record = MagicMock()+ mock_db_record.created_by = "user-A"++ mock_prisma = MagicMock()+ mock_prisma.db.litellm_managedfiletable.find_first = AsyncMock(+ return_value=mock_db_record+ )++ managed_files = _PROXY_LiteLLMManagedFiles(+ internal_usage_cache=MagicMock(),+ prisma_client=mock_prisma,+ )++ user = _make_user_api_key_dict("user-A")+ data = {"file_id": unified_file_id}++ result = await managed_files.check_managed_file_id_access(data, user)+ assert result is True+++async def test_should_block_different_user_when_record_exists():+ """Baseline: different user cannot access another user's file."""+ from litellm_enterprise.proxy.hooks.managed_files import (+ _PROXY_LiteLLMManagedFiles,+ )++ unified_file_id = _make_unified_file_id()++ mock_db_record = MagicMock()+ mock_db_record.created_by = "user-A"++ mock_prisma = MagicMock()+ mock_prisma.db.litellm_managedfiletable.find_first = AsyncMock(+ return_value=mock_db_record+ )++ managed_files = _PROXY_LiteLLMManagedFiles(+ internal_usage_cache=MagicMock(),+ prisma_client=mock_prisma,+ )++ user = _make_user_api_key_dict("user-B")+ data = {"file_id": unified_file_id}++ with pytest.raises(HTTPException) as exc_info:+ await managed_files.check_managed_file_id_access(data, user)+ assert exc_info.value.status_code == 403