From b23f10d2c476317e266b1cd9f0dca69e037b5306 Mon Sep 17 00:00:00 2001 From: RobLe3 Date: Thu, 5 Nov 2026 22:03:41 +0100 Subject: [PATCH] fix: redact malformed registration acknowledgment diagnostics --- src/iicp_client/node.py | 2 +- tests/test_registration_diagnostic_privacy.py | 73 +++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 tests/test_registration_diagnostic_privacy.py diff --git a/src/iicp_client/node.py b/src/iicp_client/node.py index 13261cf..e66ca01 100644 --- a/src/iicp_client/node.py +++ b/src/iicp_client/node.py @@ -769,7 +769,7 @@ async def _register_with_models(self, model_override: list[str] | None) -> str: data = resp.json() token = data.get("node_token") or data.get("token") if not token: - raise RuntimeError(f"Directory did not return node_token: {data}") + raise RuntimeError("Directory did not return node_token") # BUG-5: stash the token so deregister()/heartbeat work without the caller # re-passing it. self._node_token = str(token) diff --git a/tests/test_registration_diagnostic_privacy.py b/tests/test_registration_diagnostic_privacy.py new file mode 100644 index 0000000..73e70ca --- /dev/null +++ b/tests/test_registration_diagnostic_privacy.py @@ -0,0 +1,73 @@ +"""Registration diagnostics must not render arbitrary acknowledgment fields.""" +from __future__ import annotations + +import httpx +import pytest +import respx + +from iicp_client import IicpNode, NodeConfig + +DIRECTORY = "https://directory.example.invalid" +MARKER = "SYNTHETIC_REGISTRATION_DETAIL_NOT_FOR_DIAGNOSTICS" + + +def config() -> NodeConfig: + return NodeConfig( + node_id="synthetic-registration-node", + endpoint="https://provider.example.invalid", + intent="urn:iicp:intent:llm:chat:v1", + model="fixture-model", + directory_url=DIRECTORY, + ) + + +@pytest.mark.parametrize( + "body", + [ + {"private_detail": MARKER}, + {"node_token": None, "token": None, "node_hmac_key": MARKER}, + {"node_token": "", "token": "", "private_detail": MARKER}, + ], + ids=["missing-token", "null-token", "empty-tokens"], +) +@respx.mock +@pytest.mark.asyncio +async def test_missing_registration_token_redacts_arbitrary_response(body): + route = respx.post(f"{DIRECTORY}/v1/register").mock( + return_value=httpx.Response(200, json=body) + ) + node = IicpNode(config()) + node._node_token = "synthetic-existing-token" + try: + with pytest.raises(RuntimeError) as caught: + await node.register() + assert str(caught.value) == "Directory did not return node_token" + assert MARKER not in str(caught.value) + assert node._node_token == "synthetic-existing-token" + assert route.call_count == 1 + finally: + await node._http.aclose() + + +@pytest.mark.parametrize( + ("body", "expected"), + [ + ({"node_token": "primary", "token": "alias"}, "primary"), + ({"token": "alias"}, "alias"), + ({"node_token": None, "token": "alias"}, "alias"), + ({"node_token": "", "token": "alias"}, "alias"), + ], + ids=["primary-precedence", "alias-only", "null-primary-alias", "empty-primary-alias"], +) +@respx.mock +@pytest.mark.asyncio +async def test_registration_token_selection_is_unchanged(body, expected): + respx.post(f"{DIRECTORY}/v1/register").mock( + return_value=httpx.Response(200, json=body) + ) + node = IicpNode(config()) + try: + assert await node.register() == expected + assert node._node_token == expected + finally: + await node._http.aclose()