Skip to content

fix(db): grant service_role the twenty core tables it cannot read, and ratchet it - #784

Merged
thinkclay merged 1 commit into
mainfrom
fix/751-service-role-grants
Sep 17, 2026
Merged

thinkclay merged 1 commit into
mainfrom
fix/751-service-role-grants

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

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


🤖 Generated with Claude Code

…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]>
@github-actions

Copy link
Copy Markdown
Contributor

📊 Performance Benchmark Results


@thinkclay
thinkclay merged commit abe52db into main Sep 17, 2026
17 checks passed
@thinkclay
thinkclay deleted the fix/751-service-role-grants branch September 17, 2026 01:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

20 more core tables grant nothing to service_role in production

1 participant