Skip to content

The agent that grows with you

docs(dashboard): generate the basic-auth signing secret in the remote recipes

unsupportedpastels50e14f7dd8ff
diff --git a/plugins/dashboard_auth/basic/__init__.py b/plugins/dashboard_auth/basic/__init__.py
index 4d65cbf62b3..199e73178d9 100644
--- a/plugins/dashboard_auth/basic/__init__.py
+++ b/plugins/dashboard_auth/basic/__init__.py
@@ -191,10 +191,17 @@ def _load_config_basic_auth_section() -> dict:
return load_config_section(logger, _TAG, "dashboard", "basic_auth")
+# Shell syntax that was written to .env/config without being run, e.g. a
+# ``$(openssl rand -base64 32)`` line inside a quoted heredoc. Base64 and hex never
+# contain these, and everyone who copied the same line would share the same key.
+_UNEXPANDED_SHELL_MARKERS = ("$(", "`")
+
+
def _resolve_secret(cfg_section: dict) -> bytes:
"""Resolve the token-signing secret (base64, hex, or raw text). When unset, generates
a random per-process secret (sessions then don't survive a restart or span multiple
- workers — logged at INFO)."""
+ workers — logged at INFO). A value holding unexpanded shell text is never used as the
+ key: it is logged as an error and replaced by a random per-process secret."""
raw = resolve_env_or_cfg("HERMES_DASHBOARD_BASIC_AUTH_SECRET", cfg_section.get("secret"))
if not raw:
logger.info(
@@ -203,6 +210,14 @@ def _resolve_secret(cfg_section: dict) -> bytes:
"multiple workers. Set dashboard.basic_auth.secret (or "
"HERMES_DASHBOARD_BASIC_AUTH_SECRET) for stable sessions.")
return secrets.token_bytes(32)
+ if any(marker in raw for marker in _UNEXPANDED_SHELL_MARKERS):
+ logger.error(
+ "dashboard-auth-basic: the configured signing secret contains unexpanded shell "
+ "text (such as '$(openssl rand -base64 32)') instead of a random value, so it "
+ "is ignored and a random per-process signing key is used. Set "
+ "HERMES_DASHBOARD_BASIC_AUTH_SECRET (or dashboard.basic_auth.secret) to the "
+ "output of 'openssl rand -base64 32' to keep sessions across restarts.")
+ return secrets.token_bytes(32)
for decoder in (base64.b64decode, bytes.fromhex):
try:
decoded = decoder(raw)
diff --git a/tests/plugins/dashboard_auth/test_basic_secret_shell_text.py b/tests/plugins/dashboard_auth/test_basic_secret_shell_text.py
new file mode 100644
index 00000000000..b254231f093
--- /dev/null
+++ b/tests/plugins/dashboard_auth/test_basic_secret_shell_text.py
@@ -0,0 +1,70 @@
+"""BasicAuthProvider must not sign sessions with a secret that is unexpanded shell text.
+
+A ``HERMES_DASHBOARD_BASIC_AUTH_SECRET=$(openssl rand -base64 32)`` line written through
+a quoted heredoc lands in ``.env`` literally, so every install that copied it would share
+one known signing key. ``_resolve_secret`` must ignore such a value and fall back to a
+random per-process key.
+"""
+
+from __future__ import annotations
+
+import base64
+import logging
+import secrets
+
+import pytest
+
+from plugins.dashboard_auth import basic
+
+LITERAL = "$(openssl rand -base64 32)"
+
+
[email protected](autouse=True)
+def _clear_basic_env(monkeypatch):
+ for var in (
+ "HERMES_DASHBOARD_BASIC_AUTH_USERNAME",
+ "HERMES_DASHBOARD_BASIC_AUTH_PASSWORD",
+ "HERMES_DASHBOARD_BASIC_AUTH_PASSWORD_HASH",
+ "HERMES_DASHBOARD_BASIC_AUTH_SECRET",
+ "HERMES_DASHBOARD_BASIC_AUTH_TTL_SECONDS",
+ ):
+ monkeypatch.delenv(var, raising=False)
+
+
[email protected]("value", [LITERAL, "`openssl rand -base64 32`", "prefix-$(head -c 32 /dev/urandom)"])
+def test_env_secret_with_shell_text_is_not_used(monkeypatch, caplog, value):
+ monkeypatch.setenv("HERMES_DASHBOARD_BASIC_AUTH_SECRET", value)
+ with caplog.at_level(logging.ERROR, logger=basic.logger.name):
+ first = basic._resolve_secret({})
+ second = basic._resolve_secret({})
+ assert first != value.encode("utf-8")
+ assert len(first) == 32
+ assert first != second # freshly random, not derived from the text
+ errors = [r for r in caplog.records if r.levelno == logging.ERROR]
+ assert errors and "unexpanded shell" in errors[0].getMessage()
+
+
+def test_config_secret_with_shell_text_is_not_used():
+ assert basic._resolve_secret({"secret": LITERAL}) != LITERAL.encode("utf-8")
+
+
+def test_generated_secret_still_resolves(monkeypatch):
+ key = secrets.token_bytes(32)
+ monkeypatch.setenv("HERMES_DASHBOARD_BASIC_AUTH_SECRET", base64.b64encode(key).decode())
+ assert basic._resolve_secret({}) == key
+
+
+def test_token_signed_with_the_literal_text_is_rejected(monkeypatch):
+ monkeypatch.setattr(basic, "_load_config_basic_auth_section", dict)
+ monkeypatch.setenv("HERMES_DASHBOARD_BASIC_AUTH_USERNAME", "admin")
+ monkeypatch.setenv("HERMES_DASHBOARD_BASIC_AUTH_PASSWORD", "choose-a-strong-password")
+ monkeypatch.setenv("HERMES_DASHBOARD_BASIC_AUTH_SECRET", LITERAL)
+ provider = basic.BasicAuthProvider(**basic._settings())
+
+ forged = basic._sign(
+ {"sub": "admin", "kind": "access", "exp": 2**31 - 1, "jti": "x"}, LITERAL.encode("utf-8"))
+ assert provider.verify_session(access_token=forged) is None
+
+ # The provider still registers and its own sessions work.
+ session = provider.complete_password_login(username="admin", password="choose-a-strong-password")
+ assert provider.verify_session(access_token=session.access_token) is not None
diff --git a/website/docs/reference/environment-variables.md b/website/docs/reference/environment-variables.md
index fb1f9b10bd8..aba60dadc4e 100644
--- a/website/docs/reference/environment-variables.md
+++ b/website/docs/reference/environment-variables.md
@@ -562,7 +562,7 @@ Three dashboard-auth providers ship in the box. For a remote Hermes Desktop conn
| `HERMES_DASHBOARD_BASIC_AUTH_USERNAME` | Username for the bundled username/password dashboard-auth provider (`plugins/dashboard_auth/basic`). Activates the provider when set together with a password. Overrides `dashboard.basic_auth.username`. |
| `HERMES_DASHBOARD_BASIC_AUTH_PASSWORD` | Plaintext password for the basic provider (hashed in-memory at load). Wins over a config `password_hash` so you can rotate via env. Overrides `dashboard.basic_auth.password`. |
| `HERMES_DASHBOARD_BASIC_AUTH_PASSWORD_HASH` | scrypt password hash for the basic provider (preferred — no plaintext at rest). Compute with `python -c "from plugins.dashboard_auth.basic import hash_password; print(hash_password('PW'))"`. Overrides `dashboard.basic_auth.password_hash`. |
-| `HERMES_DASHBOARD_BASIC_AUTH_SECRET` | HMAC key (32+ bytes, base64/hex/raw) signing the basic provider's stateless session tokens. Set explicitly so sessions survive restarts / span multiple workers; blank → random per-process (you'll be logged out on every restart). Overrides `dashboard.basic_auth.secret`. |
+| `HERMES_DASHBOARD_BASIC_AUTH_SECRET` | HMAC key (32+ bytes, base64/hex/raw) signing the basic provider's stateless session tokens. Set explicitly so sessions survive restarts / span multiple workers; blank → random per-process (you'll be logged out on every restart). A value that still holds unexpanded shell text such as `$(openssl rand -base64 32)` is ignored with an error and a random per-process key is used instead. Overrides `dashboard.basic_auth.secret`. |
| `HERMES_DASHBOARD_BASIC_AUTH_TTL_SECONDS` | Access-token lifetime for the basic provider (default 12h). Overrides `dashboard.basic_auth.session_ttl_seconds`. |
| `HERMES_DASHBOARD_OAUTH_CLIENT_ID` | OAuth client id (`agent:{instance_id}`) for the gated/public dashboard, activating the Nous (`plugins/dashboard_auth/nous`) provider. Overrides `dashboard.oauth.client_id`. Provision it with `hermes dashboard register`. |
| `HERMES_DASHBOARD_SESSION_TOKEN` | Per-process session token for the dashboard's sensitive `/api` routes, minted by the launcher that spawns `hermes dashboard` (Desktop shell, link-style integrations). A value injected by the parent process is kept as-is: a `HERMES_DASHBOARD_SESSION_TOKEN` line in `~/.hermes/.env` does not replace it. Unset, the server mints a fresh token per start. |
diff --git a/website/docs/user-guide/desktop.md b/website/docs/user-guide/desktop.md
index 3ce45ce8912..a3f1636cd9d 100644
--- a/website/docs/user-guide/desktop.md
+++ b/website/docs/user-guide/desktop.md
@@ -626,11 +626,11 @@ Set a username and password, then start the backend bound to a reachable address
cat >> ~/.hermes/.env <<'EOF'
HERMES_DASHBOARD_BASIC_AUTH_USERNAME=admin
HERMES_DASHBOARD_BASIC_AUTH_PASSWORD=choose-a-strong-password
+EOF
# Recommended: a stable signing secret so sessions survive restarts.
# Without it a random key is generated per boot and you'll be logged out
-# on every restart.
-HERMES_DASHBOARD_BASIC_AUTH_SECRET=$(openssl rand -base64 32)
-EOF
+# on every restart. This runs openssl, so the secret is unique to you.
+echo "HERMES_DASHBOARD_BASIC_AUTH_SECRET=$(openssl rand -base64 32)" >> ~/.hermes/.env
chmod 600 ~/.hermes/.env
# 2. Run the backend bound to a reachable address. The non-loopback bind
@@ -638,6 +638,8 @@ chmod 600 ~/.hermes/.env
hermes serve --host 0.0.0.0 --port 9119
```
+An earlier version of this recipe wrote the secret line inside the quoted heredoc, which stores the text `$(openssl rand -base64 32)` instead of a random value. If `grep BASIC_AUTH_SECRET ~/.hermes/.env` shows that text, replace the line with a freshly generated secret and restart the backend.
+
Keep that `hermes serve` process running for as long as you want the desktop app to be able to connect — if it stops, the app can no longer reach the backend. Run it under `systemd`, `tmux`, or your process manager of choice so it survives logout and reboots.
Separately, make sure the **gateway is running** on the remote host if you rely on messaging channels — the `hermes serve` backend is what the desktop app talks to, but your Telegram/Discord/Slack gateway sessions are a different process that you start and keep running on their own. See [Messaging](./messaging/index.md) for gateway setup.
diff --git a/website/docs/user-guide/features/web-dashboard.md b/website/docs/user-guide/features/web-dashboard.md
index a4dc957b086..237ea018e25 100644
--- a/website/docs/user-guide/features/web-dashboard.md
+++ b/website/docs/user-guide/features/web-dashboard.md
@@ -1162,9 +1162,10 @@ The recipe below uses the username/password path because it's the quickest to st
cat >> ~/.hermes/.env <<'EOF'
HERMES_DASHBOARD_BASIC_AUTH_USERNAME=admin
HERMES_DASHBOARD_BASIC_AUTH_PASSWORD=choose-a-strong-password
-# Recommended: a stable signing secret so sessions survive restarts.
-HERMES_DASHBOARD_BASIC_AUTH_SECRET=$(openssl rand -base64 32)
EOF
+# Recommended: a stable signing secret so sessions survive restarts.
+# This runs openssl, so the secret is unique to you.
+echo "HERMES_DASHBOARD_BASIC_AUTH_SECRET=$(openssl rand -base64 32)" >> ~/.hermes/.env
chmod 600 ~/.hermes/.env
# 2. Run the dashboard bound to a reachable address. The non-loopback bind
@@ -1172,6 +1173,8 @@ chmod 600 ~/.hermes/.env
hermes dashboard --no-open --host 0.0.0.0 --port 9119
```
+An earlier version of this recipe wrote the secret line inside the quoted heredoc, which stores the text `$(openssl rand -base64 32)` instead of a random value. If `grep BASIC_AUTH_SECRET ~/.hermes/.env` shows that text, replace the line with a freshly generated secret and restart the dashboard.
+
Prefer no plaintext at rest? Use `HERMES_DASHBOARD_BASIC_AUTH_PASSWORD_HASH` with a scrypt hash instead — see [Username/password provider](#usernamepassword-provider-no-oauth-idp) for the full surface.
If you run the dashboard as a systemd service, `~/.hermes/.env` is picked up automatically when the unit has `EnvironmentFile=%h/.hermes/.env`, so the credentials are in the environment at boot.