diff --git a/src/iicp_client/client.py b/src/iicp_client/client.py index 38a53f0..9030e67 100644 --- a/src/iicp_client/client.py +++ b/src/iicp_client/client.py @@ -11,6 +11,7 @@ import re import time import uuid +from dataclasses import replace from typing import Any from urllib.parse import urlparse @@ -122,7 +123,13 @@ class IicpClient: """ def __init__(self, config: ClientConfig | None = None) -> None: - self._cfg = config or ClientConfig() + self._cfg = replace(config) if config is not None else ClientConfig() + if self._cfg.tls_verify is False: + raise IicpError( + code="SDK-05", + message="tls_verify=False is not supported; TLS certificate verification is required", + component="sdk", + ) if self._cfg.timeout_ms > _MAX_TIMEOUT_MS: # SDK-04: reject oversized timeouts at construction time raise ValueError(f"timeout_ms must be ≤ {_MAX_TIMEOUT_MS}; got {self._cfg.timeout_ms}") diff --git a/src/iicp_client/types.py b/src/iicp_client/types.py index 3a549fa..a9381a7 100644 --- a/src/iicp_client/types.py +++ b/src/iicp_client/types.py @@ -34,10 +34,13 @@ class RoutingPolicy: @dataclass class ClientConfig: + """Client options, shallow-copied by IicpClient at construction.""" + directory_url: str = "https://iicp.network/api" region: str | None = None timeout_ms: int = 30_000 max_retries: int = 3 + # False is rejected by IicpClient with SDK-05. tls_verify: bool = True # DEPRECATED/no-op (#360): IICP-CX encryption is mandatory — the client always # encrypts when the node advertises a cx_public_key, regardless of this flag. diff --git a/tests/test_tls_policy.py b/tests/test_tls_policy.py new file mode 100644 index 0000000..8471c45 --- /dev/null +++ b/tests/test_tls_policy.py @@ -0,0 +1,29 @@ +"""SDK-05 constructor controls; no request or TLS handshake is performed.""" +from unittest import TestCase, mock + +from iicp_client import ClientConfig, IicpClient, IicpError + + +class TlsCertificatePolicyTests(TestCase): + def test_default_verification_is_accepted(self): + with mock.patch("httpx.AsyncClient", side_effect=AssertionError("unexpected HTTP client")): + self.assertIsInstance(IicpClient(), IicpClient) + + def test_explicit_verification_is_accepted(self): + with mock.patch("httpx.AsyncClient", side_effect=AssertionError("unexpected HTTP client")): + self.assertIsInstance(IicpClient(ClientConfig(tls_verify=True)), IicpClient) + + def test_disabled_verification_is_refused_before_any_request(self): + with mock.patch("httpx.AsyncClient", side_effect=AssertionError("unexpected HTTP client")): + with self.assertRaises(IicpError) as caught: + IicpClient(ClientConfig(tls_verify=False)) + self.assertEqual(caught.exception.code, "SDK-05") + self.assertEqual(caught.exception.component, "sdk") + self.assertFalse(caught.exception.retryable) + + def test_caller_configuration_mutation_cannot_disable_verification(self): + config = ClientConfig(tls_verify=True) + with mock.patch("httpx.AsyncClient", side_effect=AssertionError("unexpected HTTP client")): + client = IicpClient(config) + config.tls_verify = False + self.assertTrue(client._cfg.tls_verify)