The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: redact and recover credential-bearing terminal projections
Teknium478d9b7379dc
diff --git a/gateway/run_turn_runner.py b/gateway/run_turn_runner.pyindex 6ec32f7dcf3..810e8c85c48 100644--- a/gateway/run_turn_runner.py+++ b/gateway/run_turn_runner.py@@ -769,7 +769,8 @@ class TurnRunner(GatewayTurnProgressMixin, GatewaySessionAgentMixin):def run_sync(self):from gateway.session_policy import policy_for_source, policy_scope- with policy_scope(policy_for_source(self._runner, self._ctx.source)):+ with policy_scope(policy_for_source(self._runner, self._ctx.source),+ authority=getattr(self._runner, "session_authority", None)):return self._run_sync_scoped()def _run_sync_scoped(self):diff --git a/gateway/session_policy.py b/gateway/session_policy.pyindex d1a96dc2620..bd4f9add343 100644--- a/gateway/session_policy.py+++ b/gateway/session_policy.py@@ -30,6 +30,8 @@ class LocalSessionPolicy:from gateway.session_policy_credentials import recover_config_secretssecrets = recover_config_secrets(authority, self)for path, value in secrets.items():+ if path[0] is None: # private terminal projection, not config+ continuetarget = configfor key in path[:-1]:target = target[key]@@ -128,6 +130,7 @@ def build_policy(params, config, *, private_secrets=None):request = {k: v for k, v in params.items() if k not in {'request_id', 'api_key'}}request.setdefault('source', 'cli')_extract_config_secrets(config, private_secrets)+ _extract_config_secrets(terminal, private_secrets, (None,))return LocalSessionPolicy(source, SURFACES[source], cwd, model, tuple(sorted(enabled)),json.dumps(config), json.dumps(request, sort_keys=True), json.dumps(terminal))@@ -136,10 +139,15 @@ def _extract_config_secrets(value, private, path=()):# Reuse the configuration owner's structural classification; opaque keys need# not match a vendor prefix. Only the authority keeps their original values.from hermes_cli.config import _SECRET_CONFIG_KEYS+ from agent.credential_persistence import _is_secret_payload_key+ containers = {'env', 'headers', 'extra_headers', 'docker_env', 'docker_extra_args',+ 'terminal_docker_env', 'terminal_docker_extra_args'}items = value.items() if isinstance(value, dict) else enumerate(value) if isinstance(value, list) else ()for key, child in items:child_path = path + (key,)- if isinstance(key, str) and key.lower() in _SECRET_CONFIG_KEYS and isinstance(child, str) and child:+ sensitive = isinstance(key, str) and (key.lower() in _SECRET_CONFIG_KEYS+ or key.lower() in containers or _is_secret_payload_key(key))+ if sensitive and child and child not in ('{}', '[]'):if private is None:raise RuntimeStoreError('launch_credentials_unavailable')private[child_path] = child@@ -230,14 +238,20 @@ def policy_for_source(runner, source):@contextmanager-def policy_scope(policy):+def policy_scope(policy, *, authority=None):if policy is None:yieldreturnfrom agent.runtime_cwd import set_session_cwdfrom tools.terminal_scope import set_terminal_scope, reset_terminal_scope+ terminal = json.loads(policy.terminal_json)+ if policy.config_secret_ref is not None:+ from gateway.session_policy_credentials import recover_config_secrets+ for path, value in recover_config_secrets(authority, policy).items():+ if path[0] is None:+ terminal[path[1]] = valuecwd_token = set_session_cwd(policy.cwd)- terminal_token = set_terminal_scope(json.loads(policy.terminal_json))+ terminal_token = set_terminal_scope(terminal)try:yieldfinally:diff --git a/gateway/session_policy_credentials.py b/gateway/session_policy_credentials.pyindex 338b5ffe466..d18236d10f0 100644--- a/gateway/session_policy_credentials.py+++ b/gateway/session_policy_credentials.py@@ -18,11 +18,15 @@ def policy_identity(policy):return hashlib.sha256(json.dumps(data, sort_keys=True).encode()).hexdigest()+def _fingerprint(value):+ return fingerprint_secret_value(value if isinstance(value, str) else json.dumps(value, sort_keys=True))++def config_reference(authority, session_id, policy, secrets):home = Path(authority.db.db_path).resolve().parentreturn PREFIX + json.dumps({'home': str(home), 'profile': authority.profile_id,'session': session_id, 'policy': policy_identity(policy),- 'entries': [[list(path), fingerprint_secret_value(value)] for path, value in secrets.items()]},+ 'entries': [[list(path), _fingerprint(value)] for path, value in secrets.items()]},sort_keys=True)@@ -47,9 +51,14 @@ def recover_config_secrets(authority, policy):values = {}for path, fingerprint in source['entries']:value = config- for key in path:+ lookup = path+ if path[0] is None:+ from tools.terminal_scope import build_profile_terminal_scope+ value = build_profile_terminal_scope(home)+ lookup = path[1:]+ for key in lookup:value = value[key]- actual = fingerprint_secret_value(value)+ actual = _fingerprint(value)if actual is None or not hmac.compare_digest(actual, fingerprint):raise ValueError('credential changed')values[tuple(path)] = valuediff --git a/tests/gateway/test_policy_secret_projections.py b/tests/gateway/test_policy_secret_projections.pynew file mode 100644index 00000000000..0ecf7febde1--- /dev/null+++ b/tests/gateway/test_policy_secret_projections.py@@ -0,0 +1,25 @@+import json+from dataclasses import asdict+from types import SimpleNamespace+++def test_terminal_and_config_secret_projections_recover_without_durable_values(tmp_path, monkeypatch):+ from gateway.session_policy import build_policy, bind_launch_key, policy_scope+ from tools.terminal_scope import terminal_env+ monkeypatch.setenv('HERMES_HOME', str(tmp_path))+ secret = 'opaque-terminal-header-secret'+ cfg = {'terminal': {'docker_env': {'UNCLASSIFIED': secret}},+ 'providers': {'fixture': {'extra_headers': {'X-Custom-Auth': secret}}}}+ (tmp_path / 'config.yaml').write_text(json.dumps(cfg))+ (tmp_path / '.env').write_text('TERMINAL_SERVICE_TOKEN=' + secret + '\n')+ private = {}+ policy = build_policy({'cwd': str(tmp_path), 'model': 'frozen'}, cfg, private_secrets=private)+ def owner():+ return SimpleNamespace(db=SimpleNamespace(db_path=tmp_path / 'state.db'),+ profile_id='owned', instance_id='instance', epoch=1)+ policy = bind_launch_key(owner(), 'session', policy, None, config_secrets=private)+ assert secret not in json.dumps(asdict(policy))+ assert policy.config(owner()) == cfg+ with policy_scope(policy, authority=owner()):+ assert json.loads(terminal_env('TERMINAL_DOCKER_ENV'))['UNCLASSIFIED'] == secret+ assert terminal_env('TERMINAL_SERVICE_TOKEN') == secret