Files
connector-popos/sandbox/agent
T
davidandClaude Opus 5.5 fef73e8b5b Test the agent sandbox from inside; keep srt's settings out of /tmp
Ports CoreDesktop's security checks (05b13b2, d5925fa, 53c6c8b) as
smoketest/scenarios/80-security.sh:

- The sandbox: a fake opencode, Hermes and Cline Desktop, run through
  sandbox/bin's wrappers, can't read secrets or an SMB password through
  sudo, write outside their project (home, ~/.bashrc, ~/.local/bin,
  systemd units, autostart, the connector, its sandbox config or yours,
  the settings srt starts with, their project's git hooks and config),
  reach domains, LAN addresses or loopback servers that aren't allowed,
  or see other processes or the session's D-Bus. Each check also runs
  outside, so a pass means the sandbox blocked it.
- Root: what configure installs for root to run or read is root's alone
  and names no home folder, and with a security key and fingerprint set
  up, a password user's sudo still takes the password and only it.
- The install path: every hop from federated.sh/pop is HTTPS, and plain
  HTTP doesn't serve the script.

CoreDesktop's network exposure checks (ufw, Docker, sshd) don't apply.

Writing it found that sandbox/agent put the settings srt starts with in
/tmp/federated-sandbox when XDG_RUNTIME_DIR wasn't set, and every sandbox
can write /tmp, so an agent could put its own settings, or a link, where
the next one's go. They're now always in /run/user/<uid>, which no
sandbox can write, and it won't start without it.

The smoketest's runner gives each scenario what a login does: USER,
/run/user/<uid>, and the session bus in it, as on Pop!_OS.

PLAN.md: CoreDesktop compared at 4e9b962, everything that applies ported.
Neither lint nor the Docker smoketest has run on these ports yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 19:14:55 -07:00

161 lines
6.5 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.
#
# The folder is in your runtime folder, /run/user/<uid>, which no sandbox can
# write, never in /tmp, which every sandbox can: an agent could put its own
# settings, or a link, where the next one's go. XDG_RUNTIME_DIR names it, and
# systemd makes it at login, also for services and SSH.
runtime="/run/user/$(id -u)"
if [ ! -d "$runtime" ] || [ ! -O "$runtime" ]; then
echo "There's no runtime folder ($runtime) for the sandbox's settings; it's made when you log in."
exit 1
fi
SETTINGS_DIR="$runtime/federated-sandbox"
mkdir -p -m 700 "$SETTINGS_DIR" || exit 1
[ -O "$SETTINGS_DIR" ] && [ ! -L "$SETTINGS_DIR" ] || { echo "$SETTINGS_DIR isn't yours alone."; 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" "$@"
}