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

+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