Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions litellm/proxy/management_endpoints/mcp_management_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,7 @@ async def add_session_mcp_server(

async def _get_cached_temporary_mcp_server_or_404(
server_id: str,
user_api_key_dict: UserAPIKeyAuth,
user_api_key_dict: Optional[UserAPIKeyAuth] = None,
request: Optional[Request] = None,
) -> MCPServer:
server = await get_cached_temporary_mcp_server(server_id)
Expand All @@ -1472,12 +1472,37 @@ async def _get_cached_temporary_mcp_server_or_404(
detail={"error": f"MCP server {server_id} not found"},
)

# Per-server access policy mirrors `fetch_mcp_server`: admin-view
# callers are unrestricted; non-admins must have the server in their
# allowed-servers set. Temporary cached servers come from the
# admin-only `/server/oauth/session` setup flow and are not exposed
# to non-admins.
if not _user_has_admin_view(user_api_key_dict):
# Access-control for the OAuth broker endpoints.
#
# Unauthenticated callers (browser-initiated OAuth, no API key):
# - Temp-cache servers: allowed. These are created by admins via the
# admin-only /server/oauth/session endpoint specifically to drive
# this browser flow. The LiteLLM UI always creates a temp session
# before calling /authorize, so all legitimate browser flows use a
# temp server_id.
# - Global-registry servers: rejected (403). Allowing unauthenticated
# access to global-registry servers would let any caller invoke the
# proxy's OAuth broker with the server's stored client_secret, while
# authenticated non-admins without allowlist access receive 403 —
# an unintended privilege inversion.
#
# Authenticated callers:
# - Admins: unrestricted.
# - Non-admins: temp servers are always denied (temp sessions are
# admin-internal); global servers require allowlist membership.
if user_api_key_dict is None:
if not resolved_from_temp_cache:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail={
"error": (
"Unauthenticated access to global-registry MCP server "
f"{server_id} is not permitted. "
"Pass a valid API key or use a session-scoped server ID."
)
},
)
elif not _user_has_admin_view(user_api_key_dict):
Comment thread
Sameerlite marked this conversation as resolved.
Resolved
if resolved_from_temp_cache:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
Expand All @@ -1498,12 +1523,10 @@ async def _get_cached_temporary_mcp_server_or_404(
@router.get(
"/server/oauth/{server_id}/authorize",
include_in_schema=False,
dependencies=[Depends(user_api_key_auth)],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like an insecure workaround, will message on Slack

)
async def mcp_authorize(
request: Request,
server_id: str,
user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),
client_id: Optional[str] = None,
redirect_uri: str = Query(...),
state: str = "",
Expand All @@ -1513,7 +1536,7 @@ async def mcp_authorize(
scope: Optional[str] = None,
):
mcp_server = await _get_cached_temporary_mcp_server_or_404(
server_id, user_api_key_dict, request=request
server_id, request=request
)
# Use the server's stored client_id when the caller doesn't supply one
resolved_client_id = mcp_server.client_id or client_id or ""
Expand Down Expand Up @@ -1543,12 +1566,10 @@ async def mcp_authorize(
@router.post(
"/server/oauth/{server_id}/token",
include_in_schema=False,
dependencies=[Depends(user_api_key_auth)],
)
async def mcp_token(
request: Request,
server_id: str,
user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),
grant_type: str = Form(...),
code: Optional[str] = Form(None),
redirect_uri: Optional[str] = Form(None),
Expand All @@ -1559,7 +1580,7 @@ async def mcp_token(
scope: Optional[str] = Form(None),
):
mcp_server = await _get_cached_temporary_mcp_server_or_404(
server_id, user_api_key_dict, request=request
server_id, request=request
)
resolved_client_id = mcp_server.client_id or client_id or ""
if not resolved_client_id:
Expand Down
Loading
Loading