diff --git a/docs/development/SUPABASE.md b/docs/development/SUPABASE.md index 88e2a8f9..639980e8 100644 --- a/docs/development/SUPABASE.md +++ b/docs/development/SUPABASE.md @@ -286,8 +286,40 @@ select 'rls tables public ' || count(*) from pg_class c join pg_namespace n on n SQL devsql postgres < "$B/counts.sql" > "$B/counts-before.txt" && cat "$B/counts-before.txt" # users = profiles = N (6 on 2026-10-26); the other lines are the reference of steps 3b and 5 -echo "select 'vault secrets ' || count(*) from vault.secrets;" | devsql postgres -# 0: the one table whose rows step 3b cannot reload (see its COPY error); any other value = STOP +# Privileges (read only), the reference of step 3b: effective ACLs of the application schemas, grants +# of auth and storage to the API roles, policies, triggers, constraints, extensions, postgres defaults. +cat > "$B/privs.sql" <<'SQL' +select 'acl relations ' || n.nspname || ' ' || md5(string_agg(c.relname || ':' || coalesce(c.relacl, acldefault(case c.relkind when 'S' then 's'::"char" else 'r'::"char" end, c.relowner))::text || ':' || c.relrowsecurity || c.relforcerowsecurity, '|' order by c.relname)) + from pg_class c join pg_namespace n on n.oid = c.relnamespace + where n.nspname in ('public', 'private', 'audit', 'supabase_migrations') and c.relkind in ('r', 'v', 'S', 'p') group by n.nspname order by n.nspname; +select 'acl functions ' || n.nspname || ' ' || md5(string_agg(p.oid::regprocedure::text || ':' || coalesce(p.proacl::text, '-') || ':' || p.prosecdef || ':' || coalesce(array_to_string(p.proconfig, ','), '-'), '|' order by p.oid::regprocedure::text)) + from pg_proc p join pg_namespace n on n.oid = p.pronamespace where n.nspname in ('public', 'private', 'audit') group by n.nspname order by n.nspname; +select 'acl schemas ' || md5(string_agg(nspname || ':' || coalesce(nspacl::text, '-'), '|' order by nspname)) + from pg_namespace where nspname in ('public', 'private', 'audit'); +select 'grants ' || n.nspname || ' ' || md5(string_agg(c.relname || ':' || pg_get_userbyid(a.grantee) || ':' || a.privilege_type || ':' || a.is_grantable, '|' + order by c.relname, pg_get_userbyid(a.grantee), a.privilege_type)) + from pg_class c join pg_namespace n on n.oid = c.relnamespace cross join lateral aclexplode(c.relacl) a + where n.nspname in ('auth', 'storage') and a.grantee not in (c.relowner, 'postgres'::regrole) group by n.nspname order by n.nspname; +select 'policies ' || md5(string_agg(schemaname || '.' || tablename || '.' || policyname || ':' || cmd || ':' || roles::text || ':' || coalesce(qual, '-') || ':' || coalesce(with_check, '-'), '|' + order by schemaname, tablename, policyname)) from pg_policies where schemaname in ('public', 'storage', 'audit'); +select 'triggers ' || count(*) || ' ' || md5(string_agg(tgrelid::regclass::text || '.' || tgname || ':' || tgenabled::text || ':' || tgfoid::regprocedure::text, '|' order by tgrelid::regclass::text, tgname)) + from pg_trigger t join pg_class c on c.oid = t.tgrelid join pg_namespace n on n.oid = c.relnamespace + where not tgisinternal and n.nspname in ('public', 'private', 'audit', 'auth', 'storage'); +select 'constraints ' || count(*) || ' ' || md5(string_agg(conrelid::regclass::text || '.' || conname || ':' || pg_get_constraintdef(oid), '|' order by conrelid::regclass::text, conname)) + from pg_constraint where connamespace::regnamespace::text in ('public', 'private', 'audit'); +select 'extensions ' || string_agg(extname || '@' || extversion, ',' order by extname) from pg_extension; +select 'default acl postgres ' || count(*) || ' ' || md5(string_agg(defaclnamespace::regnamespace::text || ':' || defaclobjtype::text || ':' || defaclacl::text, '|' order by defaclnamespace::regnamespace::text, defaclobjtype::text)) + from pg_default_acl where defaclrole = 'postgres'::regrole; +SQL +devsql postgres < "$B/privs.sql" > "$B/privs-before.txt" && wc -l < "$B/privs-before.txt" # 15 +cat > "$B/managed.sql" <<'SQL' +select 'vault secrets ' || count(*) from vault.secrets; +select 'managed default acl ' || count(*) from pg_default_acl + where defaclrole in ('supabase_admin'::regrole, 'supabase_auth_admin'::regrole); +SQL +devsql postgres < "$B/managed.sql" +# vault secrets 0, else STOP: no backup, no M1 to M3; vault rows need a procedure of their own (keys) +# managed default acl D (21 on 2026-10-26): step 3b leaves out exactly D + 8 entries # 3. Backup outside Git: the whole `postgres` database (auth, storage metadata, public, private, audit, # migration history, grants) and the bytes of the Storage files (the storage container's volume). @@ -299,29 +331,43 @@ docker exec -i supabase_db_faunosteo pg_restore --list < "$B/dev-before-v1-1.dum | grep -cE "TABLE DATA (auth users|public profiles|storage objects|supabase_migrations schema_migrations)" # 4 tar -tvf "$B/dev-before-v1-1-storage.tar" | grep -c '^-' # the volume's file count (11 on 2026-10-26) -# 3b. The dump restores, on the isolated stack (never on the DEV server): a scratch database of the -# sandbox, compared line by line with step 2, then dropped. `restore_check_v1_1` is the only database -# created or dropped here; never type another name. +# 3b. The dump restores, on the isolated stack (never on the DEV server), with exit status 0: a scratch +# database of the sandbox, restored in one transaction that stops at the first error, compared with +# step 2, then dropped. `restore_check_v1_1` is the only database created or dropped here. +# The restore leaves out the D + 8 TOC entries `postgres` may not replay; the dump file itself stays +# complete and unchanged (the list is a copy of its table of contents): +# - D « DEFAULT ACL … supabase_admin | supabase_auth_admin »: default privileges for objects those +# roles create (platform defaults; `postgres` may not change another role's defaults; no data, +# no application privilege; the sandbox image holds its own); +# - 5 « ACL vault »: secrets, decrypted_secrets, create_secret, update_secret, +# _crypto_aead_det_decrypt; with --no-owner their extension makes them postgres's own, and +# postgres cannot grant itself a grant option; the service_role grants of these entries come +# out identical from the extension (rehearsal of 2026-10-26); +# - 1 « ACL pg_catalog » pg_reload_conf, 1 « ACL extensions » pg_stat_statements_reset: owned by +# supabase_admin, postgres may not grant on them; +# - 1 « TABLE DATA vault secrets »: postgres may not write vault.secrets; nothing is lost only +# because step 2 read 0 rows. With rows there, this procedure does not apply. +docker exec -i faunosteo_dbsandbox_db pg_restore --list < "$B/dev-before-v1-1.dump" > "$B/full.list"; echo "list exit $?" # 0 +grep -E '^[0-9]+; [0-9]+ [0-9]+ (DEFAULT ACL [a-z_]+ DEFAULT PRIVILEGES FOR (SEQUENCES|FUNCTIONS|TABLES) (supabase_admin|supabase_auth_admin)|ACL pg_catalog FUNCTION pg_reload_conf\(\) supabase_admin|ACL extensions FUNCTION pg_stat_statements_reset\(.*\) supabase_admin|ACL vault (FUNCTION|TABLE) .* supabase_admin|TABLE DATA vault secrets supabase_admin)$' \ + "$B/full.list" > "$B/excluded.list" +cut -d' ' -f4-5 "$B/excluded.list" | sort | uniq -c +# exactly: D « DEFAULT ACL », 1 « ACL extensions », 1 « ACL pg_catalog », 5 « ACL vault », 1 « TABLE DATA »; +# any other count = STOP (never widen the pattern to make it fit) +grep -vxFf "$B/excluded.list" "$B/full.list" > "$B/restore.list" +wc -l < "$B/full.list"; wc -l < "$B/restore.list" # the second = the first minus D + 8 +docker cp "$B/restore.list" faunosteo_dbsandbox_db:/tmp/restore.list # names only, no data; gone with the sandbox docker exec faunosteo_dbsandbox_db sh -c 'PGPASSWORD="$POSTGRES_PASSWORD" createdb -h 127.0.0.1 -U postgres restore_check_v1_1' docker exec -i faunosteo_dbsandbox_db sh -c 'PGPASSWORD="$POSTGRES_PASSWORD" pg_restore -h 127.0.0.1 -U postgres \ - -d restore_check_v1_1 --no-owner' < "$B/dev-before-v1-1.dump" 2> "$B/restore-check.log"; echo "pg_restore exit $?" -# exit 1: pg_restore reports the 26 errors below and goes on; any other status = STOP -grep -c '^pg_restore:' "$B/restore-check.log" # 27: the 26 errors and the closing line -grep '^pg_restore:' "$B/restore-check.log" | grep -v 'could not execute query: ERROR:' -# only « pg_restore: warning: errors ignored on restore: 26 »; any other line (input file, connection) = STOP -grep 'ERROR:' "$B/restore-check.log" | sed -E 's/.*ERROR: +//' | sort | uniq -c -# 18 « permission denied to change default privileges », 5 « grant options cannot be granted back to -# your own grantor », 1 each « permission denied for » table secrets, function pg_stat_statements_reset, -# function pg_reload_conf -grep '^Command was:' "$B/restore-check.log" | cut -d' ' -f3-4 | sort | uniq -c -# 18 ALTER DEFAULT, 1 COPY vault.secrets, 5 GRANT ALL, 2 GRANT SELECT,REFERENCES,DELETE,TRUNCATE; -# any other message or command = STOP. These are pg_restore errors, not privilege tests (none runs -# here): 25 statements `postgres` may not issue on Supabase-managed objects (default privileges of -# supabase_admin and supabase_auth_admin, grant options on vault, pg_catalog and extensions -# functions), which the image sets up itself; and the load of vault.secrets, lossless only because -# step 2 found that table empty + -d restore_check_v1_1 --no-owner --use-list=/tmp/restore.list --single-transaction --exit-on-error' \ + < "$B/dev-before-v1-1.dump" 2> "$B/restore-check.log"; echo "pg_restore exit $?" +wc -c < "$B/restore-check.log" # 0: the log, rewritten by this run, holds no message +# exit 0 and an empty log, else STOP: the single transaction rolled back, the scratch database is +# empty; keep the log (a COPY error can quote a row: report only its ERROR and Command was lines), +# drop the database (last line), report boxsql restore_check_v1_1 < "$B/counts.sql" > "$B/counts-restored.txt" -diff "$B/counts-before.txt" "$B/counts-restored.txt" && echo "restore identical" # no line may differ +diff "$B/counts-before.txt" "$B/counts-restored.txt" && echo "rows identical" # the 28 lines +boxsql restore_check_v1_1 < "$B/privs.sql" > "$B/privs-restored.txt" +diff "$B/privs-before.txt" "$B/privs-restored.txt" && echo "privileges identical" # the 15 lines docker exec faunosteo_dbsandbox_db sh -c 'PGPASSWORD="$POSTGRES_PASSWORD" dropdb -h 127.0.0.1 -U postgres restore_check_v1_1' # 4. Apply without reset (M1's backfill runs inside the migration); no app session open on DEV meanwhile. @@ -331,14 +377,30 @@ pnpm supabase migration up --local; echo "migration up exit $?" # 20261026100000_animals_relations.sql; exit 0. Keep the whole output for the report. ``` -Step 3b was rehearsed twice on the sandbox with fictitious data (2026-10-26: -a 119 MB load-test dump, then the same database piped into the scratch -database): the 26 pg_restore errors above (same messages, same 26 commands) -and nothing else, and on the second run the step 2 output (2 144 accounts, -row fingerprints, structure) identical line by line. The exit status was not -recorded then; pg_restore exits 1 whenever it ends on « errors ignored on -restore ». This backup is no full cluster backup. What it covers and what it -does not: +Step 3b was rehearsed on the sandbox with fictitious data, never with DEV +data (2026-10-26, a 156 MB dump of the sandbox's `postgres` database at M3, +2 144 accounts, vault empty; D = 18 there, the sandbox has no +`supabase_functions` schema). Four variants, exit status read from the shell: + +- `--no-owner` alone (the earlier procedure): exit 1, 26 errors, the 26 + entries above (18 default privileges, 5 vault ACL, 2 ACL on supabase_admin + functions, the vault.secrets COPY); rows identical; +- `--no-acl`: exit 1 (the vault.secrets COPY), and every grant of the + application schemas lost: rejected, it removes what the check must prove; +- `--exit-on-error` on the whole list: exit 1 at the vault.secrets COPY, + before the policies: rejected; +- the filtered list with `--single-transaction --exit-on-error`: exit 0, empty + log, the 28 lines and the 15 privilege lines identical. Retained. + +A table whose stored ACL is the explicit owner-only one comes back with no +ACL, the same privileges (pg_dump leaves out an ACL equal to the default): +`privs.sql` therefore compares effective ACLs (`acldefault`). DEV differs +from the sandbox (read-only check, 2026-10-26): D = 21, the three more rows +being the defaults of its `supabase_functions` schema, so 29 entries left +out; its other own objects (`supabase_functions`, `_realtime`) grant +`postgres` no grant option. An entry that would still fail on the DEV dump +stops the single transaction: exit status not 0, nothing kept, STOP. This +backup is no full cluster backup. What it covers and what it does not: - the dump holds the whole `postgres` database: auth (accounts, identities, sessions, password hashes: the files are as sensitive as DEV itself), @@ -346,7 +408,8 @@ does not: the grants. It does not hold the cluster roles (created by the image), the `_supabase` database (services' internal metadata), nor the Storage file bytes. It holds the vault rows, but a restore as `postgres` cannot reload - them (the COPY error of step 3b; DEV has none, step 2); + them (left out of step 3b; it only proves anything because DEV has none, + step 2, and says nothing of a restore with vault rows); - the tar holds the Storage file bytes; restoring it is not rehearsed, and M1 to M3 touch neither Storage nor `assets`; - a restore of this dump into the DEV stack is not rehearsed: step 3b proves @@ -426,9 +489,10 @@ the scripts: Stop and report instead of continuing when: `migration list` shows another pending migration or a remote-only one; `db:types:check` before step 4 shows a removed line or another name than the 24 of step 1; the dump check misses a -line; the restore check prints another error or a differing line; `migration -up` applies anything else than the three V1-1 migrations or fails; any check -after it differs from the expectation. +line; `vault.secrets` is not empty; the excluded entries differ from D + 8; +the restore check exits other than 0, logs anything or shows a differing +line; `migration up` applies anything else than the three V1-1 migrations +or fails; any check after it differs from the expectation. When something fails: