diff --git a/tests/tools/test_managed_scope_private_url_pin.py b/tests/tools/test_managed_scope_private_url_pin.py new file mode 100644 index 0000000000..b54835093f --- /dev/null +++ b/tests/tools/test_managed_scope_private_url_pin.py @@ -0,0 +1,123 @@ +"""A managed ``allow_private_urls`` pin decides the private-URL toggle before anything the user controls. + +The managed-scope guide: a pinned key wins over the profile's ``config.yaml``, its ``.env`` and the shell +environment. For this toggle the user-controlled inputs are ``HERMES_ALLOW_PRIVATE_URLS`` (shell or profile +``.env``) and the profile's two spellings of the key (``security.allow_private_urls`` and the legacy +``browser.allow_private_urls``). Without a pin the order stays environment variable, then config. +""" +import socket +from unittest.mock import patch + +import pytest + +from hermes_cli import managed_scope +from hermes_constants import reset_hermes_home_override, set_hermes_home_override +from tools import url_safety + + +def _reset() -> None: + managed_scope.invalidate_managed_cache() + url_safety._reset_allow_private_cache() + + +@pytest.fixture +def managed(tmp_path, monkeypatch): + """Write the managed config.yaml (HERMES_MANAGED_DIR) and drop the caches that hold the toggle.""" + md = tmp_path / "managed" + md.mkdir() + monkeypatch.setenv("HERMES_MANAGED_DIR", str(md)) + monkeypatch.delenv("HERMES_ALLOW_PRIVATE_URLS", raising=False) + + def write(text: str) -> None: + (md / "config.yaml").write_text(text, encoding="utf-8") + _reset() + + yield write + _reset() + + +def _profile(config: dict): + return patch("hermes_cli.config.read_raw_config", return_value=config) + + +def _resolves_to(ip: str): + return patch("socket.getaddrinfo", return_value=[(socket.AF_INET, socket.SOCK_STREAM, 6, "", (ip, 0))]) + + +@pytest.mark.parametrize("env_value", ["true", "1", "yes"]) +def test_a_managed_false_pin_beats_the_environment_variable(managed, monkeypatch, env_value): + managed("security:\n allow_private_urls: false\n") + monkeypatch.setenv("HERMES_ALLOW_PRIVATE_URLS", env_value) + with _profile({}): + assert url_safety._global_allow_private_urls() is False + with _resolves_to("10.0.0.5"): + assert url_safety.is_safe_url("https://intranet.example/") is False + + +@pytest.mark.parametrize("env_value", ["false", "0", "no"]) +def test_a_managed_true_pin_beats_the_environment_variable(managed, monkeypatch, env_value): + managed("security:\n allow_private_urls: true\n") + monkeypatch.setenv("HERMES_ALLOW_PRIVATE_URLS", env_value) + with _profile({}): + assert url_safety._global_allow_private_urls() is True + with _resolves_to("10.0.0.5"): + assert url_safety.is_safe_url("https://intranet.example/") is True + + +def test_a_managed_false_pin_beats_the_profiles_legacy_key(managed): + managed("security:\n allow_private_urls: false\n") + with _profile({"browser": {"allow_private_urls": True}}): + assert url_safety._global_allow_private_urls() is False + + +def test_a_managed_false_pin_beats_a_routed_profiles_own_file(managed, tmp_path): + """The profile-scoped read (a multiplex gateway turn) resolves the pin the same way, from real files.""" + managed("security:\n allow_private_urls: false\n") + home = tmp_path / "home" + home.mkdir() + (home / "config.yaml").write_text( + "security:\n allow_private_urls: true\nbrowser:\n allow_private_urls: true\n", encoding="utf-8") + token = set_hermes_home_override(home) + try: + assert url_safety._global_allow_private_urls() is False + finally: + reset_hermes_home_override(token) + + +@pytest.mark.parametrize("pinned, env_value, profile", [ + (False, "true", {"security": {"allow_private_urls": True}}), + (True, "false", {"security": {"allow_private_urls": False}}), +]) +def test_a_managed_pin_on_the_legacy_key_decides_too(managed, monkeypatch, pinned, env_value, profile): + managed(f"browser:\n allow_private_urls: {str(pinned).lower()}\n") + monkeypatch.setenv("HERMES_ALLOW_PRIVATE_URLS", env_value) + with _profile(profile): + assert url_safety._global_allow_private_urls() is pinned + + +def test_the_preferred_key_decides_when_the_managed_scope_pins_both(managed): + managed("security:\n allow_private_urls: false\nbrowser:\n allow_private_urls: true\n") + with _profile({}): + assert url_safety._global_allow_private_urls() is False + + +@pytest.mark.parametrize("managed_text", [ + "security:\n fake_ip_ranges: [198.18.0.0/15]\nterminal:\n env_passthrough: [SSL_CERT_FILE]\n", # a sandbox launcher's + "security:\n allow_private_urls: null\n", # a null value pins nothing +]) +@pytest.mark.parametrize("env_value, profile, expected", [ + ("true", {"security": {"allow_private_urls": False}}, True), + ("false", {"security": {"allow_private_urls": True}, "browser": {"allow_private_urls": True}}, False), +]) +def test_without_a_pin_the_environment_variable_still_decides(managed, monkeypatch, managed_text, env_value, + profile, expected): + managed(managed_text) + monkeypatch.setenv("HERMES_ALLOW_PRIVATE_URLS", env_value) + with _profile(profile): + assert url_safety._global_allow_private_urls() is expected + + +def test_without_a_pin_either_profile_key_still_opts_in(managed): + managed("security:\n fake_ip_ranges: [198.18.0.0/15]\n") + with _profile({"security": {"allow_private_urls": False}, "browser": {"allow_private_urls": True}}): + assert url_safety._global_allow_private_urls() is True diff --git a/tools/url_safety.py b/tools/url_safety.py index 41e39ed101..f64ea3af2a 100644 --- a/tools/url_safety.py +++ b/tools/url_safety.py @@ -126,10 +126,11 @@ _fake_ip_resolved, _cached_fake_ip_ranges = False, () def _global_allow_private_urls() -> bool: - """True when the user has opted out of private-IP blocking. Priority: ``HERMES_ALLOW_PRIVATE_URLS`` - env, ``security.allow_private_urls``, legacy ``browser.allow_private_urls``. Profile-scoped turns - (``get_hermes_home_override()`` set) bypass the process-global cache — a multiplex gateway serves - several profiles in one process; the first profile's opt-out must not disable blocking for later ones.""" + """True when the user has opted out of private-IP blocking. Priority: a managed-scope pin, then + ``HERMES_ALLOW_PRIVATE_URLS`` env, ``security.allow_private_urls``, legacy + ``browser.allow_private_urls``. Profile-scoped turns (``get_hermes_home_override()`` set) bypass the + process-global cache — a multiplex gateway serves several profiles in one process; the first + profile's opt-out must not disable blocking for later ones.""" global _allow_private_resolved, _cached_allow_private if get_hermes_home_override() is not None: return _resolve_allow_private_urls() @@ -140,6 +141,9 @@ def _global_allow_private_urls() -> bool: def _resolve_allow_private_urls() -> bool: """Resolve the effective private-URL toggle from the active config scope.""" + pinned = _managed_allow_private_urls() + if pinned is not None: + return pinned # an administrator's pin outranks the environment variable and the profile's keys env_val = os.getenv("HERMES_ALLOW_PRIVATE_URLS", "").strip().lower() if env_val in {"true", "1", "yes"}: return True @@ -156,6 +160,27 @@ def _resolve_allow_private_urls() -> bool: return False +def _managed_allow_private_urls() -> Optional[bool]: + """The managed scope's own value for the toggle, or None when it pins neither spelling. + + Read apart from the profile so a pin is resolved before everything the user controls: + ``HERMES_ALLOW_PRIVATE_URLS`` (shell or profile ``.env``) and the profile's two keys, either of + which opts in. ``security.allow_private_urls`` first, then the legacy + ``browser.allow_private_urls``; a null value pins nothing. + """ + try: + from hermes_cli.managed_scope import apply_managed_overlay + except ImportError: + return None # managed scope unavailable (early import): nothing is pinned + managed = apply_managed_overlay({}) # fail-open: {} without a scope or when its file cannot be read + for section in ("security", "browser"): # preferred, then legacy + block = managed.get(section) + value = block.get("allow_private_urls") if isinstance(block, dict) else None + if value is not None: + return is_truthy_value(value, default=False) + return None + + def _policy_config() -> dict: """config.yaml with managed scope laid on top, as ``load_config()`` lays it. diff --git a/website/docs/user-guide/security.md b/website/docs/user-guide/security.md index 05e1580dcb..2894fa95e7 100644 --- a/website/docs/user-guide/security.md +++ b/website/docs/user-guide/security.md @@ -846,6 +846,8 @@ When on, web tools, the browser, vision URL fetches, gateway media downloads, cr The host-substring guard (which blocks lookalike Unicode domain tricks even when the underlying IP is public) stays on regardless of this setting. +The [managed scope](./managed-scope.md) can pin this key (or the legacy `browser.allow_private_urls`). A pinned value decides this guard in either direction: `HERMES_ALLOW_PRIVATE_URLS` and the profile's own keys are read only when the managed scope pins neither. Cloud and CDP browsers also read `browser.allow_private_urls` as their own switch, so pin that key as well to hold them. + #### What `allow_private_urls` covers, and what it doesn't The setting guards the fetches Hermes makes or hands to a remote browser; it does