The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cron): bind a stored provider key to its configured origin, not its hostname
John Paul Soliva1903b2b6e317
diff --git a/tests/tools/test_cronjob_tools.py b/tests/tools/test_cronjob_tools.pyindex 47e6da8eff6..d21aa7453fa 100644--- a/tests/tools/test_cronjob_tools.py+++ b/tests/tools/test_cronjob_tools.py@@ -667,8 +667,27 @@ class TestValidateCronBaseUrl:def test_named_custom_matching_host_allowed(self, monkeypatch):self._patch_named_legit(monkeypatch)assert self._v("custom:legit", "https://legit.example/v1") is None- # subdomain of the configured host is still the provider's own endpoint- assert self._v("custom:legit", "https://eu.legit.example/v1") is None+ # Same origin spelled differently: explicit default port, case, trailing dot/slash.+ assert self._v("custom:legit", "https://LEGIT.example.:443/v1/") is None++ def test_stored_key_only_goes_to_the_configured_origin(self, monkeypatch):+ # The stored key is bound to the configured origin, not to its hostname: another+ # scheme, port or subdomain is a different endpoint the operator never configured.+ from hermes_cli.auth import PROVIDER_REGISTRY+ reg, known = next((k, p.inference_base_url) for k, p in PROVIDER_REGISTRY.items()+ if str(getattr(p, "inference_base_url", "")).startswith("https://"))++ def refused_off_origin(prov, h):+ for bu in (f"http://{h}/v1", f"http://{h}:443/v1", f"https://{h}:8443/v1",+ f"https://eu.{h}/v1",+ f"{h}/v1", "http://[::1/v1"):+ err = self._v(prov, bu)+ assert err and "not allowed" in err, (prov, bu)++ assert self._v(reg, known) is None # registry provider arm+ refused_off_origin(reg, known.split("://", 1)[1].split("/", 1)[0])+ self._patch_named_legit(monkeypatch) # named custom provider arm+ refused_off_origin("custom:legit", "legit.example")def test_named_custom_lookalike_host_blocked(self, monkeypatch):self._patch_named_legit(monkeypatch)diff --git a/tools/cronjob_job_args.py b/tools/cronjob_job_args.pyindex 29ad04cabea..8ff209688c3 100644--- a/tools/cronjob_job_args.py+++ b/tools/cronjob_job_args.py@@ -308,12 +308,24 @@ def _resolve_cron_context_deliver(deliver: Optional[str]) -> Optional[str]:return ",".join(dict.fromkeys(resolved)) or None+def _same_origin(base_url: str, configured: str) -> bool:+ """Origin (scheme, host, effective port) equality, as for any bearer secret moved to a+ new URL: a host-only or subdomain match would let a job send the stored key over plain+ HTTP, to another port, or to a host the operator never configured."""+ from utils import base_url_origin+ try:+ want, got = base_url_origin(configured), base_url_origin(base_url)+ except ValueError: # malformed URL (e.g. unclosed IPv6 bracket) cannot match+ return False+ return bool(want[1]) and got == want++def _validate_cron_base_url(provider: Optional[Any], base_url: Optional[Any]) -> Optional[str]:- """Reject pairing a named provider's stored credential with an off-host base_url (a+ """Reject pairing a named provider's stored credential with an off-origin base_url (aprompt-injected job could exfil the key). Allowed: no override; bare 'custom' (pure BYOK,- key derived from the base_url); an override whose host matches the named provider's own- endpoint. Everything else fails closed. Returns an error string if blocked, else None."""+ key derived from the base_url); an override with the same origin as the named provider's+ own endpoint. Everything else fails closed. Returns an error string if blocked, else None."""bu = _normalize_optional_job_value(base_url, strip_trailing_slash=True)if not bu:return None@@ -328,7 +340,6 @@ def _validate_cron_base_url(resolve_requested_provider,_get_named_custom_provider)from hermes_cli.auth import PROVIDER_REGISTRY- from utils import base_url_host_matches, base_url_hostnameexcept Exception:return f"Unable to validate base_url override for provider {prov!r}; refused."@@ -340,25 +351,26 @@ def _validate_cron_base_url(cp = _get_named_custom_provider(prov)except Exception:cp = None- cfg_host = base_url_hostname((cp or {}).get("base_url", "")) if cp else ""- if cfg_host and base_url_host_matches(bu, cfg_host):+ cfg_url = str((cp or {}).get("base_url") or "")+ if cfg_url and _same_origin(bu, cfg_url):return Nonereturn (f"base_url {bu!r} is not allowed for provider {prov!r}. A named "f"custom provider's stored credential may only be sent to its own "- f"configured endpoint ({cfg_host or 'unknown'}).")+ f"configured endpoint ({cfg_url or 'unknown'}): same scheme, host and port.")try:resolved = resolve_requested_provider(prov)except Exception:resolved = provpconfig = PROVIDER_REGISTRY.get(resolved) if isinstance(resolved, str) else None- known_host = base_url_hostname(getattr(pconfig, "inference_base_url", "") if pconfig else "")- if known_host and base_url_host_matches(bu, known_host):+ known_url = str(getattr(pconfig, "inference_base_url", "") or "") if pconfig else ""+ if known_url and _same_origin(bu, known_url):return None- # Fail closed: named providers with stored credentials AND unknown names we cannot host-match.+ # Fail closed: named providers with stored credentials AND unknown names we cannot origin-match.return (f"base_url {bu!r} is not allowed for provider {prov!r}. A named "- f"provider's stored credential may only be sent to its own endpoint; "+ f"provider's stored credential may only be sent to its own endpoint "+ f"(same scheme, host and port); "f'use a configured custom provider (provider="custom") for a custom base_url.')diff --git a/website/docs/user-guide/features/cron.md b/website/docs/user-guide/features/cron.mdindex 603aad36c14..eb12e777414 100644--- a/website/docs/user-guide/features/cron.md+++ b/website/docs/user-guide/features/cron.md@@ -1354,6 +1354,8 @@ If a hand edit leaves `jobs.json` malformed, the scheduler repairs it on the nexJobs may store `model` and `provider` as `null`. When those fields are omitted, Hermes resolves them at execution time from the global configuration. They only appear in the job record when a per-job override is set.+A per-job `base_url` override needs an explicit `provider`. For a provider with a stored key (a named custom provider or a built-in one), the override must have the same origin as that provider's configured endpoint: the same scheme, host and port. Another scheme, port or subdomain is refused, so the stored key is only ever sent where you configured it. A bare `provider: custom` takes any `base_url`, because no stored key goes with it.+The storage uses atomic file writes so interrupted writes do not leave a partially written job file behind.## Self-contained prompts still matterdiff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/features/cron.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/features/cron.mdindex 305e6048509..3c1ecd42257 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/features/cron.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/features/cron.md@@ -746,6 +746,8 @@ cronjob(action="create", name="daily-digest",任务可能将 `model` 和 `provider` 存储为 `null`。省略这些字段时,Hermes 在执行时从全局配置中解析它们。只有设置了单任务覆盖时,这些字段才会出现在任务记录中。+单任务的 `base_url` 覆盖必须同时指定 `provider`。对于保存了密钥的 provider(具名自定义 provider 或内置 provider),覆盖地址必须与该 provider 配置的端点同源:协议、主机和端口都相同。其他协议、端口或子域名都会被拒绝,因此保存的密钥只会发往你配置的地址。单独的 `provider: custom` 可以使用任意 `base_url`,因为它不携带保存的密钥。+存储使用原子文件写入,因此中断的写入不会留下部分写入的任务文件。## 自包含的 prompt 仍然重要