fix(supabase): e2e:avatar runs on the sandbox only, DEV runner removed

scripts/avatar-e2e.sh read the DEV keys through `supabase status`, started
the worker on the DEV network and let the suite write and purge on DEV.
Port the V1-2 sandbox runner (`db-sandbox.sh avatar-e2e`: loopback gateway,
worker in a 600 env file) and the guarded suite (fixed URL, container and
key issuer, per-run accounts); refusals and the positive run proven under
strace in .claude/audits/2026-11-02-avatar-e2e-sandbox-only.md.
This commit is contained in:
bchanot committed 2026-11-02 10:45:03 +01:00
1 parent 6287e5bb9f
commit 3a4614bc86
17 files changed
+589 -117

No files matched your search

-58
View File
@@ -1,58 +0,0 @@
#!/usr/bin/env bash
# Real-stack end-to-end run of the avatar flow (docs/development/STORAGE.md §6):
# starts the worker container against the local Supabase stack with a fast sweep
# and no quarantine grace, runs packages/supabase/tests/e2e, stops the container.
# Needs: `pnpm db:start` done, Docker, the worker image (`pnpm worker:image`,
# built here when absent). The keys come from `supabase status`; the service key
# reaches the container through a 600-mode temporary env file, never the command line.
set -euo pipefail
cd "$(dirname "$0")/.."
network="${SUPABASE_NETWORK:-supabase_network_faunosteo}"
kong="${SUPABASE_INTERNAL_URL:-http://supabase_kong_faunosteo:8000}"
image="${WORKER_IMAGE:-faunosteo_worker:dev}"
name="faunosteo_worker_e2e"
. scripts/dev-env.sh
status="$(node_modules/.bin/supabase status -o env 2>/dev/null)" \
|| { echo "avatar-e2e: the local stack is not running (pnpm db:start)" >&2; exit 1; }
value() { printf '%s\n' "$status" | sed -n "s/^$1=\"\(.*\)\"$/\1/p"; }
api_url="$(value API_URL)"
anon_key="$(value ANON_KEY)"
service_key="$(value SERVICE_ROLE_KEY)"
[ -n "$api_url" ] && [ -n "$anon_key" ] && [ -n "$service_key" ] \
|| { echo "avatar-e2e: could not read the keys from supabase status" >&2; exit 1; }
if ! docker image inspect "$image" >/dev/null 2>&1; then
echo "== building $image"
docker build -f crates/worker/Dockerfile -t "$image" .
fi
env_file="$(mktemp)"
chmod 600 "$env_file"
cleanup() {
echo "== worker log (last lines)"
docker logs --tail 40 "$name" 2>/dev/null || true
docker rm -f "$name" >/dev/null 2>&1 || true
rm -f "$env_file"
}
trap cleanup EXIT
{
echo "SUPABASE_URL=$kong"
echo "SUPABASE_SERVICE_ROLE_KEY=$service_key"
echo "WORKER_ID=e2e"
echo "WORKER_POLL_SECONDS=1"
echo "WORKER_SWEEP_SECONDS=5"
echo "WORKER_QUARANTINE_GRACE_SECONDS=0"
} > "$env_file"
docker rm -f "$name" >/dev/null 2>&1 || true
echo "== starting $name on $network"
docker run -d --name "$name" --read-only --cap-drop ALL \
--security-opt no-new-privileges:true --memory 512m --cpus 1 --pids-limit 64 \
--network "$network" --env-file "$env_file" "$image" run >/dev/null
echo "== e2e suite"
SUPABASE_URL="$api_url" SUPABASE_ANON_KEY="$anon_key" SUPABASE_SERVICE_ROLE_KEY="$service_key" \
pnpm --filter @faunosteo/supabase test:e2e
+97 -4
View File
@@ -14,6 +14,9 @@
# pnpm db:sandbox types [--check] generated types (write, or diff with the committed file)
# pnpm db:sandbox test packages/supabase/tests/sandbox (HTTP, concurrency)
# pnpm db:sandbox e2e-shell [args] = pnpm e2e:shell: the /app shell in Chromium (apps/app/e2e); args go to Playwright (a spec file, --grep)
# pnpm db:sandbox avatar-e2e [args] (= pnpm e2e:avatar) the avatar worker (default mode: avatars + sweep) against the
# sandbox: a gateway on 127.0.0.1:$PORT_GATEWAY, the worker
# container, then tests/e2e/avatar-flow.e2e.ts (args go to vitest)
# pnpm db:sandbox sql psql as postgres on stdin
# pnpm db:sandbox down
#
@@ -36,14 +39,21 @@ AUTH=faunosteo_dbsandbox_auth
REST=faunosteo_dbsandbox_rest
STORAGE=faunosteo_dbsandbox_storage
MAIL=faunosteo_dbsandbox_mail
PORT_DB=57432 PORT_AUTH=57999 PORT_REST=57300 PORT_STORAGE=57500 PORT_MAIL=57025
GATEWAY=faunosteo_dbsandbox_gateway
WORKER=faunosteo_dbsandbox_worker_verify
AVATAR_WORKER=faunosteo_dbsandbox_worker_avatar
GATEWAY_IMAGE=nginx:1.31-alpine
WORKER_IMAGE=faunosteo_worker:dev
PORT_DB=57432 PORT_AUTH=57999 PORT_REST=57300 PORT_STORAGE=57500 PORT_MAIL=57025 PORT_GATEWAY=57800
STATE="${FAUNOSTEO_SANDBOX_DIR:-$HOME/.cache/faunosteo-db-sandbox}"
SECRETS="$STATE/secrets.sh"
CLI=node_modules/.bin/supabase
die() { echo "db-sandbox: $*" >&2; exit 1; }
# Both sides resolved: a repository reached through a symlink is still the repository.
REPO="$(realpath "$PWD")"
case "$(realpath -m "$STATE")" in
"$PWD" | "$PWD"/*) die "FAUNOSTEO_SANDBOX_DIR must be outside the repository" ;;
"$REPO" | "$REPO"/*) die "FAUNOSTEO_SANDBOX_DIR must be outside the repository" ;;
esac
# Every value must be present: an empty password would turn --db-url into an
# argument the CLI might read as absent and fall back to the DEV database.
@@ -219,7 +229,7 @@ cmd_up() {
}
cmd_down() {
docker rm -f "$REST" "$STORAGE" "$AUTH" "$MAIL" "$DB" >/dev/null 2>&1 || true
docker rm -f "$AVATAR_WORKER" "$WORKER" "$GATEWAY" "$REST" "$STORAGE" "$AUTH" "$MAIL" "$DB" >/dev/null 2>&1 || true
docker network rm "$NET" >/dev/null 2>&1 || true
rm -f "$SECRETS" "$STATE/db.env" "$STATE/auth.env" "$STATE/storage.env" "$STATE/rest.env" "$STATE/service-header"
echo "== sandbox removed"
@@ -234,6 +244,88 @@ with_sandbox_env() (
"$@"
)
# The Kong routes of the CLI stack (/auth/v1/, /rest/v1/, /storage/v1/) on the
# sandbox network; host port only for avatar-e2e, on 127.0.0.1 like the services.
GATEWAY_CONF='server {
listen 8000;
client_max_body_size 8m;
proxy_http_version 1.1;
location /auth/v1/ { proxy_pass http://'"$AUTH"':9999/; }
location /rest/v1/ { proxy_pass http://'"$REST"':3000/; }
location /storage/v1/ { proxy_pass http://'"$STORAGE"':5000/; }
location / { return 404; }
}'
start_gateway() { # [docker run options]
docker run -d --name "$GATEWAY" --network "$NET" --security-opt no-new-privileges:true "$@" \
--entrypoint sh "$GATEWAY_IMAGE" -c 'printf "%s\n" "$0" > /etc/nginx/conf.d/default.conf && exec nginx -g "daemon off;"' \
"$GATEWAY_CONF" >/dev/null
wait_for "gateway" docker exec "$GATEWAY" wget -q -O /dev/null http://127.0.0.1:8000/rest/v1/
}
# The service key reaches the container through a 600 file removed once it started;
# memory as the compose service of the same mode (docker-compose.yml).
start_worker() { # name worker-id memory extra-env-lines args...
local name="$1" id="$2" memory="$3" extra="$4"; shift 4
worker_env="$(umask 077; mktemp "$STATE/worker.XXXXXX")"
printf 'SUPABASE_URL=http://%s:8000\nSUPABASE_SERVICE_ROLE_KEY=%s\nWORKER_ID=%s\nWORKER_POLL_SECONDS=1\n%s' \
"$GATEWAY" "$SB_SERVICE_KEY" "$id" "$extra" > "$worker_env"
docker run -d --name "$name" --network "$NET" --env-file "$worker_env" \
--read-only --cap-drop ALL --security-opt no-new-privileges:true \
--memory "$memory" --cpus 1 --pids-limit 64 --user nonroot \
"$WORKER_IMAGE" "$@" >/dev/null
rm -f "$worker_env"; worker_env=
for _ in $(seq 1 30); do
docker logs "$name" 2>&1 | grep -q '"event":"worker_start"' && break
[ "$(docker inspect -f '{{.State.Running}}' "$name")" = true ] || die "worker exited before worker_start"
sleep 1
done
docker logs "$name" 2>&1 | grep -q '"event":"worker_start"' || die "timeout waiting for worker_start"
echo "== worker up ($WORKER_IMAGE, $*, via $GATEWAY)"
}
# The worker's JSON lines hold no secret by design (crates/worker log allowlist);
# they are printed only after checking that neither key appears in them.
print_worker_log() { # name
local lines
lines="$(docker logs "$1" 2>&1)" || return 0
case "$lines" in
*"$SB_SERVICE_KEY"* | *"$SB_ANON_KEY"*) echo "== worker log withheld: it contains a key" ;;
*) echo "== worker log"; printf '%s\n' "$lines" ;;
esac
}
worker_cleanup() { # name
[ -n "${worker_env:-}" ] && rm -f "$worker_env"
print_worker_log "$1"
docker rm -f "$1" "$GATEWAY" >/dev/null 2>&1 || true
}
worker_preflight() { # name
load_secrets
docker image inspect "$WORKER_IMAGE" >/dev/null 2>&1 || die "$WORKER_IMAGE missing (pnpm worker:image)"
docker rm -f "$1" "$GATEWAY" >/dev/null 2>&1 || true
}
# The avatar suite drives supabase-js through the gateway's loopback port; its
# psql calls name the sandbox database container. The suite accepts no other
# target, and a port already taken stops the run before anything starts.
cmd_avatar_e2e() {
worker_preflight "$AVATAR_WORKER"
if (exec 3<>"/dev/tcp/127.0.0.1/$PORT_GATEWAY") 2>/dev/null; then
die "port 127.0.0.1:$PORT_GATEWAY is already in use: free it, nothing was started"
fi
trap 'worker_cleanup "$AVATAR_WORKER"' EXIT
start_gateway -p 127.0.0.1:$PORT_GATEWAY:8000
[ "$(docker port "$GATEWAY" 8000/tcp)" = "127.0.0.1:$PORT_GATEWAY" ] \
|| die "the gateway is not published on 127.0.0.1:$PORT_GATEWAY only"
start_worker "$AVATAR_WORKER" sandbox-avatar 512m \
$'WORKER_SWEEP_SECONDS=5\nWORKER_QUARANTINE_GRACE_SECONDS=0\n' run
SUPABASE_URL="http://127.0.0.1:$PORT_GATEWAY" SUPABASE_ANON_KEY="$SB_ANON_KEY" \
SUPABASE_SERVICE_ROLE_KEY="$SB_SERVICE_KEY" AVATAR_E2E_DB_CONTAINER="$DB" \
node_modules/.bin/pnpm --filter @faunosteo/supabase test:e2e "$@"
}
case "${1:-}" in
up) shift; cmd_up "$@" ;;
down) cmd_down ;;
@@ -248,6 +340,7 @@ case "${1:-}" in
fi ;;
test) load_secrets; with_sandbox_env node_modules/.bin/pnpm --filter @faunosteo/supabase test:sandbox ;;
e2e-shell) shift; load_secrets; with_sandbox_env node_modules/.bin/pnpm --filter @faunosteo/app e2e:shell "$@" ;;
avatar-e2e) shift; cmd_avatar_e2e "$@" ;;
sql) load_secrets; sb_psql ;;
*) sed -n '2,23p' "$0"; exit 1 ;;
*) sed -n '2,26p' "$0"; exit 1 ;;
esac
+1 -1
View File
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
# shellcheck shell=bash
# The one place the DEV origin is decided (docs/development/ENVIRONMENTS.md §2).
# Sourced by the wrappers (scripts/supabase.sh, scripts/dev.sh, scripts/avatar-e2e.sh):
# Sourced by the wrappers (scripts/supabase.sh, scripts/dev.sh, scripts/db-sandbox.sh):
# every service the browser of another device must reach is addressed by the
# host's LAN address, never by 127.0.0.1 or localhost.
#