From 1445ad7bb1b6f9032f4ff3fab49a83f5aa2c01d0 Mon Sep 17 00:00:00 2001 From: David Young Date: Sat, 28 Nov 2026 18:55:37 -0700 Subject: [PATCH] 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 --- PLAN.md | 23 ++++---- README.md | 6 +-- configure.d/30-agents.sh | 28 ++++++---- configure.d/33-hermes.sh | 41 ++++++++++++-- configure.d/lib/agents.sh | 5 +- configure.d/lib/json.sh | 5 +- sandbox/agent | 16 +++--- sandbox/config/base.json | 1 + sandbox/hermes-homeserver.sh | 55 +++++++++++++++++++ smoketest/README.md | 3 +- smoketest/scenarios/50-sandbox.sh | 5 ++ smoketest/scenarios/55-hermes-homeserver.sh | 59 +++++++++++++++++++++ 12 files changed, 210 insertions(+), 37 deletions(-) create mode 100644 sandbox/hermes-homeserver.sh create mode 100644 smoketest/scenarios/55-hermes-homeserver.sh diff --git a/PLAN.md b/PLAN.md index f5f18b5..e9ed971 100644 --- a/PLAN.md +++ b/PLAN.md @@ -48,12 +48,21 @@ git -C ../coredesktop log --oneline ..HEAD -- configure configure.d sand Since `4a4a3bd`, to look at: - [ ] `16e5a9c` Find storage and AI servers on the tailnet, not only over mDNS -- [ ] `e991264` Let Hermes' gateway reach only a Matrix server you've allowed -- [ ] `d974b91` configure: treat an empty settings file like a missing one -- [ ] `05b13b2` Test the agent sandbox from inside, and hide CoreDesktop's settings +- [ ] `05b13b2` Test the agent sandbox from inside: port + `smoketest/scenarios/60-security.sh`, which runs a script through + sandbox/bin's wrappers and checks what it can't read, write or reach, + and its later checks from `d5925fa` (what root runs is root's alone) and + `53c6c8b` (the install path). Hiding the connector's settings from + agents is done. - [ ] `4fb3539` Claude notifications: clicking one brings back the app Claude runs in -- [ ] `9dc5eb1` Upgrade safeguards, hardware setup, sign-in options and Taildrop: - Taildrop is likely to apply; the rest is probably Arch-only +- [ ] `9dc5eb1` Upgrade safeguards, hardware setup, sign-in options and Taildrop. + To port: `configure security-key` and `configure fingerprint` (through + `pam-auth-update` and fprintd on Pop!_OS, not by editing PAM files), + Taildrop's service that saves files sent here to Downloads (COSMIC Files + has no Scripts menu for sending), keeping the computer awake during + `update`, and the captive portal script that pauses a Tailscale exit + node. Arch only: the pacman guard, unowned files, `hardware.sh` + (System76 does its own). - [ ] `ffbaf71` Keep other machines on the network from reaching Docker containers: only if the connector ever installs Docker @@ -61,10 +70,6 @@ Arch or GNOME only, nothing to port: `e14a345` and `ba4dc17` (login screen logo), `1e2581d` (kernel modules), `d456e31` (initramfs), `5d7db2c` (reboot after update: Pop!_OS' own updates handle this). -- [ ] CoreDesktop's PLAN.md still plans to merge Pop!_OS support into its - `configure` and archive this repo ("Merge Pop!_OS support into - configure"). Update it there to say this project stays separate. - ## Pop!_OS-specific - [ ] The manual checks in [TEST.md](TEST.md) on a real Pop!_OS 24.04 machine, diff --git a/README.md b/README.md index fb2b44d..b9a3f21 100644 --- a/README.md +++ b/README.md @@ -127,18 +127,18 @@ restic snapshots opencode, Hermes and Cline Desktop run in Anthropic's sandbox runtime (srt), which uses bubblewrap. In the sandbox, an agent: - can change files only in the folder you start it in (plus its own settings, /tmp and a few package caches), so start it from a project folder -- can't read your SSH keys, keyrings, password managers, browser profiles, Evolution's mail, calendars and contacts, Thunderbird, Element, cloud and Git credentials, or the other agents' logins +- can't read your SSH keys, keyrings, password managers, browser profiles, Evolution's mail, calendars and contacts, Thunderbird, Element, cloud and Git credentials, the other agents' logins, or Federated Connector's settings (your Core's domain and user name, and what the sandbox allows) - can reach only AI providers and common developer sites: GitHub, Gitea, npm, PyPI, crates.io, RubyGems, the Go module proxy, and Ubuntu's and Pop!_OS' sites Typing `opencode`, `hermes` or `hermes-agent` runs it in the sandbox: `configure agents` adds a block at the end of ~/.bashrc that puts `sandbox/bin` first on PATH. To run one without the sandbox, type `unsandboxed opencode`. Claude Code uses its own built-in sandbox, also on bubblewrap, for every command it runs, which `configure agents` turns on (see `/sandbox` inside Claude). If you had no Claude Code settings, it also adds notifications for when Claude needs you and when it's done. -If Hermes' gateway is set up as a service, `configure agents` runs it in the sandbox too, and lets it reach the Matrix server in Hermes' settings. If Cline Desktop is installed, its entry in the app library opens it in the sandbox. +If Hermes' gateway is set up as a service, `configure agents` runs it in the sandbox too. Hermes can change its own settings from inside the sandbox, so the Matrix server they name isn't trusted on its own: `configure hermes` asks whether it's your server and may be reached, and keeps your answer in `~/.config/federated-connector/sandbox/hermes.json`, which no sandbox can read or write. For other messaging services, add their domains to that file as shown below. If Cline Desktop is installed, its entry in the app library opens it in the sandbox. srt needs Node.js 22.12 or later, and Pop!_OS 24.04's is older. Unless a new enough one is installed already, `configure agents` downloads the current LTS from nodejs.org into `~/.local/share/federated-connector/node`, checks it against the SHA-256 sums nodejs.org publishes, and uses it only for srt. Ubuntu 24.04 lets only programs AppArmor allows make the user namespaces bubblewrap needs. If that stops the sandbox here, `configure agents` offers to add an AppArmor profile for `/usr/bin/bwrap` (`/etc/apparmor.d/federated-connector-bwrap`), as Ubuntu suggests. -To allow more, put a file in the same format as `sandbox/config/base.json` in `~/.config/federated-connector/sandbox/`: `base.json` for every agent, or `.json` for one. Lists in it are added to the built-in ones. Domains in `FEDERATED_SANDBOX_DOMAINS` (space-separated) are allowed too. +To allow more, put a file in the same format as `sandbox/config/base.json` in `~/.config/federated-connector/sandbox/`: `base.json` for every agent, or `.json` for one. Lists in it are added to the built-in ones. ## Where things are diff --git a/configure.d/30-agents.sh b/configure.d/30-agents.sh index a950b9f..2b5062f 100644 --- a/configure.d/30-agents.sh +++ b/configure.d/30-agents.sh @@ -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 diff --git a/configure.d/33-hermes.sh b/configure.d/33-hermes.sh index c33f92b..1a6daf1 100644 --- a/configure.d/33-hermes.sh +++ b/configure.d/33-hermes.sh @@ -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 +} diff --git a/configure.d/lib/agents.sh b/configure.d/lib/agents.sh index dd08475..1f58fc2 100644 --- a/configure.d/lib/agents.sh +++ b/configure.d/lib/agents.sh @@ -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 diff --git a/configure.d/lib/json.sh b/configure.d/lib/json.sh index 6a15c10..7fa449d 100644 --- a/configure.d/lib/json.sh +++ b/configure.d/lib/json.sh @@ -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" diff --git a/sandbox/agent b/sandbox/agent index dccb162..03431d7 100755 --- a/sandbox/agent +++ b/sandbox/agent @@ -12,9 +12,7 @@ # The config is sandbox/config/base.json, then sandbox/config/.json, # then your own ~/.config/federated-connector/sandbox/base.json and # .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 diff --git a/sandbox/config/base.json b/sandbox/config/base.json index d148ca8..4b40f75 100644 --- a/sandbox/config/base.json +++ b/sandbox/config/base.json @@ -67,6 +67,7 @@ "~/.var/app", "~/.davfs2", "~/.winboat", + "~/.config/federated-connector", "~/.claude", "~/.claude.json", "~/.hermes", diff --git a/sandbox/hermes-homeserver.sh b/sandbox/hermes-homeserver.sh new file mode 100644 index 0000000..2e69c7b --- /dev/null +++ b/sandbox/hermes-homeserver.sh @@ -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" +} diff --git a/smoketest/README.md b/smoketest/README.md index 285bd19..56437e0 100644 --- a/smoketest/README.md +++ b/smoketest/README.md @@ -32,7 +32,8 @@ Each run writes its logs to `smoketest/results/