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:
2026-11-16 16:13:32 -07:00
co-authored by Claude Opus 5.5
parent d974b91144
commit e991264ddc
7 changed files with 305 additions and 38 deletions
@@ -1,11 +1,12 @@
---
name: coredesktop-security-checks
description: Plan and progress for CoreDesktop's security test suite. The report receiver and report's redaction are tested; the checks still to write run in the smoketest VM (the agent sandbox, network exposure, what root runs, the install path). Use when resuming, continuing, checking the status of, or changing CoreDesktop's security or penetration tests.
description: Plan and progress for CoreDesktop's security test suite. The report receiver, report's redaction and the agent sandbox are tested; the checks still to write run in the smoketest VM (network exposure, what root runs, the install path). Use when resuming, continuing, checking the status of, or changing CoreDesktop's security or penetration tests.
---
# CoreDesktop's security checks
Written on 2026-11-16. Work out the current state before doing anything:
Written on 2026-11-16, updated the same day after step 3. Work out the current
state before doing anything:
```
git -C ~/Projects/coredesktop fetch && git -C ~/Projects/coredesktop log --oneline origin/main -15
@@ -22,7 +23,7 @@ leak data or give away control, in this order:
1. The report receiver, the only part on the public internet. **Done.**
2. `report`'s redaction, which decides what leaves a user's computer. **Done.**
3. The agent sandbox: the one place that runs code CoreDesktop doesn't trust.
3. The agent sandbox: the one place that runs code CoreDesktop doesn't trust. **Done.**
4. Network exposure from outside.
5. Files root runs, and secrets only root may read.
6. The install path (federated.sh, the `[federated]` repo).
@@ -64,32 +65,66 @@ Still open from these steps:
- **The daily limit.** The 500-a-day limit for everyone lets one sender block
reports for a day. That was left as a design trade-off; ask before changing it.
## Next: steps 3 to 6 in the smoketest VM
## Done: step 3 (the commits "Test the agent sandbox from inside..." and the Hermes homeserver fix)
Write them as one new scenario, `smoketest/scenarios/60-security.sh`,
`IMAGE=installed`, `TIER="nightly"`, with `assert_vm`/`refute_vm` checks (see
`smoketest/lib/scenario.sh` and `44-low-disk.sh` for the style). Where a
check needs a second machine, say so in the scenario and skip it, rather than
faking it. The VM comes from the `coredesktop-dev` sibling project (see
smoketest/README.md); it isn't in this repository.
`smoketest/scenarios/60-security.sh` runs a script as opencode, Hermes and
Cline Desktop would, through `sandbox/bin`'s wrappers with a fake agent first
on PATH, so it gets the real config, srt and proxy. About 130 checks; each
runs outside the sandbox too, where it must work. It checks that agents can't:
- read `~/.ssh`, `~/.gnupg`, the keyring, Bitwarden (package and Flatpak),
`~/.config/coredesktop`, `~/.winboat`, or `/etc/coredesktop/smb` via sudo
- write home, another project, `~/.bashrc`, `~/.local/bin`, systemd user
units, autostart, either sandbox config, srt's settings file, or the
project's `.git/hooks` and `.git/config`
- reach domains, LAN addresses or this machine's loopback servers that aren't
allowed, or anything without the proxy; an allowed LAN `host:port` works
and another port on that host doesn't
- see root's processes or GNOME Shell, or talk to the D-Bus session bus or
`systemctl --user`
### 3. The agent sandbox (sandbox/, `sandbox/agent`, `sandbox/bin`)
What it found, all fixed:
- every sandbox could read `~/.config/coredesktop` (the Core's domain and
user name); `sandbox/config/base.json` now hides it
- found by reading the code, not the scenario: Hermes could write
`MATRIX_HOMESERVER` in `~/.hermes/.env`, and `sandbox/setup.sh` then allowed
that host for the gateway. The allowed homeserver now lives in
`~/.config/coredesktop/sandbox/hermes.json`, which `configure hermes` asks
about (`sandbox/hermes-homeserver.sh`); a migration carried over the one
allowed before. Tested in `tests/sandbox.bats`.
- `edit_json` (`configure.d/lib/json.sh`) wrote nothing back to an empty file,
so `configure ai-servers` could report a server added that wasn't.
Run a script as an agent would (through `sandbox/bin`'s wrappers, the way
opencode and Hermes start) and check that each of these fails:
- reading `~/.ssh`, `~/.gnupg`, the GNOME keyring (`~/.local/share/keyrings`),
Bitwarden's data, `~/.config/coredesktop` (Core settings),
`/etc/coredesktop/smb` and `~/.winboat` (hidden unless `configure sandbox`
shows it)
- writing outside the folder it was started in and its own state
- reaching a LAN address that isn't allowed (`configure ai-servers` adds
allowed ones), and anything not through its proxy
- seeing other users' processes, or the D-Bus session bus (which could ask
GNOME to run things outside the sandbox)
Lessons for the next scenarios:
- A write check must look at the real file from outside. srt hides a denied
folder by mounting an empty, writable tmpfs over it, so a write in the
sandbox can "work" and go nowhere. See `write_blocked`.
- Every in-sandbox check prints a marker first, so a wrapper that fails to
start isn't counted as the sandbox blocking something.
- The scenario's `check` records a failure and goes on, and fails at the end,
so one run shows every gap. Keep that for steps 4 to 6.
- Outside checks can leave state behind (an empty config file broke a later
check once); make them look, not change.
Also check that the same reads work from outside the sandbox, so a passing
test means the sandbox blocked them, not that the files were missing.
`tests/sandbox.bats` checks only the sandbox's configuration.
## Running the smoketest
- `coredesktop-dev smoketest run 60-security`, outside Claude Code's sandbox
(it needs KVM). The first run builds the installed image (an hour or more);
later runs take about five minutes.
- It tests a commit. To test uncommitted changes, including new files, make a
throwaway commit without touching the branch: write a tree from a temporary
index (`GIT_INDEX_FILE=... git add -A; git write-tree; git commit-tree`)
and pass it with `--ref`. The scenario files themselves are read from the
working copy.
- The user's global git config has no name or email. Commits so far used
`git -c user.name="David Young" -c user.email=...` from the repo's history.
## Next: steps 4 to 6 in the smoketest VM
Add them to `smoketest/scenarios/60-security.sh` (or a sibling scenario if it
grows too long), with the same `check` style. Where a check needs a second
machine, say so in the scenario and skip it, rather than faking it. The VM
comes from the `coredesktop-dev` sibling project (see smoketest/README.md); it
isn't in this repository.
### 4. Network exposure
+39 -3
View File
@@ -6,16 +6,29 @@ TITLE="Hermes Agent"
# shellcheck disable=SC2034
DESCRIPTION="Choose a model provider and tools"
source "$COREDESKTOP/sandbox/hermes-homeserver.sh"
is_installed() {
[ -x "$HOME/.local/bin/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
@@ -26,3 +39,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
}
+17
View File
@@ -0,0 +1,17 @@
#!/bin/bash
# Hermes' gateway used to be allowed whatever Matrix server Hermes' own
# settings named, which Hermes can change from inside its sandbox. Now it
# reaches only a server in ~/.config/coredesktop/sandbox/hermes.json, which
# configure hermes asks about. Carry over the server the gateway reaches now,
# from its systemd drop-in, which the sandbox can't write, so a gateway that
# works keeps working; sandbox/setup.sh then rewrites the drop-in without it.
DROPIN="$HOME/.config/systemd/user/hermes-gateway.service.d/coredesktop-sandbox.conf"
[ -f "$DROPIN" ] || exit 0
COREDESKTOP="${COREDESKTOP:-$HOME/.local/share/coredesktop}"
source "$COREDESKTOP/sandbox/hermes-homeserver.sh"
host="$(sed -nE 's#^Environment="?COREDESKTOP_SANDBOX_DOMAINS=([^" ]*)"?$#\1#p' "$DROPIN" | head -n 1)"
[[ "$host" =~ ^[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?$ ]] || exit 0
hermes_homeserver_allowed "$host" && exit 0
hermes_allow_homeserver "$host"
+3 -6
View File
@@ -11,9 +11,7 @@
#
# The config is sandbox/config/base.json, then sandbox/config/<agent>.json,
# then your own ~/.config/coredesktop/sandbox/base.json and <agent>.json, in
# the same format. Later files add to the lists in earlier ones. Domains in
# COREDESKTOP_SANDBOX_DOMAINS (space-separated) are allowed too; setup.sh uses
# it for Hermes' gateway, whose messaging server differs on each machine.
# the same format. Later files add to the lists in earlier ones.
# Wrapped in { ...; exit; } so bash reads the whole script up front; update
# replaces this file (git pull) while agents are running.
@@ -114,9 +112,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 "$COREDESKTOP_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
+35
View File
@@ -0,0 +1,35 @@
#!/bin/bash
#
# Purpose : The Matrix server Hermes' gateway talks to, and whether you've let
# its sandbox reach it. Source this file; used by sandbox/setup.sh,
# configure hermes and a migration.
#
# 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;
# configure hermes asks you before adding it there.
HERMES_ENV="$HOME/.hermes/.env"
HERMES_SANDBOX_CONF="$HOME/.config/coredesktop/sandbox/hermes.json"
# The host in MATRIX_HOMESERVER, e.g. matrix.example.org, or nothing. Only a
# plain host name or IPv4 address counts, so it can't be a wildcard or carry
# characters that would change what a terminal shows when it's asked about.
hermes_homeserver() {
local host
host="$(sed -nE "s#^MATRIX_HOMESERVER=[\"']?https?://([^/:\"']+).*#\1#p" "$HERMES_ENV" 2> /dev/null | head -n 1)"
[[ "$host" =~ ^[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?$ ]] && 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 "${COREDESKTOP:-$HOME/.local/share/coredesktop}/configure.d/lib/json.sh"
edit_json "$HERMES_SANDBOX_CONF" '{}' --arg host "$1" \
'.network.allowedDomains = ((.network.allowedDomains // []) - [$host] + [$host])'
}
+9 -4
View File
@@ -5,7 +5,7 @@
# Claude Code's built-in sandbox. Run by install.sh and on every
# update (updates.d/72-sandbox.sh).
COREDESKTOP="$HOME/.local/share/coredesktop"
COREDESKTOP="${COREDESKTOP:-$HOME/.local/share/coredesktop}"
STATUS=0
# npm's global prefix is under /usr, so install to ~/.local; srt lands in ~/.local/bin.
@@ -66,18 +66,18 @@ fi
# 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/coredesktop/sandbox/hermes.json.
GATEWAY_UNIT="$HOME/.config/systemd/user/hermes-gateway.service"
if [ -f "$GATEWAY_UNIT" ]; then
if [ -x "$HOME/.local/bin/srt" ]; then
homeserver="$(sed -nE "s#^MATRIX_HOMESERVER=[\"']?https?://([^/:\"']+).*#\1#p" "$HOME/.hermes/.env" 2> /dev/null | head -n 1)"
dropin="$GATEWAY_UNIT.d/coredesktop-sandbox.conf"
tmp="$(mktemp)"
cat > "$tmp" << EOF
# Written by CoreDesktop (sandbox/setup.sh): run Hermes' gateway in the sandbox
[Service]
Environment="COREDESKTOP_SANDBOX_DOMAINS=$homeserver"
ExecStart=
ExecStart=%h/.local/share/coredesktop/sandbox/bin/hermes gateway run
EOF
@@ -88,6 +88,11 @@ EOF
mkdir -p "$(dirname "$dropin")" && mv -f "$tmp" "$dropin" && chmod 644 "$dropin" \
&& systemctl --user daemon-reload || STATUS=1
fi
source "$COREDESKTOP/sandbox/hermes-homeserver.sh"
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
else
echo "Hermes' gateway isn't sandboxed yet: the sandbox (srt) isn't installed."
STATUS=1
+142
View File
@@ -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" ]
}