diff --git a/gateway/session_prompt_hooks.py b/gateway/session_prompt_hooks.py index f8633dbe61..8aaa75186f 100644 --- a/gateway/session_prompt_hooks.py +++ b/gateway/session_prompt_hooks.py @@ -11,7 +11,8 @@ Observers only: return values are ignored and a failing plugin never affects the order on one worker thread, so plugin code never runs under ``event_stream.lock`` or ``session_server_requests._lock``, on the agent thread or on the event loop; each runs in the owning profile's scope (``session_authorities.owner_scope``). ``params`` is a deep copy with every string -force-redacted (as ``tools/human_input_hooks.py`` redacts its ``prompt``) and never holds an answer. +force-redacted (as ``tools/human_input_hooks.py`` redacts its ``prompt``) inside that owner scope, so +the owning profile's registered vault values are scrubbed too, and never holds an answer. """ from __future__ import annotations @@ -137,13 +138,13 @@ def _drain() -> None: def _dispatch(hook_name, authority, payload) -> None: from hermes_cli.config_backend import ConfigBackendUnavailable try: - if "params" in payload: - payload = {**payload, "params": _redacted(payload["params"])} scope = contextlib.nullcontext() if authority is not None: from gateway.session_authorities import owner_scope scope = owner_scope(authority) with scope: + if "params" in payload: # in the owner's scope: its registered vault values are scrubbed too + payload = {**payload, "params": _redacted(payload["params"])} from hermes_cli.lifecycle import invoke_hook invoke_hook(hook_name, **payload) except (Exception, ConfigBackendUnavailable): # health: allow BLE001 -- observers never break a prompt diff --git a/tests/gateway/test_session_prompt_hooks_scope.py b/tests/gateway/test_session_prompt_hooks_scope.py index 57712b83db..7cdd376f71 100644 --- a/tests/gateway/test_session_prompt_hooks_scope.py +++ b/tests/gateway/test_session_prompt_hooks_scope.py @@ -136,3 +136,32 @@ def test_without_a_gateway_api_a_request_frame_runs_in_the_askers_scope(profiles asker.join(10) beta.plugin.wait(SETTLED) assert profiles.alpha.plugin.calls == [] + + +@pytest.mark.parametrize("asked_from", ["unbound", "other profile"]) +def test_params_are_scrubbed_of_the_owning_profiles_vault_values(profiles, monkeypatch, asked_from): + """A password-manager value registered for the owning profile only never reaches its plugins, even + when the prompt is asked from a thread bound to another profile or to none.""" + from agent.redact import clear_vault_redaction_values, redact_approval_display, register_vault_redaction_value + from gateway import run_api + from gateway.session_server_requests import ask_viewer, resolve + monkeypatch.setattr(run_api, "_ACTIVE_RUNNER", profiles.runner) # the gateway API is up + beta, secret = profiles.beta, "orchard violet river lantern" + home = None if asked_from == "unbound" else profiles.alpha.home + _in_scope(beta.home, lambda: register_vault_redaction_value(secret)) + try: + assert secret in _in_scope(home, lambda: redact_approval_display(secret, force=True)) # not the asker's value + beta.live.event_stream.fanout.attach(_Peer()) + params = {"site": "example", "note": f"save {secret}?", "fields": [{"value": secret}]} + out = {} + asker = threading.Thread(target=lambda: out.setdefault("value", _in_scope( + home, lambda: ask_viewer(beta.live.event_stream, "s", "vault.save_login", params)))) + asker.start() + [opened] = beta.plugin.wait(OPENED) + assert opened["home"] == beta.home and opened["params"]["site"] == "example" + assert secret not in repr(opened["params"]) + assert resolve({"jsonrpc": "2.0", "id": opened["prompt_id"], "result": {"value": "yes"}}) + asker.join(10) + assert out["value"] == "yes" + finally: + _in_scope(beta.home, clear_vault_redaction_values)