Files
connector-popos/smoketest/scenarios/80-security.sh
T
davidandClaude Opus 5.5 c84e6d3aa0 Start opencode's sandbox on Pop!_OS; lint and the smoketest now pass
The first run of lint and the full Docker smoketest since the CoreDesktop
ports found:

- opencode's sandbox didn't start. Its profile didn't allow all Unix sockets,
  so srt added its socket filter, which needs a user namespace inside
  bubblewrap, and Pop!_OS' AppArmor (bwrap-userns-restrict) denies that.
  opencode now allows them and hides their paths (/run/user, /run/dbus,
  Docker, Podman, Tailscale, X11, tmux, ssh-agent), as Hermes and Cline
  Desktop do. 80-security shows it still can't reach the session D-Bus.
- 80-security's agent list was named AGENTS, and 30-agents.sh, loaded later,
  replaced it with its own, so every sandbox check ran a command that
  doesn't exist. It's FAKE_AGENTS now.
- 50-sandbox's "can't" checks would pass if the sandbox never started; a
  check that it starts now comes first.
- shellcheck read a jq variable named done as bash's keyword (SC1010).

TEST.md says to install shellcheck and Docker, which the connector doesn't.
PLAN.md: pin srt, try the real opencode, and settle on 24.04 or 26.04.

make lint passes. smoketest/run --tier all passed every scenario but 50 and
80 before the opencode change, and 50 and 80 pass after it.

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

325 lines
14 KiB
Bash

# shellcheck shell=bash
# Scenarios set what the runner and items read; the checks' scripts are
# expanded later, inside or outside the sandbox
# shellcheck disable=SC2034,SC2016
DESCRIPTION="security: what agents can't do from the sandbox, what root runs, the install path"
TIER=all
PRIVILEGED=1
# Ported from CoreDesktop's smoketest/scenarios/60-security.sh (05b13b2, d5925fa,
# 53c6c8b). Its network exposure checks (ufw, Docker, sshd) don't apply: the
# connector installs none of them.
#
# The sandbox: each check is made twice. Outside the sandbox it must work, so
# the thing is really there. Inside it must fail, while a marker shows that the
# sandbox did start, so a wrapper that fails to start doesn't count as a pass.
# An agent is played by a fake one, first on PATH, that runs bash with the
# wrapper's arguments. sandbox/agent finds it as the agent's real command and
# runs it exactly as it would run opencode, Hermes or Cline Desktop: same
# config, same srt, same proxy.
FAKE_AGENTS=(opencode hermes cline-desktop)
FAKE_BIN="$HOME/smoketest-agents"
# Where opencode and Hermes are started; Cline Desktop always starts in ~/Projects
PROJECT="$HOME/Projects/smoketest-project"
# A listener on the container's own address, the way an Ollama server on
# another machine would be, and one on loopback, the way one on this machine
# would be
LAN_PORT=18080
LAN_PORT_OTHER=18081
LOOPBACK_PORT=18082
USER_SANDBOX_CONF="$HOME/.config/federated-connector/sandbox/base.json"
SMB_SECRET=/etc/federated-connector/smb/smoketest
# Secret folders the sandbox must hide. Each gets a canary, so reading it can
# succeed outside.
SECRETS=(
.ssh
.gnupg
.local/share/keyrings
.config/Bitwarden
.var/app/com.bitwarden.desktop
.config/federated-connector
.config/goa-1.0
.local/share/evolution
.config/Element
.davfs2
.winboat
)
# --- The sandbox ----------------------------------------------------------------
# sandbox_out AGENT SCRIPT: SCRIPT's output, run in AGENT's sandbox, after a
# marker that shows the sandbox started and, if it works, another
sandbox_out() {
local dir="$PROJECT"
[ "$1" = cline-desktop ] && dir="$HOME/Projects"
(cd "$dir" && PATH="$FAKE_BIN:$PATH" "$SANDBOX_BIN/$1" -c "echo SANDBOX-STARTED; $2 && echo SCRIPT-WORKED") 2>&1
}
# works_in AGENT SCRIPT: SCRIPT works in AGENT's sandbox
works_in() {
local out
out="$(sandbox_out "$1" "$2")"
printf '%s\n' "$out"
[[ $out == *SANDBOX-STARTED* ]] || { echo "$1's sandbox didn't start"; return 1; }
[[ $out == *SCRIPT-WORKED* ]]
}
# fails_in AGENT SCRIPT: SCRIPT fails in AGENT's sandbox, which did start
fails_in() {
local out
out="$(sandbox_out "$1" "$2")"
printf '%s\n' "$out"
[[ $out == *SANDBOX-STARTED* ]] || { echo "$1's sandbox didn't start"; return 1; }
[[ $out != *SCRIPT-WORKED* ]]
}
outside() {
(cd "$PROJECT" && bash -c "$1")
}
# blocked DESCRIPTION SCRIPT: SCRIPT works outside the sandbox and in no
# agent's sandbox
blocked() {
local agent
check "outside the sandbox: $1" outside "$2"
for agent in "${FAKE_AGENTS[@]}"; do
check "$agent: $1" fails_in "$agent" "$2"
done
}
# write_stays_in AGENT PATH TOKEN: true if TOKEN, appended to PATH in AGENT's
# sandbox, isn't in the real PATH. The write itself may seem to work, e.g. into
# the empty folder srt mounts over a hidden one, so the real file is looked at.
write_stays_in() {
local out
out="$(sandbox_out "$1" "mkdir -p \"\$(dirname $2)\"; echo $3 >> $2")"
printf '%s\n' "$out"
[[ $out == *SANDBOX-STARTED* ]] || { echo "$1's sandbox didn't start"; return 1; }
! grep -qs "$3" "$(eval echo "$2")"
}
# write_blocked DESCRIPTION PATH: you may write PATH, and no agent's write
# reaches it. Outside, only the permissions are looked at, so nothing changes.
write_blocked() {
local agent
check "outside the sandbox: $1" outside "[ -w \"\$(dirname $2)\" ] && { [ ! -e $2 ] || [ -w $2 ]; }"
for agent in "${FAKE_AGENTS[@]}"; do
check "$agent: $1" write_stays_in "$agent" "$2" "smoketest-escape-$agent"
done
}
setup_sandbox() {
local secret agent
source "$CONNECTOR/configure.d/30-agents.sh"
sudo apt-get update > /dev/null && sudo apt-get install -y "${SANDBOX_PACKAGES[@]}" git dbus-bin > /dev/null 2>&1
check "srt installs" install_srt
mkdir -p "$FAKE_BIN" "$PROJECT" "$HOME/smoketest-other" "$HOME/.local/bin" \
"$HOME/.config/systemd/user" "$HOME/.config/autostart" "$(dirname "$USER_SANDBOX_CONF")"
for agent in "${FAKE_AGENTS[@]}"; do
printf '#!/bin/bash\nexec bash "$@"\n' > "$FAKE_BIN/$agent"
chmod +x "$FAKE_BIN/$agent"
done
git -C "$PROJECT" init -q
for secret in "${SECRETS[@]}"; do
mkdir -p "$HOME/$secret"
chmod 700 "$HOME/$secret"
echo canary > "$HOME/$secret/smoketest-canary"
done
# Root's alone, as configure storage keeps SMB passwords; you reach it only
# through sudo
echo 'password=canary' | sudo install -D -m 600 -o root -g root /dev/stdin "$SMB_SECRET"
# Another user's process, which a sandboxed agent mustn't see
sudo setsid sleep 3600 > /dev/null 2>&1 < /dev/null &
# socat is there for srt; it answers each connection with this script
LAN_IP="$(ip -4 route get 1.1.1.1 | sed -n 's/.* src \([0-9.]*\).*/\1/p')"
printf '#!/bin/sh\nprintf "HTTP/1.0 200 OK\\r\\n\\r\\nlistener\\n"\n' > "$HOME/smoketest-respond"
chmod +x "$HOME/smoketest-respond"
for listen in "$LAN_IP:$LAN_PORT" "$LAN_IP:$LAN_PORT_OTHER" "127.0.0.1:$LOOPBACK_PORT"; do
setsid socat "TCP-LISTEN:${listen#*:},bind=${listen%:*},fork,reuseaddr" \
"EXEC:$HOME/smoketest-respond" > /dev/null 2>&1 < /dev/null &
done
for _ in $(seq 50); do
curl -sf "http://$LAN_IP:$LAN_PORT_OTHER/" > /dev/null && curl -sf "http://127.0.0.1:$LOOPBACK_PORT/" > /dev/null && break
sleep 0.1
done
check "the listeners answer" curl -sf "http://$LAN_IP:$LAN_PORT/"
}
sandbox_checks() {
local agent secret lan
step "The sandbox: what agents must be able to do, or the rest proves nothing"
setup_sandbox
lan="http://$LAN_IP"
for agent in "${FAKE_AGENTS[@]}"; do
check "$agent: writes in the folder it started in" works_in "$agent" 'echo x > ./smoketest-probe && rm ./smoketest-probe'
check "$agent: writes in /tmp" works_in "$agent" 'echo x > /tmp/smoketest-probe-$$ && rm /tmp/smoketest-probe-$$'
check "$agent: reaches an allowed domain through its proxy" works_in "$agent" 'curl -sf -o /dev/null -m 20 https://github.com/'
done
check "opencode: writes its own state" works_in opencode 'touch ~/.local/share/opencode/smoketest-probe'
check "hermes: writes its own state" works_in hermes 'touch ~/.hermes/smoketest-probe'
step "The sandbox: reading secrets"
for secret in "${SECRETS[@]}"; do
blocked "reads ~/$secret" "cat ~/$secret/smoketest-canary"
done
blocked "reads an SMB password through sudo" "sudo -n cat $SMB_SECRET"
step "The sandbox: writing outside the folder it started in"
write_blocked "writes in the home folder" '$HOME/smoketest-escaped'
write_blocked "writes in another project" '$HOME/smoketest-other/escaped'
write_blocked "writes ~/.bashrc" '$HOME/.bashrc'
write_blocked "writes in ~/.local/bin, which is on PATH" '$HOME/.local/bin/smoketest-escaped'
write_blocked "writes a systemd user unit" '$HOME/.config/systemd/user/smoketest.service'
write_blocked "writes an autostart entry" '$HOME/.config/autostart/smoketest.desktop'
write_blocked "changes the connector's sandbox config" "$CONNECTOR/sandbox/config/base.json"
write_blocked "changes the connector itself" "$CONNECTOR/sandbox/agent"
write_blocked "changes your sandbox config" "$USER_SANDBOX_CONF"
write_blocked "changes the settings srt starts with" '$XDG_RUNTIME_DIR/federated-sandbox/smoketest.json'
# In a project, where Cline Desktop may write, but run outside the sandbox
# the next time you commit
write_blocked "writes a git hook in its project" "$PROJECT/.git/hooks/pre-commit"
write_blocked "changes its project's git config" "$PROJECT/.git/config"
step "The sandbox: the network"
blocked "reaches a domain that isn't allowed" 'curl -sf -o /dev/null -m 20 https://example.com/'
blocked "reaches an allowed domain without its proxy" 'curl -sf -o /dev/null -m 20 --noproxy "*" https://github.com/'
blocked "reaches a LAN address that isn't allowed" "curl -sf -m 10 $lan:$LAN_PORT/"
blocked "reaches a LAN address that isn't allowed, without its proxy" "curl -sf -m 10 --noproxy '*' $lan:$LAN_PORT/"
# Its own loopback is its own; this machine's servers aren't on it
blocked "reaches a server on this machine's loopback" "curl -sf -m 10 http://127.0.0.1:$LOOPBACK_PORT/"
# The way configure ai-servers allows a server: that host and port, no other
source "$CONNECTOR/configure.d/lib/json.sh"
check "allow one LAN server, as configure ai-servers does" \
edit_json "$USER_SANDBOX_CONF" '{}' --arg s "$LAN_IP:$LAN_PORT" '.network.allowedDomains += [$s]'
for agent in "${FAKE_AGENTS[@]}"; do
check "$agent: reaches the allowed LAN server" works_in "$agent" "curl -sf -m 10 $lan:$LAN_PORT/"
done
blocked "reaches another port on the allowed LAN server" "curl -sf -m 10 $lan:$LAN_PORT_OTHER/"
step "The sandbox: processes and D-Bus"
blocked "sees other users' processes" '[ -n "$(ps -e -o user= | grep -vx "$USER")" ]'
# The session's own processes, whose /proc/PID/environ you can read
blocked "sees the session's D-Bus daemon" 'pgrep -u "$USER" -x dbus-daemon'
# The session bus runs things outside the sandbox when asked, e.g. through
# systemd --user. (A container has no systemd --user to check directly.)
blocked "talks to the D-Bus session bus" \
'dbus-send --session --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.ListNames'
}
# --- What root runs, and sudo's password ------------------------------------------
# A user who has to type a password for sudo: pop's sudo needs none, and then
# PAM's auth step never runs
PAM_USER=pamtest
PAM_PASSWORD=Smoketest-pw-1
# pam_sudo PASSWORD: PAM_USER's sudo takes PASSWORD
pam_sudo() {
sudo -u "$PAM_USER" bash -c 'printf "%s\n" "$1" | timeout 60 sudo -S -k true' _ "$1" 2>&1
}
pam_sudo_without_password() {
sudo -u "$PAM_USER" bash -c 'timeout 60 sudo -S -k true < /dev/null' 2>&1
}
# root_only PATH: PATH and every folder above it are root's, and nobody else
# can change them; and if PATH is a link, so is where it leads
root_only() {
local p="$1"
[ -e "$p" ] || { echo "$p isn't there"; return 1; }
p="$(realpath -e "$1")"
while :; do
[ "$(stat -c %u "$p")" = 0 ] || { echo "$p isn't root's: $(stat -c %U "$p")"; return 1; }
[ $(( 0$(stat -c %a "$p") & 022 )) -eq 0 ] || { echo "$p can be changed by others: $(stat -c %a "$p")"; return 1; }
[ "$p" = / ] && return 0
p="$(dirname "$p")"
done
}
root_checks() {
local file root_files
step "Root: what configure installs for root to run or read"
# polkitd for its PAM file, which configure copies to add the lines to
sudo apt-get install -y libpam-u2f libpam-fprintd polkitd > /dev/null 2>&1
sudo useradd -m -G sudo "$PAM_USER"
echo "$PAM_USER:$PAM_PASSWORD" | sudo chpasswd
check "pam: before: $PAM_USER's password works for sudo" pam_sudo "$PAM_PASSWORD"
check_not "pam: before: a wrong password doesn't" pam_sudo wrong-password
# As configure security-key, configure fingerprint and configure tailscale
# set them up, but without a key, a finger or systemd --user
(
source "$CONNECTOR/configure.d/70-security-key.sh"
echo "$USER:smoketest-credential" | sudo tee -a "$U2F_MAPPINGS" > /dev/null
use_keys
) > /dev/null
(source "$CONNECTOR/configure.d/71-fingerprint.sh" && use_fingerprint) > /dev/null
(
systemctl() { :; }
source "$CONNECTOR/configure.d/15-tailscale.sh"
install_extras
) > /dev/null
root_files=(
/etc/pam.d/sudo
/etc/pam.d/polkit-1
/etc/u2f_mappings
/usr/local/lib/federated-connector/lid-closed
/etc/NetworkManager/dispatcher.d/90-federated-connector-captive-portal
"$SMB_SECRET"
)
for file in "${root_files[@]}"; do
check "root: $file is root's alone" root_only "$file"
done
check_not "root: none of them names a path in a home folder" \
sudo grep -lE '/home/|~/' "${root_files[@]}"
check_eq "root: SMB passwords are readable only by root" 600 "$(sudo stat -c %a "$SMB_SECRET")"
step "PAM: sudo's password still works with a key and a fingerprint set up"
check "pam: sudo takes a security key" grep -q pam_u2f.so /etc/pam.d/sudo
check "pam: and a fingerprint" grep -q pam_fprintd.so /etc/pam.d/sudo
check "pam: after: $PAM_USER's password still works for sudo" pam_sudo "$PAM_PASSWORD"
check_not "pam: after: a wrong password still doesn't" pam_sudo wrong-password
check_not "pam: after: no password at all doesn't" pam_sudo_without_password
}
# --- The install path ----------------------------------------------------------------
# Every hop from federated.sh/pop to the script, as the README's command
# fetches it, but refusing anything that isn't HTTPS
fetch_https_only() {
curl -fsSL --proto '=https' --proto-redir '=https' --max-time 60 https://federated.sh/pop
}
install_path_checks() {
local script location
step "The install path"
script="$(fetch_https_only)"
check "install: every hop from federated.sh/pop to the script is HTTPS" test -n "$script"
check_eq "install: it's a bash script" "#!/bin/bash" "$(head -n 1 <<< "$script")"
check "install: it's Federated Connector for Pop!_OS's" grep -q "Install Federated Connector for Pop!_OS" <<< "$script"
location="$(curl -sS -o /dev/null -w '%{redirect_url}' --max-time 30 https://federated.sh/pop)"
check "install: federated.sh sends curl to install.sh on Gitea, over HTTPS" \
grep -qE '^https://gitea\.federated\.computer/Federated_Computer/connector-popos/.*install\.sh' <<< "$location"
check_not "install: plain HTTP to federated.sh doesn't serve the script" \
bash -c 'curl -s --max-time 30 http://federated.sh/pop | grep -q "^#!/bin/bash"'
check "install: plain HTTP goes to HTTPS" \
bash -c 'curl -sS -o /dev/null -w "%{redirect_url}" --max-time 30 http://federated.sh/pop | grep -q "^https://"'
# What install.sh and update download, from the working copy
check_not "install: install.sh downloads nothing over plain HTTP" grep -nE 'http://' "$CONNECTOR/install.sh"
}
scenario() {
sandbox_checks
root_checks
install_path_checks
}