fix(mcp): skip legacy blake2b lock key when the runtime has no blake2b

This commit is contained in:
yucheng-berri 2026-10-14 02:15:35 -07:00
parent 3d912c6cbd
commit 57d7522a67
2 changed files with 77 additions and 9 deletions

View file

@ -663,19 +663,44 @@ async def find_mcp_server_identifier_conflict(
)
_legacy_lock_key_unavailable_logged: bool = False
def _legacy_blake2b_lock_key(digest: bytes) -> int | None:
"""Legacy blake2b advisory-lock key for mixed-version rolling upgrades.
Returns ``None`` when the runtime has no blake2b (FIPS-enforcing builds),
where writes must not fail: the sha256 key alone still serialises
new-worker writes there."""
global _legacy_lock_key_unavailable_logged
try:
return int.from_bytes(hashlib.blake2b(digest, digest_size=8).digest(), "big", signed=True)
except (AttributeError, ValueError):
if not _legacy_lock_key_unavailable_logged:
_legacy_lock_key_unavailable_logged = True
verbose_proxy_logger.warning(
"blake2b is unavailable on this runtime; MCP locks skip the legacy "
"mixed-version key and use the sha256 key only"
)
return None
def _mcp_identifier_lock_keys(*identifiers: str | None) -> tuple[int, ...]:
"""Deterministic advisory-lock keys for the lowercased identifiers, sorted
so concurrent requests for the same pair always lock in the same order.
Each identifier locks its sha256 key and then the legacy blake2b key:
transition window for mixed-version rolling upgrades, where pre-change
workers still take only the blake2b lock. Delete the legacy element once
main-only workers are gone."""
workers still take only the blake2b lock. The legacy element is skipped on
runtimes without blake2b (FIPS-enforcing builds). Delete the legacy element
once main-only workers are gone."""
keys: list[int] = []
for normalized in sorted(frozenset(value.lower() for value in identifiers if value)):
digest: Final = f"mcp_identifier:{normalized}".encode()
keys.append(int.from_bytes(hashlib.sha256(digest).digest()[:8], "big", signed=True))
keys.append(int.from_bytes(hashlib.blake2b(digest, digest_size=8).digest(), "big", signed=True))
legacy: Final = _legacy_blake2b_lock_key(digest)
if legacy is not None:
keys.append(legacy)
return tuple(keys)
@ -2429,13 +2454,15 @@ def _mcp_user_env_var_lock_keys(user_id: str, server_id: str) -> tuple[int, ...]
The sha256 key comes first, then the legacy blake2b key: transition window
for mixed-version rolling upgrades, where pre-change workers still take
only the blake2b lock. Delete the legacy element once main-only workers
are gone."""
only the blake2b lock. The legacy element is skipped on runtimes without
blake2b (FIPS-enforcing builds). Delete the legacy element once main-only
workers are gone."""
digest: Final = f"{user_id}:{server_id}".encode()
return (
int.from_bytes(hashlib.sha256(digest).digest()[:8], "big", signed=True),
int.from_bytes(hashlib.blake2b(digest, digest_size=8).digest(), "big", signed=True),
)
keys: Final = [int.from_bytes(hashlib.sha256(digest).digest()[:8], "big", signed=True)]
legacy: Final = _legacy_blake2b_lock_key(digest)
if legacy is not None:
keys.append(legacy)
return tuple(keys)
async def merge_user_env_vars(

View file

@ -1690,3 +1690,44 @@ def test_mcp_user_env_var_lock_keys_use_sha256_and_legacy_blake2b_for_user_and_s
int.from_bytes(hashlib.sha256(b"alice:srv-1").digest()[:8], "big", signed=True),
int.from_bytes(hashlib.blake2b(b"alice:srv-1", digest_size=8).digest(), "big", signed=True),
)
def _blake2b_unavailable(*args, **kwargs):
raise ValueError("blake2b is not available on this FIPS-enforcing runtime")
def test_mcp_identifier_lock_keys_drop_the_legacy_key_when_blake2b_is_unavailable(monkeypatch: pytest.MonkeyPatch):
"""A FIPS-enforcing runtime without blake2b must not crash MCP writes: the
legacy key is skipped and the sha256 key alone serialises new workers."""
monkeypatch.setattr(hashlib, "blake2b", _blake2b_unavailable)
keys: Final = _mcp_identifier_lock_keys("Name")
assert keys == (int.from_bytes(hashlib.sha256(b"mcp_identifier:name").digest()[:8], "big", signed=True),)
def test_mcp_user_env_var_lock_keys_drop_the_legacy_key_when_blake2b_is_unavailable(
monkeypatch: pytest.MonkeyPatch,
):
from litellm.proxy._experimental.mcp_server.db import _mcp_user_env_var_lock_keys
monkeypatch.setattr(hashlib, "blake2b", _blake2b_unavailable)
keys: Final = _mcp_user_env_var_lock_keys("alice", "srv-1")
assert keys == (int.from_bytes(hashlib.sha256(b"alice:srv-1").digest()[:8], "big", signed=True),)
@pytest.mark.asyncio
async def test_merge_user_env_vars_succeeds_when_blake2b_is_unavailable(
env_vars_salt_key, monkeypatch: pytest.MonkeyPatch
):
monkeypatch.setattr(hashlib, "blake2b", _blake2b_unavailable)
prisma: Final = _transactional_env_vars_prisma()
merged: Final = await merge_user_env_vars(prisma, "alice", "srv-1", {"TOKEN": "x"}, allowed_names=["TOKEN"])
assert merged == {"TOKEN": "x"}
assert list(prisma.db._store.locks) == [
int.from_bytes(hashlib.sha256(b"alice:srv-1").digest()[:8], "big", signed=True)
]