Files
connector-popos/configure.d/30-agents.sh
T
davidandClaude Opus 5.5 1445ad7bb1 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>
2026-11-28 18:55:37 -07:00

267 lines
9.6 KiB
Bash

#!/bin/bash
# configure reads TITLE and DESCRIPTION
# shellcheck disable=SC2034
TITLE="AI agents"
# shellcheck disable=SC2034
DESCRIPTION="Install Claude Code, opencode and Hermes, sandboxed"
# Exists once you've run this, even if you installed no agents
AGENTS_CONF="$CONF_DIR/agents"
CLAUDE_SETTINGS="$HOME/.claude/settings.json"
BASE_CONFIG="$CONNECTOR/sandbox/config/base.json"
BASHRC="$HOME/.bashrc"
BLOCK_START="# >>> Federated Connector: agents >>>"
BLOCK_END="# <<< Federated Connector: agents <<<"
APPARMOR_PROFILE="/etc/apparmor.d/federated-connector-bwrap"
source "$CONNECTOR/configure.d/lib/json.sh"
source "$CONNECTOR/configure.d/lib/tools.sh"
# name|title|installer|installer's arguments. Each agent's own installer, from
# its makers. --non-interactive skips Hermes' setup questions and its gateway
# service; configure hermes chooses a model provider afterwards.
AGENTS=(
"claude|Claude Code (Anthropic)|https://claude.ai/install.sh|"
"opencode|opencode|https://opencode.ai/install|"
"hermes|Hermes Agent (Nous Research)|https://hermes-agent.nousresearch.com/install.sh|--non-interactive"
)
# srt (and Claude Code's own sandbox) use bubblewrap for the files an agent
# can see, socat to link it to the network proxy, and ripgrep to find the
# files it must not change. Node.js' own builds need libatomic.
SANDBOX_PACKAGES=(bubblewrap socat ripgrep libatomic1)
# --- Typing an agent's name runs it in the sandbox -------------------------------
# The block goes last in ~/.bashrc, so agents' installers adding their own
# folders to PATH don't come before it
path_block() {
cat << EOF
$BLOCK_START
# Typing an AI agent's name runs it in the sandbox: configure agents
export PATH="$SANDBOX_BIN:\$PATH"
$BLOCK_END
EOF
}
on_path() {
[ -f "$BASHRC" ] && [ "$(grep -v '^[[:space:]]*$' "$BASHRC" | tail -n 4)" = "$(path_block)" ]
}
put_on_path() {
local tmp
touch "$BASHRC" || return 1
tmp="$(mktemp)" || return 1
awk -v start="$BLOCK_START" -v end="$BLOCK_END" \
'$0 == start { skip = 1 } !skip { print } $0 == end { skip = 0 }' "$BASHRC" > "$tmp"
{ cat "$tmp"; echo; path_block; } > "$BASHRC"
rm -f "$tmp"
echo "Agents run in the sandbox in new terminals. To run one without it: unsandboxed <agent>"
}
# --- bubblewrap and Ubuntu's AppArmor ---------------------------------------------
# Ubuntu 24.04 lets only programs AppArmor allows make the user namespaces
# bubblewrap needs, and has no rule for bubblewrap itself
bwrap_works() {
bwrap --ro-bind / / --unshare-user --unshare-net true 2> /dev/null
}
allow_bwrap() {
echo "Ubuntu's AppArmor doesn't let bubblewrap, which the sandbox is built on, make"
echo "user namespaces. A profile for /usr/bin/bwrap allows it, as Ubuntu suggests."
confirm_yes "Add it ($APPARMOR_PROFILE)?" || return 1
sudo tee "$APPARMOR_PROFILE" > /dev/null << 'EOF' || return 1
# Written by Federated Connector (configure agents): let bubblewrap, which the
# AI agent sandbox uses, make user namespaces
abi <abi/4.0>,
include <tunables/global>
profile federated-connector-bwrap /usr/bin/bwrap flags=(unconfined) {
userns,
include if exists <local/federated-connector-bwrap>
}
EOF
sudo apparmor_parser -r "$APPARMOR_PROFILE" || return 1
bwrap_works || { echo "bubblewrap still can't make user namespaces."; return 1; }
}
# --- Claude Code -------------------------------------------------------------------
# Claude Code runs every command it starts in its own sandbox, also built on
# bubblewrap and socat; it can't run inside srt because bubblewrap can't be
# nested. Anything already in the sandbox section of your settings wins over
# these defaults.
claude_sandbox_on() {
jq -e '.sandbox.enabled == true' "$CLAUDE_SETTINGS" &> /dev/null
}
turn_on_claude_sandbox() {
local domains
domains="$(jq '.network.allowedDomains' "$BASE_CONFIG")" || return 1
edit_json "$CLAUDE_SETTINGS" '{}' --argjson domains "$domains" \
'.sandbox = ({enabled: true, network: {allowedDomains: $domains}} * (.sandbox // {}))' || return 1
echo "Claude Code runs the commands it starts in its own sandbox."
}
# A desktop notification when Claude Code needs you or is done, if you have no
# settings of your own yet
claude_notifications() {
[ -f "$CLAUDE_SETTINGS" ] && return 0
edit_json "$CLAUDE_SETTINGS" '{}' \
--arg needs "notify-send 'Claude Code' 'Needs your input'" \
--arg finished "notify-send 'Claude Code' 'Task completed'" \
'.hooks = {
Notification: [{matcher: "", hooks: [{type: "command", command: $needs}]}],
Stop: [{matcher: "", hooks: [{type: "command", command: $finished}]}]
}' || return 1
echo "Claude Code shows a notification when it needs you and when it's done."
}
# --- Hermes' gateway and Cline Desktop -----------------------------------------------
# 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. 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.
source "$CONNECTOR/sandbox/hermes-homeserver.sh"
gateway_dropin() {
cat << EOF
# Written by Federated Connector (configure agents): run Hermes' gateway in the sandbox
[Service]
ExecStart=
ExecStart=$SANDBOX_BIN/hermes gateway run
EOF
}
gateway_ok() {
[ ! -f "$HERMES_GATEWAY_UNIT" ] || [ "$(cat "$HERMES_GATEWAY_DROPIN" 2> /dev/null)" = "$(gateway_dropin)" ]
}
sandbox_gateway() {
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
# ~/.local/share/applications, which wins over the package's, runs it in the
# sandbox. It's removed when the package is.
CLINE_DESKTOP="/usr/share/applications/cline-desktop.desktop"
CLINE_ENTRY="$HOME/.local/share/applications/cline-desktop.desktop"
cline_entry() {
sed "s#^Exec=.*#Exec=$SANDBOX_BIN/cline-desktop %U#" "$CLINE_DESKTOP"
}
cline_ok() {
if [ -f "$CLINE_DESKTOP" ]; then
[ "$(cat "$CLINE_ENTRY" 2> /dev/null)" = "$(cline_entry)" ]
else
[ ! -f "$CLINE_ENTRY" ]
fi
}
sandbox_cline() {
cline_ok && return 0
if [ -f "$CLINE_DESKTOP" ]; then
mkdir -p "$(dirname "$CLINE_ENTRY")" && cline_entry > "$CLINE_ENTRY" || return 1
echo "Cline Desktop runs in the sandbox."
else
rm -f "$CLINE_ENTRY"
fi
}
# --- The item -------------------------------------------------------------------------
sandbox_ready() {
srt_path > /dev/null && on_path && pkg_installed "${SANDBOX_PACKAGES[@]}" && bwrap_works \
&& { ! agent_installed claude || claude_sandbox_on; } && gateway_ok && cline_ok
}
is_configured() {
[ -f "$AGENTS_CONF" ] && sandbox_ready
}
# Download the installer, then run it, rather than piping it into bash
install_agent() {
local title="$1" url="$2" args="$3" tmp status
tmp="$(mktemp -d)" || return 1
echo "Downloading $title's installer from $url"
if curl -fsSL --max-time 120 -o "$tmp/install.sh" "$url"; then
# shellcheck disable=SC2086
(cd "$tmp" && bash ./install.sh $args)
status=$?
else
echo "Couldn't download it."
status=1
fi
rm -rf "$tmp"
return $status
}
run_configure() {
local entry name title url args status=0
mkdir -p "$CONF_DIR" && touch "$AGENTS_CONF" || return 1
echo "AI coding agents, run in a sandbox: an agent can change files only in the folder"
echo "you start it in, can't read your keys, passwords, browsers, mail or messages, and"
echo "reaches only AI providers and developer sites."
echo
for entry in "${AGENTS[@]}"; do
IFS='|' read -r name title url args <<< "$entry"
if agent_installed "$name"; then
echo " $title: installed"
elif confirm_yes "Install $title?"; then
if ! install_agent "$title" "$url" "$args" || ! agent_installed "$name"; then
echo -e "\e[31m$title was not installed.\e[0m"
status=1
fi
fi
done
echo
pkg_ensure "The sandbox" "${SANDBOX_PACKAGES[@]}" || return 1
if ! bwrap_works; then
if [ "$(sysctl -n kernel.apparmor_restrict_unprivileged_userns 2> /dev/null)" = 1 ]; then
allow_bwrap || { echo -e "\e[31mWithout it, the agents can't run in the sandbox.\e[0m"; return 1; }
else
echo -e "\e[31mbubblewrap can't make user namespaces on this computer, so the sandbox can't run.\e[0m"
return 1
fi
fi
srt_ensure || { echo -e "\e[31mWithout srt, opencode and Hermes can't run in the sandbox.\e[0m"; return 1; }
if ! on_path; then
echo "Typing opencode or hermes should run it in the sandbox. That takes a line at the"
echo "end of ~/.bashrc."
if confirm_yes "Add it?"; then
put_on_path || status=1
else
echo "Then run them sandboxed as $SANDBOX_BIN/<agent>."
fi
fi
if agent_installed claude; then
claude_notifications || status=1
claude_sandbox_on || turn_on_claude_sandbox || status=1
fi
sandbox_gateway || status=1
sandbox_cline || status=1
echo
echo "To sign in to them: configure claude, configure opencode, configure hermes"
return $status
}