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
@@ -0,0 +1,59 @@
# shellcheck shell=bash
# Scenarios set what the runner and items read
# shellcheck disable=SC2034
DESCRIPTION="Hermes' gateway reaches only a Matrix server you've allowed; edit_json and empty files"
TIER=quick
scenario() {
local out
source "$CONNECTOR/configure.d/30-agents.sh"
source "$CONNECTOR/configure.d/lib/json.sh"
mkdir -p "$HOME/.hermes" "$(dirname "$HERMES_GATEWAY_UNIT")"
step "edit_json"
mkdir -p "$CONF_DIR/sandbox"
: > "$CONF_DIR/sandbox/test.json"
check "it edits an empty file" edit_json "$CONF_DIR/sandbox/test.json" '{}' '.a = 1'
check_eq "from its default" 1 "$(jq .a "$CONF_DIR/sandbox/test.json")"
rm -f "$CONF_DIR/sandbox/test.json"
step "What Hermes' settings name"
echo 'MATRIX_HOMESERVER=https://matrix.smoke.invalid:8448/' > "$HERMES_ENV"
check_eq "the host in MATRIX_HOMESERVER" matrix.smoke.invalid "$(hermes_homeserver)"
echo 'MATRIX_HOMESERVER=https://*.smoke.invalid' > "$HERMES_ENV"
check_eq "not a wildcard" "" "$(hermes_homeserver)"
printf 'MATRIX_HOMESERVER=https://evil\033[2Jhost\n' > "$HERMES_ENV"
check_eq "not escape codes" "" "$(hermes_homeserver)"
step "configure hermes asks before it's reached"
source "$CONNECTOR/configure.d/33-hermes.sh"
echo 'MATRIX_HOMESERVER=https://matrix.smoke.invalid' > "$HERMES_ENV"
echo 'OPENROUTER_API_KEY=x' >> "$HERMES_ENV"
check_not "a server not allowed yet isn't" hermes_homeserver_allowed matrix.smoke.invalid
check_eq "it's to do for that server" matrix.smoke.invalid "$(pending_homeserver)"
check_not "Hermes is to do" is_configured
check "run_configure asks only about the server" run_configure
check "the server is allowed" hermes_homeserver_allowed matrix.smoke.invalid
check "Hermes is done" is_configured
echo 'MATRIX_HOMESERVER=https://other.smoke.invalid' > "$HERMES_ENV"
check_not "a server Hermes names later isn't allowed" hermes_homeserver_allowed other.smoke.invalid
step "The gateway's drop-in"
# No systemd user manager in a container
systemctl() { :; }
rm -f "$HERMES_SANDBOX_CONF"
touch "$HERMES_GATEWAY_UNIT"
mkdir -p "$(dirname "$HERMES_GATEWAY_DROPIN")"
printf '[Service]\nEnvironment="FEDERATED_SANDBOX_DOMAINS=matrix.smoke.invalid"\nExecStart=\nExecStart=x\n' > "$HERMES_GATEWAY_DROPIN"
check_not "an old drop-in isn't ok" gateway_ok
out="$(sandbox_gateway 2>&1)"
check "the server an old drop-in allowed is carried over" hermes_homeserver_allowed matrix.smoke.invalid
check "the new drop-in is ok" gateway_ok
check_not "it allows no domains itself" grep -q FEDERATED_SANDBOX_DOMAINS "$HERMES_GATEWAY_DROPIN"
check "it says the server Hermes names now needs allowing" grep -q "other.smoke.invalid.*configure hermes" <<< "$out"
printf '[Service]\nEnvironment="FEDERATED_SANDBOX_DOMAINS=*"\n' > "$HERMES_GATEWAY_DROPIN"
rm -f "$HERMES_SANDBOX_CONF"
hermes_carry_over_homeserver
check_not "a wildcard isn't carried over" test -s "$HERMES_SANDBOX_CONF"
rm -rf "$HERMES_GATEWAY_UNIT" "$HERMES_GATEWAY_UNIT.d" "$HOME/.hermes"
}