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]

masking sensitive values

yuneng-jiang3e3b21e6c388
diff --git a/litellm/proxy/search_endpoints/search_tool_management.py b/litellm/proxy/search_endpoints/search_tool_management.py
index 5a353bd60df..4754316795e 100644
--- a/litellm/proxy/search_endpoints/search_tool_management.py
+++ b/litellm/proxy/search_endpoints/search_tool_management.py
@@ -143,11 +143,18 @@ async def list_search_tools():
]
for search_tool in search_tools_from_db:
+ litellm_params_dict = dict(search_tool.get("litellm_params", {}))
+ masked_litellm_params_dict = _get_masked_values(
+ litellm_params_dict,
+ unmasked_length=4,
+ number_of_asterisks=4,
+ )
+
search_tool_configs.append(
SearchToolInfoResponse(
search_tool_id=search_tool.get("search_tool_id"),
search_tool_name=search_tool.get("search_tool_name", ""),
- litellm_params=dict(search_tool.get("litellm_params", {})),
+ litellm_params=masked_litellm_params_dict,
search_tool_info=search_tool.get("search_tool_info"),
created_at=_convert_datetime_to_str(search_tool.get("created_at")),
updated_at=_convert_datetime_to_str(search_tool.get("updated_at")),
diff --git a/tests/test_litellm/proxy/management_endpoints/search_endpoints/test_search_tool_management.py b/tests/test_litellm/proxy/management_endpoints/search_endpoints/test_search_tool_management.py
index 4cb85601c9b..c7e3fba94ee 100644
--- a/tests/test_litellm/proxy/management_endpoints/search_endpoints/test_search_tool_management.py
+++ b/tests/test_litellm/proxy/management_endpoints/search_endpoints/test_search_tool_management.py
@@ -73,6 +73,10 @@ async def test_list_search_tools_db_only(monkeypatch):
# Verify datetime conversion to ISO string
assert tool["created_at"] == "2023-11-09T12:34:56"
assert tool["updated_at"] == "2023-11-09T13:45:12"
+ # Verify masking of sensitive values
+ assert tool["litellm_params"]["api_key"] != "sk-test"
+ assert "****" in tool["litellm_params"]["api_key"]
+ assert tool["litellm_params"]["search_provider"] == "perplexity"
finally:
app.dependency_overrides.pop(user_api_key_auth, None)
@@ -205,6 +209,10 @@ async def test_list_search_tools_filters_duplicate_config_tools(monkeypatch):
assert db_tool is not None
assert db_tool["is_from_config"] is False
assert db_tool["search_tool_id"] == "db-id-1"
+ # Verify masking of sensitive values in DB tool
+ assert db_tool["litellm_params"]["api_key"] != "sk-db"
+ assert "****" in db_tool["litellm_params"]["api_key"]
+ assert db_tool["litellm_params"]["search_provider"] == "perplexity"
# Verify unique config tool is present
config_tool = next(
@@ -302,6 +310,9 @@ async def test_list_search_tools_datetime_conversion(monkeypatch):
assert tool1["created_at"] == "2024-01-15T10:30:45.123456"
assert isinstance(tool1["updated_at"], str)
assert tool1["updated_at"] == "2024-01-16T14:20:30.789012"
+ # Verify masking of sensitive values
+ assert tool1["litellm_params"]["api_key"] != "sk-test"
+ assert "****" in tool1["litellm_params"]["api_key"]
# Test None handling for tool 2
tool2 = next(
@@ -311,6 +322,9 @@ async def test_list_search_tools_datetime_conversion(monkeypatch):
assert tool2 is not None
assert tool2["created_at"] is None
assert tool2["updated_at"] is None
+ # Verify masking of sensitive values
+ assert tool2["litellm_params"]["api_key"] != "tvly-test"
+ assert "****" in tool2["litellm_params"]["api_key"]
# Test string passthrough for tool 3
tool3 = next(
@@ -320,6 +334,9 @@ async def test_list_search_tools_datetime_conversion(monkeypatch):
assert tool3 is not None
assert tool3["created_at"] == "2024-01-17T08:15:00"
assert tool3["updated_at"] == "2024-01-18T09:25:00"
+ # Verify masking of sensitive values
+ assert tool3["litellm_params"]["api_key"] != "sk-test"
+ assert "****" in tool3["litellm_params"]["api_key"]
finally:
app.dependency_overrides.pop(user_api_key_auth, None)
@@ -369,6 +386,9 @@ async def test_list_search_tools_config_error_handling(monkeypatch):
# Should only have DB tools since config failed
assert len(data["search_tools"]) == 1
assert data["search_tools"][0]["search_tool_name"] == "db-tool-1"
+ # Verify masking of sensitive values
+ assert data["search_tools"][0]["litellm_params"]["api_key"] != "sk-test"
+ assert "****" in data["search_tools"][0]["litellm_params"]["api_key"]
finally:
app.dependency_overrides.pop(user_api_key_auth, None)
@@ -391,3 +411,136 @@ async def test_list_search_tools_no_prisma_client(monkeypatch):
assert "Prisma client not initialized" in data["detail"]
finally:
app.dependency_overrides.pop(user_api_key_auth, None)
+
+
+async def test_list_search_tools_db_masking_sensitive_values(monkeypatch):
+ """
+ Test that sensitive values in DB search tools are properly masked.
+ This tests the new masking logic added for database search tools.
+ """
+ # Mock DB tools with various sensitive fields
+ db_tools = [
+ {
+ "search_tool_id": "test-id-1",
+ "search_tool_name": "perplexity-tool",
+ "litellm_params": {
+ "search_provider": "perplexity",
+ "api_key": "pplx-sk-1234567890abcdef",
+ "api_base": "https://api.perplexity.ai",
+ },
+ "search_tool_info": {"description": "Perplexity tool"},
+ "created_at": datetime(2023, 11, 9, 12, 34, 56),
+ "updated_at": datetime(2023, 11, 9, 13, 45, 12),
+ },
+ {
+ "search_tool_id": "test-id-2",
+ "search_tool_name": "tavily-tool",
+ "litellm_params": {
+ "search_provider": "tavily",
+ "api_key": "tvly-secret-key-12345",
+ "api_base": "https://api.tavily.com",
+ },
+ "search_tool_info": {"description": "Tavily tool"},
+ "created_at": datetime(2023, 11, 9, 12, 34, 56),
+ "updated_at": datetime(2023, 11, 9, 13, 45, 12),
+ },
+ {
+ "search_tool_id": "test-id-3",
+ "search_tool_name": "tool-with-token",
+ "litellm_params": {
+ "search_provider": "custom",
+ "access_token": "token-abcdefghijklmnop",
+ "secret_key": "secret-xyz123",
+ },
+ "search_tool_info": {"description": "Tool with token"},
+ "created_at": datetime(2023, 11, 9, 12, 34, 56),
+ "updated_at": datetime(2023, 11, 9, 13, 45, 12),
+ },
+ {
+ "search_tool_id": "test-id-4",
+ "search_tool_name": "tool-with-non-sensitive",
+ "litellm_params": {
+ "search_provider": "custom",
+ "max_results": 10,
+ "timeout": 30,
+ },
+ "search_tool_info": {"description": "Tool without sensitive fields"},
+ "created_at": datetime(2023, 11, 9, 12, 34, 56),
+ "updated_at": datetime(2023, 11, 9, 13, 45, 12),
+ },
+ ]
+
+ # Mock SearchToolRegistry
+ mock_registry = MagicMock()
+ mock_registry.get_all_search_tools_from_db = AsyncMock(return_value=db_tools)
+ with patch(
+ "litellm.proxy.search_endpoints.search_tool_management.SEARCH_TOOL_REGISTRY",
+ mock_registry,
+ ):
+ # Mock prisma_client
+ mock_prisma = MagicMock()
+ with patch("litellm.proxy.proxy_server.prisma_client", mock_prisma):
+ # Mock proxy_config
+ mock_proxy_config = MagicMock()
+ mock_proxy_config.get_config = AsyncMock(return_value={})
+ mock_proxy_config.parse_search_tools = MagicMock(return_value=None)
+ with patch("litellm.proxy.proxy_server.proxy_config", mock_proxy_config):
+ # Mock auth
+ from litellm.proxy.auth.user_api_key_auth import user_api_key_auth
+
+ app.dependency_overrides[user_api_key_auth] = lambda: UserAPIKeyAuth(
+ user_role=LitellmUserRoles.PROXY_ADMIN, user_id="admin_user"
+ )
+
+ try:
+ test_client = TestClient(app)
+ response = test_client.get("/search_tools/list")
+ assert response.status_code == 200
+ data = response.json()
+ assert "search_tools" in data
+ assert len(data["search_tools"]) == 4
+
+ # Test tool 1: api_key should be masked
+ tool1 = next(
+ (t for t in data["search_tools"] if t["search_tool_name"] == "perplexity-tool"),
+ None,
+ )
+ assert tool1 is not None
+ assert tool1["litellm_params"]["api_key"] != "pplx-sk-1234567890abcdef"
+ assert "****" in tool1["litellm_params"]["api_key"]
+ assert tool1["litellm_params"]["search_provider"] == "perplexity"
+ assert tool1["litellm_params"]["api_base"] == "https://api.perplexity.ai"
+
+ # Test tool 2: api_key should be masked
+ tool2 = next(
+ (t for t in data["search_tools"] if t["search_tool_name"] == "tavily-tool"),
+ None,
+ )
+ assert tool2 is not None
+ assert tool2["litellm_params"]["api_key"] != "tvly-secret-key-12345"
+ assert "****" in tool2["litellm_params"]["api_key"]
+ assert tool2["litellm_params"]["search_provider"] == "tavily"
+
+ # Test tool 3: access_token and secret_key should be masked
+ tool3 = next(
+ (t for t in data["search_tools"] if t["search_tool_name"] == "tool-with-token"),
+ None,
+ )
+ assert tool3 is not None
+ assert tool3["litellm_params"]["access_token"] != "token-abcdefghijklmnop"
+ assert "****" in tool3["litellm_params"]["access_token"]
+ assert tool3["litellm_params"]["secret_key"] != "secret-xyz123"
+ assert "****" in tool3["litellm_params"]["secret_key"]
+
+ # Test tool 4: non-sensitive fields should remain unmasked
+ tool4 = next(
+ (t for t in data["search_tools"] if t["search_tool_name"] == "tool-with-non-sensitive"),
+ None,
+ )
+ assert tool4 is not None
+ assert tool4["litellm_params"]["max_results"] == 10
+ assert tool4["litellm_params"]["timeout"] == 30
+ assert tool4["litellm_params"]["search_provider"] == "custom"
+ finally:
+ app.dependency_overrides.pop(user_api_key_auth, None)