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 +