2026-04-10 07:42:02 +00:00
|
|
|
"""Hermes Web UI -- startup helpers."""
|
|
|
|
|
from __future__ import annotations
|
v0.46.0: security, Docker UID/GID, model discovery, i18n, cancel fix
* fix: decode HTML entities before markdown processing + zh/zh-Hant translations (#239)
Adds decode() helper in renderMd() to fix double-escaping of HTML entities
from LLM output (e.g. <code> becoming &lt;code&gt; instead
of rendering). XSS-safe: decode runs before esc(), only 5 entity patterns.
Also adds 40+ missing zh (Simplified Chinese) translation keys and a new
zh-Hant (Traditional Chinese) locale with 163 keys.
Fix applied: removed duplicate settings_label_notifications key in both
zh and zh-Hant locales.
Fixes #240
* fix: restore custom model list discovery with config api key (#238)
get_available_models() now reads api_key from config.yaml before env vars:
1. model.api_key
2. providers.<active>.api_key / providers.custom.api_key
3. env var fallbacks (HERMES_API_KEY, OPENAI_API_KEY, etc.)
Also adds OpenAI/Python User-Agent header and a regression test covering
authenticated /v1/models discovery.
Fixes users with LM Studio / Ollama custom endpoints configured in
config.yaml whose model picker silently collapsed to the default model.
* feat: Docker UID/GID matching to avoid root-owned .hermes files (#237)
Adds docker_init.bash with hermeswebuitoo/hermeswebui user pattern so
container files match the host user UID/GID. Prevents .hermes volume
mounts from being owned by root when using a non-root host user.
Configure via WANTED_UID and WANTED_GID env vars (default 1000/1000).
Readme updated with setup instructions.
Fix applied: removed duplicate WANTED_GID=1000 line in docker-compose.yml
that was overriding the ${GID:-1000} variable expansion.
* security: redact credentials from API responses and fix credential file permissions (#243)
Adds response-layer credential redaction to three endpoints:
- GET /api/session — messages[], tool_calls[], and title
- GET /api/session/export — download also redacted
- SSE done event — session payload in stream
- GET /api/memory — MEMORY.md and USER.md content
Adds api/startup.py with fix_credential_permissions() at server startup.
Adds 13 tests in tests/test_security_redaction.py.
Merged with #237 container detection changes in server.py.
* fix: cancel button now interrupts agent and cleans up UI state (#244)
Wires agent.interrupt() into cancel_stream() so the backend actually
stops tool execution when the user clicks Cancel, rather than only
stopping the SSE stream while the agent keeps running.
Changes:
- api/config.py: adds AGENT_INSTANCES dict (stream_id -> AIAgent)
- api/streaming.py: stores agent in AGENT_INSTANCES after creation,
checks CANCEL_FLAGS immediately after store (race condition fix),
calls agent.interrupt() in cancel_stream(), cleans up in finally block
- static/boot.js: removes stale setStatus(cancelling) call
- static/messages.js: setBusy(false)/setStatus('') unconditionally on cancel
Race condition fix: after storing agent in AGENT_INSTANCES, immediately
checks if CANCEL_FLAGS[stream_id] is already set (cancel arrived during
agent init) and interrupts before starting. Check is inside the same
STREAMS_LOCK acquisition, making it atomic.
New test file: tests/test_cancel_interrupt.py with 6 unit tests.
* docs: v0.46.0 release notes, bump version, update test counts
---------
Co-authored-by: Nathan Esquenazi <nesquena@gmail.com>
2026-04-11 17:17:52 +00:00
|
|
|
import os, stat, subprocess, sys
|
2026-04-10 07:42:02 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
v0.46.0: security, Docker UID/GID, model discovery, i18n, cancel fix
* fix: decode HTML entities before markdown processing + zh/zh-Hant translations (#239)
Adds decode() helper in renderMd() to fix double-escaping of HTML entities
from LLM output (e.g. <code> becoming &lt;code&gt; instead
of rendering). XSS-safe: decode runs before esc(), only 5 entity patterns.
Also adds 40+ missing zh (Simplified Chinese) translation keys and a new
zh-Hant (Traditional Chinese) locale with 163 keys.
Fix applied: removed duplicate settings_label_notifications key in both
zh and zh-Hant locales.
Fixes #240
* fix: restore custom model list discovery with config api key (#238)
get_available_models() now reads api_key from config.yaml before env vars:
1. model.api_key
2. providers.<active>.api_key / providers.custom.api_key
3. env var fallbacks (HERMES_API_KEY, OPENAI_API_KEY, etc.)
Also adds OpenAI/Python User-Agent header and a regression test covering
authenticated /v1/models discovery.
Fixes users with LM Studio / Ollama custom endpoints configured in
config.yaml whose model picker silently collapsed to the default model.
* feat: Docker UID/GID matching to avoid root-owned .hermes files (#237)
Adds docker_init.bash with hermeswebuitoo/hermeswebui user pattern so
container files match the host user UID/GID. Prevents .hermes volume
mounts from being owned by root when using a non-root host user.
Configure via WANTED_UID and WANTED_GID env vars (default 1000/1000).
Readme updated with setup instructions.
Fix applied: removed duplicate WANTED_GID=1000 line in docker-compose.yml
that was overriding the ${GID:-1000} variable expansion.
* security: redact credentials from API responses and fix credential file permissions (#243)
Adds response-layer credential redaction to three endpoints:
- GET /api/session — messages[], tool_calls[], and title
- GET /api/session/export — download also redacted
- SSE done event — session payload in stream
- GET /api/memory — MEMORY.md and USER.md content
Adds api/startup.py with fix_credential_permissions() at server startup.
Adds 13 tests in tests/test_security_redaction.py.
Merged with #237 container detection changes in server.py.
* fix: cancel button now interrupts agent and cleans up UI state (#244)
Wires agent.interrupt() into cancel_stream() so the backend actually
stops tool execution when the user clicks Cancel, rather than only
stopping the SSE stream while the agent keeps running.
Changes:
- api/config.py: adds AGENT_INSTANCES dict (stream_id -> AIAgent)
- api/streaming.py: stores agent in AGENT_INSTANCES after creation,
checks CANCEL_FLAGS immediately after store (race condition fix),
calls agent.interrupt() in cancel_stream(), cleans up in finally block
- static/boot.js: removes stale setStatus(cancelling) call
- static/messages.js: setBusy(false)/setStatus('') unconditionally on cancel
Race condition fix: after storing agent in AGENT_INSTANCES, immediately
checks if CANCEL_FLAGS[stream_id] is already set (cancel arrived during
agent init) and interrupts before starting. Check is inside the same
STREAMS_LOCK acquisition, making it atomic.
New test file: tests/test_cancel_interrupt.py with 6 unit tests.
* docs: v0.46.0 release notes, bump version, update test counts
---------
Co-authored-by: Nathan Esquenazi <nesquena@gmail.com>
2026-04-11 17:17:52 +00:00
|
|
|
# Credential files that should never be world-readable
|
|
|
|
|
_SENSITIVE_FILES = (
|
|
|
|
|
'.env',
|
|
|
|
|
'google_token.json',
|
|
|
|
|
'google_client_secret.json',
|
|
|
|
|
'.signing_key',
|
|
|
|
|
'auth.json',
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def fix_credential_permissions() -> None:
|
2026-05-01 12:10:48 +00:00
|
|
|
"""Ensure sensitive files in HERMES_HOME have safe permissions.
|
|
|
|
|
|
|
|
|
|
Respects:
|
|
|
|
|
- HERMES_SKIP_CHMOD=1 → bypass entirely
|
|
|
|
|
- HERMES_HOME_MODE → group bits are allowed if set by the operator,
|
|
|
|
|
only world-readable/world-writable files are fixed
|
|
|
|
|
"""
|
|
|
|
|
if os.environ.get('HERMES_SKIP_CHMOD', '').strip() in ('1', 'true'):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Parse operator-declared mode to know if group bits are intentional
|
|
|
|
|
declared_mode = None
|
|
|
|
|
raw_mode = os.environ.get('HERMES_HOME_MODE', '').strip()
|
|
|
|
|
if raw_mode:
|
|
|
|
|
try:
|
|
|
|
|
declared_mode = int(raw_mode, 8)
|
|
|
|
|
except ValueError:
|
|
|
|
|
pass
|
|
|
|
|
|
v0.46.0: security, Docker UID/GID, model discovery, i18n, cancel fix
* fix: decode HTML entities before markdown processing + zh/zh-Hant translations (#239)
Adds decode() helper in renderMd() to fix double-escaping of HTML entities
from LLM output (e.g. <code> becoming &lt;code&gt; instead
of rendering). XSS-safe: decode runs before esc(), only 5 entity patterns.
Also adds 40+ missing zh (Simplified Chinese) translation keys and a new
zh-Hant (Traditional Chinese) locale with 163 keys.
Fix applied: removed duplicate settings_label_notifications key in both
zh and zh-Hant locales.
Fixes #240
* fix: restore custom model list discovery with config api key (#238)
get_available_models() now reads api_key from config.yaml before env vars:
1. model.api_key
2. providers.<active>.api_key / providers.custom.api_key
3. env var fallbacks (HERMES_API_KEY, OPENAI_API_KEY, etc.)
Also adds OpenAI/Python User-Agent header and a regression test covering
authenticated /v1/models discovery.
Fixes users with LM Studio / Ollama custom endpoints configured in
config.yaml whose model picker silently collapsed to the default model.
* feat: Docker UID/GID matching to avoid root-owned .hermes files (#237)
Adds docker_init.bash with hermeswebuitoo/hermeswebui user pattern so
container files match the host user UID/GID. Prevents .hermes volume
mounts from being owned by root when using a non-root host user.
Configure via WANTED_UID and WANTED_GID env vars (default 1000/1000).
Readme updated with setup instructions.
Fix applied: removed duplicate WANTED_GID=1000 line in docker-compose.yml
that was overriding the ${GID:-1000} variable expansion.
* security: redact credentials from API responses and fix credential file permissions (#243)
Adds response-layer credential redaction to three endpoints:
- GET /api/session — messages[], tool_calls[], and title
- GET /api/session/export — download also redacted
- SSE done event — session payload in stream
- GET /api/memory — MEMORY.md and USER.md content
Adds api/startup.py with fix_credential_permissions() at server startup.
Adds 13 tests in tests/test_security_redaction.py.
Merged with #237 container detection changes in server.py.
* fix: cancel button now interrupts agent and cleans up UI state (#244)
Wires agent.interrupt() into cancel_stream() so the backend actually
stops tool execution when the user clicks Cancel, rather than only
stopping the SSE stream while the agent keeps running.
Changes:
- api/config.py: adds AGENT_INSTANCES dict (stream_id -> AIAgent)
- api/streaming.py: stores agent in AGENT_INSTANCES after creation,
checks CANCEL_FLAGS immediately after store (race condition fix),
calls agent.interrupt() in cancel_stream(), cleans up in finally block
- static/boot.js: removes stale setStatus(cancelling) call
- static/messages.js: setBusy(false)/setStatus('') unconditionally on cancel
Race condition fix: after storing agent in AGENT_INSTANCES, immediately
checks if CANCEL_FLAGS[stream_id] is already set (cancel arrived during
agent init) and interrupts before starting. Check is inside the same
STREAMS_LOCK acquisition, making it atomic.
New test file: tests/test_cancel_interrupt.py with 6 unit tests.
* docs: v0.46.0 release notes, bump version, update test counts
---------
Co-authored-by: Nathan Esquenazi <nesquena@gmail.com>
2026-04-11 17:17:52 +00:00
|
|
|
hermes_home = Path(os.environ.get('HERMES_HOME', str(Path.home() / '.hermes')))
|
|
|
|
|
if not hermes_home.is_dir():
|
|
|
|
|
return
|
|
|
|
|
for name in _SENSITIVE_FILES:
|
|
|
|
|
fpath = hermes_home / name
|
|
|
|
|
if not fpath.exists():
|
|
|
|
|
continue
|
|
|
|
|
try:
|
|
|
|
|
current = stat.S_IMODE(fpath.stat().st_mode)
|
2026-05-01 12:10:48 +00:00
|
|
|
# If operator declared a mode, allow group bits but still fix world bits
|
|
|
|
|
if declared_mode is not None:
|
|
|
|
|
if current & 0o007: # other bits set (world-readable/writable)
|
|
|
|
|
fpath.chmod(current & ~0o007)
|
|
|
|
|
print(f' [security] removed world bits on {fpath.name} ({oct(current)} -> {oct(current & ~0o007)})', flush=True)
|
|
|
|
|
else:
|
|
|
|
|
if current & 0o077: # group or other bits set
|
|
|
|
|
fpath.chmod(0o600)
|
|
|
|
|
print(f' [security] fixed permissions on {fpath.name} ({oct(current)} -> 0600)', flush=True)
|
v0.46.0: security, Docker UID/GID, model discovery, i18n, cancel fix
* fix: decode HTML entities before markdown processing + zh/zh-Hant translations (#239)
Adds decode() helper in renderMd() to fix double-escaping of HTML entities
from LLM output (e.g. <code> becoming &lt;code&gt; instead
of rendering). XSS-safe: decode runs before esc(), only 5 entity patterns.
Also adds 40+ missing zh (Simplified Chinese) translation keys and a new
zh-Hant (Traditional Chinese) locale with 163 keys.
Fix applied: removed duplicate settings_label_notifications key in both
zh and zh-Hant locales.
Fixes #240
* fix: restore custom model list discovery with config api key (#238)
get_available_models() now reads api_key from config.yaml before env vars:
1. model.api_key
2. providers.<active>.api_key / providers.custom.api_key
3. env var fallbacks (HERMES_API_KEY, OPENAI_API_KEY, etc.)
Also adds OpenAI/Python User-Agent header and a regression test covering
authenticated /v1/models discovery.
Fixes users with LM Studio / Ollama custom endpoints configured in
config.yaml whose model picker silently collapsed to the default model.
* feat: Docker UID/GID matching to avoid root-owned .hermes files (#237)
Adds docker_init.bash with hermeswebuitoo/hermeswebui user pattern so
container files match the host user UID/GID. Prevents .hermes volume
mounts from being owned by root when using a non-root host user.
Configure via WANTED_UID and WANTED_GID env vars (default 1000/1000).
Readme updated with setup instructions.
Fix applied: removed duplicate WANTED_GID=1000 line in docker-compose.yml
that was overriding the ${GID:-1000} variable expansion.
* security: redact credentials from API responses and fix credential file permissions (#243)
Adds response-layer credential redaction to three endpoints:
- GET /api/session — messages[], tool_calls[], and title
- GET /api/session/export — download also redacted
- SSE done event — session payload in stream
- GET /api/memory — MEMORY.md and USER.md content
Adds api/startup.py with fix_credential_permissions() at server startup.
Adds 13 tests in tests/test_security_redaction.py.
Merged with #237 container detection changes in server.py.
* fix: cancel button now interrupts agent and cleans up UI state (#244)
Wires agent.interrupt() into cancel_stream() so the backend actually
stops tool execution when the user clicks Cancel, rather than only
stopping the SSE stream while the agent keeps running.
Changes:
- api/config.py: adds AGENT_INSTANCES dict (stream_id -> AIAgent)
- api/streaming.py: stores agent in AGENT_INSTANCES after creation,
checks CANCEL_FLAGS immediately after store (race condition fix),
calls agent.interrupt() in cancel_stream(), cleans up in finally block
- static/boot.js: removes stale setStatus(cancelling) call
- static/messages.js: setBusy(false)/setStatus('') unconditionally on cancel
Race condition fix: after storing agent in AGENT_INSTANCES, immediately
checks if CANCEL_FLAGS[stream_id] is already set (cancel arrived during
agent init) and interrupts before starting. Check is inside the same
STREAMS_LOCK acquisition, making it atomic.
New test file: tests/test_cancel_interrupt.py with 6 unit tests.
* docs: v0.46.0 release notes, bump version, update test counts
---------
Co-authored-by: Nathan Esquenazi <nesquena@gmail.com>
2026-04-11 17:17:52 +00:00
|
|
|
except OSError:
|
|
|
|
|
pass # best-effort; don't abort startup
|
|
|
|
|
|
|
|
|
|
|
2026-04-10 07:42:02 +00:00
|
|
|
def _agent_dir() -> Path | None:
|
|
|
|
|
hermes_home = Path(os.environ.get('HERMES_HOME', str(Path.home() / '.hermes')))
|
|
|
|
|
for raw in [os.environ.get('HERMES_WEBUI_AGENT_DIR', '').strip(), str(hermes_home / 'hermes-agent')]:
|
|
|
|
|
if not raw:
|
|
|
|
|
continue
|
|
|
|
|
p = Path(raw).expanduser()
|
|
|
|
|
if p.is_dir():
|
|
|
|
|
return p.resolve()
|
|
|
|
|
return None
|
|
|
|
|
|
2026-04-22 20:49:28 +00:00
|
|
|
def _trusted_agent_dir(agent_dir: Path) -> bool:
|
|
|
|
|
"""Return True if agent_dir passes ownership and permission checks.
|
|
|
|
|
|
|
|
|
|
Validates that the directory is not world- or group-writable and,
|
|
|
|
|
on POSIX systems, is owned by the current process user.
|
|
|
|
|
|
|
|
|
|
Intentionally does NOT enforce a canonical path (i.e. does not require
|
|
|
|
|
the dir to be ~/.hermes/hermes-agent), so custom HERMES_WEBUI_AGENT_DIR
|
|
|
|
|
paths work correctly when HERMES_WEBUI_AUTO_INSTALL=1 is set.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
st = agent_dir.stat()
|
|
|
|
|
if stat.S_IMODE(st.st_mode) & 0o022:
|
|
|
|
|
# World- or group-writable — untrusted
|
|
|
|
|
return False
|
|
|
|
|
if hasattr(os, 'getuid') and st.st_uid != os.getuid():
|
|
|
|
|
# Not owned by current user (POSIX only; Windows fallback skips)
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
except OSError:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
2026-04-10 07:42:02 +00:00
|
|
|
def auto_install_agent_deps() -> bool:
|
2026-04-22 20:49:28 +00:00
|
|
|
enabled = os.environ.get('HERMES_WEBUI_AUTO_INSTALL', '').strip().lower() in ('1', 'true', 'yes')
|
|
|
|
|
if not enabled:
|
|
|
|
|
print('[!!] Auto-install disabled. Set HERMES_WEBUI_AUTO_INSTALL=1 to enable.', flush=True)
|
|
|
|
|
return False
|
2026-04-10 07:42:02 +00:00
|
|
|
agent_dir = _agent_dir()
|
|
|
|
|
if agent_dir is None:
|
|
|
|
|
print('[!!] Auto-install skipped: agent directory not found.', flush=True)
|
|
|
|
|
return False
|
2026-04-22 20:49:28 +00:00
|
|
|
if not _trusted_agent_dir(agent_dir):
|
|
|
|
|
print('[!!] Auto-install skipped: agent directory failed trust check (check ownership/permissions).', flush=True)
|
|
|
|
|
return False
|
2026-04-10 07:42:02 +00:00
|
|
|
req_file = agent_dir / 'requirements.txt'
|
|
|
|
|
pyproject = agent_dir / 'pyproject.toml'
|
|
|
|
|
if req_file.exists():
|
|
|
|
|
install_args = [sys.executable, '-m', 'pip', 'install', '--quiet', '-r', str(req_file)]
|
|
|
|
|
print(f' Installing from {req_file} ...', flush=True)
|
|
|
|
|
elif pyproject.exists():
|
|
|
|
|
install_args = [sys.executable, '-m', 'pip', 'install', '--quiet', str(agent_dir)]
|
|
|
|
|
print(f' Installing from {agent_dir} (pyproject.toml) ...', flush=True)
|
|
|
|
|
else:
|
|
|
|
|
print('[!!] Auto-install skipped: no requirements.txt or pyproject.toml in agent dir.', flush=True)
|
|
|
|
|
return False
|
|
|
|
|
try:
|
|
|
|
|
result = subprocess.run(install_args, capture_output=True, text=True, timeout=120)
|
|
|
|
|
if result.returncode != 0:
|
|
|
|
|
print(f'[!!] pip install failed (exit {result.returncode}):', flush=True)
|
|
|
|
|
for line in (result.stderr or '').splitlines()[-10:]:
|
|
|
|
|
print(f' {line}', flush=True)
|
|
|
|
|
return False
|
|
|
|
|
print('[ok] pip install completed.', flush=True)
|
|
|
|
|
return True
|
|
|
|
|
except subprocess.TimeoutExpired:
|
|
|
|
|
print('[!!] Auto-install timed out after 120s.', flush=True)
|
|
|
|
|
return False
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f'[!!] Auto-install error: {e}', flush=True)
|
|
|
|
|
return False
|