Skip to content

The agent that grows with you

fix(dashboard): reject credential preview writeback

JoaoMarcos44eb4f92bc23a3
diff --git a/hermes_cli/web_routers/config_env.py b/hermes_cli/web_routers/config_env.py
index 606495eca3f..28e092ecde4 100644
--- a/hermes_cli/web_routers/config_env.py
+++ b/hermes_cli/web_routers/config_env.py
@@ -47,6 +47,10 @@ _reveal_timestamps: List[float] = []
_REVEAL_MAX_PER_WINDOW = 5
_REVEAL_WINDOW_SECONDS = 30
+_REDACTED_CREDENTIAL_WRITE_DETAIL = (
+ "Refusing to save a redacted credential preview; re-enter the full secret to replace it."
+)
+
# Display order for tabs — unlisted categories sort alphabetically after these.
_CATEGORY_ORDER = [
"general", "agent", "terminal", "display", "delegation",
@@ -295,9 +299,13 @@ async def set_env_var(body: EnvVarUpdate, profile: Optional[str] = None):
with _env_write_errors("PUT /api/env failed", http_passthrough=False):
from hermes_cli.credential_lifecycle import save_provider_env_credential
- return await scoped_to_thread(
- body.profile or profile, lambda: save_provider_env_credential(body.key, body.value)
- )
+ def _save():
+ current = load_env().get(body.key)
+ if current and body.value == redact_key(current):
+ raise ValueError(_REDACTED_CREDENTIAL_WRITE_DETAIL)
+ return save_provider_env_credential(body.key, body.value)
+
+ return await scoped_to_thread(body.profile or profile, _save)
# Live credential probes keyed by env var: (url, auth) where auth is "bearer"
@@ -620,6 +628,11 @@ def _write_custom_endpoint(cfg: Dict[str, Any], body: CustomEndpointUpdate) -> T
env_var = custom_endpoint_key_env(endpoint_id)
submitted_key = body.api_key.strip() if body.api_key is not None else None
if submitted_key:
+ display_preview = _api_key_display(existing)[1]
+ existing_key_env = str(existing.get("key_env") or "").strip()
+ stored_secret = load_env().get(existing_key_env) if existing_key_env else None
+ if submitted_key == display_preview or (stored_secret and submitted_key == redact_key(stored_secret)):
+ raise HTTPException(status_code=400, detail=_REDACTED_CREDENTIAL_WRITE_DETAIL)
save_env_value(env_var, submitted_key)
entry["key_env"] = env_var
entry.pop("api_key", None)
diff --git a/hermes_cli/web_routers/messaging.py b/hermes_cli/web_routers/messaging.py
index 5d96faf7434..7dde2c7cc76 100644
--- a/hermes_cli/web_routers/messaging.py
+++ b/hermes_cli/web_routers/messaging.py
@@ -871,17 +871,32 @@ async def update_messaging_platform(platform_id: str, body: MessagingPlatformUpd
raise HTTPException(status_code=400, detail=f"{key} is not configurable for {entry['name']}")
def _apply():
- with _profile_scope(target_profile):
+ with _profile_scope(target_profile) as scoped_dir:
+ env_on_disk = load_env()
+ updates: dict[str, str] = {}
+
+ # Validate the whole request against the same credential snapshot the GET
+ # response uses before clearing or replacing anything.
for key in body.clear_env:
_check_allowed(key)
- remove_env_value(key)
-
for key, value in body.env.items():
_check_allowed(key)
trimmed = value.strip()
- if trimmed:
- _validate_messaging_env_value(platform_id, key, trimmed)
- save_env_value(key, trimmed)
+ if not trimmed:
+ continue
+ current = env_on_disk.get(key) or ("" if scoped_dir is not None else os.getenv(key, ""))
+ if current and trimmed == redact_key(current):
+ raise HTTPException(
+ status_code=400,
+ detail="Refusing to save a redacted credential preview; re-enter the full secret to replace it.",
+ )
+ _validate_messaging_env_value(platform_id, key, trimmed)
+ updates[key] = trimmed
+
+ for key in body.clear_env:
+ remove_env_value(key)
+ for key, value in updates.items():
+ save_env_value(key, value)
if body.enabled is not None:
_write_platform_enabled(platform_id, body.enabled)