diff --git a/litellm/proxy/rag_endpoints/endpoints.py b/litellm/proxy/rag_endpoints/endpoints.py index 755fc5964f8..2d81dc32181 100644 --- a/litellm/proxy/rag_endpoints/endpoints.py +++ b/litellm/proxy/rag_endpoints/endpoints.py @@ -53,7 +53,8 @@ from litellm.proxy.rag_endpoints.upload_security import ( ) from litellm.proxy.vector_store_endpoints.endpoints import ( build_request_data_from_managed_vector_store, - reject_caller_store_selection_params, + reject_caller_embedding_selection_params, + reject_caller_opensearch_index_name, ) from litellm.proxy.vector_store_endpoints.utils import ( assert_user_can_access_vector_store_id, @@ -892,7 +893,7 @@ async def rag_query( status_code=400, detail={"error": "vector_store_id must be a non-empty string"}, ) - reject_caller_store_selection_params(payload=retrieval_config, source="retrieval_config") + reject_caller_embedding_selection_params(payload=retrieval_config, source="retrieval_config") resolved_stores: Final = await _authorize_nested_vector_store_ids( payload=retrieval_config, user_api_key_dict=user_api_key_dict, @@ -913,6 +914,11 @@ async def rag_query( **retrieval_config, **store_data, } + reject_caller_opensearch_index_name( + caller_body=retrieval_config, + request_fields=merged_retrieval_config, + source="retrieval_config", + ) effective_vector_store_config: Final = ( managed_store_params if managed_store is not None diff --git a/litellm/proxy/vector_store_endpoints/endpoints.py b/litellm/proxy/vector_store_endpoints/endpoints.py index 21053343f9e..90f1f00eed5 100644 --- a/litellm/proxy/vector_store_endpoints/endpoints.py +++ b/litellm/proxy/vector_store_endpoints/endpoints.py @@ -17,6 +17,7 @@ from litellm.proxy._types import CommonProxyErrors, UserAPIKeyAuth from litellm.proxy.auth.user_api_key_auth import user_api_key_auth from litellm.proxy.common_request_processing import ProxyBaseLLMRequestProcessing from litellm.proxy.common_utils.http_parsing_utils import read_raw_json_body +from litellm.proxy.route_llm_request import JSON_OBJECT_ADAPTER from litellm.proxy.utils import jsonify_object from litellm.proxy.vector_store_endpoints.utils import ( assert_proxy_admin_for_vector_store_index_management, @@ -24,31 +25,47 @@ from litellm.proxy.vector_store_endpoints.utils import ( get_litellm_managed_vector_store, ) from litellm.repositories.table_repositories import ManagedVectorStoreIndexRepository +from litellm.types.utils import LlmProviders from litellm.types.vector_stores import IndexCreateRequest, IndexListResponse from litellm.vector_stores.utils import VectorStoreWriteBody, callers_metadata_snapshot from litellm.vector_stores.vector_store_registry import VectorStoreIndexRegistry router: Final = APIRouter() -BLOCKED_CALLER_STORE_SELECTION_PARAMS: Final = frozenset( +BLOCKED_QUERY_EMBEDDING_SELECTION_PARAMS: Final = frozenset( { "embedding_model", "litellm_embedding_model", "litellm_embedding_config", "litellm_credential_name", - "opensearch_index_name", } ) -def reject_caller_store_selection_params(payload: Mapping[str, object], source: str) -> None: - blocked: Final = sorted(BLOCKED_CALLER_STORE_SELECTION_PARAMS & payload.keys()) +def reject_caller_embedding_selection_params(payload: Mapping[str, object], source: str) -> None: + blocked: Final = sorted(BLOCKED_QUERY_EMBEDDING_SELECTION_PARAMS & payload.keys()) if blocked: raise HTTPException( status_code=400, detail={ "error": f"'{blocked[0]}' cannot be set in {source}. " - "The embedding model, credential, and index come from the vector store's server-side registration." + "Embedding configuration comes from the vector store's server-side registration." + }, + ) + + +def reject_caller_opensearch_index_name( + caller_body: Mapping[str, object], request_fields: Mapping[str, object], source: str +) -> None: + if ( + request_fields.get("custom_llm_provider") == LlmProviders.OPENSEARCH.value + and "opensearch_index_name" in caller_body + ): + raise HTTPException( + status_code=400, + detail={ + "error": f"'opensearch_index_name' cannot be set in {source}. " + "The index comes from the vector store's server-side registration." }, ) @@ -137,13 +154,19 @@ async def vector_store_search( ) data = await read_request_body(request=request) # rebind-ok: pre-existing rebinding on a rename-only line - reject_caller_store_selection_params(payload=data, source="the search request body") + reject_caller_embedding_selection_params(payload=data, source="the search request body") data["vector_store_id"] = vector_store_id + caller_body: Final = JSON_OBJECT_ADAPTER.validate_python(data) # Check for legacy vector store registry (non-managed vector stores) data = await _update_request_data_with_litellm_managed_vector_store_registry( data=data, vector_store_id=vector_store_id, user_api_key_dict=user_api_key_dict ) + reject_caller_opensearch_index_name( + caller_body=caller_body, + request_fields=JSON_OBJECT_ADAPTER.validate_python(data), + source="the search request body", + ) # The managed_vector_stores pre-call hook will handle: # 1. Decoding managed vector store IDs diff --git a/tests/unit/proxy/rag_endpoints/test_rag_endpoints.py b/tests/unit/proxy/rag_endpoints/test_rag_endpoints.py index 2bf2d6bfb1e..1f838547065 100644 --- a/tests/unit/proxy/rag_endpoints/test_rag_endpoints.py +++ b/tests/unit/proxy/rag_endpoints/test_rag_endpoints.py @@ -1649,21 +1649,14 @@ def test_rag_query_forwards_managed_store_credentials_to_search(client_internal_ @pytest.mark.parametrize( "blocked_key", - [ - "embedding_model", - "litellm_embedding_model", - "litellm_embedding_config", - "litellm_credential_name", - "opensearch_index_name", - ], + ["embedding_model", "litellm_embedding_model", "litellm_embedding_config", "litellm_credential_name"], ) -def test_rag_query_rejects_caller_store_selection_params(client_internal_user, blocked_key): +def test_rag_query_rejects_caller_embedding_selection_params(client_internal_user, blocked_key): """ - Regression: a caller must not pick the embedding model, credential, or index - used at search time. The model and credential resolve through the Router with the - proxy's credentials, bypassing the key's model permissions, and the index can be one - no store grants the key, so they may only come from the managed store's - server-side registration. + Regression: a caller must not pick the embedding model or credential used at + search time. Those resolve through the Router with the proxy's credentials, + bypassing the key's model permissions, so they may only come from the + managed store's server-side registration. """ response = client_internal_user.post( "/v1/rag/query", @@ -1678,6 +1671,62 @@ def test_rag_query_rejects_caller_store_selection_params(client_internal_user, b assert blocked_key in str(response.json()) +@pytest.mark.parametrize( + ("provider", "expected_status", "expected_searches"), + [("opensearch", 400, 0), ("openai", 200, 1)], +) +def test_rag_query_rejects_caller_opensearch_index_name_only_on_opensearch_stores( + client_internal_user, provider, expected_status, expected_searches +): + """ + Regression: retrieval_config must not pick the index an OpenSearch store searches, + while other providers keep ignoring the key as they did before OpenSearch support. + """ + import litellm + from litellm import Router + from litellm.types.vector_stores import VectorStoreSearchResponse + + store = { + "vector_store_id": "kb", + "custom_llm_provider": provider, + "litellm_params": {"vector_store_id": "kb", "custom_llm_provider": provider}, + } + registry = MagicMock() + registry.get_litellm_managed_vector_store_from_registry.return_value = store + fake_search = AsyncMock( + return_value=VectorStoreSearchResponse(object="vector_store.search_results.page", search_query="q", data=[]) + ) + router = Router( + model_list=[ + { + "model_name": "gpt-4o-mini", + "litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-test", "mock_response": "hi"}, + } + ] + ) + + with ( + patch("litellm.vector_stores.asearch", new=fake_search), # test-quality-ok: the search boundary under test + patch.object(litellm, "vector_store_registry", registry), # test-quality-ok: seeds the store under test + patch("litellm.proxy.proxy_server.llm_router", router), # test-quality-ok: mock-response router for completion + patch( # test-quality-ok: store access is not under test, so the request reaches the search boundary + "litellm.proxy.vector_store_endpoints.utils.can_user_access_vector_store", + new=AsyncMock(return_value=True), + ), + ): + response = client_internal_user.post( + "/v1/rag/query", + json={ + "model": "gpt-4o-mini", + "messages": [{"role": "user", "content": "hello"}], + "retrieval_config": {"vector_store_id": "kb", "opensearch_index_name": "another-teams-index"}, + }, + ) + + assert response.status_code == expected_status, response.json() + assert fake_search.await_count == expected_searches + + EICAR = r"X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*" INGEST_REQUEST = '{"ingest_options":{"vector_store":{"custom_llm_provider":"openai"}}}' diff --git a/tests/unit/proxy/vector_store_endpoints/test_vector_store_endpoints.py b/tests/unit/proxy/vector_store_endpoints/test_vector_store_endpoints.py index af54af89a3e..3e20806bc1a 100644 --- a/tests/unit/proxy/vector_store_endpoints/test_vector_store_endpoints.py +++ b/tests/unit/proxy/vector_store_endpoints/test_vector_store_endpoints.py @@ -3036,21 +3036,14 @@ class TestAzureAIAnalyzeNamedIndexClassification: @pytest.mark.parametrize( "blocked_key", - [ - "embedding_model", - "litellm_embedding_model", - "litellm_embedding_config", - "litellm_credential_name", - "opensearch_index_name", - ], + ["embedding_model", "litellm_embedding_model", "litellm_embedding_config", "litellm_credential_name"], ) -def test_vector_store_search_rejects_caller_store_selection_params(blocked_key): +def test_vector_store_search_rejects_caller_embedding_selection_params(blocked_key): """ - Regression: the search request body must not pick the embedding model, - credential, or index used to search. The model and credential resolve through - the Router with the proxy's credentials, bypassing the key's model permissions, - and the index can be one no store grants the key, so they may only come from - the managed store's server-side registration. + Regression: the search request body must not pick the embedding model or + credential used to embed the query. Those resolve through the Router with + the proxy's credentials, bypassing the key's model permissions, so they may + only come from the managed store's server-side registration. """ from fastapi.testclient import TestClient @@ -3073,6 +3066,58 @@ def test_vector_store_search_rejects_caller_store_selection_params(blocked_key): assert blocked_key in str(response.json()) +@pytest.mark.parametrize( + ("provider", "expected_status", "expected_searches"), + [("opensearch", 400, 0), ("openai", 200, 1)], +) +def test_vector_store_search_rejects_caller_opensearch_index_name_only_on_opensearch_stores( + provider, expected_status, expected_searches +): + """ + Regression: a caller must not pick the index an OpenSearch store searches, while + other providers keep ignoring the key as they did before OpenSearch support. + """ + from fastapi.testclient import TestClient + + from litellm.proxy.auth.user_api_key_auth import user_api_key_auth + from litellm.proxy.proxy_server import app + from litellm.types.vector_stores import VectorStoreSearchResponse + + store = { + "vector_store_id": "kb", + "custom_llm_provider": provider, + "litellm_params": {"vector_store_id": "kb", "custom_llm_provider": provider}, + } + registry = MagicMock() + registry.get_litellm_managed_vector_store_from_registry.return_value = store + fake_search = AsyncMock( + return_value=VectorStoreSearchResponse(object="vector_store.search_results.page", search_query="q", data=[]) + ) + mock_auth = UserAPIKeyAuth(user_id="test_internal_user", user_role=LitellmUserRoles.INTERNAL_USER.value) + original_overrides = app.dependency_overrides.copy() + app.dependency_overrides[user_api_key_auth] = lambda: mock_auth + try: + with ( + patch("litellm.vector_stores.main.asearch", new=fake_search), # test-quality-ok: the search boundary under test + patch.object(litellm, "vector_store_registry", registry), # test-quality-ok: seeds the store under test + patch( # test-quality-ok: store access is not under test, so the request reaches the search boundary + "litellm.proxy.vector_store_endpoints.utils.can_user_access_vector_store", + new=AsyncMock(return_value=True), + ), + ): + router = litellm.Router(model_list=[]) + with patch("litellm.proxy.proxy_server.llm_router", router): # test-quality-ok: router built over the fake search + response = TestClient(app).post( + "/v1/vector_stores/kb/search", + json={"query": "q", "opensearch_index_name": "another-teams-index"}, + ) + finally: + app.dependency_overrides = original_overrides + + assert response.status_code == expected_status, response.json() + assert fake_search.await_count == expected_searches + + class TestConfigOwnedVectorStores: """Stores declared under ``vector_store_registry`` in config.yaml are owned by the config file"""