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>
150 lines
5.9 KiB
Bash
Executable File
150 lines
5.9 KiB
Bash
Executable File
#!/bin/bash
|
|
#
|
|
# Purpose : Run an AI agent inside Anthropic's sandbox runtime (srt). srt uses
|
|
# bubblewrap to limit which files the agent can read and write, and
|
|
# removes its network; socat links it to a proxy outside the sandbox
|
|
# that only lets through the domains in its config.
|
|
#
|
|
# sandbox/bin/<agent> links here and `configure agents` puts sandbox/bin first
|
|
# on PATH (in ~/.bashrc), so typing an agent's name runs it sandboxed.
|
|
# `unsandboxed <agent>` runs the agent's real command instead.
|
|
#
|
|
# 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.
|
|
|
|
# Wrapped in { ...; exit; } so bash reads the whole script up front; update
|
|
# replaces this file (git pull) while agents are running.
|
|
{
|
|
|
|
SANDBOX="$(dirname "$(realpath "$0")")"
|
|
CONNECTOR="$(dirname "$SANDBOX")"
|
|
USER_CONFIG="$HOME/.config/federated-connector/sandbox"
|
|
name="$(basename "$0")"
|
|
source "$CONNECTOR/configure.d/lib/agents.sh"
|
|
|
|
# The agent's real command: the first one on PATH outside sandbox/bin
|
|
real_command() {
|
|
agent_path "$1"
|
|
}
|
|
|
|
if [ "$name" = "unsandboxed" ]; then
|
|
if [ $# -eq 0 ]; then
|
|
echo "Usage: unsandboxed <agent> [arguments]"
|
|
exit 2
|
|
fi
|
|
agent="$1"
|
|
shift
|
|
real="$(real_command "$agent")" || { echo "$agent isn't installed."; exit 127; }
|
|
exec "$real" "$@"
|
|
fi
|
|
|
|
agent="$name"
|
|
real="$(real_command "$agent")" || { echo "$agent isn't installed."; exit 127; }
|
|
|
|
# Commands that share another agent's sandbox config
|
|
case "$agent" in
|
|
hermes-agent) profile=hermes ;;
|
|
*) 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
|
|
# are, except the Wayland socket it draws through. The sandbox has no GPU, and
|
|
# WebKit can't start its own sandbox (bubblewrap) inside this one.
|
|
gui_socket=""
|
|
case "$profile" in
|
|
cline-desktop)
|
|
mkdir -p "$HOME/Projects" && cd "$HOME/Projects" || exit 1
|
|
gui_socket="$XDG_RUNTIME_DIR/${WAYLAND_DISPLAY:-wayland-0}"
|
|
export GDK_BACKEND=wayland WEBKIT_DISABLE_DMABUF_RENDERER=1 WEBKIT_DISABLE_SANDBOX_THIS_IS_DANGEROUS=1 ;;
|
|
esac
|
|
|
|
if ! srt="$(srt_path)"; then
|
|
echo "The sandbox (srt) isn't installed; run: configure agents. To run $agent without it: unsandboxed $agent"
|
|
exit 1
|
|
fi
|
|
# srt runs on the Node.js configure agents found or installed for it
|
|
PATH="$FC_TOOLS/srt/bin:$FC_TOOLS/node/bin:$PATH"
|
|
|
|
# The agent can change anything in the directory it starts in
|
|
case "$PWD" in
|
|
"$HOME"|/)
|
|
echo "Start $agent from a project directory. In the sandbox it can change anything in the directory it starts in, and this is $PWD."
|
|
exit 1 ;;
|
|
esac
|
|
|
|
configs=("$SANDBOX/config/base.json")
|
|
[ -f "$SANDBOX/config/$profile.json" ] && configs+=("$SANDBOX/config/$profile.json")
|
|
ours=("${configs[@]}")
|
|
for file in "$USER_CONFIG/base.json" "$USER_CONFIG/$profile.json"; do
|
|
[ -f "$file" ] && configs+=("$file")
|
|
done
|
|
|
|
# Objects are merged and lists joined, so later files only add to earlier ones
|
|
MERGE='
|
|
def merge(a; b):
|
|
reduce (b | to_entries[]) as $e (a;
|
|
.[$e.key] |= (
|
|
if type == "object" and ($e.value | type) == "object" then merge(.; $e.value)
|
|
elif type == "array" and ($e.value | type) == "array" then . + $e.value | unique
|
|
else $e.value end));
|
|
reduce .[] as $config ({}; merge(.; $config))'
|
|
|
|
# srt runs in place of this script (exec below), so a stop signal, e.g. from
|
|
# systemd, reaches it and it passes it on to the agent. That leaves nothing to
|
|
# remove the settings file afterwards, so it's named after this process, and
|
|
# files whose process has ended are removed here.
|
|
SETTINGS_DIR="${XDG_RUNTIME_DIR:-/tmp}/federated-sandbox"
|
|
mkdir -p -m 700 "$SETTINGS_DIR" || exit 1
|
|
for old in "$SETTINGS_DIR"/*.json; do
|
|
pid="${old%.json}"
|
|
pid="${pid##*.}"
|
|
[[ "$pid" =~ ^[0-9]+$ ]] && ! kill -0 "$pid" 2> /dev/null && rm -f "$old"
|
|
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 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
|
|
fi
|
|
|
|
# Bubblewrap can only make writable a directory that exists, and the agent
|
|
# can't create one itself in a parent it may not write to
|
|
while IFS= read -r path; do
|
|
path="${path/#\~/$HOME}"
|
|
case "$path" in "$HOME"/*) mkdir -p "$path" ;; esac
|
|
done < <(jq -rs "$MERGE | .filesystem.allowWrite[]? | strings" "${ours[@]}")
|
|
|
|
# srt sets TMPDIR to /tmp/claude, which nothing creates, unless told otherwise;
|
|
# temp files then fail, and GTK aborts. /tmp is writable in every profile.
|
|
export CLAUDE_CODE_TMPDIR=/tmp
|
|
|
|
# srt tells the agent to connect directly to private addresses (192.168.x.x
|
|
# and so on), but the sandbox has no network to do it with, so a server on
|
|
# your local network that the config allows by IP, e.g. an Ollama server from
|
|
# `configure ai-servers`, couldn't be reached. Only loopback is direct here;
|
|
# everything else goes through the proxy, which still allows only the config's
|
|
# domains and addresses.
|
|
NO_PROXY_DIRECT=localhost,127.0.0.1,::1
|
|
direct=(env "NO_PROXY=$NO_PROXY_DIRECT" "no_proxy=$NO_PROXY_DIRECT")
|
|
|
|
# Hermes' terminal reaches its gateway, in another sandbox, through a relay
|
|
case "$profile" in
|
|
hermes) exec "$srt" --settings "$settings" -- "${direct[@]}" bash "$SANDBOX/hermes-bridge" "$real" "$@" ;;
|
|
esac
|
|
exec "$srt" --settings "$settings" -- "${direct[@]}" "$real" "$@"
|
|
}
|