Context
My own client portal (Next.js 16, Supabase, Stripe, Resend) is live in production: clients log in, see their projects, pay invoices, and download files only after payment. It was built with Claude Code. Row Level Security was in place and had already passed 10 manual checks, including cross-account ID-swap tests and expired signed URLs. The code looked safe.
What I did
Inventoried every table, policy, function and trigger from the migrations and app code.
Ran six read-only SQL queries directly against the production database and compared the results with the migrations, instead of trusting the code alone.
What I found
• Leftover default privileges. The authenticated role held TRUNCATE, TRIGGER, REFERENCES and MAINTAIN on three tables. TRUNCATE bypasses RLS. It wasn't reachable through the API, but it broke least privilege, and Supabase's default privileges would re-grant it on every new table.
• Drift between production and source control. A SECURITY DEFINER function existed in production but not in the migrations. It had been created by a dashboard setting and was executable by anonymous users.
• One layer guarding admin access. Only a single RLS policy stopped a client from changing their own role to admin.
The fix
• Mapped exactly which operations the app performs, revoked everything from anon and authenticated, and re-granted only those, including column-level INSERT/UPDATE. 28 privilege rows → 11.
• Revoked execute on the undocumented function while keeping its feature working.
• Added a trigger that blocks role changes from client sessions. Escalation is now blocked by privileges, RLS and a trigger.
• Wrote rollback SQL before touching production, applied the migration in a single transaction, then re-verified with read-only queries and tested every user flow as both client and admin.
Then I attacked it
I rebuilt the database locally from the same migrations, created test accounts for two client companies, and attacked the REST, RPC and Storage APIs directly, using only the public anon key and normal logins. 124 of 124 checks passed: no cross-company reads, writes or file downloads, and no unpaid files, even by exact ID or file path. After every write attempt, the script re-read the data to prove nothing had changed.
Later I added column-level checks. Hiding a column in the UI doesn't protect it: anyone calling the API directly can read every column their privileges and RLS allow. So I listed exactly which columns each role should be able to read, and checked them two ways: the column privileges in the database, and what an actual select * returns. To prove the check works, I removed one column from the expected list on purpose, and the script caught it. With these added, 145 of 145 checks pass. One column, the Stripe session ID, stays readable to the client who owns the project. I accepted it as low risk, because hiding it would also hide it from the admin.
Then I reviewed the server code
RLS doesn't protect code that bypasses it. Three places in the app use Supabase's service_role key, which skips RLS entirely, so I reviewed the server code separately and fixed three weaker spots:
• Login links, Stripe Checkout return URLs and email links were built from request headers. They now come from a fixed site URL in the config.
• The service_role client was guarded only by a code comment. It can now only be obtained through a function that checks for admin first, or a webhook-only function that a lint rule blocks everywhere else.
• File downloads relied on RLS alone. The route now also checks that the file belongs to the signed-in client, and returns 404 if it doesn't.
All three fixes are deployed and checked in production.
The access checks have since grown to 422 as the portal added features, and they now run on every pull request in GitHub Actions as a required check on main. A test PR that made every company's projects visible failed 12 of 422 checks and could not be merged.

A deliberately broken PR: 12 of 422 checks failed and the merge was blocked.
What this shows about AI-built apps
AI tools write working RLS quickly, but they don't check what the platform adds by default or what's been changed through the dashboard. In the same week, an AI agent tried to add 12 exclusions to a package-manager safety setting just to force an upgrade. I rolled it back. I use AI to build fast, and a human review of the live system to make it safe.
Stack: Supabase (Postgres, RLS, Storage) · Next.js · Stripe · SQL · Claude Code
0 likes · 9 views · Nov 10, 2026
An AI-built app passed every RLS test, but a live audit found leftover privileges. Fixed 28 → 11, then 145/145 direct API and column-read checks passed.