The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(simplex): warn about name allowlist entries once per process, as authz reads them
kshitijk4poorf9771a445def
diff --git a/plugins/platforms/simplex/adapter.py b/plugins/platforms/simplex/adapter.pyindex fff5b5d30b6..53e671be022 100644--- a/plugins/platforms/simplex/adapter.py+++ b/plugins/platforms/simplex/adapter.py@@ -25,9 +25,11 @@ from typing import Any, Dict, List, Optionalfrom urllib.parse import unquotefrom gateway.platforms._shared import (- get_scoped_secret as _get_scoped_secret, seed_extra_from_env as _seed_extra_from_env, send_error+ decode_json_list_literal as _decode_json_list_literal, get_scoped_secret as _get_scoped_secret,+ platform_gate_env as _platform_gate_env, seed_extra_from_env as _seed_extra_from_env, send_error)from gateway.config import Platform, PlatformConfig+from hermes_constants import hermes_home_keyfrom gateway.platforms.base import BasePlatformAdapter, SendResult, cache_image_from_urlfrom gateway.platforms.helpers import cancel_taskfrom gateway.platforms.event import MessageEvent, MessageType@@ -52,6 +54,29 @@ def _parse_comma_list(value: str) -> List[str]:return [v.strip() for v in value.split(",") if v.strip()]+# (hermes home key, names) already warned about. Module-level: every reconnect builds a FRESH adapter+# (gateway/run_adapters.py), so an instance flag would re-warn on every retry while the daemon is down.+_NAME_ALLOWLIST_WARNED: set = set()+++def _warn_name_allowlist_entries() -> None:+ """Warn once per process per profile/allowlist value about SIMPLEX_ALLOWED_USERS entries authz ignores.++ Reads and decodes the value exactly as authz does (``platform_gate_env`` + JSON list literal), so the+ warning names only entries that really fail the contactId check."""+ raw = _decode_json_list_literal(_platform_gate_env("SIMPLEX_ALLOWED_USERS"))+ entries = [str(e).strip() for e in raw] if isinstance(raw, list) else _parse_comma_list(raw)+ names = [u for u in entries if u and u != "*" and not u.isdigit()]+ if not names:+ return+ key = (hermes_home_key(), frozenset(names))+ if key in _NAME_ALLOWLIST_WARNED:+ return+ _NAME_ALLOWLIST_WARNED.add(key)+ logger.warning("SimpleX: SIMPLEX_ALLOWED_USERS entries %s are not numeric contactIds and are ignored "+ "(display names are not trusted; see /contacts for IDs)", names)++def _redact_id(contact_id: str) -> str:if not contact_id:return "<none>"@@ -118,7 +143,6 @@ class SimplexAdapter(BasePlatformAdapter):self._pending_file_transfers: Dict[int, dict] = {} # awaiting rcvFileComplete, by fileIdself._pending_responses: Dict[str, asyncio.Future] = {} # awaited command repliesself._corr_counter = 0- self._allowlist_warned = False # name-entry allowlist warning fires once per adapter# SimpleX has no client-side split, so the split delay equals the plain one.self._text_batch_delay_seconds = float(os.getenv("HERMES_SIMPLEX_TEXT_BATCH_DELAY", "0.8"))self._text_batch_split_delay_seconds = self._text_batch_delay_seconds@@ -135,15 +159,7 @@ class SimplexAdapter(BasePlatformAdapter):if not self.ws_url:logger.error("SimpleX: SIMPLEX_WS_URL is required")return False- # Once per adapter, before the probe so a daemon-down cold boot still warns (later connects- # arrive as is_reconnect=True). Scoped read matches what authz enforces for this profile.- if not self._allowlist_warned:- self._allowlist_warned = True- names = [u for u in _parse_comma_list(_get_scoped_secret("SIMPLEX_ALLOWED_USERS", "") or "")- if u != "*" and not u.isdigit()]- if names:- logger.warning("SimpleX: SIMPLEX_ALLOWED_USERS entries %s are not numeric contactIds and are ignored "- "(display names are not trusted; see /contacts for IDs)", names)+ _warn_name_allowlist_entries() # before the probe so a daemon-down cold boot still warnstry: # quick connectivity check — open and immediately closeasync with _wsclient.connect(self.ws_url, open_timeout=10):passdiff --git a/tests/gateway/test_simplex_plugin.py b/tests/gateway/test_simplex_plugin.pyindex d8ee90b7eb3..80bba5290c4 100644--- a/tests/gateway/test_simplex_plugin.py+++ b/tests/gateway/test_simplex_plugin.py@@ -488,9 +488,10 @@ def test_multiplex_scope_reads_profile_own_env_not_default(@pytest.mark.asyncioasync def test_name_allowlist_warning_once_scoped_even_if_first_connect_fails(monkeypatch, caplog):"""Name entries in SIMPLEX_ALLOWED_USERS are ignored by authz, so connect()- warns -- exactly once per adapter, from the profile-scoped value authz- enforces (not the default profile's os.environ), and even when the first- connect fails (daemon down at cold boot; retries arrive as is_reconnect)."""+ warns -- exactly once per process for this profile/value, read the way authz+ reads it (the profile scope, not the default profile's os.environ), and even+ when the first connect fails. Reconnects build a FRESH adapter each attempt+ (gateway/run_adapters.py), so the dedup cannot live on the instance."""import loggingimport websockets@@ -498,6 +499,7 @@ async def test_name_allowlist_warning_once_scoped_even_if_first_connect_fails(mofrom gateway.config import PlatformConfigmonkeypatch.setenv("SIMPLEX_ALLOWED_USERS", "bob") # default profile's bridge output+ monkeypatch.setattr(_simplex, "_NAME_ALLOWLIST_WARNED", set(), raising=False)class DummyWs:async def __aenter__(self):@@ -517,19 +519,23 @@ async def test_name_allowlist_warning_once_scoped_even_if_first_connect_fails(moasync def _idle():return None+ def _fresh_adapter():+ adapter = SimplexAdapter(PlatformConfig(enabled=True, extra={"ws_url": "ws://localhost:5225"}))+ monkeypatch.setattr(adapter, "_ws_listener", _idle)+ monkeypatch.setattr(adapter, "_health_monitor", _idle)+ return adapter+monkeypatch.setattr(websockets, "connect", fake_connect)- adapter = SimplexAdapter(PlatformConfig(enabled=True, extra={"ws_url": "ws://localhost:5225"}))- monkeypatch.setattr(adapter, "_ws_listener", _idle)- monkeypatch.setattr(adapter, "_health_monitor", _idle)# Scope set inside the test: a sync fixture's ContextVar token can't be reset from here.set_multiplex_active(True)token = set_secret_scope({"SIMPLEX_ALLOWED_USERS": "4, alice"})try:with caplog.at_level(logging.WARNING):- assert await adapter.connect() is False- assert await adapter.connect(is_reconnect=True) is True- await adapter.disconnect()+ assert await _fresh_adapter().connect() is False # cold boot, daemon down+ retry = _fresh_adapter() # the reconnect watcher builds a new adapter+ assert await retry.connect(is_reconnect=True) is True+ await retry.disconnect()finally:reset_secret_scope(token)set_multiplex_active(False)