diff --git a/tests/tools/test_mcp_oauth.py b/tests/tools/test_mcp_oauth.py index 3547079f1e..c63fd9afbc 100644 --- a/tests/tools/test_mcp_oauth.py +++ b/tests/tools/test_mcp_oauth.py @@ -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 diff --git a/tools/mcp_oauth.py b/tools/mcp_oauth.py index e1c91e8343..c5b3883242 100644 --- a/tools/mcp_oauth.py +++ b/tools/mcp_oauth.py @@ -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")):