hermes-agent/tests/hermes_cli/test_audio_router_scope.py
beardthelion 0cbe552888 fix(scope): failed profile-scoped secret reads must not borrow os.environ
Eight secret readers wrapped the scoped get_secret() call in a broad
except Exception / contextlib.suppress and fell through to os.environ.
Under multiplex that env holds the default profile's value, so a bound
scope whose resolution fails silently borrowed another profile's
credential: the pairing allowlist reader could then persist the foreign
list into the served profile's .env, and the proxy key, tool gateway
token, OpenRouter and aux provider keys, ElevenLabs key, and Slack token
probe had the same shape.

Keep the deliberate UnscopedSecretError -> os.environ fallback (the
unscoped default-profile path legitimately reads its own env) and let
every other scoped-read failure propagate; the two availability probes
fail closed instead. config._scoped_environ_get now propagates as its
docstring already claimed.
2026-09-23 06:46:02 -07:00

58 lines
2 KiB
Python

"""Scope-isolation regression for the ElevenLabs voices route's env fallback.
``get_elevenlabs_voices`` reads ``ELEVENLABS_API_KEY`` through the secret scope
when ``.env`` has no key. A bound scope whose read fails must leave the route
unavailable -- never borrow the ambient ``os.environ`` key (another profile's
under multiplex). Only the unscoped default-profile path (UnscopedSecretError)
may read the env.
"""
import pytest
@pytest.fixture(autouse=True)
def _restore_process_scope_state():
"""``_config_profile_scope``/``launch_secret_scope`` can freeze the launch-env
snapshot (one-way process state); restore it so sibling suites are unaffected."""
import agent.secret_scope as secret_scope
from tui_gateway import launch_profile_policy
was_active = secret_scope.is_multiplex_active()
snapshot = launch_profile_policy._snapshot
try:
yield
finally:
secret_scope.set_multiplex_active(was_active)
launch_profile_policy._snapshot = snapshot
class _ExplodingScope(dict):
"""A bound secret scope whose resolution fails (resolver/backend error)."""
def get(self, name, default=None):
raise RuntimeError("resolver boom")
@pytest.mark.asyncio
async def test_voices_route_scope_failure_never_borrows_env(tmp_path, monkeypatch):
from agent import secret_scope as ss
from hermes_cli.web_routers.audio import get_elevenlabs_voices
monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
(tmp_path / ".hermes").mkdir()
monkeypatch.setenv("ELEVENLABS_API_KEY", "sk-foreign-profile")
def _urlopen(*a, **k):
raise AssertionError("network must not be reached without a key")
monkeypatch.setattr("urllib.request.urlopen", _urlopen)
was_active = ss.is_multiplex_active()
ss.set_multiplex_active(True)
token = ss.set_secret_scope(_ExplodingScope())
try:
result = await get_elevenlabs_voices()
finally:
ss.reset_secret_scope(token)
ss.set_multiplex_active(was_active)
assert result == {"available": False, "voices": []}