Repository navigation
fix(db): grant service_role the twenty core tables it cannot read, and ratchet it - #784
Merged
Merged
Conversation
…d ratchet it
Fifth time for this defect. Migrations 332, 335, 347, 352, 355 and 356 each
fixed whichever tables somebody had just tripped over, because each was found
the same way — an endpoint answering `42501 permission denied` somewhere a
person happened to be looking. `service_role` is the server's own identity and
bypasses RLS by design, but a missing table-level GRANT fires before RLS is
evaluated, so no policy can rescue it and the error names nothing useful.
Measured across all three remote databases rather than taken from the issue:
prod 20 tables preview 20 tables dev 12 tables
Migration 357 grants the twenty, scoped to a fixed list. `GRANT ALL ON ALL
TABLES IN SCHEMA core` would close today's gap and silently swallow every table
added afterwards, which is the opposite of what the new check enforces. GRANT is
idempotent, so the eight dev already had are a no-op there.
**#751 asked whether a migration had been applied in one place and not another.
It had not.** All three ledgers were already complete and identical — 192 of 192
recorded, nothing unrecorded — and on dev those eight tables carry the full
privilege set (DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE),
which is the shape `GRANT ALL` leaves rather than a SELECT-only fix. They were
granted by hand against dev, almost certainly while chasing one of these 500s,
and never carried further. The ledger was not lying; nobody wrote it down.
## The ratchet
`scripts/check-migrations.mjs` gains a third check: every core table a migration
creates must be granted to service_role by that migration or a later one, with a
shrink-only `BASELINE_UNGRANTED` — empty, and 357 is what emptied it.
Creation order is load-bearing, not a detail. Migration 096 contains
DO $$ BEGIN
IF EXISTS (SELECT 1 FROM information_schema.tables
WHERE table_schema='core' AND table_name='stripe_settings')
THEN GRANT ALL ON core.stripe_settings TO service_role;
END IF;
END $$;
and `core.stripe_settings` is created in migration **098**. When 096 ran the
table did not exist, the condition was false, and the grant never happened —
while the file reads as though it did. A first pass without ordering reported 19
and production reported 20; that table was the difference. So a grant only
counts from the migration that creates the table onwards.
core is the only schema checked: `onet` and `community` have
ALTER DEFAULT PRIVILEGES for service_role (migrations 002 and 314), so new
tables there are covered automatically. core has no such default, which is
exactly why this keeps happening to core.
## Verified
Calibrated against reality rather than asserted — with 357 excluded the check
reports precisely the twenty tables `has_table_privilege` reports missing on
production, and with it included, none. Probed in all four directions:
357 removed ✗ names all twenty, with creating migration
new ungranted core table ✗ "core.probe_widget (created in migration 358)"
same table, granted ✓
stale baseline entry ✗ "BASELINE_UNGRANTED lists tables that now have a grant"
Applied and recorded on all three environments. Re-measured after:
prod 0 preview 0 dev 0 ledgers 193/193, 0 unrecorded
Closes #751
Co-Authored-By: Claude Opus 5 <[email protected]>
Contributor
📊 Performance Benchmark Results |
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.
Fifth time for this defect. Migrations 332, 335, 347, 352, 355 and 356 each
fixed whichever tables somebody had just tripped over, because each was found
the same way — an endpoint answering
42501 permission deniedsomewhere aperson happened to be looking.
service_roleis the server's own identity andbypasses RLS by design, but a missing table-level GRANT fires before RLS is
evaluated, so no policy can rescue it and the error names nothing useful.
Measured across all three remote databases rather than taken from the issue:
Migration 357 grants the twenty, scoped to a fixed list.
GRANT ALL ON ALL TABLES IN SCHEMA corewould close today's gap and silently swallow every tableadded afterwards, which is the opposite of what the new check enforces. GRANT is
idempotent, so the eight dev already had are a no-op there.
#751 asked whether a migration had been applied in one place and not another.
It had not. All three ledgers were already complete and identical — 192 of 192
recorded, nothing unrecorded — and on dev those eight tables carry the full
privilege set (DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE),
which is the shape
GRANT ALLleaves rather than a SELECT-only fix. They weregranted by hand against dev, almost certainly while chasing one of these 500s,
and never carried further. The ledger was not lying; nobody wrote it down.
The ratchet
scripts/check-migrations.mjsgains a third check: every core table a migrationcreates must be granted to service_role by that migration or a later one, with a
shrink-only
BASELINE_UNGRANTED— empty, and 357 is what emptied it.Creation order is load-bearing, not a detail. Migration 096 contains
and
core.stripe_settingsis created in migration 098. When 096 ran thetable did not exist, the condition was false, and the grant never happened —
while the file reads as though it did. A first pass without ordering reported 19
and production reported 20; that table was the difference. So a grant only
counts from the migration that creates the table onwards.
core is the only schema checked:
onetandcommunityhaveALTER DEFAULT PRIVILEGES for service_role (migrations 002 and 314), so new
tables there are covered automatically. core has no such default, which is
exactly why this keeps happening to core.
Verified
Calibrated against reality rather than asserted — with 357 excluded the check
reports precisely the twenty tables
has_table_privilegereports missing onproduction, and with it included, none. Probed in all four directions:
Applied and recorded on all three environments. Re-measured after:
Closes #751
🤖 Generated with Claude Code