The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(url_safety): trim the fake-ip salvage to the existing cache shape and two invariants
teknium1e4c00097b41a
diff --git a/contributors/emails/[email protected] b/contributors/emails/[email protected]new file mode 100644index 00000000000..04e876904d8--- /dev/null+++ b/contributors/emails/[email protected]@@ -0,0 +1 @@+AYin-Zdiff --git a/tests/tools/test_url_safety.py b/tests/tools/test_url_safety.pyindex a996f2af136..1630452c011 100644--- a/tests/tools/test_url_safety.py+++ b/tests/tools/test_url_safety.py@@ -19,7 +19,6 @@ from tools.url_safety import (_is_blocked_ip,_global_allow_private_urls,_reset_allow_private_cache,- _reset_fake_ip_cache,)import ipaddress@@ -515,26 +514,23 @@ class TestDeclaredFakeIpSentinelRanges:"hermes_cli.config.read_raw_config",lambda: {"security": {"fake_ip_ranges": ["198.18.0.0/15"]}},)- _reset_fake_ip_cache()+ _reset_allow_private_cache()yield- _reset_fake_ip_cache()-- def test_undeclared_host_is_unaffected(self):- with _resolves_to("198.18.0.23"):- assert is_safe_url("https://example.com/file.jpg") is False+ _reset_allow_private_cache()- def test_declared_sentinel_is_dialable_with_private_blocking_on(self, declared):+ def test_declared_sentinel_is_dialable_pre_flight_and_at_connect_time(self, declared):with _resolves_to("198.18.1.125"):assert is_safe_url("https://example.com/") is True-- def test_declared_sentinel_passes_the_connect_time_check_too(self, declared):with _resolves_to("198.18.0.55"):assert _resolved_http_connect_ips("example.com", 443, "https") == ["198.18.0.55"]- def test_declaration_does_not_excuse_real_private_answers(self, declared):+ def test_declaration_excuses_only_the_declared_block(self, declared):+ # Real private answers and the cloud-metadata floor stay blocked under the declaration...with _resolves_to("192.168.99.99"):assert is_safe_url("https://example.com/") is False-- def test_metadata_floor_outranks_the_declaration(self, declared):with _resolves_to("169.254.169.254"):assert is_safe_url("http://example.com/") is False+ # ...and the sentinel block itself is blocked again once the declaration is gone.+ _reset_allow_private_cache()+ with patch("hermes_cli.config.read_raw_config", lambda: {}), _resolves_to("198.18.0.23"):+ assert is_safe_url("https://example.com/file.jpg") is Falsediff --git a/tools/url_safety.py b/tools/url_safety.pyindex 094f4a432d1..c54326b2b2a 100644--- a/tools/url_safety.py+++ b/tools/url_safety.py@@ -159,9 +159,10 @@ def _resolve_allow_private_urls() -> bool:def _reset_allow_private_cache() -> None:- """Reset the cached toggle — only for tests."""- global _allow_private_resolved, _cached_allow_private- _allow_private_resolved = _cached_allow_private = False+ """Reset the cached toggle and the cached fake-ip ranges — only for tests."""+ global _allow_private_resolved, _cached_allow_private, _fake_ip_resolved, _cached_fake_ip_ranges+ _allow_private_resolved = _cached_allow_private = _fake_ip_resolved = False+ _cached_fake_ip_ranges = ()def _resolve_fake_ip_ranges() -> tuple:@@ -177,37 +178,29 @@ def _resolve_fake_ip_ranges() -> tuple:from hermes_cli.config import read_raw_configblock = read_raw_config().get("security", {})raw = block.get("fake_ip_ranges") if isinstance(block, dict) else None- if isinstance(raw, str):- raw = [part.strip() for part in raw.split(",")]- if not isinstance(raw, (list, tuple)):- return ()- networks = []- for entry in raw:- if not str(entry).strip():- continue- try:- networks.append(ipaddress.ip_network(str(entry).strip(), strict=False))- except ValueError:- logger.warning("Ignoring unparseable security.fake_ip_ranges entry: %r", entry)- return tuple(networks)except Exception:return () # config unavailable (tests, early import) — keep the secure default+ entries = [raw] if isinstance(raw, str) else raw if isinstance(raw, (list, tuple)) else ()+ networks = []+ for entry in entries:+ try:+ networks.append(ipaddress.ip_network(str(entry).strip(), strict=False))+ except ValueError:+ logger.warning("Ignoring unparseable security.fake_ip_ranges entry: %r", entry)+ return tuple(networks)def _global_fake_ip_ranges() -> tuple:- """Process-lifetime cache, same shape as the allow_private toggle."""+ """Process-lifetime cache with the same profile-scope bypass as ``_global_allow_private_urls``:+ a multiplex gateway must not apply the first profile's declaration to later ones."""global _fake_ip_resolved, _cached_fake_ip_ranges+ if get_hermes_home_override() is not None:+ return _resolve_fake_ip_ranges()if not _fake_ip_resolved:_fake_ip_resolved, _cached_fake_ip_ranges = True, _resolve_fake_ip_ranges()return _cached_fake_ip_ranges-def _reset_fake_ip_cache() -> None:- """Reset the cached sentinel ranges — only for tests."""- global _fake_ip_resolved, _cached_fake_ip_ranges- _fake_ip_resolved, _cached_fake_ip_ranges = False, ()--def _normalize_hostname(host: Optional[str]) -> str:return (host or "").strip().lower().rstrip(".")@@ -237,19 +230,13 @@ def _is_always_blocked_ip(ip: _IPAddress) -> bool:return ip in _ALWAYS_BLOCKED_IPS or any(ip in net for net in _ALWAYS_BLOCKED_NETWORKS)-def _is_local_proxy_sentinel(ip: _IPAddress) -> bool:- """True when *ip* is in a fake-ip block this host declared in ``security.fake_ip_ranges``.-- The dial still goes to the local proxy, which resolves and connects to the real target, so- exempting a declared block grants no reach an attacker lacks through the proxy's own DNS.- Undeclared ranges keep the ordinary private-address verdict.- """- networks = _global_fake_ip_ranges()- if not networks:- return False+def _is_declared_fake_ip(ip: _IPAddress) -> bool:+ """True when *ip* falls in a block declared in ``security.fake_ip_ranges``. The dial still goes+ to the local proxy, which resolves and connects to the real target, so a declared block grants+ no reach an attacker lacks through the proxy's own DNS; undeclared ranges keep the private verdict."""if isinstance(ip, ipaddress.IPv6Address) and ip.ipv4_mapped is not None:ip = ip.ipv4_mapped- return any(ip in net for net in networks)+ return any(ip in net for net in _global_fake_ip_ranges())def _is_blocked_ip(ip: _IPAddress) -> bool:@@ -310,11 +297,7 @@ def _resolved_ip_block_reason(ip: _IPAddress, allow_private: bool) -> Optional[signores ``allow_private``; ordinary private/internal classes are blocked only when it is False."""if _is_always_blocked_ip(ip):return "cloud metadata address"- if not allow_private and _is_blocked_ip(ip):- # A fake-ip sentinel is the local proxy's own address, not an internal target — dialable either way.- if _is_local_proxy_sentinel(ip):- logger.debug("Allowing local-proxy fake-ip sentinel address: %s", ip)- return None+ if not allow_private and _is_blocked_ip(ip) and not _is_declared_fake_ip(ip):return "private/internal address"return None