Skip to content

The agent that grows with you

fix(plugins): plugin OAuth endpoints are https only and use the plugin's own variables

Teknium9f188e6598cb
diff --git a/hermes_cli/plugin_oauth.py b/hermes_cli/plugin_oauth.py
index 764f0b00fc4..690446466b1 100644
--- a/hermes_cli/plugin_oauth.py
+++ b/hermes_cli/plugin_oauth.py
@@ -33,7 +33,6 @@ _ID_RE = re.compile(r"^[a-z0-9][a-z0-9-]*$")
_ENV_RE = re.compile(r"^[A-Z][A-Z0-9_]*$")
# Lowercase DNS name with at least one dot whose last label starts with a letter: no IP literal, port or scheme.
_HOST_RE = re.compile(r"^(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z](?:[a-z0-9-]*[a-z0-9])?$")
-_LOOPBACK_LITERALS = frozenset({"127.0.0.1", "::1"})
@dataclass(frozen=True)
@@ -74,11 +73,20 @@ def _url_problems(where: str, url: Any) -> list[str]:
if (parsed is None or not parsed.hostname or parsed.username is not None or parsed.password is not None
or parsed.fragment):
return [f"{where}: must be an absolute URL without user info or fragment"]
- if parsed.scheme != "https" and not (parsed.scheme == "http" and parsed.hostname in _LOOPBACK_LITERALS):
- return [f"{where}: must use https (plain http only for a loopback address)"]
+ if parsed.scheme != "https": # no loopback exception: the code, refresh token and client secret travel here
+ return [f"{where}: must use https"]
return []
+def _hermes_owned_env(name: str) -> bool:
+ """A variable Hermes itself reads (a provider key, a platform token): never a plugin's client variable,
+ or a manifest could have Hermes POST that secret to the token URL it names."""
+ from hermes_cli.auth import PROVIDER_REGISTRY
+ from hermes_cli.config_defaults import OPTIONAL_ENV_VARS
+
+ return name in OPTIONAL_ENV_VARS or any(name in p.api_key_env_vars for p in PROVIDER_REGISTRY.values())
+
+
def _entry_problems(entry: dict, where: str) -> list[str]:
problems = [] if _ID_RE.match(str(entry.get("id") or "")) else [
f"{where}.id: {entry.get('id')!r} must be lowercase letters, digits and dashes"]
@@ -91,6 +99,8 @@ def _entry_problems(entry: dict, where: str) -> list[str]:
name = entry.get(field)
if (required or name) and not (isinstance(name, str) and _ENV_RE.match(name)):
problems.append(f"{where}.{field}: {name!r} must be an UPPER_SNAKE environment variable name")
+ elif name and _hermes_owned_env(name):
+ problems.append(f"{where}.{field}: {name} is a variable Hermes uses itself; declare the plugin's own")
hosts = entry.get("hosts")
if not (isinstance(hosts, list) and hosts and all(isinstance(h, str) and _HOST_RE.match(h) for h in hosts)):
problems.append(f"{where}.hosts: must list lowercase DNS host names (no scheme, port, path or IP address)")
@@ -150,11 +160,15 @@ def plugin_key(plugin_dir: Path) -> str:
return plugin_dir.name
+def _idp_hosts(client: OAuthClient) -> str:
+ return ", ".join(dict.fromkeys(urlparse(url).hostname for url in (client.authorize_url, client.token_url)))
+
+
def oauth_notice(manifest: Mapping[str, Any]) -> str:
"""Install/show text for the declared clients; empty when the plugin declares none."""
name = str((manifest or {}).get("name") or "<plugin>")
return "; ".join(
- f"Signs in to {c.id} for {', '.join(c.hosts)} with `hermes auth add {name}:{c.id}` "
+ f"Signs in to {c.id} at {_idp_hosts(c)} for {', '.join(c.hosts)} with `hermes auth add {name}:{c.id}` "
"(Hermes stores and refreshes the token; the plugin never receives it)"
for c in parse_oauth_clients(manifest)[0])
@@ -164,8 +178,7 @@ def oauth_surface(manifest: Mapping[str, Any]) -> set[str]:
hosts or scopes, shows up as added. Any other change only alters the fingerprint, so the user signs in again."""
lines: set[str] = set()
for c in parse_oauth_clients(manifest)[0]:
- idp_hosts = ", ".join(dict.fromkeys(urlparse(url).hostname for url in (c.authorize_url, c.token_url)))
- lines.add(f"{c.id} ({idp_hosts} -> {', '.join(c.hosts)}; {' '.join(c.scopes)})")
+ lines.add(f"{c.id} ({_idp_hosts(c)} -> {', '.join(c.hosts)}; {' '.join(c.scopes)})")
return lines
@@ -194,10 +207,18 @@ def oauth_admission_problems(manifest: Mapping[str, Any], plugin_dir: Path) -> l
return problems
-def token_url_mentions(manifest: Mapping[str, Any], plugin_dir: Path) -> list[str]:
- """Reviewer warnings for plugin code that names a declared token URL (Hermes refreshes the grant)."""
- urls = sorted({c.token_url for c in parse_oauth_clients(manifest)[0]})
- warnings: list[str] = []
+def _parent(host: str) -> str:
+ return ".".join(host.split(".")[-2:])
+
+
+def oauth_admission_warnings(manifest: Mapping[str, Any], plugin_dir: Path) -> list[str]:
+ """Reviewer warnings: a host outside the sign-in provider's domains (does the IdP own it?), and plugin
+ code that names a declared token URL (Hermes refreshes the grant)."""
+ clients = parse_oauth_clients(manifest)[0]
+ warnings = [f"oauth {c.id!r}: host {host} shares no domain with {_idp_hosts(c)}; check the provider owns it"
+ for c in clients for host in c.hosts
+ if _parent(host) not in {_parent(urlparse(u).hostname or "") for u in (c.authorize_url, c.token_url)}]
+ urls = sorted({c.token_url for c in clients})
for path in sorted(plugin_dir.rglob("*.py")):
try:
text = path.read_text(encoding="utf-8-sig", errors="replace")
@@ -494,6 +515,6 @@ def handle_auth_action(action: str, args: Any, provider: str) -> Optional[bool]:
__all__ = [
- "OAuthClient", "handle_auth_action", "oauth_admission_problems", "oauth_notice", "oauth_surface",
- "parse_oauth_clients", "plugin_key", "token_url_mentions",
+ "OAuthClient", "handle_auth_action", "oauth_admission_problems", "oauth_admission_warnings",
+ "oauth_notice", "oauth_surface", "parse_oauth_clients", "plugin_key",
]
diff --git a/hermes_cli/plugin_validate.py b/hermes_cli/plugin_validate.py
index 9b8448b8f7a..1f18c048365 100644
--- a/hermes_cli/plugin_validate.py
+++ b/hermes_cli/plugin_validate.py
@@ -186,16 +186,16 @@ def _check_requires_env(report: ValidationReport, manifest: dict) -> None:
def _check_oauth(report: ValidationReport, manifest: dict, plugin_dir: Path) -> None:
- """``oauth:`` clients (hermes_cli.plugin_oauth): shape and declared variables fail admission; plugin
- code that names a token URL is a reviewer warning, since Hermes refreshes the grant itself."""
- from hermes_cli.plugin_oauth import oauth_admission_problems, token_url_mentions
+ """``oauth:`` clients (hermes_cli.plugin_oauth): shape, https and declared variables fail admission; a
+ host outside the IdP's domains and plugin code that names a token URL are reviewer warnings."""
+ from hermes_cli.plugin_oauth import oauth_admission_problems, oauth_admission_warnings
if manifest.get("oauth") in (None, []):
report.add("oauth", True, "not declared")
return
problems = oauth_admission_problems(manifest, plugin_dir)
report.add("oauth", not problems, "; ".join(problems) or "clients valid")
- for warning in token_url_mentions(manifest, plugin_dir):
+ for warning in oauth_admission_warnings(manifest, plugin_dir):
report.warn(warning)
diff --git a/plugin-catalog/README.md b/plugin-catalog/README.md
index e479041c238..ffdea34d73e 100644
--- a/plugin-catalog/README.md
+++ b/plugin-catalog/README.md
@@ -91,7 +91,8 @@ meaningful:
OAuth client for a third-party API (the user's own Google Cloud client, for
example) is declared under `oauth:` in `plugin.yaml`, signed in with
`hermes auth add <plugin>:<id>` and used through the same helper; Hermes
- stores and refreshes that token too. A plugin never reads `.env` files, Hermes's `auth.json`, another tool's login (a
+ stores and refreshes that token too. Its `authorize_url`, `token_url` and
+ `hosts` are `https` only, and its `hosts` belong to the API the scopes are for. A plugin never reads `.env` files, Hermes's `auth.json`, another tool's login (a
vendor CLI's token file, a browser profile, a keychain) or any other
credential store directly, and never refreshes, rotates or writes OAuth tokens
or presents itself as another vendor's client. If Hermes has no route for a
diff --git a/website/docs/developer-guide/plugins/catalog-submission.md b/website/docs/developer-guide/plugins/catalog-submission.md
index 89261b832de..8c3214ab60f 100644
--- a/website/docs/developer-guide/plugins/catalog-submission.md
+++ b/website/docs/developer-guide/plugins/catalog-submission.md
@@ -149,7 +149,8 @@ the gallery. There is no separate listing to maintain.
OAuth client for a third-party API (the user's own Google Cloud client, for
example) is declared under `oauth:` in `plugin.yaml`, signed in with
`hermes auth add <plugin>:<id>` and used through the same helper; Hermes
- stores and refreshes that token too. A plugin never reads `.env` files, Hermes's `auth.json`, another tool's login (a
+ stores and refreshes that token too. Its `authorize_url`, `token_url` and
+ `hosts` are `https` only, and its `hosts` belong to the API the scopes are for. A plugin never reads `.env` files, Hermes's `auth.json`, another tool's login (a
vendor CLI's token file, a browser profile, a keychain) or any other
credential store directly, and never refreshes, rotates or writes OAuth tokens
or presents itself as another vendor's client. If Hermes has no route for a