Skip to content

Add row level security to processed_bills - #229

Draft
MaxGhenis wants to merge 8 commits into
mainfrom
fix/processed-bills-row-level-security
Draft

MaxGhenis wants to merge 8 commits into
mainfrom
fix/processed-bills-row-level-security

Conversation

@MaxGhenis

@MaxGhenis MaxGhenis commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Every other migration that creates a table enables row level security on it. scripts/sql/009_processed_bills_rls.sql does the same for processed_bills:

  • enables row level security on processed_bills;
  • adds the two policies that research, reform_impacts, validation_metadata and dataset_hashes use: public read and service write;
  • revokes every privilege on the table from PUBLIC, anon and authenticated, then grants SELECT back to anon and authenticated, and SELECT, INSERT, UPDATE, DELETE to service_role;
  • revokes every privilege on the sequences the table owns (serial or identity) from the same three, and grants USAGE, SELECT on them to service_role only;
  • revokes every privilege on each view built on processed_bills, directly or through other views, from the same three, and grants SELECT on it to anon, authenticated and service_role.

By default PostgreSQL writes through a view with the view owner's privileges, and a table's owner bypasses the table's policies. So when one role owns both the table and an updatable view on it, anon can change rows past the policies with a write grant on that view.

The app reads processed_bills only through useProcessedBills in BillActivityFeed.jsx, with the anon key, and never writes it. refresh_bill_status.py, auto_triage.py, congress_monitor.py, openstates_monitor.py and the /triage-bills command write with SUPABASE_KEY, which CLAUDE.md names as the service key.

How to apply it

Maintainers run the scripts/sql files by hand in the Supabase dashboard, so merging changes nothing in the live database. The README's migration list now ends with 009. PostgreSQL ignores a table's policies until someone turns on row level security, so 009 also brings into force any policy processed_bills already has. List them before running it:

select policyname, permissive, roles, cmd, qual, with_check
from pg_policies
where schemaname = 'public' and tablename = 'processed_bills';

Once 009 runs, only a client with the service role key can write processed_bills, so check that SUPABASE_KEY holds that key wherever the writers run.

Run 009 again after anyone creates a view on processed_bills, or drops and recreates one, since PostgreSQL gives a new view its creator's default privileges. PostgreSQL keeps the grants 009 set through a CREATE OR REPLACE VIEW.

Invariants

For each of anon and authenticated:

  • it reads every processed_bills and pending_analysis row and column that the table owner reads, whatever bills the writers store;
  • it holds SELECT on processed_bills and nothing else on the table, its columns or the sequences the table owns, so it can't insert, update, delete or truncate;
  • it holds SELECT and nothing else on every view built on processed_bills, directly or through other views, so it can't write the table through a view;
  • a stray insert, update or delete grant still changes no rows, and an insert still fails. Row level security doesn't cover TRUNCATE, so only the revoke stops it;
  • if the table already has an open policy, or an open policy and PUBLIC grants of everything, 009 still closes writes. The role ends with the same privileges as on a table with neither.

PUBLIC ends with no privilege on the table, its columns, its sequences or its views. service_role keeps every read and write the monitors and triage use, including the upsert on bill_id, even when its only grants came through PUBLIC. A second run of 009 leaves anon, authenticated and PUBLIC with the same reads and privileges.

Tests

scripts/sql/access.test.js builds a temporary database with Supabase's roles, default grants and auth.role(), replays scripts/sql and seeds nine bills. The seed includes a federal bill, a skipped bill, a bill with no action date, and scores of 0 and null. It also has two ids in congress_monitor.py's range, with its two matched_query labels, and an id in openstates_monitor.py's range with the 'openstates' source that 006 lists. Each statement runs as the role, with the role claim a Data API request carries, so a policy that reads auth.role() sees what it would see on a real request.

The tests then:

  • compare each role's reads with the owner's, on the seed and on bills a fast-check property generates across the writers' id ranges, matched_query labels, sources and nulls;
  • run the bill activity feed's filter and sort as anon;
  • try each write as each role, directly and through a new single-table view, a view on that view and a CREATE OR REPLACE rewrite of pending_analysis that PostgreSQL can write through;
  • check every privilege type the server knows, for anon, authenticated and PUBLIC, on the table, its columns, its sequences and every view built on it;
  • repeat those checks after an open policy and PUBLIC grants of everything, and after a second run of 009;
  • confirm that service_role keeps the scripts' reads and writes, with its own grants and with grants that came only through PUBLIC, and that every table in public keeps row level security.

The privilege checks find the sequences through pg_get_serial_sequence and the views through information_schema, independently of the catalog queries 009 runs. The property uses fast-check, now a dev dependency pinned to 4.10.2, with a fixed seed and 30 runs that each roll back. Every test runs on a bigserial id and on identity ids generated by default and always, since nothing in the repo says how the live table numbers its rows.

The new SQL access workflow runs the tests on postgres:17 for pull requests and pushes to main that touch scripts/sql, the workflow, the Vitest config or setup, package.json or bun.lock. It sets TRACKER_TEST_POSTGRES_REQUIRED, so a missing database URL fails the job. Without TRACKER_TEST_POSTGRES_URL, bun run test skips these tests and runs the rest.

On PostgreSQL 14.24 and 17.11, all 90 tests pass.

Limits

  • 009 leaves alone any sequence processed_bills doesn't own, since such a sequence may number another table's rows.
  • No file creates processed_bills, so the fixture builds it from the columns in docs/SUPABASE_MIGRATION.md, an id key that refresh_bill_status.py selects and updates by, and a unique bill_id that congress_monitor.py's upserts conflict on.
  • The replay skips 004_add_confidence_score.sql: its pending_analysis has columns that 005 drops, so PostgreSQL refuses to run both.
  • The tests run SQL as each role. They don't drive PostgREST or the app.

Generated with Claude Code

MaxGhenis and others added 8 commits November 2, 2026 19:33
The app keeps reading through a public read policy. The monitors and
triage keep writing with the service key. Anon and authenticated lose
their write grants, so no other policy on the table can admit a write.

A new workflow replays scripts/sql on Postgres 17 and tests each role.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- 009 revokes every privilege on the table from PUBLIC, anon and
  authenticated, grants SELECT back to anon and authenticated and writes
  to service_role, and does the same for the table's sequences.
- The access test seeds varied bills, compares each role's reads with
  the owner's, runs each statement with the request's role claim, checks
  table and sequence privileges, and covers PUBLIC write grants.
- The SQL access workflow also runs on pushes to main and on Vitest and
  package changes, and fails when the database URL is missing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A view writes its tables with its owner's privileges, so a write grant on
an updatable view over processed_bills would let anon write the table past
its policies. 009 now walks every view built on processed_bills, directly
or through other views, revokes everything on it from PUBLIC, anon and
authenticated, and grants SELECT to anon, authenticated and service_role.
The sequence block's comment now says it covers the sequences the table
owns (serial or identity) and why it leaves others alone.

The access test finds the same views through information_schema, checks
every privilege type on each for anon, authenticated and PUBLIC, and tries
writes through a single-table view, a view on it and a CREATE OR REPLACE
rewrite of pending_analysis. The suite's tests and hooks get explicit
timeouts, since a busy machine can outlast Vitest's defaults.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Writes with no grant insert an explicit id and expect permission denied
  for the table, so a sequence error can't stand in for that refusal.
- The privilege report covers column privileges and column ACL entries on
  processed_bills and the sequences it owns, for PUBLIC as well as anon and
  authenticated, and for every privilege type the server knows. The PUBLIC
  scenario grants PUBLIC everything on the table, the sequence and
  pending_analysis, plus column grants, and checks the report after 009.
- Every test runs on a bigserial id and on identity ids generated by
  default and always. The always-identity sequence has its own name.
- The seed adds the ids and labels congress_monitor.py and
  openstates_monitor.py write, and the stray-grant test inserts such ids.
- A fast-check property (fast-check 4.10.2, pinned) generates bills across
  the writers' id ranges, sources, matched_query values and nulls, and
  checks that each role reads exactly what the owner reads. It runs a
  fixed seed and a bounded number of runs, each in a transaction that
  rolls back.
- The fixture's comment says where its columns come from, and the fixture
  drops created_at, which no writer writes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Maintainers run scripts/sql by hand from this list, so 009 joins it, with
the query that lists the policies on processed_bills to run first:
turning on row level security brings any of them into force.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
009's view comment now names anon and authenticated as the roles left with
SELECT alone, and says a new view takes its creator's default privileges.
The seed and property comments stop crediting openstates_monitor.py with
the 'openstates' source: it builds that value but never writes it, so the
comments name 006, which lists it. Comments only; no behaviour changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…BLIC

The service role's PUBLIC-only test now runs congress_monitor.py's upsert
on bill_id alongside the update, insert and delete, so the invariant that
it keeps every write the scripts use, upsert included, has a test in both
grant states. The upsert lives in one constant both tests share.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… states

Both service role tests now compare its read of processed_bills and
pending_analysis with the owner's, with its own grants and with grants
that came only through PUBLIC. That comparison covers the PUBLIC-only
test's old pending_analysis row count, which goes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant