refactor(redis): own MCP single-use marker identities

This commit is contained in:
Devin AI 2026-10-30 17:55:26 +00:00
parent 8603e873f1
commit 0836f36cff
5 changed files with 309 additions and 85 deletions

View file

@ -98,7 +98,16 @@ from litellm.proxy.mcp_server.outbound_credentials.session_token import (
open_session_refresh_token,
open_session_token,
)
from litellm.redis.stores.mcp_single_use import TransportMcpSingleUseStore
from litellm.redis.stores.mcp_single_use import (
CodeMarker,
FlowMarker,
IdJagMarker,
McpSingleUseMarker,
RefreshMarker,
RevokedRefreshFamilyMarker,
TransportMcpSingleUseStore,
marker_key,
)
from litellm.redis.transport.contract import RedisFailure
from litellm.types.llms.base import LiteLLMBaseModel
from litellm.types.mcp_server.mcp_server_manager import MCPServer
@ -132,11 +141,7 @@ the 10-minute ceiling RFC 6749 section 4.1.2 recommends. Single-use and PKCE bin
unchanged, so the longer window only extends how long the legitimate holder has to paste
it, not what an observer could do with it."""
_CLAIM_TTL_BUFFER_SECONDS: Final = 60
_USED_CODE_CACHE_PREFIX: Final = "mcp_gateway_dcr_code_used:"
_USED_FLOW_CACHE_PREFIX: Final = "mcp_gateway_dcr_flow_used:"
_USED_REFRESH_CACHE_PREFIX: Final = "mcp_gateway_dcr_refresh_used:"
_REFRESH_REPLAYED_DESCRIPTION: Final = "the refresh token was already used"
_REVOKED_REFRESH_FAMILY_CACHE_PREFIX: Final = "mcp_gateway_dcr_refresh_family_revoked:"
_REFRESH_FAMILY_REVOKED_DESCRIPTION: Final = "the refresh token was revoked"
MAX_REDIRECT_URIS: Final = 4
@ -262,7 +267,7 @@ class AssertionIdentity(SubjectIdentity):
marker must outlive it, the ``scope`` the credential is issued for, and the route groups that
scope narrows the credential to (``None`` leaves it the full ``lite login`` credential)."""
replay_key: str = Field(min_length=1)
replay_marker: IdJagMarker
replay_ttl_seconds: int = Field(gt=0)
scope: str | None = None
allowed_routes: tuple[str, ...] | None = None
@ -472,14 +477,6 @@ def resolve_session_signing(master_key: str | None, caller: str) -> SessionSigni
return SessionSigning(keys=keys, now=datetime.now(timezone.utc))
def _refresh_claim_key(jti: str) -> str:
return f"{_USED_REFRESH_CACHE_PREFIX}{jti}"
def _refresh_family_key(family: str) -> str:
return f"{_REVOKED_REFRESH_FAMILY_CACHE_PREFIX}{family}"
_REFRESH_CLAIM_TTL_SECONDS: Final = SESSION_REFRESH_TTL_SECONDS + _CLAIM_TTL_BUFFER_SECONDS
@ -1231,7 +1228,7 @@ async def complete_connect_flow(
return _oauth_error(400, "invalid_request", "authorize the requested MCP server before finishing")
flow_refusal: Final = _claim_refusal(
await _SingleUseGuard(cache).claim(
f"{_USED_FLOW_CACHE_PREFIX}{opened.jti}", CONNECT_FLOW_TTL_SECONDS + _CLAIM_TTL_BUFFER_SECONDS
FlowMarker(opened.jti), CONNECT_FLOW_TTL_SECONDS + _CLAIM_TTL_BUFFER_SECONDS
),
replayed=_oauth_error(
400, "invalid_request", "this connect flow was already completed; restart the connection"
@ -1363,8 +1360,8 @@ class _SingleUseGuard:
self._cache = cache
@with_service_target(_DCR_CLAIMS_TARGET)
async def claim(self, key: str, ttl_seconds: int) -> ClaimOutcome:
"""Atomically claim ``key``. ``"first"`` iff this caller is the first (increment to 1),
async def claim(self, marker: McpSingleUseMarker, ttl_seconds: int) -> ClaimOutcome:
"""Atomically claim ``marker``. ``"first"`` iff this caller is the first (increment to 1),
``"replayed"`` on a replay (>1), and ``"unavailable"`` when the claim could not be recorded in
the shared backend, which every caller treats as a refusal (fail closed)."""
from litellm.proxy.proxy_server import redis_usage_cache # noqa: PLC0415 # circular import at module load
@ -1377,7 +1374,7 @@ class _SingleUseGuard:
redis_cache: Final = redis_usage_cache or getattr(self._cache, "redis_cache", None)
if redis_cache is not None:
match await TransportMcpSingleUseStore(redis_cache.transport()).claim(
key, ttl=timedelta(seconds=ttl_seconds)
marker, ttl=timedelta(seconds=ttl_seconds)
):
case RedisFailure():
return "unavailable"
@ -1386,11 +1383,11 @@ class _SingleUseGuard:
case _:
return "replayed"
# No shared backend configured (single-replica): the in-memory increment is authoritative.
count: Final = await self._cache.async_increment_cache(key, 1, ttl=ttl_seconds, local_only=True)
count: Final = await self._cache.async_increment_cache(marker_key(marker), 1, ttl=ttl_seconds, local_only=True)
return "first" if count == 1 else "replayed"
@with_service_target(_DCR_CLAIMS_TARGET)
async def peek(self, key: str) -> Literal["unclaimed", "claimed", "unavailable"]:
async def peek(self, marker: McpSingleUseMarker) -> Literal["unclaimed", "claimed", "unavailable"]:
"""Read-only view of a single-use marker, resolved against the same shared authority as
:meth:`claim` so introspection observes exactly the record redemption and revocation wrote.
A backend fault is ``"unavailable"`` (fail closed) rather than a guess either way."""
@ -1398,14 +1395,14 @@ class _SingleUseGuard:
redis_cache: Final = redis_usage_cache or getattr(self._cache, "redis_cache", None)
if redis_cache is not None:
match await TransportMcpSingleUseStore(redis_cache.transport()).peek(key):
match await TransportMcpSingleUseStore(redis_cache.transport()).peek(marker):
case RedisFailure():
return "unavailable"
case True:
return "claimed"
case False:
return "unclaimed"
local: Final = await self._cache.async_get_cache(key, local_only=True)
local: Final = await self._cache.async_get_cache(marker_key(marker), local_only=True)
return "unclaimed" if local is None else "claimed"
@ -1414,7 +1411,7 @@ async def _family_refusal(guard: _SingleUseGuard, family: str | None) -> Respons
minted; a backend that cannot say answers 503."""
if family is None:
return None
peeked: Final = await guard.peek(_refresh_family_key(family))
peeked: Final = await guard.peek(RevokedRefreshFamilyMarker(family))
match peeked:
case "unclaimed":
return None
@ -1663,7 +1660,7 @@ class _ProxyCredentialIssuer:
async def __call__(
self,
principal: SessionPrincipal,
claim_key: str,
claim_marker: McpSingleUseMarker,
claim_ttl_seconds: int,
replayed: str,
family: str | None = None,
@ -1675,7 +1672,7 @@ class _ProxyCredentialIssuer:
if not isinstance(minted, MintedProxyCredential):
return _mint_failure_response(minted)
refusal: Final = _claim_refusal(
await self._guard.claim(claim_key, claim_ttl_seconds),
await self._guard.claim(claim_marker, claim_ttl_seconds),
replayed=_oauth_error(400, "invalid_grant", replayed),
)
if refusal is not None:
@ -1709,21 +1706,26 @@ class _GrantIssuer:
async def __call__(
self,
principal: SessionPrincipal,
claim_key: str,
claim_marker: McpSingleUseMarker,
claim_ttl_seconds: int,
replayed: str,
family: str | None = None,
) -> Response:
match principal.audience:
case None:
return await self._issue_session_pair(principal, claim_key, claim_ttl_seconds, replayed, family)
return await self._issue_session_pair(principal, claim_marker, claim_ttl_seconds, replayed, family)
case "proxy_api":
return await self._issue_proxy_credential(principal, claim_key, claim_ttl_seconds, replayed, family)
return await self._issue_proxy_credential(principal, claim_marker, claim_ttl_seconds, replayed, family)
case _:
assert_never(principal.audience)
async def _issue_session_pair(
self, principal: SessionPrincipal, claim_key: str, claim_ttl_seconds: int, replayed: str, family: str | None
self,
principal: SessionPrincipal,
claim_marker: McpSingleUseMarker,
claim_ttl_seconds: int,
replayed: str,
family: str | None,
) -> Response:
ended: Final = await _family_refusal(self._guard, family)
if ended is not None:
@ -1732,7 +1734,7 @@ class _GrantIssuer:
if failure is not None:
return _reload_failure_response(failure)
refusal: Final = _claim_refusal(
await self._guard.claim(claim_key, claim_ttl_seconds),
await self._guard.claim(claim_marker, claim_ttl_seconds),
replayed=_oauth_error(400, "invalid_grant", replayed),
)
if refusal is not None:
@ -1740,12 +1742,17 @@ class _GrantIssuer:
return _session_token_pair(principal, self._signing.keys, self._signing.now, family)
async def _issue_proxy_credential(
self, principal: SessionPrincipal, claim_key: str, claim_ttl_seconds: int, replayed: str, family: str | None
self,
principal: SessionPrincipal,
claim_marker: McpSingleUseMarker,
claim_ttl_seconds: int,
replayed: str,
family: str | None,
) -> Response:
target_refusal: Final = self._proxy_api_target_refusal()
if target_refusal is not None:
return target_refusal
return await self._proxy_credential(principal, claim_key, claim_ttl_seconds, replayed, family)
return await self._proxy_credential(principal, claim_marker, claim_ttl_seconds, replayed, family)
async def exchange(
self, subject_token: str, client_id: str, exchange_subject_token: ExchangeSubjectToken
@ -1784,7 +1791,7 @@ class _GrantIssuer:
if isinstance(identity, SubjectTokenRefusal):
return _subject_token_refusal_response(identity)
refused: Final = _claim_refusal(
await self._guard.claim(identity.replay_key, identity.replay_ttl_seconds),
await self._guard.claim(identity.replay_marker, identity.replay_ttl_seconds),
_oauth_error(400, "invalid_grant", "the assertion has already been redeemed"),
)
if refused is not None:
@ -1850,7 +1857,7 @@ async def _authorization_code_grant(
audience=parsed.audience,
team_id=parsed.team_id,
),
claim_key=f"{_USED_CODE_CACHE_PREFIX}{parsed.jti}",
claim_marker=CodeMarker(parsed.jti),
claim_ttl_seconds=parsed.exp - int(now.timestamp()) + _CLAIM_TTL_BUFFER_SECONDS,
replayed="the authorization code was already used",
)
@ -1888,7 +1895,7 @@ async def _refresh_token_grant(
# credential file, and the chain it recovers into has to be alive. Only a revocation ends it.
return await issue(
opened.principal,
claim_key=_refresh_claim_key(opened.jti),
claim_marker=RefreshMarker(opened.jti),
claim_ttl_seconds=_REFRESH_CLAIM_TTL_SECONDS,
replayed=_REFRESH_REPLAYED_DESCRIPTION,
family=opened.family,
@ -1919,7 +1926,7 @@ async def refresh_proxy_credential(
issue: Final = _ProxyCredentialIssuer(signing, mint_proxy_credential, _SingleUseGuard(cache))
return await issue(
opened.principal,
claim_key=_refresh_claim_key(opened.jti),
claim_marker=RefreshMarker(opened.jti),
claim_ttl_seconds=_REFRESH_CLAIM_TTL_SECONDS,
replayed=_REFRESH_REPLAYED_DESCRIPTION,
family=opened.family,
@ -1996,8 +2003,8 @@ async def revoke_session_refresh_token(
opened: Final = open_session_refresh_bearer(token, signing.keys, signing.now, expected_client_id=client_id)
if isinstance(opened, SessionRefreshOpened):
guard: Final = _SingleUseGuard(cache)
ended: Final = await guard.claim(_refresh_family_key(opened.family), _REFRESH_CLAIM_TTL_SECONDS)
burned: Final = await guard.claim(_refresh_claim_key(opened.jti), _REFRESH_CLAIM_TTL_SECONDS)
ended: Final = await guard.claim(RevokedRefreshFamilyMarker(opened.family), _REFRESH_CLAIM_TTL_SECONDS)
burned: Final = await guard.claim(RefreshMarker(opened.jti), _REFRESH_CLAIM_TTL_SECONDS)
if "unavailable" in (burned, ended):
return _oauth_error(503, "temporarily_unavailable", _CLAIM_UNAVAILABLE_DESCRIPTION)
return Response(content="{}", media_type="application/json", headers=TOKEN_NO_CACHE_HEADERS)
@ -2082,8 +2089,8 @@ async def introspect_gateway_token(
return _inactive_introspection_response()
if opened.kind == "session_refresh":
guard: Final = _SingleUseGuard(cache)
used: Final = await guard.peek(_refresh_claim_key(opened.jti))
ended: Final = await guard.peek(_refresh_family_key(opened.family))
used: Final = await guard.peek(RefreshMarker(opened.jti))
ended: Final = await guard.peek(RevokedRefreshFamilyMarker(opened.family))
if "unavailable" in (used, ended):
return _oauth_error(503, "temporarily_unavailable", _CLAIM_UNAVAILABLE_DESCRIPTION)
if "claimed" in (used, ended):

View file

@ -10,7 +10,6 @@ from __future__ import annotations
import base64
import binascii
import hashlib
import hmac
import math
from collections.abc import Awaitable, Iterator, Mapping, Sequence
@ -41,12 +40,12 @@ from litellm.proxy.mcp_server.idp_token_exchange import (
read_token_exchange_prerequisites,
)
from litellm.proxy.mcp_server.oauth_utils import proxy_base_url_from_env
from litellm.redis.stores.mcp_single_use import IdJagMarker
ID_JAG_REQUIRED_CLAIMS: Final = ("iss", "sub", "aud", "exp", "iat", "jti", "client_id")
REJECTED_ASSERTION: Final = "assertion was rejected"
TEAM_HEADER: Final = "x-litellm-team-id"
_INACTIVE_SUBJECT: Final = frozenset(("no_active_key", "unresolvable"))
_REPLAY_KEY_PREFIX: Final = "mcp_gateway_dcr:id_jag:"
SCOPE_MCP: Final = "litellm:mcp"
SCOPE_LLM: Final = "litellm:llm"
_RESOURCE_CLAIM_ADAPTER: Final = TypeAdapter(list[str])
@ -479,7 +478,7 @@ async def identity_from_id_jag(
marker: Final = _replay_marker(verified, redemption)
if isinstance(marker, SubjectTokenRefusal):
return marker
replay_key, replay_ttl_seconds = marker
replay_marker, replay_ttl_seconds = marker
mcp_resource: Final = resource_names_mcp(verified, prerequisites.base_url)
if isinstance(mcp_resource, SubjectTokenRefusal):
return mcp_resource
@ -502,7 +501,7 @@ async def identity_from_id_jag(
return AssertionIdentity(
user_id=subject.user_id,
team_id=subject.team_id,
replay_key=replay_key,
replay_marker=replay_marker,
replay_ttl_seconds=replay_ttl_seconds,
scope=scope.scope,
allowed_routes=scope.allowed_routes,
@ -511,7 +510,7 @@ async def identity_from_id_jag(
def _replay_marker(
verified: Mapping[str, object], redemption: AssertionRedemption
) -> tuple[str, int] | SubjectTokenRefusal:
) -> tuple[IdJagMarker, int] | SubjectTokenRefusal:
"""The single-use key and its lifetime for a verified assertion, once the assertion is bound to
the presenting app, carries no proof-of-possession the gateway cannot check, and is still alive."""
if verified.get("client_id") != redemption.client_id:
@ -526,7 +525,7 @@ def _replay_marker(
replay_ttl_seconds: Final = math.ceil(exp + redemption.leeway_seconds - redemption.now)
if replay_ttl_seconds <= 0:
return _rejected("the assertion has expired")
return _REPLAY_KEY_PREFIX + hashlib.sha256(f"{issuer}\n{jti}".encode()).hexdigest(), replay_ttl_seconds
return IdJagMarker(issuer, jti), replay_ttl_seconds
def _rejected(reason: str) -> SubjectTokenRefusal:

View file

@ -1,6 +1,9 @@
import hashlib
from dataclasses import dataclass
from datetime import timedelta
from typing import Final, Protocol
from typing import Final, Protocol, TypeAlias
from typing_extensions import assert_never
from litellm.redis.transport.contract import EvalReply, RedisFailure, RedisTransport
@ -13,22 +16,67 @@ return count
"""
class McpSingleUseStore(Protocol):
async def claim(self, key: str, *, ttl: timedelta) -> int | RedisFailure: ...
@dataclass(frozen=True, slots=True)
class CodeMarker:
jti: str
async def peek(self, key: str) -> bool | RedisFailure: ...
@dataclass(frozen=True, slots=True)
class FlowMarker:
jti: str
@dataclass(frozen=True, slots=True)
class RefreshMarker:
jti: str
@dataclass(frozen=True, slots=True)
class RevokedRefreshFamilyMarker:
family: str
@dataclass(frozen=True, slots=True)
class IdJagMarker:
issuer: str
jti: str
McpSingleUseMarker: TypeAlias = CodeMarker | FlowMarker | RefreshMarker | RevokedRefreshFamilyMarker | IdJagMarker
def marker_key(marker: McpSingleUseMarker) -> str:
match marker:
case CodeMarker(jti):
return f"mcp_gateway_dcr_code_used:{jti}"
case FlowMarker(jti):
return f"mcp_gateway_dcr_flow_used:{jti}"
case RefreshMarker(jti):
return f"mcp_gateway_dcr_refresh_used:{jti}"
case RevokedRefreshFamilyMarker(family):
return f"mcp_gateway_dcr_refresh_family_revoked:{family}"
case IdJagMarker(issuer, jti):
return "mcp_gateway_dcr:id_jag:" + hashlib.sha256(f"{issuer}\n{jti}".encode()).hexdigest()
case _:
assert_never(marker)
class McpSingleUseStore(Protocol):
async def claim(self, marker: McpSingleUseMarker, *, ttl: timedelta) -> int | RedisFailure: ...
async def peek(self, marker: McpSingleUseMarker) -> bool | RedisFailure: ...
@dataclass(frozen=True, slots=True)
class TransportMcpSingleUseStore:
transport: RedisTransport
async def claim(self, key: str, *, ttl: timedelta) -> int | RedisFailure:
reply: Final = await self.transport.eval(_CLAIM, (key,), (1, ttl // timedelta(seconds=1)))
async def claim(self, marker: McpSingleUseMarker, *, ttl: timedelta) -> int | RedisFailure:
reply: Final = await self.transport.eval(_CLAIM, (marker_key(marker),), (1, ttl // timedelta(seconds=1)))
return reply if isinstance(reply, RedisFailure) else _count(reply)
async def peek(self, key: str) -> bool | RedisFailure:
reply: Final = await self.transport.get(key)
async def peek(self, marker: McpSingleUseMarker) -> bool | RedisFailure:
reply: Final = await self.transport.get(marker_key(marker))
return reply if isinstance(reply, RedisFailure) else reply is not None
@ -37,5 +85,5 @@ def _count(reply: EvalReply) -> int | RedisFailure:
return RedisFailure(kind="decode", message=f"single-use count is not a bulk string: {reply!r}")
try:
return int(float(reply))
except ValueError:
except (ValueError, OverflowError):
return RedisFailure(kind="decode", message=f"single-use count is not a number: {reply!r}")

View file

@ -1,8 +1,10 @@
"""Tests for the aggregate gateway DCR flow (register, authorize, complete, token)."""
import asyncio
import hashlib
import json
import re
import time
from base64 import urlsafe_b64encode
from datetime import datetime, timedelta, timezone
from http.cookies import SimpleCookie
@ -66,6 +68,14 @@ from litellm.proxy.mcp_server.outbound_credentials.session_token import (
mint_session_refresh_token,
mint_session_token,
)
from litellm.redis.stores.mcp_single_use import (
CodeMarker,
FlowMarker,
IdJagMarker,
McpSingleUseMarker,
RefreshMarker,
RevokedRefreshFamilyMarker,
)
from tests._support.fake_redis import fake_redis_cache
MASTER_KEY = "sk-gateway-dcr-flow-tests"
@ -689,8 +699,8 @@ async def test_single_use_guard_in_memory_is_single_use_within_process():
from litellm.proxy.mcp_server.gateway_dcr_flow import _SingleUseGuard
guard = _SingleUseGuard(DualCache()) # redis_cache is None
assert await guard.claim("jti-inmem", 60) == "first"
assert await guard.claim("jti-inmem", 60) == "replayed"
assert await guard.claim(CodeMarker("jti-inmem"), 60) == "first"
assert await guard.claim(CodeMarker("jti-inmem"), 60) == "replayed"
@pytest.mark.asyncio
@ -703,10 +713,13 @@ async def test_single_use_guard_uses_redis_as_sole_authority_when_configured():
cache = DualCache(redis_cache=fake_redis_cache(server))
guard = _SingleUseGuard(cache)
assert await guard.claim("jti-redis", 60) == "first"
assert await guard.claim("jti-redis", 60) == "replayed"
assert await _SingleUseGuard(DualCache(redis_cache=fake_redis_cache(server))).claim("jti-redis", 60) == "replayed"
assert cache.in_memory_cache.get_cache("jti-redis") is None
assert await guard.claim(CodeMarker("jti-redis"), 60) == "first"
assert await guard.claim(CodeMarker("jti-redis"), 60) == "replayed"
assert (
await _SingleUseGuard(DualCache(redis_cache=fake_redis_cache(server))).claim(CodeMarker("jti-redis"), 60)
== "replayed"
)
assert cache.in_memory_cache.get_cache("mcp_gateway_dcr_code_used:jti-redis") is None
@pytest.mark.asyncio
@ -720,14 +733,14 @@ async def test_single_use_guard_writes_the_marker_the_old_increment_wrote_with_i
raw = fakeredis.FakeAsyncRedis(server=server)
guard = _SingleUseGuard(DualCache(redis_cache=redis_cache))
assert await guard.claim("new-first", 60) == "first"
assert await raw.get("ns:new-first") == b"1"
assert 59_000 < await raw.pttl("ns:new-first") <= 60_000
assert await redis_cache.async_increment("new-first", 1, ttl=60) == 2.0
assert await guard.claim(CodeMarker("new-first"), 60) == "first"
assert await raw.get("ns:mcp_gateway_dcr_code_used:new-first") == b"1"
assert 59_000 < await raw.pttl("ns:mcp_gateway_dcr_code_used:new-first") <= 60_000
assert await redis_cache.async_increment("mcp_gateway_dcr_code_used:new-first", 1, ttl=60) == 2.0
assert await redis_cache.async_increment("old-first", 1, ttl=60) == 1.0
assert await guard.claim("old-first", 60) == "replayed"
assert await guard.peek("old-first") == "claimed"
assert await redis_cache.async_increment("mcp_gateway_dcr_code_used:old-first", 1, ttl=60) == 1.0
assert await guard.claim(CodeMarker("old-first"), 60) == "replayed"
assert await guard.peek(CodeMarker("old-first")) == "claimed"
@pytest.mark.asyncio
@ -738,9 +751,9 @@ async def test_single_use_guard_keeps_the_ttl_a_marker_already_has():
raw = fakeredis.FakeAsyncRedis(server=server)
guard = _SingleUseGuard(DualCache(redis_cache=fake_redis_cache(server)))
assert await guard.claim("jti-ttl", 600) == "first"
assert await guard.claim("jti-ttl", 5) == "replayed"
assert 590_000 < await raw.pttl("jti-ttl") <= 600_000
assert await guard.claim(CodeMarker("jti-ttl"), 600) == "first"
assert await guard.claim(CodeMarker("jti-ttl"), 5) == "replayed"
assert 590_000 < await raw.pttl("mcp_gateway_dcr_code_used:jti-ttl") <= 600_000
@pytest.mark.asyncio
@ -753,8 +766,8 @@ async def test_single_use_guard_fails_closed_when_redis_errors():
cache = DualCache(redis_cache=fake_redis_cache(server))
server.connected = False
assert await _SingleUseGuard(cache).claim("jti-fault", 60) == "unavailable"
assert cache.in_memory_cache.get_cache("jti-fault") is None
assert await _SingleUseGuard(cache).claim(CodeMarker("jti-fault"), 60) == "unavailable"
assert cache.in_memory_cache.get_cache("mcp_gateway_dcr_code_used:jti-fault") is None
@pytest.mark.asyncio
@ -765,13 +778,13 @@ async def test_single_use_guard_peek_fails_closed_when_redis_errors():
server = fakeredis.FakeServer()
guard = _SingleUseGuard(DualCache(redis_cache=fake_redis_cache(server)))
assert await guard.claim("family-fault", 60) == "first"
assert await guard.claim(RevokedRefreshFamilyMarker("family-fault"), 60) == "first"
server.connected = False
assert await guard.peek("family-fault") == "unavailable"
assert await guard.peek(RevokedRefreshFamilyMarker("family-fault")) == "unavailable"
server.connected = True
assert await guard.peek("family-fault") == "claimed"
assert await guard.peek(RevokedRefreshFamilyMarker("family-fault")) == "claimed"
@pytest.mark.asyncio
@ -784,14 +797,169 @@ async def test_single_use_guard_peek_reads_the_key_under_the_namespace_claim_wro
server = fakeredis.FakeServer()
guard = _SingleUseGuard(DualCache(redis_cache=fake_redis_cache(server, namespace="ns")))
assert await guard.peek("family-ns") == "unclaimed"
assert await guard.claim("family-ns", 60) == "first"
assert await fakeredis.FakeAsyncRedis(server=server).keys("*") == [b"ns:family-ns"]
assert await guard.peek("family-ns") == "claimed"
assert await guard.peek(RevokedRefreshFamilyMarker("family-ns")) == "unclaimed"
assert await guard.claim(RevokedRefreshFamilyMarker("family-ns"), 60) == "first"
assert await fakeredis.FakeAsyncRedis(server=server).keys("*") == [
b"ns:mcp_gateway_dcr_refresh_family_revoked:family-ns"
]
assert await guard.peek(RevokedRefreshFamilyMarker("family-ns")) == "claimed"
LOOPBACK_REDIRECT_URI = "http://localhost:3118/callback"
_MARKER_CASES: Final = (
(CodeMarker("jti: é\n1"), "mcp_gateway_dcr_code_used:jti: é\n1"),
(FlowMarker("jti: é\n1"), "mcp_gateway_dcr_flow_used:jti: é\n1"),
(RefreshMarker("jti: é\n1"), "mcp_gateway_dcr_refresh_used:jti: é\n1"),
(RevokedRefreshFamilyMarker("jti: é\n1"), "mcp_gateway_dcr_refresh_family_revoked:jti: é\n1"),
(
IdJagMarker("https://issuer.example/租户", "jti: é\n1"),
"mcp_gateway_dcr:id_jag:dabba8ce01737ba5cb47a28665f7bec742b072d198672c80bbc6e70a2781a221",
),
)
@pytest.mark.asyncio
@pytest.mark.parametrize(("marker", "key"), _MARKER_CASES)
async def test_marker_families_interoperate_with_old_writers_and_preserve_expiry(
marker: McpSingleUseMarker, key: str, monkeypatch: pytest.MonkeyPatch
) -> None:
from litellm.proxy.mcp_server.gateway_dcr_flow import _SingleUseGuard
monkeypatch.setattr(time, "time", lambda: 1_700_000_000.0)
server: Final = fakeredis.FakeServer()
redis_cache: Final = fake_redis_cache(server, namespace="ns")
raw: Final = fakeredis.FakeAsyncRedis(server=server)
guard: Final = _SingleUseGuard(DualCache(redis_cache=redis_cache))
wire_key: Final = f"ns:{key}"
assert await guard.peek(marker) == "unclaimed"
assert await guard.claim(marker, 60) == "first"
assert await raw.keys("*") == [wire_key.encode()]
assert await raw.get(wire_key) == b"1"
assert await raw.pttl(wire_key) == 60_000
assert await redis_cache.async_get_cache(key) == 1
assert await redis_cache.async_increment(key, 1, ttl=5) == 2.0
assert await guard.peek(marker) == "claimed"
assert await guard.claim(marker, 120) == "replayed"
assert await raw.get(wire_key) == b"3"
assert await raw.pttl(wire_key) == 60_000
assert await raw.delete(wire_key) == 1
assert await redis_cache.async_increment(key, 1, ttl=60) == 1.0
assert await raw.persist(wire_key)
assert await raw.pttl(wire_key) == -1
other_worker: Final = _SingleUseGuard(DualCache(redis_cache=fake_redis_cache(server, namespace="ns")))
assert await other_worker.peek(marker) == "claimed"
assert await other_worker.claim(marker, 17) == "replayed"
assert await raw.get(wire_key) == b"2"
assert await raw.pttl(wire_key) == 17_000
monkeypatch.setattr(time, "time", lambda: 1_700_000_018.0)
assert await guard.peek(marker) == "unclaimed"
claimed: Final = await asyncio.gather(guard.claim(marker, 30), other_worker.claim(marker, 30))
assert sorted(claimed) == ["first", "replayed"]
assert await raw.get(wire_key) == b"2"
assert await raw.pttl(wire_key) == 30_000
@pytest.mark.asyncio
@pytest.mark.parametrize(("marker", "key"), _MARKER_CASES)
async def test_marker_families_use_canonical_keys_only_for_local_fallback(marker: McpSingleUseMarker, key: str) -> None:
from litellm.proxy.mcp_server.gateway_dcr_flow import _SingleUseGuard
cache: Final = DualCache()
guard: Final = _SingleUseGuard(cache)
assert await guard.peek(marker) == "unclaimed"
assert await guard.claim(marker, 60) == "first"
assert cache.in_memory_cache.get_cache(key) == 1
assert await guard.peek(marker) == "claimed"
assert await guard.claim(marker, 60) == "replayed"
assert cache.in_memory_cache.get_cache(key) == 2
@pytest.mark.asyncio
@pytest.mark.parametrize(("marker", "key"), _MARKER_CASES)
async def test_marker_families_fail_closed_on_outage_and_invalid_counter(marker: McpSingleUseMarker, key: str) -> None:
from litellm.proxy.mcp_server.gateway_dcr_flow import _SingleUseGuard
server: Final = fakeredis.FakeServer()
cache: Final = DualCache(redis_cache=fake_redis_cache(server, namespace="ns"))
raw: Final = fakeredis.FakeAsyncRedis(server=server)
guard: Final = _SingleUseGuard(cache)
server.connected = False
assert await guard.claim(marker, 60) == "unavailable"
assert await guard.peek(marker) == "unavailable"
assert cache.in_memory_cache.get_cache(key) is None
server.connected = True
assert await raw.set(f"ns:{key}", b"not a counter")
assert await guard.claim(marker, 60) == "unavailable"
assert await guard.peek(marker) == "claimed"
assert await raw.get(f"ns:{key}") == b"not a counter"
assert await raw.pttl(f"ns:{key}") == -1
assert cache.in_memory_cache.get_cache(key) is None
class _RedisThatLosesClaimReply(fakeredis.FakeAsyncRedis):
async def eval(self, script: str, numkeys: int, *keys_and_args: str | bytes | float) -> object:
await super().eval(script, numkeys, *keys_and_args)
raise RedisConnectionError("claim applied but reply was lost")
class _RedisThatCorruptsClaimReply(fakeredis.FakeAsyncRedis):
def __init__(self, server: fakeredis.FakeServer, reply: bytes | int) -> None:
super().__init__(server=server)
self._reply = reply
async def eval(self, script: str, numkeys: int, *keys_and_args: str | bytes | float) -> bytes | int:
await super().eval(script, numkeys, *keys_and_args)
return self._reply
@pytest.mark.asyncio
@pytest.mark.parametrize("reply", (b"inf", b"nan", b"invalid", 1))
async def test_malformed_claim_reply_is_unavailable_without_local_fallback(reply: bytes | int) -> None:
from litellm.proxy.mcp_server.gateway_dcr_flow import _SingleUseGuard
server: Final = fakeredis.FakeServer()
redis_cache: Final = fake_redis_cache(server, namespace="ns")
litellm.in_memory_llm_clients_cache.set_cache(
key=redis_cache._get_async_client_cache_key(),
value=_RedisThatCorruptsClaimReply(server=server, reply=reply),
)
cache: Final = DualCache(redis_cache=redis_cache)
assert await _SingleUseGuard(cache).claim(CodeMarker("invalid-reply"), 60) == "unavailable"
assert cache.in_memory_cache.get_cache("mcp_gateway_dcr_code_used:invalid-reply") is None
assert await fakeredis.FakeAsyncRedis(server=server).get("ns:mcp_gateway_dcr_code_used:invalid-reply") == b"1"
@pytest.mark.asyncio
@pytest.mark.parametrize(("marker", "key"), _MARKER_CASES)
async def test_marker_families_never_replay_a_claim_after_a_lost_reply(
marker: McpSingleUseMarker, key: str, monkeypatch: pytest.MonkeyPatch
) -> None:
from litellm.proxy.mcp_server.gateway_dcr_flow import _SingleUseGuard
monkeypatch.setattr(time, "time", lambda: 1_700_000_000.0)
server: Final = fakeredis.FakeServer()
redis_cache: Final = fake_redis_cache(server, namespace="ns")
litellm.in_memory_llm_clients_cache.set_cache(
key=redis_cache._get_async_client_cache_key(),
value=_RedisThatLosesClaimReply(server=server),
)
cache: Final = DualCache(redis_cache=redis_cache)
guard: Final = _SingleUseGuard(cache)
raw: Final = fakeredis.FakeAsyncRedis(server=server)
assert await guard.claim(marker, 60) == "unavailable"
assert await raw.get(f"ns:{key}") == b"1"
assert await raw.pttl(f"ns:{key}") == 60_000
assert cache.in_memory_cache.get_cache(key) is None
other_worker: Final = _SingleUseGuard(DualCache(redis_cache=fake_redis_cache(server, namespace="ns")))
assert await other_worker.claim(marker, 120) == "replayed"
assert await other_worker.peek(marker) == "claimed"
assert await raw.get(f"ns:{key}") == b"2"
assert await raw.pttl(f"ns:{key}") == 60_000
async def _complete(redirect_uri: str, delivery, cookies=None, handle=None, session_user_id="u1"):
client_id = (await _register([redirect_uri]))["client_id"]
@ -3281,7 +3449,7 @@ REQUESTING_APP = "0oa-requesting-app"
ASSERTED = AssertionIdentity(
user_id="u1",
team_id="team-b",
replay_key="mcp_gateway_dcr:id_jag:jti-1",
replay_marker=IdJagMarker("https://issuer.example", "jti-1"),
replay_ttl_seconds=300,
scope="litellm:mcp",
allowed_routes=("mcp_inference_routes",),
@ -3332,7 +3500,9 @@ async def test_jwt_bearer_grant_mints_the_proxy_credential_for_the_asserted_user
assert replayed.status_code == 400
assert json.loads(replayed.body)["error"] == "invalid_grant"
assert minter.calls == [("u1", "team-b")]
fresh = _Redeemer(ASSERTED.model_copy(update={"replay_key": "mcp_gateway_dcr:id_jag:jti-2", "scope": None}))
fresh = _Redeemer(
ASSERTED.model_copy(update={"replay_marker": IdJagMarker("https://issuer.example", "jti-2"), "scope": None})
)
again = await _redeem_assertion_native(minter, fresh, cache=cache)
assert again.status_code == 200
assert "scope" not in json.loads(again.body)

View file

@ -1,4 +1,3 @@
import hashlib
import logging
import math
from base64 import b64encode
@ -204,11 +203,12 @@ async def test_a_valid_id_jag_names_the_user_its_single_use_key_and_its_scope():
assert resolver.calls == ((_claims(), "team-b"),)
assert (identity.user_id, identity.team_id, identity.scope) == ("u1", "team-b", "litellm:mcp")
assert identity.allowed_routes == ("mcp_inference_routes",)
assert identity.replay_key == "mcp_gateway_dcr:id_jag:" + hashlib.sha256(f"{ISSUER}\njti-1".encode()).hexdigest()
assert identity.replay_marker.issuer == ISSUER
assert identity.replay_marker.jti == "jti-1"
assert identity.replay_ttl_seconds == math.ceil(300)
other_jti = await _identity(_redemption(_assertion(jti="jti-2")), _Verifier(), _Resolver())
assert isinstance(other_jti, AssertionIdentity)
assert other_jti.replay_key != identity.replay_key
assert other_jti.replay_marker != identity.replay_marker
teamless = await _identity(_redemption(_assertion(scope=""), team_id=None))
assert isinstance(teamless, AssertionIdentity)
assert (teamless.team_id, teamless.scope, teamless.allowed_routes) == (None, None, None)