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:
+10
-6
@@ -12,9 +12,7 @@
|
||||
# The config is sandbox/config/base.json, then sandbox/config/<agent>.json,
|
||||
# then your own ~/.config/federated-connector/sandbox/base.json and
|
||||
# <agent>.json, in the same format. Later files add to the lists in earlier
|
||||
# ones. Domains in FEDERATED_SANDBOX_DOMAINS (space-separated) are allowed too;
|
||||
# `configure agents` uses it for Hermes' gateway, whose messaging server
|
||||
# differs on each machine.
|
||||
# ones.
|
||||
|
||||
# Wrapped in { ...; exit; } so bash reads the whole script up front; update
|
||||
# replaces this file (git pull) while agents are running.
|
||||
@@ -51,6 +49,13 @@ case "$agent" in
|
||||
*) profile="$agent" ;;
|
||||
esac
|
||||
|
||||
# Hermes' gateway, started by a drop-in from before the Matrix server it may
|
||||
# reach moved to hermes.json: carry that server over
|
||||
if [ "$profile" = hermes ] && [ -n "$FEDERATED_SANDBOX_DOMAINS" ]; then
|
||||
source "$SANDBOX/hermes-homeserver.sh"
|
||||
hermes_carry_over_homeserver &> /dev/null
|
||||
fi
|
||||
|
||||
# Cline Desktop is a window, usually opened from the app menu in your home
|
||||
# directory, so it always starts in ~/Projects and can change projects there.
|
||||
# Its profile hides /run/user, where your session's D-Bus and keyring sockets
|
||||
@@ -109,9 +114,8 @@ done
|
||||
|
||||
# Outside the sandbox's writable paths, so the agent can't change its own rules
|
||||
settings="$SETTINGS_DIR/$agent.$$.json"
|
||||
if ! jq -s --arg domains "$FEDERATED_SANDBOX_DOMAINS" --arg socket "$gui_socket" \
|
||||
"$MERGE"' | .network.allowedDomains += ($domains | split(" ") | map(select(. != "")))
|
||||
| if $socket != "" then .filesystem.allowRead += [$socket] else . end' \
|
||||
if ! jq -s --arg socket "$gui_socket" \
|
||||
"$MERGE"' | if $socket != "" then .filesystem.allowRead += [$socket] else . end' \
|
||||
"${configs[@]}" > "$settings"; then
|
||||
echo "Couldn't read the sandbox config: ${configs[*]}"
|
||||
exit 1
|
||||
|
||||
@@ -67,6 +67,7 @@
|
||||
"~/.var/app",
|
||||
"~/.davfs2",
|
||||
"~/.winboat",
|
||||
"~/.config/federated-connector",
|
||||
"~/.claude",
|
||||
"~/.claude.json",
|
||||
"~/.hermes",
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Purpose : The Matrix server Hermes' gateway talks to, and whether you've let
|
||||
# its sandbox reach it. Source this file; used by configure agents,
|
||||
# configure hermes and sandbox/agent. Needs CONNECTOR.
|
||||
#
|
||||
# Hermes' settings (~/.hermes/.env) name the server, but Hermes can change them
|
||||
# from inside its sandbox, so they say only what Hermes wants to reach. The
|
||||
# server it may reach is in your own hermes.json, which no sandbox can write
|
||||
# (or read); configure hermes asks you before adding it there.
|
||||
|
||||
HERMES_ENV="$HOME/.hermes/.env"
|
||||
HERMES_SANDBOX_CONF="$HOME/.config/federated-connector/sandbox/hermes.json"
|
||||
HERMES_GATEWAY_UNIT="$HOME/.config/systemd/user/hermes-gateway.service"
|
||||
HERMES_GATEWAY_DROPIN="$HERMES_GATEWAY_UNIT.d/federated-connector-sandbox.conf"
|
||||
|
||||
# A plain host name or IPv4 address, so it can't be a wildcard or carry
|
||||
# characters that would change what a terminal shows when it's asked about
|
||||
hermes_valid_host() {
|
||||
[[ "$1" =~ ^[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?$ ]]
|
||||
}
|
||||
|
||||
# The host in MATRIX_HOMESERVER, e.g. matrix.example.org, or nothing
|
||||
hermes_homeserver() {
|
||||
local host
|
||||
host="$(sed -nE "s#^MATRIX_HOMESERVER=[\"']?https?://([^/:\"']+).*#\1#p" "$HERMES_ENV" 2> /dev/null | head -n 1)"
|
||||
hermes_valid_host "$host" && echo "$host"
|
||||
}
|
||||
|
||||
# hermes_homeserver_allowed HOST: true if Hermes' sandbox may reach HOST
|
||||
hermes_homeserver_allowed() {
|
||||
[ -n "$1" ] && jq -e --arg host "$1" '(.network.allowedDomains // []) | index($host) != null' \
|
||||
"$HERMES_SANDBOX_CONF" > /dev/null 2>&1
|
||||
}
|
||||
|
||||
# hermes_allow_homeserver HOST: let Hermes' sandbox reach HOST
|
||||
hermes_allow_homeserver() {
|
||||
source "$CONNECTOR/configure.d/lib/json.sh"
|
||||
edit_json "$HERMES_SANDBOX_CONF" '{}' --arg host "$1" \
|
||||
'.network.allowedDomains = ((.network.allowedDomains // []) - [$host] + [$host])'
|
||||
}
|
||||
|
||||
# The gateway's drop-in used to allow it whatever server Hermes' settings
|
||||
# named, through FEDERATED_SANDBOX_DOMAINS. Carry over the server an old
|
||||
# drop-in allows, so a gateway that works keeps working until configure agents
|
||||
# rewrites the drop-in without it. The drop-in is read, not the variable: the
|
||||
# sandbox can't write the drop-in, but an agent's children inherit the variable.
|
||||
hermes_carry_over_homeserver() {
|
||||
local host
|
||||
[ -f "$HERMES_GATEWAY_DROPIN" ] || return 0
|
||||
host="$(sed -nE 's#^Environment="?FEDERATED_SANDBOX_DOMAINS=([^" ]*)"?$#\1#p' "$HERMES_GATEWAY_DROPIN" | head -n 1)"
|
||||
hermes_valid_host "$host" || return 0
|
||||
hermes_homeserver_allowed "$host" && return 0
|
||||
hermes_allow_homeserver "$host"
|
||||
}
|
||||
Reference in New Issue
Block a user