Skip to content

The agent that grows with you

fix(dashboard-auth): print the ignored basic-auth secret notice once on stderr

unsupportedpastels074d150fabdb
diff --git a/plugins/dashboard_auth/basic/__init__.py b/plugins/dashboard_auth/basic/__init__.py
index a0423d12462..e1c31e09c50 100644
--- a/plugins/dashboard_auth/basic/__init__.py
+++ b/plugins/dashboard_auth/basic/__init__.py
@@ -17,6 +17,7 @@ import json
import logging
import os
import secrets
+import sys
import time
from typing import Optional
@@ -197,6 +198,10 @@ def _load_config_basic_auth_section() -> dict:
# these (nor a space), and everyone who copied the same line would share the same key.
_UNEXPANDED_SHELL_MARKERS = ("$(", "`", "openssl rand")
+# The error above only reaches the log files, not the console of ``hermes serve`` /
+# ``hermes dashboard``; one stderr line per process makes it visible there too.
+_shell_text_secret_reported = False
+
def _resolve_secret(cfg_section: dict) -> bytes:
"""Resolve the token-signing secret (base64, hex, or raw text). When unset, generates
@@ -218,6 +223,14 @@ def _resolve_secret(cfg_section: dict) -> bytes:
"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.")
+ global _shell_text_secret_reported
+ if not _shell_text_secret_reported:
+ _shell_text_secret_reported = True
+ print(
+ "hermes: dashboard login: HERMES_DASHBOARD_BASIC_AUTH_SECRET (or "
+ "dashboard.basic_auth.secret) holds shell text instead of a random value; "
+ "using a per-process key, so sessions end on restart. Replace it with the "
+ "output of: openssl rand -base64 32", file=sys.stderr, flush=True)
return secrets.token_bytes(32)
for decoder in (base64.b64decode, bytes.fromhex):
try:
diff --git a/tests/plugins/dashboard_auth/test_basic_secret_shell_text.py b/tests/plugins/dashboard_auth/test_basic_secret_shell_text.py
index 6d8af2eada5..36c224eb4e6 100644
--- a/tests/plugins/dashboard_auth/test_basic_secret_shell_text.py
+++ b/tests/plugins/dashboard_auth/test_basic_secret_shell_text.py
@@ -48,6 +48,18 @@ def test_env_secret_with_shell_text_is_not_used(monkeypatch, caplog, value):
assert errors and "unexpanded shell" in errors[0].getMessage()
+def test_shell_text_secret_is_reported_once_on_stderr(monkeypatch, capsys):
+ # The log files are not the console of `hermes serve` / `hermes dashboard`.
+ monkeypatch.setattr(basic, "_shell_text_secret_reported", False, raising=False)
+ monkeypatch.setenv("HERMES_DASHBOARD_BASIC_AUTH_SECRET", "$(openssl rand -hex 32)")
+ basic._resolve_secret({})
+ basic._resolve_secret({})
+ err = capsys.readouterr().err
+ assert err.count("HERMES_DASHBOARD_BASIC_AUTH_SECRET") == 1
+ assert len(err.strip().splitlines()) == 1
+ assert "$(openssl rand -hex 32)" not in err
+
+
def test_config_secret_with_shell_text_is_not_used():
assert basic._resolve_secret({"secret": LITERAL}) != LITERAL.encode("utf-8")