The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(mcp): seed malformed client.json through HermesTokenStorage in redirect tests
teknium1d4f7f90c8fee
diff --git a/tests/tools/test_mcp_oauth.py b/tests/tools/test_mcp_oauth.pyindex 56ff0070312..3547079f1eb 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_redirect,_can_open_browser,_is_interactive,_make_callback_handler,@@ -639,32 +640,29 @@ class TestCallbackPortReservation:assert result.code == "flowA"assert result.state == "sA"- def _seed_client_info(self, tmp_path, monkeypatch, redirect_uris):- import json as _json- import tools.mcp_oauth as mod-- monkeypatch.setenv("HERMES_HOME", str(tmp_path))- storage = HermesTokenStorage("srv")- d = tmp_path / "mcp-tokens"- d.mkdir(parents=True, exist_ok=True)- (d / "srv.client.json").write_text(_json.dumps({- "client_id": "client-a", "redirect_uris": redirect_uris}))- return storage, mod+ @staticmethod+ def _seed_client_info(tmp_path, payload):+ """Write *payload* verbatim to the real ``mcp-tokens/srv.client.json`` under a temp home."""+ storage = HermesTokenStorage("srv", hermes_home=tmp_path)+ path = storage._client_info_path()+ path.parent.mkdir(parents=True, exist_ok=True)+ path.write_text(json.dumps(payload), encoding="utf-8")+ return storage@pytest.mark.parametrize("bad_uri", ["http://127.0.0.1:abc/callback", # .port raises: non-numeric"http://127.0.0.1:99999/callback", # .port raises: out of range"http://[bad/callback", # urlparse itself raises: bad IPv6 bracket])- def test_cached_redirect_skips_malformed_entries(self, tmp_path, monkeypatch, bad_uri):+ def test_cached_redirect_skips_malformed_entries(self, tmp_path, bad_uri):"""DCR-supplied redirect_uris persist to client.json. urlparse() alone does notvalidate ports — .port is lazy and raises ValueError on access — so the try/exceptaround urlparse never fires. A poisoned entry must be skipped like every other- malformed one, not crash the whole OAuth flow."""- storage, mod = self._seed_client_info(- tmp_path, monkeypatch,- [bad_uri, "http://127.0.0.1:1455/callback", "https://proxy.example.com/cb"])- assert mod._cached_redirect(storage) == ("https://proxy.example.com/cb", 1455)+ malformed one, not crash the whole OAuth flow (#112568)."""+ storage = self._seed_client_info(tmp_path, {+ "client_id": "client-a",+ "redirect_uris": [bad_uri, "http://127.0.0.1:1455/callback", "https://proxy.example.com/cb"]})+ assert _cached_redirect(storage) == ("https://proxy.example.com/cb", 1455)@pytest.mark.parametrize("payload", [["not", "a", "dict"], # non-dict client.json: .get would AttributeError@@ -673,19 +671,12 @@ class TestCallbackPortReservation:{"redirect_uris": None}, # explicit null{"client_id": "c"}, # missing key entirely])- def test_cached_redirect_tolerates_misshaped_client_info(self, tmp_path, monkeypatch, payload):+ def test_cached_redirect_tolerates_misshaped_client_info(self, tmp_path, payload):"""_read_json returns whatever the file holds — the crash class isn't limited tobad URIs inside a well-formed list. Any misshaped payload must degrade to- (None, None), not propagate AttributeError/TypeError through the OAuth flow."""- import json as _json- import tools.mcp_oauth as mod-- monkeypatch.setenv("HERMES_HOME", str(tmp_path))- storage = HermesTokenStorage("srv")- d = tmp_path / "mcp-tokens"- d.mkdir(parents=True, exist_ok=True)- (d / "srv.client.json").write_text(_json.dumps(payload))- assert mod._cached_redirect(storage) == (None, None)+ (None, None), not propagate AttributeError/TypeError through the OAuth flow (#112568)."""+ storage = self._seed_client_info(tmp_path, payload)+ assert _cached_redirect(storage) == (None, None)# ---------------------------------------------------------------------------