Skip to content

Commit 5725efa

Browse files
nftpoetristteknium1
authored andcommitted
fix(gateway): scope resolve_proxy_url()'s platform_env_var read by multiplex profile
resolve_proxy_url() read platform_env_var (TELEGRAM_PROXY, DISCORD_PROXY, MATTERMOST_PROXY, MATRIX_PROXY) via raw os.environ.get() — a shared chokepoint used by 7+ adapters. Under a secondary multiplex profile, os.environ holds the default profile's YAML-to-env bridge output, so a secondary profile with its own (different or absent) proxy config would silently borrow the default profile's proxy, or vice versa — and proxy URLs can embed credentials (http://user:pass@host). Fix: read platform_env_var through gateway.platforms._shared's get_scoped_secret(), which is scope-aware (profile's own scope first, falling back to os.environ only when unscoped/default-profile) and already used by 10+ other adapters for the same class of setting. The generic HTTPS_PROXY/HTTP_PROXY/ALL_PROXY fallback stays a raw env read — those are OS/system-level network settings, not a per-profile Hermes concept. This closes a gap PR #100448 explicitly deferred ("proxy_url is deliberately NOT scoped here ... left for a dedicated fix to that shared helper").
1 parent 0f1668e commit 5725efa

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

‎gateway/platforms/base.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,16 @@ def resolve_proxy_url(
334334
target_hosts: str | list[str] | tuple[str, ...] | set[str] | None = None) -> str | None:
335335
"""Proxy URL: *platform_env_var* (e.g. ``DISCORD_PROXY``) first, then HTTPS_PROXY /
336336
HTTP_PROXY / ALL_PROXY (any case), then the macOS system proxy — the latter two only when
337-
``gateway.trust_env`` is true. None when nothing is found or NO_PROXY matches a target."""
338-
value = (os.environ.get(platform_env_var) or "").strip() if platform_env_var else ""
337+
``gateway.trust_env`` is true. None when nothing is found or NO_PROXY matches a target.
338+
339+
*platform_env_var* is a per-adapter, per-profile-configurable setting (each proxy URL can
340+
embed credentials, e.g. ``http://user:pass@host``) so it is read scope-aware: under a
341+
secondary multiplex profile it comes from that profile's own ``.env``, not the shared
342+
process env another profile's ``TELEGRAM_PROXY``/``DISCORD_PROXY``/etc. may hold. The
343+
generic ``HTTPS_PROXY``/``HTTP_PROXY``/``ALL_PROXY`` fallback stays a raw process-env read —
344+
those are OS/system-level network settings, not a per-profile Hermes concept."""
345+
from gateway.platforms._shared import get_scoped_secret as _get_scoped_proxy_var
346+
value = (_get_scoped_proxy_var(platform_env_var, "") or "").strip() if platform_env_var else ""
339347
if not value:
340348
if not gateway_trust_env(): # only the explicit per-platform var is honored
341349
return None

‎tests/gateway/test_gateway_trust_env.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,51 @@ def test_gateway_trust_env_reads_config(tmp_path, monkeypatch, yaml_body, expect
3636
assert gw_base.resolve_proxy_url("X_PLATFORM_PROXY") == "http://127.0.0.1:1080"
3737

3838

39+
class TestResolveProxyUrlMultiplexScope:
40+
"""A secondary multiplex profile's own TELEGRAM_PROXY/DISCORD_PROXY/etc. must gate its
41+
adapter, not the default profile's YAML-to-env bridge output sitting in the shared
42+
process's os.environ (the #72348 class, applied to this shared chokepoint used by
43+
Telegram, Discord, Mattermost, Matrix, SMS, and Slack)."""
44+
45+
def test_scoped_profile_uses_its_own_value(self, monkeypatch):
46+
from agent.secret_scope import reset_secret_scope, set_multiplex_active, set_secret_scope
47+
48+
monkeypatch.setenv("DISCORD_PROXY", "http://default-profile-proxy:8080")
49+
monkeypatch.delenv("NO_PROXY", raising=False)
50+
monkeypatch.delenv("no_proxy", raising=False)
51+
52+
set_multiplex_active(True)
53+
token = set_secret_scope({"DISCORD_PROXY": "http://secondary-profile-proxy:9090"})
54+
try:
55+
assert gw_base.resolve_proxy_url("DISCORD_PROXY") == "http://secondary-profile-proxy:9090"
56+
finally:
57+
reset_secret_scope(token)
58+
set_multiplex_active(False)
59+
60+
def test_scoped_profile_without_own_value_does_not_borrow_default(self, monkeypatch):
61+
from agent.secret_scope import reset_secret_scope, set_multiplex_active, set_secret_scope
62+
63+
monkeypatch.setenv("DISCORD_PROXY", "http://default-profile-proxy:8080")
64+
monkeypatch.delenv("NO_PROXY", raising=False)
65+
monkeypatch.delenv("no_proxy", raising=False)
66+
67+
set_multiplex_active(True)
68+
token = set_secret_scope({})
69+
try:
70+
assert gw_base.resolve_proxy_url("DISCORD_PROXY") is None
71+
finally:
72+
reset_secret_scope(token)
73+
set_multiplex_active(False)
74+
75+
def test_unscoped_default_profile_still_reads_env(self, monkeypatch):
76+
"""Control: outside multiplex (or the default profile), the legacy env read is
77+
unchanged."""
78+
monkeypatch.setenv("DISCORD_PROXY", "http://default-profile-proxy:8080")
79+
monkeypatch.delenv("NO_PROXY", raising=False)
80+
monkeypatch.delenv("no_proxy", raising=False)
81+
assert gw_base.resolve_proxy_url("DISCORD_PROXY") == "http://default-profile-proxy:8080"
82+
83+
3984
def test_no_bare_trust_env_literal_in_adapters():
4085
"""Every aiohttp session in gateway/ + plugins/platforms/ must go through gateway_trust_env()."""
4186
bare = re.compile(r"trust_env\s*=\s*(True|False)\b")

0 commit comments

Comments
 (0)