fix(url-safety): a managed allow_private_urls pin decides first

The first commit on this branch laid the managed scope over the profile's
config for security.allow_private_urls, but _resolve_allow_private_urls()
still returned on HERMES_ALLOW_PRIVATE_URLS before reading any config, and
accepted either spelling of the key. A managed
`security.allow_private_urls: false` therefore lost to the variable (shell
or the profile's .env) and to a profile's legacy
`browser.allow_private_urls: true`.

_managed_allow_private_urls() now reads the managed scope's own value first
(security.allow_private_urls, else the legacy browser.allow_private_urls; a
null value pins nothing), and that value decides the guard in both
directions. Without a pin the order is unchanged: the variable, then either
config key. This is the managed-scope guide's rule for a pinned key, which
wins over the user's config.yaml, .env and the shell environment.

Cloud and CDP browsers read browser.allow_private_urls as their own switch,
so an administrator pins that key as well to hold them. That reader is
unchanged; the security guide now says both.
This commit is contained in:
emozilla
2026-11-16 21:09:47 -05:00
committed by Jeffrey Quesnelle
parent 42e7338303
commit b04799a90d
3 changed files with 154 additions and 4 deletions

View File

@@ -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

View File

@@ -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.

View File

@@ -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