Let Hermes' gateway reach only a Matrix server you've allowed
sandbox/setup.sh allowed Hermes' gateway the host in MATRIX_HOMESERVER from ~/.hermes/.env, which Hermes can write from inside its sandbox. On the next update, Hermes could have opened the way out to any host it chose. The allowed server is now in ~/.config/coredesktop/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; update says so. Only a plain host name counts, so it can't be a wildcard or carry escape codes into the question. A migration carries over the server the gateway's systemd drop-in allows now, so a working gateway keeps working. sandbox/agent no longer takes domains from COREDESKTOP_SANDBOX_DOMAINS, which nothing else used. The security checks skill now records step 3 as done. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -84,3 +84,145 @@ setup_configure() {
|
||||
is_installed
|
||||
is_configured
|
||||
}
|
||||
|
||||
# --- Hermes' Matrix server ----------------------------------------------------------
|
||||
|
||||
# Hermes' settings, with a Matrix server and an API key
|
||||
hermes_env() {
|
||||
mkdir -p "$HOME/.hermes"
|
||||
printf 'OPENROUTER_API_KEY=sk-test\nMATRIX_HOMESERVER=%s\n' "$1" > "$HOME/.hermes/.env"
|
||||
}
|
||||
|
||||
allow_in_hermes_json() {
|
||||
mkdir -p "$HOME/.config/coredesktop/sandbox"
|
||||
jq -n --arg h "$1" '{network: {allowedDomains: [$h]}}' > "$HOME/.config/coredesktop/sandbox/hermes.json"
|
||||
}
|
||||
|
||||
@test "hermes_homeserver takes the host from Hermes' settings, and only a plain host" {
|
||||
source "$REPO/sandbox/hermes-homeserver.sh"
|
||||
hermes_env 'https://matrix.example.org:8448/'
|
||||
[ "$(hermes_homeserver)" = "matrix.example.org" ]
|
||||
hermes_env '"http://10.0.0.5"'
|
||||
[ "$(hermes_homeserver)" = "10.0.0.5" ]
|
||||
for bad in 'https://*.example.org' $'https://evil.example\e[2Korg' 'https://-x.org' 'ftp://matrix.example.org'; do
|
||||
hermes_env "$bad"
|
||||
[ -z "$(hermes_homeserver)" ]
|
||||
done
|
||||
}
|
||||
|
||||
@test "the sandbox no longer takes extra domains from the environment, but does from hermes.json" {
|
||||
cp "$REPO/sandbox/config/hermes.json" "$SANDBOX/config/"
|
||||
cp "$REPO/sandbox/hermes-bridge" "$SANDBOX/"
|
||||
ln -s "$REPO/sandbox/agent" "$SANDBOX/bin/hermes"
|
||||
stub hermes 'true'
|
||||
allow_in_hermes_json matrix.example.org
|
||||
COREDESKTOP_SANDBOX_DOMAINS=evil.example.com run "$SANDBOX/bin/hermes"
|
||||
[ "$status" -eq 0 ]
|
||||
jq -e '.network.allowedDomains | index("matrix.example.org")' "$SETTINGS_COPY"
|
||||
run jq -e '.network.allowedDomains | index("evil.example.com")' "$SETTINGS_COPY"
|
||||
[ "$status" -ne 0 ]
|
||||
}
|
||||
|
||||
# Run sandbox/setup.sh with Hermes and its gateway set up, and nothing to install
|
||||
run_setup() {
|
||||
export COREDESKTOP="$REPO"
|
||||
stub npm
|
||||
stub systemctl
|
||||
mkdir -p "$HOME/.local/bin" "$HOME/.config/systemd/user"
|
||||
printf '#!/bin/sh\n' > "$HOME/.local/bin/srt"
|
||||
printf '#!/bin/sh\n' > "$HOME/.local/bin/hermes"
|
||||
chmod +x "$HOME/.local/bin/srt" "$HOME/.local/bin/hermes"
|
||||
: > "$HOME/.config/systemd/user/hermes-gateway.service"
|
||||
run bash "$REPO/sandbox/setup.sh"
|
||||
DROPIN="$HOME/.config/systemd/user/hermes-gateway.service.d/coredesktop-sandbox.conf"
|
||||
}
|
||||
|
||||
@test "setup.sh doesn't let Hermes' settings choose what its gateway reaches, and says what to do" {
|
||||
hermes_env https://matrix.example.org
|
||||
run_setup
|
||||
[ "$status" -eq 0 ]
|
||||
grep -q '^ExecStart=%h/.local/share/coredesktop/sandbox/bin/hermes gateway run$' "$DROPIN"
|
||||
run grep -q 'matrix.example.org\|COREDESKTOP_SANDBOX_DOMAINS' "$DROPIN"
|
||||
[ "$status" -ne 0 ]
|
||||
[[ "$output" == "" ]]
|
||||
run bash "$REPO/sandbox/setup.sh"
|
||||
[[ "$output" == *"Matrix server matrix.example.org, which its gateway can't reach until you allow it: configure hermes"* ]]
|
||||
}
|
||||
|
||||
@test "setup.sh says nothing about a Matrix server you've allowed" {
|
||||
hermes_env https://matrix.example.org
|
||||
allow_in_hermes_json matrix.example.org
|
||||
run_setup
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" != *"configure hermes"* ]]
|
||||
}
|
||||
|
||||
setup_hermes_configure() {
|
||||
export COREDESKTOP="$REPO"
|
||||
unsandboxed() { echo "unsandboxed $*" >> "$STUB_LOG"; }
|
||||
source "$REPO/configure.d/32-hermes.sh"
|
||||
}
|
||||
|
||||
@test "configure hermes is to do while Hermes names a Matrix server you haven't allowed" {
|
||||
setup_hermes_configure
|
||||
hermes_env https://matrix.example.org
|
||||
run is_configured
|
||||
[ "$status" -ne 0 ]
|
||||
allow_in_hermes_json matrix.example.org
|
||||
is_configured
|
||||
}
|
||||
|
||||
@test "configure hermes asks before letting Hermes reach its Matrix server, and only about that" {
|
||||
setup_hermes_configure
|
||||
hermes_env https://matrix.example.org
|
||||
|
||||
confirm() { return 1; }
|
||||
run run_configure < /dev/null
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"Hermes can't reach matrix.example.org"* ]]
|
||||
[[ "$output" != *"Which provider?"* ]]
|
||||
run hermes_homeserver_allowed matrix.example.org
|
||||
[ "$status" -ne 0 ]
|
||||
|
||||
confirm() { return 0; }
|
||||
run run_configure < /dev/null
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"Hermes can reach matrix.example.org"* ]]
|
||||
hermes_homeserver_allowed matrix.example.org
|
||||
not_called unsandboxed
|
||||
}
|
||||
|
||||
@test "configure hermes asks for a provider when there's nothing else to do" {
|
||||
setup_hermes_configure
|
||||
hermes_env https://matrix.example.org
|
||||
allow_in_hermes_json matrix.example.org
|
||||
run run_configure <<< "1"
|
||||
[ "$status" -eq 0 ]
|
||||
called unsandboxed hermes setup --portal
|
||||
}
|
||||
|
||||
@test "the migration keeps the Matrix server the gateway reaches now, not one Hermes names" {
|
||||
export COREDESKTOP="$REPO"
|
||||
dropin="$HOME/.config/systemd/user/hermes-gateway.service.d/coredesktop-sandbox.conf"
|
||||
mkdir -p "$(dirname "$dropin")"
|
||||
printf '[Service]\nEnvironment="COREDESKTOP_SANDBOX_DOMAINS=matrix.example.org"\nExecStart=\n' > "$dropin"
|
||||
hermes_env https://evil.example.com
|
||||
run bash "$REPO/migrations/1794870061.sh"
|
||||
[ "$status" -eq 0 ]
|
||||
[ "$(jq -c '.network.allowedDomains' "$HOME/.config/coredesktop/sandbox/hermes.json")" = '["matrix.example.org"]' ]
|
||||
# Again: no change
|
||||
run bash "$REPO/migrations/1794870061.sh"
|
||||
[ "$(jq -c '.network.allowedDomains' "$HOME/.config/coredesktop/sandbox/hermes.json")" = '["matrix.example.org"]' ]
|
||||
}
|
||||
|
||||
@test "the migration does nothing without a gateway drop-in, or one with no server" {
|
||||
export COREDESKTOP="$REPO"
|
||||
run bash "$REPO/migrations/1794870061.sh"
|
||||
[ "$status" -eq 0 ]
|
||||
dropin="$HOME/.config/systemd/user/hermes-gateway.service.d/coredesktop-sandbox.conf"
|
||||
mkdir -p "$(dirname "$dropin")"
|
||||
printf '[Service]\nEnvironment="COREDESKTOP_SANDBOX_DOMAINS="\n' > "$dropin"
|
||||
run bash "$REPO/migrations/1794870061.sh"
|
||||
[ "$status" -eq 0 ]
|
||||
[ ! -e "$HOME/.config/coredesktop/sandbox/hermes.json" ]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user