Skip to content

The agent that grows with you

fix(secrets): scope-aware standalone-send and startup credential reads in platform adapters

Teknium359ff01c239c
diff --git a/plugins/platforms/discord/adapter.py b/plugins/platforms/discord/adapter.py
index 9ef2659fbd7..88d87bc8b35 100644
--- a/plugins/platforms/discord/adapter.py
+++ b/plugins/platforms/discord/adapter.py
@@ -9486,7 +9486,14 @@ async def _standalone_send(
except ImportError:
return {"error": "aiohttp not installed. Run: pip install aiohttp"}
- token = (getattr(pconfig, "token", None) or os.getenv("DISCORD_BOT_TOKEN", "")).strip()
+ token = (getattr(pconfig, "token", None) or "").strip()
+ if not token:
+ # Profile-scoped read: under multiplex the process env may hold a
+ # different profile's bot token, so honor the secret scope's verdict
+ # (scoped miss ⇒ no token; unscoped multiplex ⇒ UnscopedSecretError).
+ from agent.secret_scope import get_secret
+
+ token = (get_secret("DISCORD_BOT_TOKEN", "") or "").strip()
if not token:
return {"error": "Discord standalone send: DISCORD_BOT_TOKEN is not set"}
diff --git a/plugins/platforms/matrix/adapter.py b/plugins/platforms/matrix/adapter.py
index 93926d21c39..b8461ae27da 100644
--- a/plugins/platforms/matrix/adapter.py
+++ b/plugins/platforms/matrix/adapter.py
@@ -874,6 +874,20 @@ def _pre_sanitize_matrix_markdown(text: str) -> str:
return result
+def _startup_env_secret(name: str) -> str:
+ """Read a Matrix credential at adapter-startup time, scope-aware.
+
+ Slack pattern (#59739): a scoped read honors the installed profile's
+ secret scope verdict (scoped miss ⇒ empty, no borrowing the process
+ env); only an UNSCOPED read under multiplex (default-profile startup
+ loop) falls back to ``os.environ``, which is that profile's own value.
+ """
+ try:
+ return (get_secret(name) or "").strip()
+ except UnscopedSecretError:
+ return os.getenv(name, "").strip()
+
+
def check_matrix_requirements() -> bool:
"""Return True if the Matrix adapter can be used.
@@ -885,8 +899,8 @@ def check_matrix_requirements() -> bool:
forever and broke E2EE connect with ``No module named 'asyncpg'``
(#31116). Rebinds module-level type globals on success.
"""
- token = os.getenv("MATRIX_ACCESS_TOKEN", "")
- password = os.getenv("MATRIX_PASSWORD", "")
+ token = _startup_env_secret("MATRIX_ACCESS_TOKEN")
+ password = _startup_env_secret("MATRIX_PASSWORD")
homeserver = os.getenv("MATRIX_HOMESERVER", "")
if not token and not password:
@@ -1013,12 +1027,14 @@ class MatrixAdapter(BasePlatformAdapter):
self._homeserver: str = (
config.extra.get("homeserver", "") or os.getenv("MATRIX_HOMESERVER", "")
).rstrip("/")
- self._access_token: str = config.token or os.getenv("MATRIX_ACCESS_TOKEN", "")
+ self._access_token: str = config.token or _startup_env_secret(
+ "MATRIX_ACCESS_TOKEN"
+ )
self._user_id: str = config.extra.get("user_id", "") or os.getenv(
"MATRIX_USER_ID", ""
)
- self._password: str = config.extra.get("password", "") or os.getenv(
- "MATRIX_PASSWORD", ""
+ self._password: str = config.extra.get("password", "") or _startup_env_secret(
+ "MATRIX_PASSWORD"
)
self._e2ee_mode: str = _resolve_e2ee_mode(config.extra)
self._encryption: bool = self._e2ee_mode != "off"
@@ -4801,7 +4817,10 @@ async def _standalone_send(
return {"error": "aiohttp not installed. Run: pip install aiohttp"}
try:
homeserver = (extra.get("homeserver") or os.getenv("MATRIX_HOMESERVER", "")).rstrip("/")
- token = token or os.getenv("MATRIX_ACCESS_TOKEN", "")
+ # In-turn read: standalone sends run inside an installed secret
+ # scope, so honor get_secret's verdict directly (no env fallback on
+ # a scoped miss).
+ token = token or get_secret("MATRIX_ACCESS_TOKEN", "") or ""
if not homeserver or not token:
return {"error": "Matrix not configured (MATRIX_HOMESERVER, MATRIX_ACCESS_TOKEN required)"}
txn_id = f"hermes_{int(time.time() * 1000)}_{os.urandom(4).hex()}"
diff --git a/plugins/platforms/slack/adapter.py b/plugins/platforms/slack/adapter.py
index 736598dba88..ee50a4578ca 100644
--- a/plugins/platforms/slack/adapter.py
+++ b/plugins/platforms/slack/adapter.py
@@ -8609,7 +8609,10 @@ async def _standalone_send(
``chat.postMessage``.
"""
del force_document # signature parity with other standalone senders
- raw_token = getattr(pconfig, "token", None) or os.getenv("SLACK_BOT_TOKEN", "")
+ # Profile-scoped read: under multiplex os.environ may hold ANOTHER
+ # profile's bot token (first-writer-wins env bridges), so honor the
+ # secret scope's verdict instead of reading the process env directly.
+ raw_token = getattr(pconfig, "token", None) or get_secret("SLACK_BOT_TOKEN", "")
# ``SLACK_BOT_TOKEN`` can be a comma-separated list in multi-workspace
# gateways, and OAuth installs persist per-workspace tokens in
diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.py
index ef31587d223..674a49d2be9 100644
--- a/plugins/platforms/telegram/adapter.py
+++ b/plugins/platforms/telegram/adapter.py
@@ -4038,7 +4038,20 @@ class TelegramAdapter(BasePlatformAdapter):
os.getenv("TELEGRAM_WEBHOOK_HOST", "").strip()
or str((self.config.extra or {}).get("webhook_host") or "").strip()
)
- webhook_secret = os.getenv("TELEGRAM_WEBHOOK_SECRET", "").strip()
+ # Profile-scoped read (adapter startup, Slack pattern
+ # #59739): a scoped read honors the profile's own secret;
+ # only an UNSCOPED read under multiplex (default-profile
+ # startup loop) falls back to the process env, which is that
+ # profile's own value.
+ from agent.secret_scope import (
+ UnscopedSecretError,
+ get_secret,
+ )
+
+ try:
+ webhook_secret = (get_secret("TELEGRAM_WEBHOOK_SECRET") or "").strip()
+ except UnscopedSecretError:
+ webhook_secret = os.getenv("TELEGRAM_WEBHOOK_SECRET", "").strip()
if not webhook_secret:
raise RuntimeError(
"TELEGRAM_WEBHOOK_SECRET is required when "
@@ -9932,7 +9945,13 @@ async def _standalone_send(
parse-mode fallback). Implements the standalone_sender_fn contract so
deliver=telegram cron jobs succeed when cron runs separately from the
gateway."""
- token = getattr(pconfig, "token", None) or os.getenv("TELEGRAM_BOT_TOKEN", "")
+ token = getattr(pconfig, "token", None)
+ if not token:
+ # Profile-scoped read: honor the secret scope's verdict rather than
+ # borrowing another profile's env-bridged token under multiplex.
+ from agent.secret_scope import get_secret
+
+ token = get_secret("TELEGRAM_BOT_TOKEN", "") or ""
disable_link_previews = bool(
getattr(pconfig, "extra", {}) and pconfig.extra.get("disable_link_previews")
)