#!/usr/bin/env bats load test_helper # sandbox/agent runs a fake agent through a fake srt, which sets NO_PROXY the # way the real one does, saves the settings it was given and runs the command. setup() { common_setup SANDBOX="$HOME/.local/share/coredesktop/sandbox" mkdir -p "$SANDBOX/bin" "$SANDBOX/config" "$BATS_TEST_TMPDIR/project" cp "$REPO/sandbox/config/base.json" "$SANDBOX/config/" ln -s "$REPO/sandbox/agent" "$SANDBOX/bin/fakeagent" export XDG_RUNTIME_DIR="$BATS_TEST_TMPDIR/run" mkdir -p "$XDG_RUNTIME_DIR" export SETTINGS_COPY="$BATS_TEST_TMPDIR/settings.json" stub srt 'cp "$2" "$SETTINGS_COPY" while [ "$1" != "--" ]; do shift; done shift export NO_PROXY=localhost,127.0.0.1,::1,10.0.0.0/8,192.168.0.0/16 no_proxy=localhost,192.168.0.0/16 exec "$@"' stub fakeagent 'echo "NO_PROXY=$NO_PROXY no_proxy=$no_proxy args=$*"' cd "$BATS_TEST_TMPDIR/project" } @test "an agent connects directly only to loopback, so allowed LAN addresses go through the proxy" { run "$SANDBOX/bin/fakeagent" one "two words" [ "$status" -eq 0 ] [ "$output" = "NO_PROXY=localhost,127.0.0.1,::1 no_proxy=localhost,127.0.0.1,::1 args=one two words" ] } @test "a server allowed in your own base.json is in the agent's settings" { mkdir -p "$HOME/.config/coredesktop/sandbox" echo '{"network": {"allowedDomains": ["llm.local:11434"]}}' > "$HOME/.config/coredesktop/sandbox/base.json" run "$SANDBOX/bin/fakeagent" [ "$status" -eq 0 ] jq -e '.network.allowedDomains | index("llm.local:11434") and index("github.com")' "$SETTINGS_COPY" } # --- configure sandbox ---------------------------------------------------------------- setup_configure() { export COREDESKTOP="$REPO" confirm() { return 0; } source "$REPO/configure.d/65-sandbox.sh" } @test "the sandbox hides WinBoat's settings by default" { jq -e '.filesystem.denyRead | index("~/.winboat")' "$REPO/sandbox/config/base.json" run "$SANDBOX/bin/fakeagent" [ "$status" -eq 0 ] jq -e '.filesystem.denyRead | index("~/.winboat")' "$SETTINGS_COPY" run jq -e '.filesystem.allowRead | index("~/.winboat")' "$SETTINGS_COPY" [ "$status" -ne 0 ] } @test "configure sandbox lets agents read WinBoat's settings, and hides them again" { setup_configure mkdir -p "$HOME/.config/coredesktop/sandbox" echo '{"network": {"allowedDomains": ["llm.local:11434"]}}' > "$SANDBOX_CONF" run run_configure <<< $'1\nq' [ "$status" -eq 0 ] [[ "$output" == *"Agents can read ~/.winboat."* ]] [ "$(jq -c '.filesystem.allowRead' "$SANDBOX_CONF")" = '["~/.winboat"]' ] [ "$(jq -c '.network.allowedDomains' "$SANDBOX_CONF")" = '["llm.local:11434"]' ] # What the agent gets: still denied, but re-allowed, which srt lets win run "$SANDBOX/bin/fakeagent" jq -e '(.filesystem.denyRead | index("~/.winboat")) and (.filesystem.allowRead | index("~/.winboat"))' "$SETTINGS_COPY" run run_configure <<< $'1\nq' [ "$status" -eq 0 ] [[ "$output" == *"~/.winboat is hidden from agents again."* ]] [ "$(jq -c '.filesystem.allowRead' "$SANDBOX_CONF")" = '[]' ] } @test "configure sandbox is offered only where WinBoat is set up, and is never to do" { setup_configure run is_installed [ "$status" -ne 0 ] mkdir -p "$HOME/.winboat" is_installed is_configured } # --- Hermes' Matrix server ---------------------------------------------------------- # Hermes' settings, with a Matrix server and an API key hermes_env() { mkdir -p "$HOME/.hermes" printf 'OPENROUTER_API_KEY=sk-test\nMATRIX_HOMESERVER=%s\n' "$1" > "$HOME/.hermes/.env" } allow_in_hermes_json() { mkdir -p "$HOME/.config/coredesktop/sandbox" jq -n --arg h "$1" '{network: {allowedDomains: [$h]}}' > "$HOME/.config/coredesktop/sandbox/hermes.json" } @test "hermes_homeserver takes the host from Hermes' settings, and only a plain host" { source "$REPO/sandbox/hermes-homeserver.sh" hermes_env 'https://matrix.example.org:8448/' [ "$(hermes_homeserver)" = "matrix.example.org" ] hermes_env '"http://10.0.0.5"' [ "$(hermes_homeserver)" = "10.0.0.5" ] for bad in 'https://*.example.org' $'https://evil.example\e[2Korg' 'https://-x.org' 'ftp://matrix.example.org'; do hermes_env "$bad" [ -z "$(hermes_homeserver)" ] done } @test "the sandbox no longer takes extra domains from the environment, but does from hermes.json" { cp "$REPO/sandbox/config/hermes.json" "$SANDBOX/config/" cp "$REPO/sandbox/hermes-bridge" "$SANDBOX/" ln -s "$REPO/sandbox/agent" "$SANDBOX/bin/hermes" stub hermes 'true' allow_in_hermes_json matrix.example.org COREDESKTOP_SANDBOX_DOMAINS=evil.example.com run "$SANDBOX/bin/hermes" [ "$status" -eq 0 ] jq -e '.network.allowedDomains | index("matrix.example.org")' "$SETTINGS_COPY" run jq -e '.network.allowedDomains | index("evil.example.com")' "$SETTINGS_COPY" [ "$status" -ne 0 ] } # Run sandbox/setup.sh with Hermes and its gateway set up, and nothing to install run_setup() { export COREDESKTOP="$REPO" stub npm stub systemctl mkdir -p "$HOME/.local/bin" "$HOME/.config/systemd/user" printf '#!/bin/sh\n' > "$HOME/.local/bin/srt" printf '#!/bin/sh\n' > "$HOME/.local/bin/hermes" chmod +x "$HOME/.local/bin/srt" "$HOME/.local/bin/hermes" : > "$HOME/.config/systemd/user/hermes-gateway.service" run bash "$REPO/sandbox/setup.sh" DROPIN="$HOME/.config/systemd/user/hermes-gateway.service.d/coredesktop-sandbox.conf" } @test "setup.sh doesn't let Hermes' settings choose what its gateway reaches, and says what to do" { hermes_env https://matrix.example.org run_setup [ "$status" -eq 0 ] grep -q '^ExecStart=%h/.local/share/coredesktop/sandbox/bin/hermes gateway run$' "$DROPIN" run grep -q 'matrix.example.org\|COREDESKTOP_SANDBOX_DOMAINS' "$DROPIN" [ "$status" -ne 0 ] [[ "$output" == "" ]] run bash "$REPO/sandbox/setup.sh" [[ "$output" == *"Matrix server matrix.example.org, which its gateway can't reach until you allow it: configure hermes"* ]] } @test "setup.sh says nothing about a Matrix server you've allowed" { hermes_env https://matrix.example.org allow_in_hermes_json matrix.example.org run_setup [ "$status" -eq 0 ] [[ "$output" != *"configure hermes"* ]] } setup_hermes_configure() { export COREDESKTOP="$REPO" unsandboxed() { echo "unsandboxed $*" >> "$STUB_LOG"; } source "$REPO/configure.d/32-hermes.sh" } @test "configure hermes is to do while Hermes names a Matrix server you haven't allowed" { setup_hermes_configure hermes_env https://matrix.example.org run is_configured [ "$status" -ne 0 ] allow_in_hermes_json matrix.example.org is_configured } @test "configure hermes asks before letting Hermes reach its Matrix server, and only about that" { setup_hermes_configure hermes_env https://matrix.example.org confirm() { return 1; } run run_configure < /dev/null [ "$status" -eq 0 ] [[ "$output" == *"Hermes can't reach matrix.example.org"* ]] [[ "$output" != *"Which provider?"* ]] run hermes_homeserver_allowed matrix.example.org [ "$status" -ne 0 ] confirm() { return 0; } run run_configure < /dev/null [ "$status" -eq 0 ] [[ "$output" == *"Hermes can reach matrix.example.org"* ]] hermes_homeserver_allowed matrix.example.org not_called unsandboxed } @test "configure hermes asks for a provider when there's nothing else to do" { setup_hermes_configure hermes_env https://matrix.example.org allow_in_hermes_json matrix.example.org run run_configure <<< "1" [ "$status" -eq 0 ] called unsandboxed hermes setup --portal } @test "the migration keeps the Matrix server the gateway reaches now, not one Hermes names" { export COREDESKTOP="$REPO" dropin="$HOME/.config/systemd/user/hermes-gateway.service.d/coredesktop-sandbox.conf" mkdir -p "$(dirname "$dropin")" printf '[Service]\nEnvironment="COREDESKTOP_SANDBOX_DOMAINS=matrix.example.org"\nExecStart=\n' > "$dropin" hermes_env https://evil.example.com run bash "$REPO/migrations/1794870061.sh" [ "$status" -eq 0 ] [ "$(jq -c '.network.allowedDomains' "$HOME/.config/coredesktop/sandbox/hermes.json")" = '["matrix.example.org"]' ] # Again: no change run bash "$REPO/migrations/1794870061.sh" [ "$(jq -c '.network.allowedDomains' "$HOME/.config/coredesktop/sandbox/hermes.json")" = '["matrix.example.org"]' ] } @test "the migration does nothing without a gateway drop-in, or one with no server" { export COREDESKTOP="$REPO" run bash "$REPO/migrations/1794870061.sh" [ "$status" -eq 0 ] dropin="$HOME/.config/systemd/user/hermes-gateway.service.d/coredesktop-sandbox.conf" mkdir -p "$(dirname "$dropin")" printf '[Service]\nEnvironment="COREDESKTOP_SANDBOX_DOMAINS="\n' > "$dropin" run bash "$REPO/migrations/1794870061.sh" [ "$status" -eq 0 ] [ ! -e "$HOME/.config/coredesktop/sandbox/hermes.json" ] }