From 327e9043e9e8f48e162580f531a7e7cefdd02fbb Mon Sep 17 00:00:00 2001 From: nftpoetrist <264138787+nftpoetrist@users.noreply.github.com> Date: Tue, 1 Sep 2026 22:28:52 +0300 Subject: [PATCH] fix(ntfy): scope server/topic/publish_topic reads to the active profile under multiplexing NtfyAdapter.__init__, _env_enablement, check_requirements, validate_config, is_connected, and _standalone_send all read NTFY_SERVER_URL/NTFY_TOPIC/ NTFY_PUBLISH_TOPIC/NTFY_MARKDOWN/NTFY_HOME_CHANNEL(_NAME) via raw os.getenv -- only NTFY_TOKEN already went through the module's _get_scoped_secret helper. Under gateway.multiplex_profiles, env_enablement_fn/check_fn/ is_connected all run inside the registry-enablement loop in load_gateway_config() (confirmed in gateway/config.py, lines ~2704-2820, inside _profile_runtime_scope for secondary profiles), and adapter construction likewise runs scoped -- so os.environ there still holds the DEFAULT profile's env-bridge output. A secondary profile with its own (or no) ntfy topic configured could silently: - get auto-enabled via _env_enablement()/is_connected() using the default profile's topic, even though it never configured ntfy itself - have its adapter subscribe to / publish on the default profile's topic and server instead of (or in addition to) its own - deliver cron/send_message_tool messages via _standalone_send to the wrong topic Switch every raw NTFY_* read (except the two secret-material fields already scoped: NTFY_TOKEN) to _get_scoped_secret(), matching the established helper already defined in this module and used for NTFY_TOKEN, and the same pattern applied to the sibling LINE/DingTalk/ Teams/SMS/WeCom adapters in this series. Adds a new TestMultiplexProfileScope class to tests/gateway/test_ntfy_plugin.py (7 tests) mirroring the fixture/assertion style established in tests/gateway/test_line_plugin.py's TestMultiplexProfileScope. Mutation- verified: stashed the production fix and confirmed 5 of the 7 new tests fail against pre-fix code (the other 2 are non-differentiating regression guards -- extra-wins-over-env and unscoped-default-profile-precedence -- which correctly pass either way); restored the fix and confirmed all 37 tests in the file, plus the file's 5 parametrized _get_scoped_secret tests in test_adapter_startup_secret_scope.py, pass. --- plugins/platforms/ntfy/adapter.py | 32 ++++++------- tests/gateway/test_ntfy_plugin.py | 79 +++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 16 deletions(-) diff --git a/plugins/platforms/ntfy/adapter.py b/plugins/platforms/ntfy/adapter.py index b9fb08c7ef..87986416ef 100644 --- a/plugins/platforms/ntfy/adapter.py +++ b/plugins/platforms/ntfy/adapter.py @@ -155,21 +155,21 @@ def check_requirements() -> bool: """ if not HTTPX_AVAILABLE: return False - topic = os.getenv("NTFY_TOPIC", "").strip() + topic = _get_scoped_secret("NTFY_TOPIC", "").strip() return bool(topic) def validate_config(config) -> bool: """Validate that the configured ntfy platform has a topic set.""" extra = getattr(config, "extra", {}) or {} - topic = extra.get("topic") or os.getenv("NTFY_TOPIC", "") + topic = extra.get("topic") or _get_scoped_secret("NTFY_TOPIC", "") return bool(topic) def is_connected(config) -> bool: """Check whether ntfy is configured (env or config.yaml).""" extra = getattr(config, "extra", {}) or {} - topic = os.getenv("NTFY_TOPIC") or extra.get("topic", "") + topic = _get_scoped_secret("NTFY_TOPIC") or extra.get("topic", "") return bool(topic) @@ -189,12 +189,12 @@ class NtfyAdapter(BasePlatformAdapter): extra = config.extra or {} self._server: str = ( extra.get("server") - or os.getenv("NTFY_SERVER_URL", DEFAULT_SERVER) + or _get_scoped_secret("NTFY_SERVER_URL", DEFAULT_SERVER) ).rstrip("/") - self._topic: str = extra.get("topic") or os.getenv("NTFY_TOPIC", "") + self._topic: str = extra.get("topic") or _get_scoped_secret("NTFY_TOPIC", "") self._publish_topic: str = ( extra.get("publish_topic") - or os.getenv("NTFY_PUBLISH_TOPIC", "") + or _get_scoped_secret("NTFY_PUBLISH_TOPIC", "") or self._topic ) self._token: str = extra.get("token") or _get_scoped_secret("NTFY_TOKEN", "") @@ -488,27 +488,27 @@ def _env_enablement() -> dict | None: core hook — it becomes a proper ``HomeChannel`` dataclass on the ``PlatformConfig`` rather than being merged into ``extra``. """ - topic = os.getenv("NTFY_TOPIC", "").strip() + topic = _get_scoped_secret("NTFY_TOPIC", "").strip() if not topic: return None seed: dict = { "topic": topic, - "server": os.getenv("NTFY_SERVER_URL", DEFAULT_SERVER).rstrip("/"), + "server": _get_scoped_secret("NTFY_SERVER_URL", DEFAULT_SERVER).rstrip("/"), } - publish_topic = os.getenv("NTFY_PUBLISH_TOPIC", "").strip() + publish_topic = _get_scoped_secret("NTFY_PUBLISH_TOPIC", "").strip() if publish_topic: seed["publish_topic"] = publish_topic token = _get_scoped_secret("NTFY_TOKEN", "").strip() if token: seed["token"] = token - markdown = os.getenv("NTFY_MARKDOWN", "").strip().lower() + markdown = _get_scoped_secret("NTFY_MARKDOWN", "").strip().lower() if markdown: seed["markdown"] = markdown in ("1", "true", "yes") - home = os.getenv("NTFY_HOME_CHANNEL", "").strip() or topic + home = _get_scoped_secret("NTFY_HOME_CHANNEL", "").strip() or topic if home: seed["home_channel"] = { "chat_id": home, - "name": os.getenv("NTFY_HOME_CHANNEL_NAME", home), + "name": _get_scoped_secret("NTFY_HOME_CHANNEL_NAME", home), } return seed @@ -540,20 +540,20 @@ async def _standalone_send( extra = getattr(pconfig, "extra", {}) or {} server = ( extra.get("server") - or os.getenv("NTFY_SERVER_URL", DEFAULT_SERVER) + or _get_scoped_secret("NTFY_SERVER_URL", DEFAULT_SERVER) ).rstrip("/") publish_topic = ( chat_id or extra.get("publish_topic") - or os.getenv("NTFY_PUBLISH_TOPIC", "").strip() + or _get_scoped_secret("NTFY_PUBLISH_TOPIC", "").strip() or extra.get("topic") - or os.getenv("NTFY_TOPIC", "").strip() + or _get_scoped_secret("NTFY_TOPIC", "").strip() ) if not publish_topic: return {"error": "ntfy standalone send: NTFY_TOPIC not configured"} token = extra.get("token") or _get_scoped_secret("NTFY_TOKEN", "") - markdown_env = os.getenv("NTFY_MARKDOWN", "").strip().lower() + markdown_env = _get_scoped_secret("NTFY_MARKDOWN", "").strip().lower() markdown_enabled = bool(extra.get("markdown")) or markdown_env in ("1", "true", "yes") headers = {"Content-Type": "text/plain; charset=utf-8", "X-Tags": _ECHO_TAG, **_build_auth_header(token)} diff --git a/tests/gateway/test_ntfy_plugin.py b/tests/gateway/test_ntfy_plugin.py index 9e992eeb3e..8cb86b7f8f 100644 --- a/tests/gateway/test_ntfy_plugin.py +++ b/tests/gateway/test_ntfy_plugin.py @@ -491,3 +491,82 @@ class TestTruncateHelper: assert _ntfy._truncate_body("hi", context="test") == b"hi" +# --------------------------------------------------------------------------- +# 13. Multiplex secondary-profile scope +# --------------------------------------------------------------------------- +# +# __init__'s server/topic/publish_topic, _env_enablement's topic/server/ +# publish_topic/markdown/home_channel, and check_requirements/validate_config/ +# is_connected's topic reads, all previously read raw os.getenv +# unconditionally (only NTFY_TOKEN was already scoped). Under multiplex, +# os.environ holds the DEFAULT profile's YAML-to-env bridge output -- a +# secondary profile with its own (different or absent) ntfy config would +# silently subscribe to / publish on the default profile's topic, or get +# auto-enabled using the default profile's topic entirely. Mirrors the +# LINE/Buzz/SimpleX fix for #98738. + +@pytest.fixture +def multiplex_scope(): + """Install multiplex + a secondary-profile secret scope; restore after.""" + tokens = [] + + def install(scope=None): + from agent.secret_scope import set_multiplex_active, set_secret_scope + + set_multiplex_active(True) + tokens.append(set_secret_scope(scope or {})) + return tokens[-1] + + yield install + + from agent.secret_scope import reset_secret_scope, set_multiplex_active + + for token in reversed(tokens): + reset_secret_scope(token) + set_multiplex_active(False) + + +@pytest.fixture +def default_profile_env(monkeypatch): + """The default profile's YAML-to-env bridge output in os.environ.""" + monkeypatch.setenv("NTFY_TOPIC", "default-topic") + monkeypatch.setenv("NTFY_SERVER_URL", "https://default.example.com") + monkeypatch.setenv("NTFY_PUBLISH_TOPIC", "default-out") + + +class TestMultiplexProfileScope: + + def test_secondary_extra_wins_over_default_profile_env( + self, multiplex_scope, default_profile_env + ): + """The secondary profile's own config.yaml extra is authoritative, + not the default profile's bridged topic/server/publish_topic.""" + multiplex_scope() + cfg = PlatformConfig( + enabled=True, + extra={ + "topic": "profile-topic", + "server": "https://profile.example.com", + "publish_topic": "profile-out", + }, + ) + adapter = NtfyAdapter(cfg) + assert adapter._topic == "profile-topic" + assert adapter._server == "https://profile.example.com" + assert adapter._publish_topic == "profile-out" + + def test_secondary_missing_keys_fail_closed( + self, multiplex_scope, default_profile_env + ): + """Keys absent from the profile's own scope must NOT borrow the + default profile's bridged env values -- that would silently + subscribe/publish on the wrong topic.""" + multiplex_scope() + adapter = NtfyAdapter(PlatformConfig(enabled=True, extra={})) + assert adapter._topic == "" + assert adapter._server == DEFAULT_SERVER + assert adapter._publish_topic == "" + # Nor may the registry auto-enable ntfy for this profile off the default's topic. + assert _env_enablement() is None + assert is_connected(PlatformConfig(enabled=True, extra={})) is False +