Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/iicp_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import re
import time
import uuid
from dataclasses import replace
from typing import Any
from urllib.parse import urlparse

Expand Down Expand Up @@ -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}")
Expand Down
3 changes: 3 additions & 0 deletions src/iicp_client/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions tests/test_tls_policy.py
Original file line number Diff line number Diff line change
@@ -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)
Loading