Repository navigation
fix: keep pending Auth tokens out of auth.dump and run the drill's test sign-up offline (2.11.1) - #71
Merged
Merged
Conversation
auth.dump held auth.users as pg_dump wrote it, including unused confirmation, recovery, email/phone change and reauthentication tokens. They were only cleared at restore time. GoTrue's /verify accepts a token_hash, so anyone who could read the bucket could use a pending recovery token against the source project until it expired. pg_dump can't leave column values out, so its arguments stay exactly as they were (same snapshot, same load and locks on the source) and the archive is rewritten on its way to the bucket: the COPY data of auth.users gets the same values the restore's CLEAR_PENDING_AUTH_SQL writes, and the TOC and every other data block pass through byte for byte. The result is still a custom-format archive that pg_restore, the drill, the restore and the RECOVERY.md steps read as before; the size cap, byte count and sha256 are taken from the rewritten stream. Anything the rewriter doesn't recognise (archive version, compression, recorded data offsets, a row that doesn't match the column list) fails the Auth section instead of uploading an archive that may still hold tokens. Archives from 2.10 and 2.11.0 keep getting the tokens cleared at restore.
The simulated sign-up fires the user's triggers on auth.users inside a transaction that is rolled back, but a trigger that writes elsewhere through dblink or postgres_fdw commits there regardless. On 2.11.0 a dblink trigger in the test suite wrote a row into another database during the drill. The CLI and pg_restore reach the sandbox through a published port, which neither --network none nor an --internal network allows, so the sandbox stays on its network for the restore and is taken off every network only for the sign-up. The sign-up then runs through psql inside the container over the unix socket. Afterwards every client session is terminated and confirmed gone before the sandbox is reconnected (a trigger still running must not get the network back); the new host port is picked up so a sandbox kept with --keep stays reachable. If the sandbox can't be taken off the network, no sign-up runs. A trigger that fails while the sandbox has dblink, postgres_fdw, http, pg_net or wrappers installed, or with a connection error, is reported as not tested rather than as a sign-up that would fail.
Also ignore .worktrees/, where local git worktrees of this repo live.
The rewriter kept the unfinished COPY row in memory and copied it again with each new chunk. A single huge, highly compressible metadata value could exhaust memory long before the 2 GiB cap on the archive applied. It now walks each chunk once, passes untouched fields straight through and drops only the bytes of the fields it blanks, so memory stays at one chunk however long a row is (a 256 MiB row now takes under 4 s). It also accepted an archive that ended right after the auth.users block even when the TOC still declared data for auth.identities, which pg_restore then rejected. Every TOC entry that declares data must now have its block, and a block the TOC doesn't declare is an error, so such an archive fails the Auth section instead of being uploaded.
…ign-up Reconnecting after the test sign-up relied on every client session having ended. That can't rule out a docker exec that was still starting when it was stopped, or anything a trigger left running, and either would get the network back. The sandbox now stays off the network once the test sign-up starts; nothing after it connects to the sandbox through the host port. Without --keep it is destroyed as before. With --keep the report says keptSandbox.offline: true with no connection string, and both the report and the warning give the docker exec command to get in. A stopped or timed-out Auth check ends the offline sessions through docker exec instead of the unreachable port. Having dblink, http or another network extension installed no longer turns every trigger failure into "not tested", which hid real failures such as 42501. Only a connection error (SQLSTATE 08xxx) or an error context showing a network call (dblink, http, net.http_*, a foreign table, COPY ... PROGRAM) counts as needing the network. The dblink test now runs the same call from the sandbox while it is online first (through --check-cmd), so it proves the other database is reachable before asserting the offline sign-up wrote nothing.
The rewriter accepted auth.users data with no "\." terminator and passed any bytes after it straight through. pg_dump always ends each COPY block with the terminator followed only by blank lines, so anything else now fails the Auth section instead of being uploaded. Real pg_dump 17 output still passes unchanged.
…p for sure A failed drill with --keep no longer runs the Auth test sign-up, so the kept sandbox is never taken off the network and keptSandbox.connString works exactly as in 2.11.0 (scripts rely on it). The Auth section says the sign-up was skipped because --keep keeps the sandbox reachable; the verdict doesn't change. keptSandbox also gets an inspect command. A passing drill with --keep keeps nothing, so its sign-up still runs offline as without --keep. When the local docker exec running the sign-up times out or is stopped, the sign-up inside the container is now stopped too, and the drill waits (up to about 15 s) until it is gone before reporting: a cancel file keeps a late docker exec from starting psql, the container's psql is killed by its pid file (checked to still be psql), and its sessions are terminated by application_name or the run's random marker, until two rounds in a row come back clean. Cancellation is re-checked before every network disconnect, and the sandbox is marked offline only once a disconnect has succeeded. Permission and missing-object errors (42501, 42883, 42704, 3F000, 42P01, 42703) are now reported as sign-up failures even when they come from a dblink or http call; only connection errors and other failures of a network call count as "needed the network".
Only evidence of a connection failure now marks the test sign-up as not tested: a connection-class SQLSTATE (08xxx), a network call that itself failed to connect or resolve, or a failed COPY ... PROGRAM. Every other error is a real sign-up failure, including a check violation in a statement that also calls net.http_post. The error message is read only for that decision and never reported. psql inside the disconnected sandbox now connects to 127.0.0.1 instead of the unix socket, so inet_client_addr() is set as it is for a real sign-up and triggers that need it aren't reported as broken. Starting and stopping the sign-up can no longer race: the launcher claims its pid file with an exclusive create before it checks the cancel marker, and cleanup creates the cancel marker before it reads the pid file, then kills that pid whether it is still the shell or already psql. A test pauses the launcher between the check and exec to cover the case. keptSandbox.inspect is optional in DrillReport. Archive fixtures that the local pg_restore can't read are skipped with the version it needs. Report and README wording follows the copy review.
…storable A constraint the user added to auth.users, such as CHECK (recovery_token <> '') or a unique index on a token column without a WHERE clause, can accept the original rows and reject the cleared ones, so the original archive restored while the scrubbed one didn't. The inspection now checks, in the same read-only snapshot transaction and connection, every NOT NULL, CHECK and unique index on auth.users that involves a cleared column, applied to the rows as they would be after clearing. Domain types, foreign keys and exclusion constraints on those columns, and any check that errors, count as conflicts. On a conflict the Auth section fails with the constraint's name and no auth.dump is written; the database backup is unaffected. Supabase's own partial token indexes and confirm-status CHECK pass, and the test fixture now has them. A failed COPY ... PROGRAM now counts as "may need network access" only when its message shows a connection or name-resolution failure. The cleanup of a stopped sign-up runs once per sign-up, shared by the abort path and the drill, under a single 15 s deadline. Adds a test for pg_dump exiting 0 with a broken Auth archive.
The previous commit evaluated the user's CHECK expressions and unique index keys against the cleared rows on the source database. Those expressions can call user-defined functions (dblink, http, slow or side-effecting code) and would have run with the backup role's privileges against production. The check now only reads pg_attribute, pg_constraint, pg_index and pg_depend, and decides from the definition text, conservatively: a CHECK on a cleared column must be identical to Supabase's own email_change_confirm_status check, and a unique index that involves a cleared column must be Supabase's own shape (one token column, WHERE token !~ '^[0-9 ]*$', text as printed by PG16 and PG17). NOT NULL on a column cleared to NULL, a domain type, a foreign key or an exclusion constraint on a cleared column are conflicts. A test adds a CHECK that calls a function raising if it is ever called and asserts the backup names that constraint instead of failing on the call.
…emoved When the Auth check times out without --keep, the drill removes the sandbox while the stopped sign-up is still being cleaned up, so the cleanup could not reach it and warned that it could not confirm the sign-up ended. A stopped container has nothing left running, so the warning now only appears when the container is still running.
…nition Checking each constraint for whether it could reject the cleared tokens missed cases (a generated column derived from a token with a CHECK on it, an exclusion constraint on an expression). auth.dump is now written only if everything on auth.users that COPY enforces is exactly what Supabase creates: every CHECK, foreign key and exclusion constraint, every unique or exclusion index (definition text without the name), and every generated column. NOT NULL on a column cleared to NULL and domain-typed cleared columns still count. The allowlist was read from the catalog after GoTrue v2.186.0 ran all its migrations on supabase/postgres 17.6: one CHECK on email_change_confirm_status, the id, phone and partial email unique indexes, the five partial token indexes and the confirmed_at generated column. Only catalogs are read; nothing user-defined is executed. Otherwise the Auth section fails naming the first object, and the database backup is unaffected. Catalog names in these reasons are quoted with control characters escaped, and a free-text Auth reason in RECOVERY.md is Markdown-escaped, so a name with a newline or a Markdown link can't forge a log line or a link. The test sign-up now also reads the error DETAIL, for classification only. A connection-class error counts as "failed to connect while offline" only with DNS, routing or socket evidence (including musl's "Network unreachable"); authentication and configuration failures are real sign-up failures. ASSERT failures, which WHEN OTHERS doesn't catch, are caught and reported as a failing sign-up.
The Auth reasons that name a constraint are full sentences ending in a full stop, and the warning appends ". The database backup itself is complete.", which printed "..". The reason's own full stop is dropped before the sentence is appended.
Requiring every unique index, CHECK, foreign key, exclusion constraint and generated column on auth.users to be on the Supabase allowlist meant that an unrelated change, such as UNIQUE (email), or a new index in a future GoTrue migration, would quietly stop Auth backups until the allowlist was updated. Now only objects that could see a cleared column must match Supabase's own definitions: - a CHECK whose conkey (all the columns it references; 0 for a whole-row reference) includes a cleared column or 0; - a foreign key with a cleared column in conkey; - a unique or exclusion index whose key columns, expressions or WHERE clause reference a cleared column or the whole row; - a generated column computed from a cleared column, and anything that uses such a generated column. Columns used by expressions are read from the stored expression trees (indexprs, indpred, adbin) as ":varattno N". pg_depend can't be used: an index made only of expressions always records a dependency on the whole table there, which can't be told apart from a real whole-row reference. Everything is still read from the catalog only. UNIQUE (email), a unique column added by the user, a unique index on lower(email) and a foreign key on instance_id now leave Auth included; a unique index on lower(recovery_token) is refused like the six cases before. The restore test with UNIQUE (email) asserts its snapshot has the Auth users again.
…ably Restoring auth.dump rebuilds every index on the cleared rows, not only unique ones: a plain index on an expression such as 1 / length(recovery_token) fails on ''. Any index with an expression or a WHERE clause that can see a cleared column now has to match Supabase's own definition too; indexes on plain columns only can't fail a rebuild and are left alone. Like pg_dump, only valid, ready indexes are looked at, so an index left invalid by a failed CREATE INDEX CONCURRENTLY doesn't count. The comparison reads pg_get_*def text, which changes with quote_all_identifiers and search_path. The inspection now pins both (SET LOCAL) before reading definitions, so a role or database setting can't make Supabase's own indexes look custom. In RECOVERY.md a free-text Auth reason is now a code span, fenced with one more backtick than the longest run inside, so a name with a bare URL can't turn into a link under GFM. When the schema list includes auth, the main dump still holds the Auth tables as they are, pending tokens included. The backup now says so in one warning line, and the README says it under BACKUPDRILL_SCHEMAS.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this changes
Two fixes to how the CLI handles Supabase Auth users, released as 2.11.1.
Pending Auth tokens no longer reach the bucket. Since 2.10.0,
auth.dumphas carriedauth.usersas-is, including pending confirmation, recovery, email-change, phone-change and reauthentication tokens. They were only cleared on restore. GoTrue's/verifyacceptstoken_hash, so anyone who can read the bucket could use a pending recovery token against the source project until it expired. Theauth.usersdata is now rewritten while it streams to the bucket: the same columnsCLEAR_PENDING_AUTH_SQLclears on restore are blanked, nothing else changes, and the TOC is byte-identical. The manifest's sha256, byte count and 2 GiB cap are computed on the rewritten bytes. Anything the rewriter doesn't recognise (archive format outside 1.12–1.16, lz4/zstd, offsets in the TOC, a field count mismatch, a missing data block or end marker) fails the Auth section without uploading, and the main dump is unaffected. Before dumping, the CLI reads the catalog only (it never runs user-defined expressions on the source) and skipsauth.dumpwith a reason when a non-stock constraint, index or generated column onauth.userscould reject the cleared values. If the schema list includesauth, the main dump still carries the raw Auth tables; the CLI now warns about that and the README says so. Restore still runsCLEAR_PENDING_AUTH_SQL, so snapshots from 2.10 and 2.11.0 behave as before.The drill's Auth test sign-up runs with the sandbox off the network. The simulated sign-up fires the user's own triggers on
auth.users. A trigger using dblink, an HTTP extension orCOPY ... PROGRAMcould write to an external system even though the transaction is rolled back; a control run on 2.11.0 committed a row to another database. The sandbox is now disconnected from all Docker networks before the sign-up, psql runs inside the container over TCP loopback, and the sandbox is never reconnected. A sign-up that fails only because it needed the network is reported as not tested. With--keepand a failed drill, the sign-up is skipped sokeptSandbox.connStringstill works as in 2.11.0. Stopping or timing out a sign-up ends it inside the container under one 15-second deadline.Also included: cda459d from main (a drill stopped while preparing the table list no longer counts the backup's rows first).
Testing
pnpm typecheck,pnpm build,pnpm test: 479/479, Docker on.auth.dumprewritten from real pg_dump 14, 15, 16, 17 and 18 output, default compression and--compress=0, 20,000 rows each with tabs, newlines and non-ASCII data: identical TOC, pending tokens 20,000 → 0, restores with equal row counts, md5 of all other columns matches the source.--keepkeeps a working connection string; a stopped or timed-out sign-up leaves no psql process or session in the container.Not verified here: the hosted worker (Linux) has not run the offline sign-up yet. The first hosted drill after the worker upgrade should be checked.
Review
Cross-reviewed by Codex over 7 code rounds and 4 copy rounds. The catalog check went through several designs; the final one only reads the catalog (it never runs user-defined expressions on the source) and holds
auth.dumpback when a non-stock constraint, index or generated column onauth.userscould see a cleared column. The stock list was read from a GoTrue v2.186.0 schema (74 migrations) on PG17; the stock CHECK text and token-index predicates print identically on PG15 and PG16. The full index definitions weren't compared on PG15, so the first PG15 hosted backup should be checked forincluded.Round 7's remaining code Blocker and the pre-push review's Blocker are the same case and are waived by the maintainer: an
auth.userstoken column whose type was changed (to an enum oruuid) so it rejects the cleared value would still be marked included. The push skipped the pre-push review on the maintainer's instruction. That needs a project to alter a stock Supabase column's type; stock projects aren't affected. Deferred to 2.11.2 with the other open Should-fix items: INCLUDE columns treated as keys, bounded sign-up diagnostics, a warning for schema patterns that may matchauth, bounded TOC metadata parsing, checking token column types against the stock schema, and a README note that a customisedauth.userscan stop the Auth archive (the reason names the object).