fix(supabase): keep sandbox secrets off command lines and DEV out of reach
Security gate findings on db-sandbox.sh: containers get secrets from 600 env files, psql and the CLI read PGPASSWORD (no password in --db-url), an incomplete secrets file stops the script before any CLI call, the state folder is refused inside the repository.
This commit is contained in:
1 parent
f8bd1b6861
commit
a945e79131
2 files changed
+43
-20
No files matched your search
@@ -330,8 +330,11 @@ ports on 127.0.0.1 only and distinct from DEV (db 57432, auth 57999, rest
|
|||||||
no volume, so `down` removes the containers and the network by their literal
|
no volume, so `down` removes the containers and the network by their literal
|
||||||
names and can never reach a `supabase_*_faunosteo` volume. The password, JWT
|
names and can never reach a `supabase_*_faunosteo` volume. The password, JWT
|
||||||
secret and keys are generated at each `up` into a 600 file under
|
secret and keys are generated at each `up` into a 600 file under
|
||||||
`~/.cache/faunosteo-db-sandbox` (`FAUNOSTEO_SANDBOX_DIR`), outside the
|
`~/.cache/faunosteo-db-sandbox` (`FAUNOSTEO_SANDBOX_DIR`, refused inside the
|
||||||
repository, deleted by `down`, never printed. Works with rootless Docker.
|
repository), with 600 env files that hand them to the containers; deleted by
|
||||||
|
`down`, never printed, never on a command line (the CLI and psql read
|
||||||
|
`PGPASSWORD` from the environment). An incomplete secrets file stops the
|
||||||
|
script before any CLI call. Works with rootless Docker.
|
||||||
|
|
||||||
| Command | What it does |
|
| Command | What it does |
|
||||||
|---|---|
|
|---|---|
|
||||||
|
|||||||
+38
-18
@@ -39,10 +39,22 @@ SECRETS="$STATE/secrets.sh"
|
|||||||
CLI=node_modules/.bin/supabase
|
CLI=node_modules/.bin/supabase
|
||||||
|
|
||||||
die() { echo "db-sandbox: $*" >&2; exit 1; }
|
die() { echo "db-sandbox: $*" >&2; exit 1; }
|
||||||
load_secrets() { [ -f "$SECRETS" ] || die "not running (pnpm db:sandbox up fresh)"; . "$SECRETS"; }
|
case "$(realpath -m "$STATE")" in
|
||||||
db_url() { printf 'postgresql://postgres:%s@127.0.0.1:%s/postgres?sslmode=disable' "$SB_PW" "$PORT_DB"; }
|
"$PWD" | "$PWD"/*) 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.
|
||||||
|
load_secrets() {
|
||||||
|
[ -f "$SECRETS" ] || die "not running (pnpm db:sandbox up fresh)"
|
||||||
|
. "$SECRETS"
|
||||||
|
[ -n "${SB_PW:-}" ] && [ -n "${SB_JWT_SECRET:-}" ] && [ -n "${SB_ANON_KEY:-}" ] && [ -n "${SB_SERVICE_KEY:-}" ] \
|
||||||
|
|| die "secrets file incomplete (pnpm db:sandbox down, then up)"
|
||||||
|
export PGPASSWORD="$SB_PW"
|
||||||
|
}
|
||||||
|
# No password in the URL: the CLI reads PGPASSWORD, so no secret is on a command line.
|
||||||
|
db_url() { printf 'postgresql://postgres@127.0.0.1:%s/postgres?sslmode=disable' "$PORT_DB"; }
|
||||||
sb_psql() {
|
sb_psql() {
|
||||||
docker exec -i -e PGPASSWORD="$SB_PW" "$DB" \
|
docker exec -i -e PGPASSWORD "$DB" \
|
||||||
psql -X -q -h 127.0.0.1 -U postgres -d postgres -v ON_ERROR_STOP=1 "$@"
|
psql -X -q -h 127.0.0.1 -U postgres -d postgres -v ON_ERROR_STOP=1 "$@"
|
||||||
}
|
}
|
||||||
wait_for() {
|
wait_for() {
|
||||||
@@ -64,7 +76,18 @@ new_secrets() {
|
|||||||
console.log("SB_JWT_SECRET=" + secret);
|
console.log("SB_JWT_SECRET=" + secret);
|
||||||
console.log("SB_ANON_KEY=" + jwt({ role: "anon", iss: "db-sandbox", exp }));
|
console.log("SB_ANON_KEY=" + jwt({ role: "anon", iss: "db-sandbox", exp }));
|
||||||
console.log("SB_SERVICE_KEY=" + jwt({ role: "service_role", iss: "db-sandbox", exp }));
|
console.log("SB_SERVICE_KEY=" + jwt({ role: "service_role", iss: "db-sandbox", exp }));
|
||||||
' > "$SECRETS" )
|
' > "$SECRETS.new" && mv "$SECRETS.new" "$SECRETS" )
|
||||||
|
load_secrets
|
||||||
|
# The values reach the containers through 600 env files, never through argv.
|
||||||
|
( umask 077
|
||||||
|
printf 'POSTGRES_PASSWORD=%s\nPGPASSWORD=%s\nJWT_SECRET=%s\n' "$SB_PW" "$SB_PW" "$SB_JWT_SECRET" > "$STATE/db.env"
|
||||||
|
printf 'GOTRUE_DB_DATABASE_URL=postgresql://supabase_auth_admin:%s@%s:5432/postgres\nGOTRUE_JWT_SECRET=%s\n' \
|
||||||
|
"$SB_PW" "$DB" "$SB_JWT_SECRET" > "$STATE/auth.env"
|
||||||
|
printf 'ANON_KEY=%s\nSERVICE_KEY=%s\nAUTH_JWT_SECRET=%s\nDATABASE_URL=postgresql://supabase_storage_admin:%s@%s:5432/postgres\n' \
|
||||||
|
"$SB_ANON_KEY" "$SB_SERVICE_KEY" "$SB_JWT_SECRET" "$SB_PW" "$DB" > "$STATE/storage.env"
|
||||||
|
printf 'PGRST_DB_URI=postgresql://authenticator:%s@%s:5432/postgres\nPGRST_JWT_SECRET=%s\n' \
|
||||||
|
"$SB_PW" "$DB" "$SB_JWT_SECRET" > "$STATE/rest.env"
|
||||||
|
printf 'Authorization: Bearer %s\n' "$SB_SERVICE_KEY" > "$STATE/service-header" )
|
||||||
}
|
}
|
||||||
|
|
||||||
# The role passwords the CLI sets at db start (its /etc/postgresql.schema.sql),
|
# The role passwords the CLI sets at db start (its /etc/postgresql.schema.sql),
|
||||||
@@ -83,23 +106,21 @@ EOF'
|
|||||||
start_services() {
|
start_services() {
|
||||||
docker network create "$NET" >/dev/null
|
docker network create "$NET" >/dev/null
|
||||||
docker run -d --name "$DB" --network "$NET" -p 127.0.0.1:$PORT_DB:5432 \
|
docker run -d --name "$DB" --network "$NET" -p 127.0.0.1:$PORT_DB:5432 \
|
||||||
-e POSTGRES_PASSWORD="$SB_PW" -e PGPASSWORD="$SB_PW" -e POSTGRES_HOST=/var/run/postgresql \
|
--env-file "$STATE/db.env" -e POSTGRES_HOST=/var/run/postgresql -e JWT_EXP=3600 \
|
||||||
-e JWT_SECRET="$SB_JWT_SECRET" -e JWT_EXP=3600 \
|
|
||||||
-e POSTGRES_INITDB_ARGS="--allow-group-access --locale-provider=icu --encoding=UTF-8 --icu-locale=en_US.UTF-8" \
|
-e POSTGRES_INITDB_ARGS="--allow-group-access --locale-provider=icu --encoding=UTF-8 --icu-locale=en_US.UTF-8" \
|
||||||
--entrypoint sh "$PG_IMAGE" -c "$DB_INIT" >/dev/null
|
--entrypoint sh "$PG_IMAGE" -c "$DB_INIT" >/dev/null
|
||||||
wait_for "database roles" docker exec -e PGPASSWORD="$SB_PW" "$DB" \
|
wait_for "database roles" docker exec -e PGPASSWORD "$DB" \
|
||||||
psql -h 127.0.0.1 -U supabase_auth_admin -d postgres -c 'select 1'
|
psql -h 127.0.0.1 -U supabase_auth_admin -d postgres -c 'select 1'
|
||||||
wait_for "database" docker exec -e PGPASSWORD="$SB_PW" "$DB" \
|
wait_for "database" docker exec -e PGPASSWORD "$DB" \
|
||||||
psql -h 127.0.0.1 -U postgres -d postgres -c 'select 1'
|
psql -h 127.0.0.1 -U postgres -d postgres -c 'select 1'
|
||||||
|
|
||||||
docker run -d --name "$MAIL" --network "$NET" -p 127.0.0.1:$PORT_MAIL:8025 "$MAIL_IMAGE" >/dev/null
|
docker run -d --name "$MAIL" --network "$NET" -p 127.0.0.1:$PORT_MAIL:8025 "$MAIL_IMAGE" >/dev/null
|
||||||
docker run -d --name "$AUTH" --network "$NET" -p 127.0.0.1:$PORT_AUTH:9999 \
|
docker run -d --name "$AUTH" --network "$NET" -p 127.0.0.1:$PORT_AUTH:9999 \
|
||||||
-e GOTRUE_API_HOST=0.0.0.0 -e GOTRUE_API_PORT=9999 -e API_EXTERNAL_URL=http://127.0.0.1:$PORT_AUTH \
|
-e GOTRUE_API_HOST=0.0.0.0 -e GOTRUE_API_PORT=9999 -e API_EXTERNAL_URL=http://127.0.0.1:$PORT_AUTH \
|
||||||
-e GOTRUE_DB_DRIVER=postgres \
|
-e GOTRUE_DB_DRIVER=postgres --env-file "$STATE/auth.env" \
|
||||||
-e GOTRUE_DB_DATABASE_URL="postgresql://supabase_auth_admin:$SB_PW@$DB:5432/postgres" \
|
|
||||||
-e GOTRUE_DB_MIGRATIONS_PATH=/usr/local/etc/auth/migrations \
|
-e GOTRUE_DB_MIGRATIONS_PATH=/usr/local/etc/auth/migrations \
|
||||||
-e GOTRUE_SITE_URL=http://127.0.0.1:5173 -e GOTRUE_DISABLE_SIGNUP=false \
|
-e GOTRUE_SITE_URL=http://127.0.0.1:5173 -e GOTRUE_DISABLE_SIGNUP=false \
|
||||||
-e GOTRUE_JWT_SECRET="$SB_JWT_SECRET" -e GOTRUE_JWT_EXP=3600 \
|
-e GOTRUE_JWT_EXP=3600 \
|
||||||
-e GOTRUE_JWT_AUD=authenticated -e GOTRUE_JWT_DEFAULT_GROUP_NAME=authenticated \
|
-e GOTRUE_JWT_AUD=authenticated -e GOTRUE_JWT_DEFAULT_GROUP_NAME=authenticated \
|
||||||
-e GOTRUE_JWT_ADMIN_ROLES=service_role \
|
-e GOTRUE_JWT_ADMIN_ROLES=service_role \
|
||||||
-e GOTRUE_EXTERNAL_EMAIL_ENABLED=true -e GOTRUE_EXTERNAL_PHONE_ENABLED=false \
|
-e GOTRUE_EXTERNAL_EMAIL_ENABLED=true -e GOTRUE_EXTERNAL_PHONE_ENABLED=false \
|
||||||
@@ -112,8 +133,7 @@ start_services() {
|
|||||||
-e GOTRUE_PASSWORD_MIN_LENGTH=12 "$AUTH_IMAGE" >/dev/null
|
-e GOTRUE_PASSWORD_MIN_LENGTH=12 "$AUTH_IMAGE" >/dev/null
|
||||||
wait_for "auth" curl -fsS http://127.0.0.1:$PORT_AUTH/health
|
wait_for "auth" curl -fsS http://127.0.0.1:$PORT_AUTH/health
|
||||||
docker run -d --name "$STORAGE" --network "$NET" -p 127.0.0.1:$PORT_STORAGE:5000 \
|
docker run -d --name "$STORAGE" --network "$NET" -p 127.0.0.1:$PORT_STORAGE:5000 \
|
||||||
-e ANON_KEY="$SB_ANON_KEY" -e SERVICE_KEY="$SB_SERVICE_KEY" -e AUTH_JWT_SECRET="$SB_JWT_SECRET" \
|
--env-file "$STATE/storage.env" \
|
||||||
-e DATABASE_URL="postgresql://supabase_storage_admin:$SB_PW@$DB:5432/postgres" \
|
|
||||||
-e FILE_SIZE_LIMIT=52428800 -e STORAGE_BACKEND=file -e FILE_STORAGE_BACKEND_PATH=/mnt \
|
-e FILE_SIZE_LIMIT=52428800 -e STORAGE_BACKEND=file -e FILE_STORAGE_BACKEND_PATH=/mnt \
|
||||||
-e TENANT_ID=stub -e STORAGE_S3_REGION=local -e GLOBAL_S3_BUCKET=stub \
|
-e TENANT_ID=stub -e STORAGE_S3_REGION=local -e GLOBAL_S3_BUCKET=stub \
|
||||||
-e ENABLE_IMAGE_TRANSFORMATION=false -e IMAGE_TRANSFORMATION_ENABLED=false \
|
-e ENABLE_IMAGE_TRANSFORMATION=false -e IMAGE_TRANSFORMATION_ENABLED=false \
|
||||||
@@ -123,9 +143,9 @@ start_services() {
|
|||||||
|
|
||||||
start_rest() {
|
start_rest() {
|
||||||
docker run -d --name "$REST" --network "$NET" -p 127.0.0.1:$PORT_REST:3000 \
|
docker run -d --name "$REST" --network "$NET" -p 127.0.0.1:$PORT_REST:3000 \
|
||||||
-e PGRST_DB_URI="postgresql://authenticator:$SB_PW@$DB:5432/postgres" \
|
--env-file "$STATE/rest.env" \
|
||||||
-e PGRST_DB_SCHEMAS=public -e PGRST_DB_EXTRA_SEARCH_PATH=public,extensions \
|
-e PGRST_DB_SCHEMAS=public -e PGRST_DB_EXTRA_SEARCH_PATH=public,extensions \
|
||||||
-e PGRST_DB_ANON_ROLE=anon -e PGRST_JWT_SECRET="$SB_JWT_SECRET" -e PGRST_DB_MAX_ROWS=1000 \
|
-e PGRST_DB_ANON_ROLE=anon -e PGRST_DB_MAX_ROWS=1000 \
|
||||||
"$REST_IMAGE" >/dev/null
|
"$REST_IMAGE" >/dev/null
|
||||||
wait_for "postgrest" curl -fsS -o /dev/null http://127.0.0.1:$PORT_REST/
|
wait_for "postgrest" curl -fsS -o /dev/null http://127.0.0.1:$PORT_REST/
|
||||||
}
|
}
|
||||||
@@ -145,7 +165,7 @@ seed() {
|
|||||||
|
|
||||||
gotrue_accounts() {
|
gotrue_accounts() {
|
||||||
curl -fsS -X POST http://127.0.0.1:$PORT_AUTH/admin/users \
|
curl -fsS -X POST http://127.0.0.1:$PORT_AUTH/admin/users \
|
||||||
-H "Authorization: Bearer $SB_SERVICE_KEY" -H 'Content-Type: application/json' \
|
-H @"$STATE/service-header" -H 'Content-Type: application/json' \
|
||||||
-d '{"email":"pre-confirmed@sandbox.local","password":"Sandbox-only-pw-1","email_confirm":true}' >/dev/null
|
-d '{"email":"pre-confirmed@sandbox.local","password":"Sandbox-only-pw-1","email_confirm":true}' >/dev/null
|
||||||
curl -fsS -X POST http://127.0.0.1:$PORT_AUTH/signup -H 'Content-Type: application/json' \
|
curl -fsS -X POST http://127.0.0.1:$PORT_AUTH/signup -H 'Content-Type: application/json' \
|
||||||
-d '{"email":"pre-unconfirmed@sandbox.local","password":"Sandbox-only-pw-1"}' >/dev/null
|
-d '{"email":"pre-unconfirmed@sandbox.local","password":"Sandbox-only-pw-1"}' >/dev/null
|
||||||
@@ -154,7 +174,7 @@ gotrue_accounts() {
|
|||||||
cmd_up() {
|
cmd_up() {
|
||||||
local mode="${1:-}"
|
local mode="${1:-}"
|
||||||
docker ps -a --format '{{.Names}}' | grep -q '^faunosteo_dbsandbox_' && die "already up (pnpm db:sandbox down)"
|
docker ps -a --format '{{.Names}}' | grep -q '^faunosteo_dbsandbox_' && die "already up (pnpm db:sandbox down)"
|
||||||
new_secrets; . "$SECRETS"
|
new_secrets
|
||||||
start_services
|
start_services
|
||||||
case "$mode" in
|
case "$mode" in
|
||||||
fresh)
|
fresh)
|
||||||
@@ -183,7 +203,7 @@ cmd_up() {
|
|||||||
cmd_down() {
|
cmd_down() {
|
||||||
docker rm -f "$REST" "$STORAGE" "$AUTH" "$MAIL" "$DB" >/dev/null 2>&1 || true
|
docker rm -f "$REST" "$STORAGE" "$AUTH" "$MAIL" "$DB" >/dev/null 2>&1 || true
|
||||||
docker network rm "$NET" >/dev/null 2>&1 || true
|
docker network rm "$NET" >/dev/null 2>&1 || true
|
||||||
rm -f "$SECRETS"
|
rm -f "$SECRETS" "$STATE/db.env" "$STATE/auth.env" "$STATE/storage.env" "$STATE/rest.env" "$STATE/service-header"
|
||||||
echo "== sandbox removed"
|
echo "== sandbox removed"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user