Skip to content

The agent that grows with you

fix(plugins): errors from a signed-in plugin request carry no credential

Teknium5a47d14aa2a3
diff --git a/hermes_cli/plugin_oauth.py b/hermes_cli/plugin_oauth.py
index abe812f6a01..764f0b00fc4 100644
--- a/hermes_cli/plugin_oauth.py
+++ b/hermes_cli/plugin_oauth.py
@@ -288,11 +288,14 @@ def _token_request(client: OAuthClient, key: str) -> Callable[[dict[str, str]],
form = {**fields, "client_id": _env(client.client_id_env, "oauth_client_id_missing", key)}
if client.client_secret_env:
form["client_secret"] = _env(client.client_secret_env, "oauth_client_secret_missing", key)
+ failure = ""
try:
response = httpx.post(client.token_url, data=form, headers={"Accept": "application/json"},
timeout=_TOKEN_TIMEOUT, verify=_default_verify(), follow_redirects=False)
except httpx.HTTPError as exc:
- raise _auth_error(key, f"the token request failed ({type(exc).__name__}).", "oauth_refresh_failed") from None
+ failure = type(exc).__name__
+ if failure: # raised outside the handler: httpx's error keeps the request, whose body holds the secrets
+ raise _auth_error(key, f"the token request failed ({failure}).", "oauth_refresh_failed")
payload = _json_object(response)
if not 200 <= response.status_code < 300: # a redirect is a failure too: it is never followed
raise _token_error(key, response.status_code, payload)
diff --git a/hermes_cli/plugin_provider_requests.py b/hermes_cli/plugin_provider_requests.py
index 2096b3f76bb..0f3bc4745d0 100644
--- a/hermes_cli/plugin_provider_requests.py
+++ b/hermes_cli/plugin_provider_requests.py
@@ -102,6 +102,12 @@ def _calling_plugin_dir() -> Optional[Path]:
return None
+def _detached(exc: BaseException) -> BaseException:
+ """*exc* without traceback or chain: those frames hold the token, refresh token or client secret."""
+ exc.__cause__ = exc.__context__ = None
+ return exc.with_traceback(None)
+
+
def declared_auth_providers(manifest: Mapping[str, Any]) -> list[str]:
"""``requires_auth`` from a manifest dict: the providers whose sign-in the plugin uses."""
raw = (manifest or {}).get("requires_auth")
@@ -156,15 +162,33 @@ def credentialed_provider_request(
origins, resolve = spec
if url_origin(url) not in origins:
raise PermissionError(f"Refusing to send the {provider} sign-in to {url_origin(url)[1] or url!r}")
- auth_headers, credential_base = resolve(url)
- if url_origin(credential_base) not in origins:
- # A pooled gateway key belongs to its own host only (#121486).
- raise PermissionError(f"This profile's {provider} credential routes to a custom endpoint; refusing")
- sent = {k: v for k, v in (headers or {}).items() if str(k).lower() not in {h.lower() for h in auth_headers}}
import httpx
+ # Every error the plugin receives past this point is rebuilt without traceback, chain or request: those
+ # reach the frames and httpx objects that hold the token, refresh token or client secret.
+ refusal: Optional[BaseException] = None
with httpx.Client(timeout=timeout, follow_redirects=False) as client:
- response = client.request(method, url, json=json, headers={**sent, **auth_headers})
+ request = client.build_request(method, url, json=json, headers=dict(headers or {})) # no credential yet
+ try:
+ auth_headers, credential_base = resolve(url)
+ except (RuntimeError, OSError, ValueError, LookupError, TypeError) as exc:
+ refusal = _detached(exc)
+ else:
+ if url_origin(credential_base) not in origins:
+ # A pooled gateway key belongs to its own host only (#121486).
+ refusal = PermissionError(
+ f"This profile's {provider} credential routes to a custom endpoint; refusing")
+ else:
+ for name in auth_headers:
+ request.headers.pop(name, None)
+ request.headers.update(auth_headers)
+ try:
+ response = client.send(request)
+ except httpx.HTTPError as exc:
+ refusal = type(exc)(str(exc))
+ auth_headers = request = None
+ if refusal is not None:
+ raise refusal
return ProviderResponse(response.status_code, dict(response.headers), response.content)