Let Hermes' gateway reach only a Matrix server you've allowed

Ports CoreDesktop's e991264, 05b13b2 and d974b91.

configure agents allowed Hermes' gateway the host in MATRIX_HOMESERVER
from ~/.hermes/.env, which Hermes can write from inside its sandbox, so
it could open the way out to any host it chose. The allowed server is
now in ~/.config/federated-connector/sandbox/hermes.json, which no
sandbox can write. configure hermes is to do while Hermes names a server
you haven't allowed, and asks about only that; configure agents says so.
Only a plain host name counts, so it can't be a wildcard or carry escape
codes into the question. sandbox/agent no longer reads
FEDERATED_SANDBOX_DOMAINS. The server an old drop-in allows is carried
over, read from the drop-in itself, when the gateway starts and when
configure agents rewrites it, so a working gateway keeps working.

Agents could read ~/.config/federated-connector, which has the Core's
domain and user name; base.json now hides it.

edit_json treats an empty settings file like a missing one: jq reads no
JSON from an empty file and writes nothing back, so configure ai-servers
said a server was added while the sandbox still blocked it.

agent_path joined the last folder on PATH to ~/.local/bin, so an agent
there wasn't found when ~/.local/bin wasn't on PATH (e.g. in a service).

Smoketest: 55-hermes-homeserver, and 50-sandbox checks that an agent
can't read the connector's settings. PLAN.md drops what's done.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-28 18:55:37 -07:00
co-authored by Claude Opus 5.5
parent cf31ffc929
commit 1445ad7bb1
12 changed files with 210 additions and 37 deletions
+17 -11
View File
@@ -123,32 +123,38 @@ claude_notifications() {
# Hermes' gateway acts on messages from outside this machine, so it runs in
# the sandbox too: a systemd drop-in starts it through sandbox/bin/hermes. It
# also needs to reach the Matrix server in Hermes' settings, which differs on
# each machine; for other messaging services, add their domains to
# each machine. Hermes can change those settings itself, so the server isn't
# allowed from them; configure hermes asks you (see sandbox/hermes-homeserver.sh).
# For other messaging services, add their domains to
# ~/.config/federated-connector/sandbox/hermes.json.
GATEWAY_UNIT="$HOME/.config/systemd/user/hermes-gateway.service"
GATEWAY_DROPIN="$GATEWAY_UNIT.d/federated-connector-sandbox.conf"
source "$CONNECTOR/sandbox/hermes-homeserver.sh"
gateway_dropin() {
local homeserver
homeserver="$(sed -nE "s#^MATRIX_HOMESERVER=[\"']?https?://([^/:\"']+).*#\1#p" "$HOME/.hermes/.env" 2> /dev/null | head -n 1)"
cat << EOF
# Written by Federated Connector (configure agents): run Hermes' gateway in the sandbox
[Service]
Environment="FEDERATED_SANDBOX_DOMAINS=$homeserver"
ExecStart=
ExecStart=$SANDBOX_BIN/hermes gateway run
EOF
}
gateway_ok() {
[ ! -f "$GATEWAY_UNIT" ] || [ "$(cat "$GATEWAY_DROPIN" 2> /dev/null)" = "$(gateway_dropin)" ]
[ ! -f "$HERMES_GATEWAY_UNIT" ] || [ "$(cat "$HERMES_GATEWAY_DROPIN" 2> /dev/null)" = "$(gateway_dropin)" ]
}
sandbox_gateway() {
gateway_ok && return 0
mkdir -p "$(dirname "$GATEWAY_DROPIN")" && gateway_dropin > "$GATEWAY_DROPIN" \
&& systemctl --user daemon-reload || return 1
echo "Hermes' gateway runs in the sandbox (from its next start)."
local homeserver
if ! gateway_ok; then
hermes_carry_over_homeserver
mkdir -p "$(dirname "$HERMES_GATEWAY_DROPIN")" && gateway_dropin > "$HERMES_GATEWAY_DROPIN" \
&& systemctl --user daemon-reload || return 1
echo "Hermes' gateway runs in the sandbox (from its next start)."
fi
[ -f "$HERMES_GATEWAY_UNIT" ] || return 0
homeserver="$(hermes_homeserver)"
if [ -n "$homeserver" ] && ! hermes_homeserver_allowed "$homeserver"; then
echo "Hermes' settings name the Matrix server $homeserver, which its gateway can't reach until you allow it: configure hermes"
fi
}
# Cline Desktop is opened from the app menu, so an entry of the same name in
+38 -3
View File
@@ -7,17 +7,29 @@ TITLE="Hermes Agent"
DESCRIPTION="Choose a model provider and tools"
source "$CONNECTOR/configure.d/lib/agents.sh"
source "$CONNECTOR/sandbox/hermes-homeserver.sh"
is_installed() {
agent_installed hermes
}
# Signed in with OAuth (e.g. Nous Portal), or an API key is set
is_configured() {
[ -s "$HOME/.hermes/auth.json" ] || grep -qE '^[A-Z_]*API_KEY=.+' "$HOME/.hermes/.env"
has_provider() {
[ -s "$HOME/.hermes/auth.json" ] || grep -qE '^[A-Z_]*API_KEY=.+' "$HERMES_ENV" 2> /dev/null
}
run_configure() {
# The Matrix server in Hermes' settings that its sandbox may not reach yet
pending_homeserver() {
local host
host="$(hermes_homeserver)"
[ -n "$host" ] && ! hermes_homeserver_allowed "$host" && echo "$host"
}
is_configured() {
has_provider && [ -z "$(pending_homeserver)" ]
}
choose_provider() {
echo " 1) Nous Portal (one sign-in covers the model, web search, images and more)"
echo " 2) Another provider (OpenRouter, OpenAI, Anthropic or your own endpoint)"
local choice
@@ -28,3 +40,26 @@ run_configure() {
*) echo "Choose 1 or 2."; return 1 ;;
esac
}
# Hermes can name a Matrix server in its settings from inside its sandbox, so
# its gateway reaches only one you've said yes to here
allow_homeserver() {
local host
host="$(pending_homeserver)" || return 0
echo "Hermes' settings say its gateway talks to the Matrix server $host."
echo "Hermes can change those settings itself, so it can't reach a server until you allow it."
if confirm "Is $host your Matrix server, and may Hermes reach it?"; then
hermes_allow_homeserver "$host" || return 1
echo "Hermes can reach $host. Restart its gateway to use it: systemctl --user restart hermes-gateway"
else
echo "Hermes can't reach $host. If you didn't set it, check MATRIX_HOMESERVER in $HERMES_ENV."
fi
}
run_configure() {
# Only the server is new: don't ask about the provider again
if ! has_provider || [ -z "$(pending_homeserver)" ]; then
choose_provider || return 1
fi
allow_homeserver
}
+3 -2
View File
@@ -14,9 +14,10 @@ AGENT_DIRS="$HOME/.local/bin:$HOME/.opencode/bin:$HOME/.hermes/bin:/usr/local/bi
# The real command for an agent: the first on PATH, or in AGENT_DIRS, that
# isn't in sandbox/bin
agent_path() {
local dir bin_dir IFS=:
local dir bin_dir search="$PATH:$AGENT_DIRS" IFS=:
bin_dir="$(realpath "$SANDBOX_BIN" 2> /dev/null)"
for dir in $PATH:$AGENT_DIRS; do
# Split one variable: the ":" between two would join their neighbouring folders
for dir in $search; do
[ -n "$dir" ] && [ -d "$dir" ] || continue
[ "$(realpath "$dir")" = "$bin_dir" ] && continue
if [ -x "$dir/$1" ] && [ ! -d "$dir/$1" ]; then
+3 -2
View File
@@ -2,13 +2,14 @@
#
# Purpose : Edit JSON settings files for configure.d items. Source this file.
# Edit a JSON file in place with jq, creating it from $2 if it's missing.
# Edit a JSON file in place with jq, creating it from $2 if it's missing or
# empty (jq reads no JSON from an empty file, and would write nothing back).
# Leaves the file alone if it isn't valid JSON or jq fails.
edit_json() {
local file="$1" empty="$2" tmp
shift 2
mkdir -p "$(dirname "$file")" || return 1
[ -f "$file" ] || echo "$empty" > "$file"
[ -s "$file" ] || echo "$empty" > "$file"
tmp="$(mktemp "$file.XXXXXX")" || return 1
if jq "$@" "$file" > "$tmp"; then
mv -f "$tmp" "$file"