| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- """Secret-scrub policy for Hermes child processes: pure data + predicates for which env
- names are Hermes-managed credentials. The env *builders* applying it (``_make_run_env``,
- ``_sanitize_subprocess_env``, ``hermes_subprocess_env``) live in ``tools.environments.local``."""
- import functools
- import os
- from typing import Optional
- # Prefix a caller uses in ``extra_env`` to force a blocklisted var through.
- _HERMES_PROVIDER_ENV_FORCE_PREFIX = "_HERMES_FORCE_"
- # Hermes-managed AWS *inference* credentials for ``auth_type="aws_sdk"`` (Bedrock):
- # only the Bedrock bearer token, which no aws/terraform/boto3 toolchain uses. The
- # general AWS chain stays inheritable on purpose — the local terminal is the user's
- # trusted operator shell (SECURITY.md §3.2) and env_passthrough can never re-allow a
- # blocklisted name (GHSA-rhgp-j443-p4rf), so blocking it would be unrecoverable.
- _AWS_SDK_CREDENTIAL_ENV_VARS = frozenset({"AWS_BEARER_TOKEN_BEDROCK"})
- _STATIC_PROVIDER_ENV_BLOCKLIST = frozenset({
- "OPENAI_BASE_URL", "OPENAI_API_KEY", "OPENAI_API_BASE", "OPENAI_ORG_ID",
- "OPENAI_ORGANIZATION", "OPENROUTER_API_KEY", "ANTHROPIC_BASE_URL",
- "ANTHROPIC_API_KEY", "ANTHROPIC_TOKEN", "LLM_MODEL", "GOOGLE_API_KEY",
- # Path to a GCP service-account JSON, not a bare key, so OPTIONAL_ENV_VARS
- # marks it password=False and the registry loop skips it.
- "VERTEX_CREDENTIALS_PATH", "GOOGLE_APPLICATION_CREDENTIALS", "DEEPSEEK_API_KEY",
- "MISTRAL_API_KEY", "GROQ_API_KEY", "TOGETHER_API_KEY", "PERPLEXITY_API_KEY",
- "COHERE_API_KEY", "FIREWORKS_API_KEY", "XAI_API_KEY", "HELICONE_API_KEY",
- "PARALLEL_API_KEY", "FIRECRAWL_API_KEY", "FIRECRAWL_API_URL",
- "TELEGRAM_HOME_CHANNEL", "TELEGRAM_HOME_CHANNEL_NAME", "DISCORD_HOME_CHANNEL",
- "DISCORD_HOME_CHANNEL_NAME", "DISCORD_REQUIRE_MENTION",
- "DISCORD_FREE_RESPONSE_CHANNELS", "DISCORD_AUTO_THREAD", "SLACK_HOME_CHANNEL",
- "SLACK_HOME_CHANNEL_NAME", "SLACK_ALLOWED_USERS", "WHATSAPP_ENABLED",
- "WHATSAPP_MODE", "WHATSAPP_ALLOWED_USERS", "SIGNAL_HTTP_URL", "SIGNAL_ACCOUNT",
- "SIGNAL_ALLOWED_USERS", "SIGNAL_GROUP_ALLOWED_USERS", "SIGNAL_HOME_CHANNEL",
- "SIGNAL_HOME_CHANNEL_NAME", "SIGNAL_IGNORE_STORIES",
- "EMAIL_ADDRESS", "EMAIL_PASSWORD", "EMAIL_IMAP_HOST", "EMAIL_SMTP_HOST",
- "EMAIL_HOME_ADDRESS", "EMAIL_HOME_ADDRESS_NAME", "HERMES_DASHBOARD_SESSION_TOKEN",
- "GATEWAY_ALLOWED_USERS", "GH_TOKEN", "GITHUB_APP_ID", "GITHUB_APP_PRIVATE_KEY_PATH",
- "GITHUB_APP_INSTALLATION_ID", "MODAL_TOKEN_ID", "MODAL_TOKEN_SECRET",
- "DAYTONA_API_KEY", "GATEWAY_RELAY_ID", "GATEWAY_RELAY_SECRET",
- "GATEWAY_RELAY_DELIVERY_KEY", "VERCEL_OIDC_TOKEN", "VERCEL_TOKEN",
- "VERCEL_PROJECT_ID", "VERCEL_TEAM_ID",
- # Keys the OAuth provider profiles (nous, qwen-oauth) also accept when pasted. The auth
- # registry mirrors env_vars only for api_key profiles, and discovering the provider plugins
- # from here, at import, would re-mirror them over a plugin's own registry entry.
- "NOUS_API_KEY", "QWEN_API_KEY",
- # Hermes' own secrets read in code: the anonymous-inference secret, dashboard auth
- # (basic, OIDC, drain) and the Google Meet realtime key.
- "HERMES_ANON_API_SECRET", "HERMES_DASHBOARD_BASIC_AUTH_PASSWORD",
- "HERMES_DASHBOARD_BASIC_AUTH_SECRET", "HERMES_DASHBOARD_DRAIN_SECRET",
- "HERMES_DASHBOARD_OIDC_CLIENT_SECRET", "HERMES_MEET_REALTIME_KEY",
- })
- def _left_core_env(*, secrets_only: bool) -> frozenset:
- """Env of features that left core for a catalog plugin (``hermes_cli.left_core_migration``):
- stripped exactly as when core shipped them, plugin installed or not."""
- from hermes_cli.left_core_migration import LEFT_CORE
- return frozenset(name for feature in LEFT_CORE
- for name in feature.secret_env + (() if secrets_only else feature.private_env))
- def _build_provider_env_blocklist() -> frozenset:
- """Derive the blocklist from provider, tool, and gateway config."""
- blocked: set[str] = set(_STATIC_PROVIDER_ENV_BLOCKLIST) | _left_core_env(secrets_only=False)
- try:
- from hermes_cli.auth import PROVIDER_REGISTRY
- for pconfig in PROVIDER_REGISTRY.values():
- blocked.update(pconfig.api_key_env_vars)
- if pconfig.auth_type == "aws_sdk":
- blocked.update(_AWS_SDK_CREDENTIAL_ENV_VARS)
- if pconfig.base_url_env_var:
- blocked.add(pconfig.base_url_env_var)
- except ImportError:
- pass
- try:
- from hermes_cli.config import OPTIONAL_ENV_VARS
- for name, metadata in OPTIONAL_ENV_VARS.items():
- category = metadata.get("category")
- if category in {"tool", "messaging"} or (
- category == "setting" and metadata.get("password")):
- blocked.add(name)
- except ImportError:
- pass
- # CLAUDE_CODE_OAUTH_TOKEN (via the anthropic registry entry) belongs to the user's
- # Claude Code install, not Hermes: stripping it made agent-spawned ``claude`` CLIs
- # fall through to the shared Keychain / ~/.claude store and, on auth failure, wipe
- # it — logging the user out. BUZZ_* is deliberately NOT discarded: this list feeds
- # every scrub surface, so an import-time discard would leak BUZZ_PRIVATE_KEY into
- # non-terminal children; the Buzz carve-out is terminal-only and context-gated
- # (``_is_terminal_first_party_env``).
- # It is set and owned by the user's Claude Code install (subscription OAuth), not a Hermes-managed
- # inference credential — Claude subscription auth is not a working Hermes provider path. It arrives via
- # the registry loop above (anthropic api_key_env_vars), so remove it explicitly. See #55878.
- blocked.discard("CLAUDE_CODE_OAUTH_TOKEN")
- # BUZZ_* is deliberately NOT discarded here, even for Buzz-managed agents (BUZZ_MANAGED_AGENT set by the
- # buzz-acp harness). See #76243, #78026, #78065, #78511.
- return frozenset(blocked)
- def _build_adapter_secret_env() -> frozenset:
- """Secrets the messaging adapters declare, process-wide: core ``password`` messaging entries
- of OPTIONAL_ENV_VARS, the bundled platform plugin manifests' secret entries, and the
- secret-named keys the gateway env-override table reads (WEIXIN_TOKEN, FEISHU_ENCRYPT_KEY, ...).
- Declared names only: a user's own ``SLACK_USER_TOKEN`` or ``LOCAL_LLM_API_KEY`` is not Hermes's.
- A profile's user-installed platform plugins are per home: :func:`_home_adapter_secret_env`.
- Nothing here fails soft: an unreadable bundled manifest or env table fails the import rather
- than dropping its secrets from the policy."""
- from hermes_cli.config import (
- CORE_DECLARED_ENV_NAMES, OPTIONAL_ENV_VARS, PLATFORM_SECRET_ENV_SUFFIXES, platform_manifest_secret_envs)
- from hermes_cli.profile_channels import config_env_table_keys
- # Read in code only, declared nowhere else: the Microsoft Graph app secret and webhook
- # clientState, the QQ bot's speech-to-text key, and secrets only a plugin's ``env_overrides``
- # reads (Feishu webhook keys, Yuanbao's legacy app key). Those rows live on the adapter's
- # PlatformEntry, which this import-time policy cannot load without importing every adapter;
- # tests/gateway/test_platform_env_overrides.py pins that every such secret is listed here.
- names: set[str] = {"MSGRAPH_CLIENT_SECRET", "MSGRAPH_WEBHOOK_CLIENT_STATE", "QQ_STT_API_KEY",
- "FEISHU_ENCRYPT_KEY", "FEISHU_VERIFICATION_TOKEN", "YUANBAO_APP_KEY"}
- names.update(name.upper() for name, meta in OPTIONAL_ENV_VARS.items()
- if name in CORE_DECLARED_ENV_NAMES
- and meta.get("category") == "messaging" and meta.get("password"))
- names |= platform_manifest_secret_envs(source="bundled", strict=True)
- names.update(n.upper() for n in config_env_table_keys() if n.upper().endswith(PLATFORM_SECRET_ENV_SUFFIXES))
- return frozenset(names)
- # Provider blocklist first: it imports hermes_cli.auth before hermes_cli.config, whose import
- # discovers provider plugins that expect a fully initialized auth registry.
- _PROVIDER_ENV_BLOCKLIST = _build_provider_env_blocklist()
- _ADAPTER_SECRET_ENV = _build_adapter_secret_env()
- _HERMES_PROVIDER_ENV_BLOCKLIST = _PROVIDER_ENV_BLOCKLIST | _ADAPTER_SECRET_ENV
- _HOME_ADAPTER_SECRET_CACHE: dict[str, tuple] = {}
- def _home_adapter_secret_env() -> frozenset:
- """Secrets declared by the bound profile's own user-installed platform plugins. Per home, not
- process-wide: under multiplex profile A's plugin must neither strip a same-named value from
- profile B's children nor be missing from A's. Cached per home and keyed on every manifest's
- file signature, so an edit, replacement or deletion takes effect on the next spawn. A plugin
- manifest under ``plugins/platforms/`` that cannot be read raises. A partial scan (a plugin dir
- or flat manifest unreadable or unparsable) keeps the names this home already had and is not
- cached, so a failed discovery never releases a known denial and recovery is seen at once."""
- from hermes_cli.config import platform_manifest_secret_scan, platform_manifest_stamp
- from hermes_constants import get_hermes_home, hermes_home_key
- home = get_hermes_home()
- key, stamp = hermes_home_key(home), platform_manifest_stamp(home)
- cached = _HOME_ADAPTER_SECRET_CACHE.get(key)
- if cached is None or cached[0] is None or cached[0] != stamp:
- names, complete = platform_manifest_secret_scan(home)
- names -= _ADAPTER_SECRET_ENV
- if not complete and cached is not None:
- names |= cached[1]
- cached = (stamp if complete else None, names)
- _HOME_ADAPTER_SECRET_CACHE[key] = cached
- return cached[1]
- def _registry_adapter_secret_env() -> frozenset:
- """Secret-named ``required_env`` of the adapters registered in the current profile scope.
- Tier 2 only: ``required_env`` is an unchecked setup list, so a plugin naming OPENAI_API_KEY
- must not strip it from credentialed children. No blanket fallback: a registry error surfaces
- instead of an empty (fail-open) set."""
- from gateway.platform_registry import platform_registry
- from hermes_cli.config import PLATFORM_SECRET_ENV_SUFFIXES
- return frozenset(n.upper() for n in platform_registry.required_env_names()
- if n.upper().endswith(PLATFORM_SECRET_ENV_SUFFIXES))
- def _registered_adapter_secret_env() -> frozenset:
- """Per-call adapter secrets: the bound profile's user-plugin declarations (Tier 1, see
- :func:`_home_adapter_secret_env`) plus the registered adapters' (Tier 2,
- :func:`_registry_adapter_secret_env`)."""
- return _registry_adapter_secret_env() | _home_adapter_secret_env()
- def _is_provider_env_blocklisted(name: str, _registered: frozenset | None = None) -> bool:
- """``name`` is a blocklisted provider/tool credential or adapter secret, matched the way the
- platform's environment resolves names: exact plus case-folded. On Windows the environment
- block is case-insensitive, so ``openai_api_key`` IS ``OPENAI_API_KEY``; consistent with
- ``_is_hermes_internal_secret``, which already folds (``key.upper()``). Loops pass
- ``_registered`` so the registry is read once per env, not once per key."""
- upper = name.upper()
- if name in _HERMES_PROVIDER_ENV_BLOCKLIST or upper in _HERMES_PROVIDER_ENV_BLOCKLIST:
- return True
- return upper in (_registered_adapter_secret_env() if _registered is None else _registered)
- # First-party platform credentials (``BUZZ_*``, driving the platform-mandated ``buzz``
- # CLI) carved out of the TERMINAL scrub only (``_make_run_env``,
- # ``_sanitize_subprocess_env``); execute_code, hermes_subprocess_env, docker and
- # env_passthrough registration stay sealed (GHSA-rhgp-j443-p4rf). CONTEXT-GATED via
- # ``_buzz_terminal_context_active``: a Telegram/CLI/cron session on a host that also
- # runs a Buzz gateway must not get the signing key. Values are used directly, never
- # scope-resolved (UnscopedSecretError under multiplex); the snapshot treats them as
- # profile-scoped. Prefix-based so future BUZZ_* names need no code change.
- # First-party platform credentials the agent's own platform adapters need in terminal children (e.g. the
- # ``BUZZ_*`` vars for the Buzz messaging platform, which drive the platform-mandated ``buzz`` CLI:
- # BUZZ_PRIVATE_KEY, BUZZ_AUTH_TAG, BUZZ_RELAY_URL, and the other BUZZ_* names). These are the agent's OWN
- # credentials — a Buzz community agent is expected to operate the ``buzz`` CLI — so they are carved out of
- # the terminal scrub. CONTEXT-GATED: the carve-out applies ONLY when this process/session is actually
- # operating as a Buzz agent — either the process is a Buzz-ACP managed agent (``BUZZ_MANAGED_AGENT`` is set,
- # only by Buzz Desktop's buzz-acp harness; see #76243 / #78511) or the current session's platform is
- # ``buzz`` (the gateway's ``HERMES_SESSION_PLATFORM`` ContextVar; concurrency safe under a multi-session
- # host). A Telegram/CLI/cron session on a host that also runs a Buzz gateway does NOT get BUZZ_PRIVATE_KEY
- # in its terminal children — blanket passthrough of a signing key to every terminal child on the host would
- # be wrong (maintainer triage note on #76243: don't expose the key to unrelated shell commands).
- # ``_sanitize_subprocess_env`` is also consumed by search workers (e.g. the ddgs web-search subprocess), the
- # computer-use driver binary, and user-script runners (bang ``!`` commands, quick commands, cron scripts,
- # webhook-filter scripts), so those children receive the vars too — matching the approved background/PTY
- # scope. Every other surface stays sealed — execute_code scrubbing, :func:`hermes_subprocess_env` (browser /
- # TUI host / ACP agent spawns), docker children, and ``env_passthrough`` registration (skills/config
- # still cannot register these names). The GHSA-rhgp-j443-p4rf seal is preserved because no registration path
- # is opened; this is a scrub-path exemption, not an allowlist addition. First-party matches use the merged
- # env value directly — they are the process's own env values and are never scope-resolved (a profile secret
- # scope under multiplex would otherwise raise UnscopedSecretError at passthrough-resolution call sites);
- # only skill/config passthrough names resolve through the profile secret scope. The snapshot mechanism
- # treats these names like profile-scoped passthrough names (see
- # ``LocalEnvironment._additional_profile_scoped_passthrough_names``) so they never persist in the shared
- # terminal snapshot across profiles. Contrast with CLAUDE_CODE_OAUTH_TOKEN above, which is discarded from
- # the blocklist entirely because it is NOT a Hermes credential; these ARE Hermes-managed first-party
- # platform credentials, so they stay IN the blocklist for every non-terminal surface. See issue #78026 (Buzz
- # agents could not use ``buzz`` from the terminal tool) and #76243 (Buzz Desktop managed agent wakes but
- # cannot reply).
- _TERMINAL_FIRST_PARTY_ENV_PREFIXES = ("BUZZ_",)
- def _matches_terminal_first_party_prefix(name: str) -> bool:
- """Pure name check (``BUZZ_*``), regardless of session context — the snapshot
- exclusion must stay conservative even when the carve-out is inactive.
- Case-folded: on Windows the env block is case-insensitive, so a
- lowercase-stored ``buzz_private_key`` IS the credential; it needs the
- carve-out (and the snapshot exclusion) just like the canonical name."""
- return name.upper().startswith(_TERMINAL_FIRST_PARTY_ENV_PREFIXES)
- def _buzz_terminal_context_active() -> bool:
- """True when this process/session operates as a Buzz agent: ``BUZZ_MANAGED_AGENT`` in
- the process env (set only by Buzz Desktop's buzz-acp harness), or the live session's
- platform is ``buzz`` via the gateway ContextVar — authoritative under a concurrent
- multi-session host, so a sibling Telegram session resolves its OWN platform.
- Gateway / CLI / cron / kanban processes never carry it. See #76243.
- """
- if os.environ.get("BUZZ_MANAGED_AGENT"):
- return True
- try:
- from gateway.session_context import get_session_env
- return get_session_env("HERMES_SESSION_PLATFORM", "").strip().lower() == "buzz"
- except Exception:
- return False
- def _is_terminal_first_party_env(name: str) -> bool:
- """``name`` is a first-party platform credential (``BUZZ_*``) AND the current
- process/session context entitles it to reach terminal children."""
- return _matches_terminal_first_party_prefix(name) and _buzz_terminal_context_active()
- # Active-venv markers that must NOT leak: VIRTUAL_ENV/CONDA_PREFIX make uv/poetry sync
- # ANOTHER project's deps into the Hermes venv (still reachable via PATH, so stripping
- # is safe); PYTHONHOME redirects a child interpreter's stdlib to the Hermes venv
- # (version-mismatch crashes). PYTHONPATH is handled separately (Hermes-owned entries only).
- # The gateway runs inside its own venv, so its process environment carries VIRTUAL_ENV (and possibly
- # CONDA_PREFIX). If those leak into commands the agent runs against OTHER Python projects, tools like
- # ``uv``/``poetry`` treat the inherited value as the active environment and build/sync that other project's
- # dependencies into the Hermes venv path instead of the project's own ``.venv`` — silently clobbering the
- # Hermes environment (e.g. a project pinned to a different Python version overwrites it and breaks the
- # gateway). PYTHONHOME is included because a gateway-inherited value redirects the standard-library search
- # of ANY child interpreter — including unrelated system/venv Pythons — to the Hermes venv's stdlib, which
- # crashes with version-mismatch errors before a child script even imports a package (#75018). Hermes itself
- # treats PYTHONHOME as contamination in its own child processes (managed_uv.py, sqlite_runtime.py), so
- # stripping it from subprocess envs is consistent. Users who need PYTHONHOME for a specific child can set it
- # explicitly in the command. PYTHONPATH is NOT included here — it's handled by
- # _strip_hermes_owned_pythonpath() which removes only Hermes-owned entries, preserving user-set paths.
- _ACTIVE_VENV_MARKER_VARS = ("VIRTUAL_ENV", "CONDA_PREFIX", "PYTHONHOME")
- # Identity a supervisor hands to the ONE process it started, which no child of that process owns.
- # HERMES_LAUNCHD_LABEL is the stderr-timestamp wrapper's re-export of its launchd job label for the
- # gateway it starts (#120087); HERMES_GATEWAY_EXTERNAL_SUPERVISOR is stamped into the gateway's own
- # environ by ``gateway run --external-supervisor``. Inherited, a gateway an agent starts from its
- # terminal answers ``launchd``/supervised and takes the exit-75 restart route no supervisor answers.
- # NOTIFY_SOCKET/WATCHDOG_* name the systemd unit's main process (sd_notify's unset_environment).
- # Kept on purpose: XPC_SERVICE_NAME (launchd's own per-process stamp, "0" below the wrapper, which
- # ``hermes update`` compares to tell it runs inside a job), INVOCATION_ID/JOURNAL_STREAM (systemd
- # sets them for every process of the unit, and the gateway-child dispatch reads them there), and the
- # launch markers HERMES_SUPERVISED_CHILD / HERMES_S6_SUPERVISED_CHILD (profile pinning, s6 redirect).
- _SUPERVISED_PROCESS_IDENTITY_VARS = (
- "HERMES_LAUNCHD_LABEL", "HERMES_GATEWAY_EXTERNAL_SUPERVISOR", "NOTIFY_SOCKET", "WATCHDOG_USEC", "WATCHDOG_PID",
- )
- # The supervisor markers are also profile pins: a CLI under them skips the sticky active_profile
- # (``hermes_cli.main._under_gateway_supervisor``, #74872), and HERMES_GATEWAY_EXTERNAL_SUPERVISOR is an
- # --external-supervisor gateway's ONLY one. A child that loses a marker keeps the pin under a name the
- # restart-route and launch-identity probes never read, so it stays on the gateway's profile without
- # claiming the supervisor.
- GATEWAY_PROFILE_PIN_ENV = "HERMES_GATEWAY_PROFILE_PIN"
- def carry_gateway_profile_pin(env: dict) -> dict:
- """Mark *env* with :data:`GATEWAY_PROFILE_PIN_ENV` while it still holds a pinning supervisor marker."""
- if env.get("HERMES_SUPERVISED_CHILD") or str(
- env.get("HERMES_GATEWAY_EXTERNAL_SUPERVISOR", "")).strip().lower() in {"1", "true", "yes", "on"}:
- env[GATEWAY_PROFILE_PIN_ENV] = "1"
- return env
- def strip_supervised_process_identity(env: dict) -> dict:
- """Drop :data:`_SUPERVISED_PROCESS_IDENTITY_VARS` from a child env, in place; returns *env*.
- The profile pin they carried survives (:func:`carry_gateway_profile_pin`)."""
- carry_gateway_profile_pin(env)
- for name in _SUPERVISED_PROCESS_IDENTITY_VARS:
- env.pop(name, None)
- return env
- def _is_hermes_internal_secret(key: str) -> bool:
- """True for Hermes-internal secrets injected under *dynamic* names the static
- blocklist cannot enumerate: ``AUXILIARY_<TASK>_API_KEY``/``_BASE_URL`` (per-task
- side-LLM credentials) and ``GATEWAY_RELAY_*_SECRET``/``_KEY``/``_TOKEN`` (relay
- auth; non-secret routing hints stay visible). Stripped on every spawn path
- regardless of env_passthrough registration or ``inherit_credentials``."""
- upper = key.upper()
- if upper.startswith("AUXILIARY_") and upper.endswith(("_API_KEY", "_BASE_URL")):
- return True
- return upper.startswith("GATEWAY_RELAY_") and upper.endswith(("_SECRET", "_KEY", "_TOKEN"))
- # Authorization gates: the env names platform adapters read to decide WHO may talk to the
- # agent (allow/deny lists, allow-all opt-ins, bot policy, channel scoping). Not credentials, so
- # no secret scrub touches them, and most profiles' ``.env`` files do not define them, so the
- # child's own dotenv load never overwrites an inherited value: a child spawned FOR profile B
- # from a process that loaded profile A's gates (or a unit-file ``Environment=``) would enforce
- # A's channel/user/role list as its own (#113270). The suffix is matched by shape so a gate
- # added to any adapter is covered without a second edit, but ONLY under a platform prefix
- # (``DISCORD_``, ``GATEWAY_``, a plugin adapter's name): an operator's own ``DEMO_ALLOWED_SENDER``
- # is script data, not a Hermes gate, and deleting it by name shape broke routed ``no_agent``
- # cron scripts (#119539). ``HERMES_*`` never counts (``HERMES_MEDIA_ALLOW_DIRS``,
- # ``HERMES_ALLOW_PRIVATE_URLS`` are process settings, not adapter gates).
- _PROFILE_GATE_ENV_MARKERS = (
- "_ALLOWED_", "_ALLOW_ALL_", "_ALLOW_FROM", "_ALLOW_BOTS", "_ALLOW_PUBLIC_", "_IGNORED_CHANNELS",
- "_NO_THREAD_CHANNELS", "_FREE_RESPONSE_CHANNELS", "_BACKFILL_CHANNELS", "_GROUP_ALLOWED",
- )
- # Gate owners that are not a ``Platform`` value: the cross-platform pairing gate and adapters whose
- # env prefix differs from their platform name (``qqbot`` reads ``QQ_*``).
- _EXTRA_GATE_ENV_PREFIXES = frozenset({"GATEWAY", "QQ"})
- @functools.lru_cache(maxsize=1)
- def _static_gate_env_prefixes() -> frozenset:
- """Built-in ``Platform`` values plus bundled platform plugins (directory names and manifest
- aliases) — fixed for the life of the process, so scanned once."""
- names = set(_EXTRA_GATE_ENV_PREFIXES)
- try:
- from gateway.config import Platform
- names.update(m.value for m in Platform.__members__.values())
- bundled, aliases = Platform._scan_bundled_plugin_platforms()
- names.update(bundled)
- names.update(aliases)
- except Exception:
- pass
- return frozenset(str(n).upper().replace("-", "_") for n in names if n)
- def _platform_gate_env_prefixes() -> frozenset:
- """Upper-cased owners of authorization gates: the static set plus runtime-registered plugin
- adapters (``platform_registry``, dynamic and profile-scoped, so read per call — a cheap set
- union under the registry lock)."""
- names = set(_static_gate_env_prefixes())
- try:
- from gateway.platform_registry import platform_registry
- names.update(str(n).upper().replace("-", "_") for n in platform_registry.registered_names() if n)
- except Exception:
- pass
- return frozenset(names)
- def is_profile_gate_env(name: str, _prefixes: Optional[frozenset] = None) -> bool:
- """True for a platform authorization gate (``DISCORD_ALLOWED_CHANNELS``, ``TELEGRAM_ALLOW_ALL_USERS``,
- ``GATEWAY_ALLOWED_USERS``, ``WHATSAPP_GROUP_ALLOW_FROM`` ...) — profile-scoped policy a child acting
- for ANOTHER profile must never inherit. A gate is a platform prefix AND a gate-shaped suffix;
- an operator variable that merely contains ``_ALLOWED_`` is not one."""
- upper = name.upper()
- if upper.startswith("HERMES_") or upper.startswith("_"):
- return False
- if not any(marker in upper for marker in _PROFILE_GATE_ENV_MARKERS):
- return False
- prefixes = _prefixes if _prefixes is not None else _platform_gate_env_prefixes()
- return any(upper.startswith(prefix + "_") for prefix in prefixes)
- def strip_profile_gate_env(env: dict) -> dict:
- """Drop every authorization gate from *env* in place (see :func:`is_profile_gate_env`)."""
- prefixes = _platform_gate_env_prefixes()
- for key in [k for k in env if is_profile_gate_env(k, prefixes)]:
- del env[key]
- return env
- def _plugin_terminal_env_strip_keys() -> frozenset:
- """Credential env keys owned by plugin-registered terminal backends (Tier-1:
- stripped from every spawned subprocess). Computed at call time because plugins
- register after import; fail-soft to empty."""
- try:
- from agent.terminal_env_registry import plugin_strip_env_keys
- return plugin_strip_env_keys()
- except Exception:
- return frozenset()
- # Tier-1 secrets: stripped from EVERY spawned subprocess even under inherit_credentials
- # (claude/codex/gemini). Not provider credentials — no child needs them and they are the
- # highest-value secrets to keep from a compromised dependency. Provider keys = Tier 2.
- _ALWAYS_STRIP_KEYS: frozenset[str] = frozenset({
- # GitHub auth
- "GH_TOKEN", "GITHUB_TOKEN", "GITHUB_APP_ID", "GITHUB_APP_PRIVATE_KEY_PATH",
- "GITHUB_APP_INSTALLATION_ID",
- # Gateway / messaging bot tokens and access control
- "TELEGRAM_BOT_TOKEN", "DISCORD_BOT_TOKEN", "SLACK_BOT_TOKEN", "SLACK_APP_TOKEN",
- "SLACK_SIGNING_SECRET", "GATEWAY_ALLOWED_USERS", "GATEWAY_ALLOW_ALL_USERS",
- # Gateway relay auth triplet. _SECRET/_DELIVERY_KEY are also matched by
- # _is_hermes_internal_secret, but _ID has no secret suffix, so it must be
- # enumerated here to stay stripped on the inherit_credentials=True path.
- "GATEWAY_RELAY_ID", "GATEWAY_RELAY_SECRET", "GATEWAY_RELAY_DELIVERY_KEY",
- "EMAIL_PASSWORD", "HERMES_DASHBOARD_SESSION_TOKEN",
- # Dashboard auth: the basic-auth password and session-signing secret, the OIDC client
- # secret and the drain bearer. They let a holder mint or forge dashboard sessions, and no
- # child (credentialed CLIs included) consumes them.
- "HERMES_DASHBOARD_BASIC_AUTH_PASSWORD", "HERMES_DASHBOARD_BASIC_AUTH_SECRET",
- "HERMES_DASHBOARD_OIDC_CLIENT_SECRET", "HERMES_DASHBOARD_DRAIN_SECRET",
- # Remote-compute / infrastructure secrets
- "MODAL_TOKEN_ID", "MODAL_TOKEN_SECRET", "DAYTONA_API_KEY",
- }) | _ADAPTER_SECRET_ENV | _left_core_env(secrets_only=True) # every declared adapter secret is Tier 1
- _ALWAYS_STRIP_FOLDED: frozenset[str] = frozenset(k.upper() for k in _ALWAYS_STRIP_KEYS)
|