Skip to content

The agent that grows with you

fix(terminal-env): strip adapter secrets and OAuth-profile keys from child envs

John Paul Soliva66019e5597e8
diff --git a/tests/tools/test_local_env_blocklist.py b/tests/tools/test_local_env_blocklist.py
index 97f7d9a1496..44a9f588946 100644
--- a/tests/tools/test_local_env_blocklist.py
+++ b/tests/tools/test_local_env_blocklist.py
@@ -91,6 +91,37 @@ def test_terminal_child_observes_declared_policy(child_env, monkeypatch):
assert dict(os.environ) == before
+# Secrets adapters read straight from the environment (webhook signing secrets, access tokens,
+# app secrets), built-in and plugin adapters alike, none of them listed in OPTIONAL_ENV_VARS.
+ADAPTER_SECRETS = """
+WHATSAPP_CLOUD_ACCESS_TOKEN WHATSAPP_CLOUD_APP_SECRET WHATSAPP_CLOUD_VERIFY_TOKEN WEIXIN_TOKEN
+YUANBAO_APP_SECRET FEISHU_ENCRYPT_KEY FEISHU_VERIFICATION_TOKEN TELEGRAM_WEBHOOK_SECRET
+PHOTON_SIDECAR_TOKEN RAFT_CHANNEL_TOKEN MSGRAPH_WEBHOOK_CLIENT_STATE MSGRAPH_CLIENT_SECRET
+""".split()
+
+
[email protected]("builder", ["foreground", "background", "nonterminal"])
+def test_adapter_and_provider_profile_secrets_never_reach_children(child_env, monkeypatch, builder):
+ from providers import list_providers
+ from tools.env_passthrough import is_env_passthrough, register_env_passthrough
+ secrets = set(ADAPTER_SECRETS)
+ secrets.update(name for profile in list_providers() for name in (profile.env_vars or ()))
+ secrets.discard("CLAUDE_CODE_OAUTH_TOKEN") # operator's subscription, not Hermes inference
+ operator = ["MY_APP_KEY", "DEPLOY_WEBHOOK_SECRET"] # secret-shaped, but no adapter owns them
+ for name in [*secrets, *operator]:
+ monkeypatch.setenv(name, "fake-" + name)
+ factories = {
+ "foreground": lambda: local._make_run_env({}),
+ "background": lambda: local._sanitize_subprocess_env(dict(os.environ)),
+ "nonterminal": local.hermes_subprocess_env,
+ }
+ observed = observe_child(factories[builder](), sorted(secrets | set(operator)))
+ assert observed == {**dict.fromkeys(secrets), **{k: "fake-" + k for k in operator}}
+ # Skill passthrough is what forwards a name into docker/ssh/modal and execute_code children.
+ register_env_passthrough(sorted(secrets))
+ assert not any(is_env_passthrough(name) for name in secrets)
+
+
@pytest.mark.parametrize("builder", ["foreground", "background", "factory", "nonterminal"])
def test_builders_strip_runtime_markers_and_owned_paths(child_env, monkeypatch, builder):
repo, site = Path(__file__).resolve().parents[2], _running_site()
diff --git a/tools/environments/local_env_policy.py b/tools/environments/local_env_policy.py
index c85aa79b735..940f29739f9 100644
--- a/tools/environments/local_env_policy.py
+++ b/tools/environments/local_env_policy.py
@@ -40,6 +40,9 @@ _STATIC_PROVIDER_ENV_BLOCKLIST = frozenset({
"DAYTONA_API_KEY", "GATEWAY_RELAY_ID", "GATEWAY_RELAY_SECRET",
"GATEWAY_RELAY_DELIVERY_KEY", "VERCEL_OIDC_TOKEN", "VERCEL_TOKEN",
"VERCEL_PROJECT_ID", "VERCEL_TEAM_ID",
+ # Microsoft Graph app secret and webhook clientState: read in code only, and no
+ # adapter prefix owns them for the shape rule (``_is_platform_secret_env``).
+ "MSGRAPH_CLIENT_SECRET", "MSGRAPH_WEBHOOK_CLIENT_STATE",
})
@@ -56,6 +59,14 @@ def _build_provider_env_blocklist() -> frozenset:
blocked.add(pconfig.base_url_env_var)
except ImportError:
pass
+ try:
+ # The registry mirror copies env_vars only for api_key profiles, but OAuth
+ # profiles (nous, qwen-oauth) also accept a pasted key under theirs.
+ from providers import list_providers
+ for profile in list_providers():
+ blocked.update(profile.env_vars or ())
+ except ImportError:
+ pass
try:
from hermes_cli.config import OPTIONAL_ENV_VARS
for name, metadata in OPTIONAL_ENV_VARS.items():
@@ -90,8 +101,21 @@ def _is_provider_env_blocklisted(name: str) -> bool:
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()``)."""
- return (name in _HERMES_PROVIDER_ENV_BLOCKLIST
- or name.upper() in _HERMES_PROVIDER_ENV_BLOCKLIST)
+ upper = name.upper()
+ return (name in _HERMES_PROVIDER_ENV_BLOCKLIST or upper in _HERMES_PROVIDER_ENV_BLOCKLIST
+ or _is_platform_secret_env(upper))
+
+
+# Adapters read many secrets straight from the environment without listing them in
+# OPTIONAL_ENV_VARS (TELEGRAM_WEBHOOK_SECRET, WHATSAPP_CLOUD_APP_SECRET, WEIXIN_TOKEN, ...), so
+# the list above never sees them. Match them by shape, like the authorization gates below: an
+# adapter prefix plus a secret suffix, so a new adapter secret needs no second edit.
+_PLATFORM_SECRET_ENV_SUFFIXES = ("_TOKEN", "_SECRET", "_PASSWORD", "_KEY")
+
+
+def _is_platform_secret_env(upper: str) -> bool:
+ return upper.endswith(_PLATFORM_SECRET_ENV_SUFFIXES) and any(
+ upper.startswith(prefix + "_") for prefix in _static_gate_env_prefixes())
# First-party platform credentials (``BUZZ_*``, driving the platform-mandated ``buzz``
# CLI) carved out of the TERMINAL scrub only (``_make_run_env``,