This commit is contained in:
Shotsan 2026-10-24 08:30:19 +00:00 • committed by GitHub
commit 215e302c7f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 93 additions and 14 deletions

View file

@ -472,15 +472,23 @@ def redeem_passthrough_authorization_code(
return sealed
def _session_cookie_user_id(request: Request) -> str | None:
async def _session_cookie_user_id(request: Request) -> str | None:
"""The signed-in litellm user for a browser request, or ``None``. Thin wrapper so the
aggregate DCR flow's verbs receive the identity as a plain value instead of parsing
cookies themselves."""
cookies themselves. Checks that the embedded session key is still live in the database."""
from litellm.proxy._experimental.mcp_server.byok_oauth_endpoints import ( # noqa: PLC0415 # circular import at module load
_session_identity_from_cookie,
_session_key_is_live,
user_id_from_session_cookie,
)
return user_id_from_session_cookie(request)
user_id = user_id_from_session_cookie(request)
if not user_id:
return None
_, session_key = _session_identity_from_cookie(request)
if not await _session_key_is_live(session_key):
return None
return user_id
def _redirect_to_litellm_login(request: Request) -> RedirectResponse:
@ -996,17 +1004,13 @@ async def _resolve_oauth_authorization_user(
enforce_binding: bool,
) -> str | RedirectResponse:
"""Resolve the authorization subject without replacing denied credentials with cookie grants."""
from litellm.proxy._experimental.mcp_server.byok_oauth_endpoints import ( # noqa: PLC0415 # proxy import cycle
user_id_from_session_cookie,
)
use_gateway_credential: Final = enforce_binding and await oauth_authorization_uses_gateway_credential(request)
request_user_id: Final = (
await authorize_oauth_credential_request(request, mcp_server.server_id) if use_gateway_credential else None
)
if use_gateway_credential and request_user_id is None:
return _bridge_access_denied_redirect(redirect_uri, state, mcp_server)
user_id: Final = request_user_id or user_id_from_session_cookie(request)
user_id: Final = request_user_id or await _session_cookie_user_id(request)
if user_id is None:
return _redirect_to_litellm_login(request)
if not await _user_can_reach_mcp_server(user_id, mcp_server.server_id):
@ -1024,7 +1028,7 @@ async def _managed_authorization_scope(
request
)
session_user: Final = (
_session_cookie_user_id(request) if authorized_user_id is None and not gateway_credential else None
await _session_cookie_user_id(request) if authorized_user_id is None and not gateway_credential else None
)
scope_user_id: Final = authorized_user_id or (
await authorize_oauth_credential_request(request, server.server_id)
@ -2359,7 +2363,7 @@ async def authorize_mcp_session(
code_challenge=code_challenge,
code_challenge_method=code_challenge_method,
response_type=response_type,
session_user_id=_session_cookie_user_id(request),
session_user_id=await _session_cookie_user_id(request),
resource=resource,
scope=scope,
)
@ -2382,6 +2386,7 @@ async def authorize(
):
# Redirect to real OAuth provider with PKCE support
if mcp_server_name is None and client_id and is_gateway_dcr_client_id(client_id):
session_user_id: Final = await _session_cookie_user_id(request)
if is_proxy_api_resource(request, resource):
return await native_client_authorize(
request=request,
@ -2391,7 +2396,7 @@ async def authorize(
code_challenge=code_challenge,
code_challenge_method=code_challenge_method,
response_type=response_type,
session_user_id=_session_cookie_user_id(request),
session_user_id=session_user_id,
lookup_consent_teams=lookup_consent_teams,
)
return aggregate_authorize(
@ -2402,7 +2407,7 @@ async def authorize(
code_challenge=code_challenge,
code_challenge_method=code_challenge_method,
response_type=response_type,
session_user_id=_session_cookie_user_id(request),
session_user_id=session_user_id,
resource=resource,
scope=scope,
)
@ -2560,7 +2565,7 @@ async def authorize_flow(request: Request, flow: str) -> Response:
return await describe_connect_flow(
request=request,
flow_handle=flow,
session_user_id=_session_cookie_user_id(request),
session_user_id=await _session_cookie_user_id(request),
lookup_vendor_credential=_vendor_credential_state,
lookup_server_reachability=_user_can_reach_mcp_server,
)
@ -2586,7 +2591,7 @@ async def authorize_complete(
return await complete_connect_flow(
request=request,
flow_handle=flow,
session_user_id=_session_cookie_user_id(request),
session_user_id=await _session_cookie_user_id(request),
cache=user_api_key_cache,
delivery=delivery,
team_id=team_id,

View file

@ -6744,6 +6744,80 @@ async def test_interactive_bridge_authorize_without_session_redirects_to_login()
assert "/sso/key/generate" in response.headers["location"]
@pytest.mark.asyncio
async def test_interactive_bridge_authorize_with_revoked_session_redirects_to_login():
"""When a UI session cookie's embedded key is revoked (logged out or deleted from DB),
the gateway must reject the stale session and redirect to litellm login (#46917)."""
from litellm.proxy._experimental.mcp_server.discoverable_endpoints import authorize_with_server
from litellm.types.mcp import MCPAuth
server = _bridge_server(auth_type=MCPAuth.oauth_delegate, client_id="admin-client", registration_url=None)
with (
patch(
"litellm.proxy._experimental.mcp_server.byok_oauth_endpoints.user_id_from_session_cookie",
return_value="revoked-user",
),
patch(
"litellm.proxy._experimental.mcp_server.byok_oauth_endpoints._session_key_is_live",
new=AsyncMock(return_value=False),
),
):
response = await authorize_with_server(
request=_bridge_mock_request(),
mcp_server=server,
client_id="ignored",
redirect_uri="http://127.0.0.1:60108/callback",
state="s",
code_challenge="chal",
code_challenge_method="S256",
)
assert "/sso/key/generate" in response.headers["location"]
@pytest.mark.asyncio
async def test_gateway_dcr_authorize_with_revoked_session_redirects_to_login():
"""When a UI session cookie's embedded key is revoked in the DCR aggregate authorize flow,
the gateway must treat the user as unauthenticated and redirect to login (#46917)."""
from fastapi import Request
from litellm.proxy._experimental.mcp_server.discoverable_endpoints import authorize
mock_request = MagicMock(spec=Request)
mock_request.base_url = "https://litellm.example.com/"
mock_request.headers = {}
mock_request.cookies = {"token": "revoked_jwt"}
with (
patch(
"litellm.proxy._experimental.mcp_server.discoverable_endpoints.is_gateway_dcr_client_id",
return_value=True,
),
patch(
"litellm.proxy._experimental.mcp_server.byok_oauth_endpoints.user_id_from_session_cookie",
return_value="revoked-user",
),
patch(
"litellm.proxy._experimental.mcp_server.byok_oauth_endpoints._session_key_is_live",
new=AsyncMock(return_value=False),
),
patch(
"litellm.proxy._experimental.mcp_server.gateway_dcr_flow.open_gateway_dcr_client",
return_value=MagicMock(redirect_uris=["http://127.0.0.1:60108/callback"]),
),
):
response = await authorize(
request=mock_request,
client_id="dcr_client_id",
redirect_uri="http://127.0.0.1:60108/callback",
state="client-state-1",
code_challenge="a" * 43,
code_challenge_method="S256",
response_type="code",
)
assert response.status_code == 303
assert "/sso/key/generate" in response.headers["location"]
@pytest.mark.asyncio
async def test_interactive_bridge_callback_seals_user_into_gateway_code():
"""When the OAuth state carries the captured SSO user, the callback forwards a gateway