fix(mcp): treat a non-object client.json as corrupt instead of crashing auth init
_cached_redirect already degraded a list/str/number client.json to (None, None), but one step later the MCP SDK calls storage.get_client_info() while building OAuthClientProvider, and _load_model ran the fixup (data.get) outside its try, so the same payload still raised AttributeError out of the OAuth flow. Guard isinstance(dict) in _load_model (warn + None, like any other corrupt file; also covers the tokens fixup's data.pop) and make _cached_client_info return None for non-object payloads so CIMD eligibility and redirect reuse see "no cached registration" rather than a registration that can never load. Part of #112568
This commit is contained in:
1 parent
d4f7f90c8f
commit
e166791f9f
2 files changed
+21
-1
No files matched your search
@@ -17,6 +17,7 @@ from tools.mcp_oauth import (
|
||||
OAuthNonInteractiveError,
|
||||
build_oauth_auth,
|
||||
remove_oauth_tokens,
|
||||
_cached_client_info,
|
||||
_cached_redirect,
|
||||
_can_open_browser,
|
||||
_is_interactive,
|
||||
@@ -678,6 +679,16 @@ class TestCallbackPortReservation:
|
||||
storage = self._seed_client_info(tmp_path, payload)
|
||||
assert _cached_redirect(storage) == (None, None)
|
||||
|
||||
@pytest.mark.parametrize("payload", [["not", "a", "dict"], "just-a-string", 123])
|
||||
def test_non_dict_client_info_degrades_to_fresh_registration(self, tmp_path, payload):
|
||||
"""The MCP SDK calls storage.get_client_info() while building OAuthClientProvider, one
|
||||
step after _cached_redirect. A non-object client.json must read as "no registration"
|
||||
on both paths (fresh DCR + CIMD still eligible), not AttributeError out of auth init
|
||||
(#112568)."""
|
||||
storage = self._seed_client_info(tmp_path, payload)
|
||||
assert _cached_client_info(storage) is None
|
||||
assert asyncio.run(storage.get_client_info()) is None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# remove_oauth_tokens
|
||||
|
||||
+10
-1
@@ -277,9 +277,12 @@ def _reserve_callback_port() -> int:
|
||||
def _cached_client_info(storage: "HermesTokenStorage | None") -> dict | None:
|
||||
"""The on-disk client registration for *storage*, or None."""
|
||||
try:
|
||||
return _read_json(storage._client_info_path()) if storage is not None else None
|
||||
info = _read_json(storage._client_info_path()) if storage is not None else None
|
||||
except (AttributeError, TypeError, ValueError):
|
||||
return None
|
||||
# A non-object payload is not a registration get_client_info could ever load, so callers
|
||||
# (CIMD eligibility, redirect reuse) must see "no cached registration", not "exists".
|
||||
return info if isinstance(info, dict) else None
|
||||
|
||||
|
||||
def _cached_redirect(storage: "HermesTokenStorage | None") -> "tuple[str | None, int | None]":
|
||||
@@ -434,6 +437,12 @@ class HermesTokenStorage:
|
||||
"""Read *path* into SDK model *sdk_name*; None if absent, no SDK, or corrupt.
|
||||
``fixup(data)`` may rewrite the raw dict before validation."""
|
||||
data = _read_json(path)
|
||||
if data is not None and not isinstance(data, dict):
|
||||
# A non-object payload (list/str/number) has no fields for the fixups' ``.get``/``.pop``
|
||||
# and cannot validate: treat it like any other corrupt file instead of crashing auth init.
|
||||
logger.warning("Corrupt %s at %s -- ignoring: expected a JSON object, got %s",
|
||||
label, path, type(data).__name__)
|
||||
return None
|
||||
cls = _sdk_class(sdk_name) if data is not None else None
|
||||
if (cls is not None and sdk_name == "OAuthMetadata" and isinstance(data, dict)
|
||||
and data.get("device_authorization_endpoint")):
|
||||
|
||||
Reference in new issue
Block a user