RLS is enabled on every table. Tests pass. Then a payment callback writes a row to the wrong tenant: the handler runs as service_role, so RLS never sees it, and the callback carries no tenant, so the code guesses.
1. Why this matters now
- Supabase is changing
publictables so new ones are no longer exposed to the Data API automatically. Its April 2026 changelog schedules this for all existing projects on 30 Oct 2026 (I did not confirm that it took effect). Existing tables keep their current grants, so for those RLS is what limits rows (changelog). - Large-scale exposure of Supabase apps has been reported (UpGuard report).
- This post is about the part those fixes don't touch: server-side paths.
2. The setup
(repo: https://github.com/cal-me-j/tenant-isolation-lab)
Two tenants, members, invoices, payments. M-Pesa callbacks are simulated: the tests build Daraja-shaped payloads inline (no real Safaricom calls). Local Supabase CLI 3.2.0, Postgres 17.12 (captured 2026-11-24), and GitHub Actions on ubuntu-latest.
3. Three leaks that survive "RLS is on"
- Tenant read from
user_metadata. Users can edit it; a policy trusting it lets them pick their tenant (community write-up). Result at tagvulnerable: test fails (have: 1, want: 0). - Writes that don't pin the tenant. An INSERT policy that only checks authorship, and an ownership-based UPDATE policy with no WITH CHECK. In my run a filtered UPDATE was blocked but an unfiltered one moved a row to another tenant (cause of the difference not established, U4; reproduced in SQL only, API reachability unverified, U6). Result at tag
vulnerable: insert testhave: 1, want: 0; unfiltered-update testhave: bbbbbbbb-…01, want: aaaaaaaa-…01. - A view without security_invoker. It runs with owner rights and bypasses RLS. Result at tag
vulnerable: test fails (have: 1, want: 0).
4. The M-Pesa-specific trap
- A callback is not a signed-in user. Which tenant does it belong to?
- Bad (what my vulnerable handler did): match payer phone + amount against open invoices across all tenants. The STK Push callback carries no tenant or invoice reference (per community Daraja docs, one and another; that nothing else is in there is my inference), so the handler guessed. Caveat: community and press sources report Safaricom has masked the callback phone number since March 2026 (community package, press; not Safaricom itself), so a phone-keyed lookup like mine would likely break on current Daraja. The point stands: anything inferred from payload fields is a guess. (C2B callbacks are different: they carry a customer-typed BillRefNumber (SDK example), a separate trap this repo does not model.)
- Better: resolve the tenant from a record you stored at initiation (e.g. the checkout request ID you created), and reject callbacks with no matching record.
- Result: at tag
vulnerable, 03 test 3 fails (have: 1, want: 0): a payment for tenant B was recorded against tenant A's older invoice. At HEAD, 03 passes 12/12, including replay, unknown checkout, amount mismatch and settled-checkout reuse.
5. The fix
- Tenant from a
membershipstable, notuser_metadata. -
WITH CHECKon writes. - Views with
security_invoker. - Handler logic is a SECURITY DEFINER function owned by a non-bypass role (verified:
03_callback.test.sqltest 11). Its grants are narrow and column-level;04_grants.test.sqlchecks that it cannot readmembershipsortenants, update invoiceamount_centsortenant_id, or delete invoices (listed tables and columns only).FORCE ROW LEVEL SECURITYis applied but has no effect on BYPASSRLS roles, which includepostgresandservice_rolelocally (role query, 2026-11-24) (Supabase community thread). - Tenant resolved from a stored checkout record; an invoice settles once (replay-safe).
- At tag
vulnerable: 5 failing assertions (02: 4, 03: 1). At HEAD: 0 of 28 fail. 02 is unchanged; 03 was extended and 04 added after the tag, so the suites are not identical.
6. Run it yourself
Needs Docker and pnpm.
# Current state (passes) pnpm install --frozen-lockfile pnpm supabase db reset pnpm supabase test db # Vulnerable state (fails by design): swaps only supabase/ so this README stays visible git restore --source=vulnerable --worktree -- supabase pnpm supabase db reset pnpm supabase test db git restore --source=HEAD --worktree -- supabase # back to current git status --short 7. What this does not prove
- Simulated callbacks, not live Daraja.
- Covers the patterns in this repo only; not a full audit.
- Callbacks are unsigned, per a community library's docs. My DB ignores unknown checkouts and amount mismatches, but a forged success with a valid CheckoutRequestID and amount is not blocked in SQL; a real wrapper must confirm via the STK Query API.
- The Edge Function wrapper is not built. A service_role key still bypasses RLS; only the handler logic is least-privilege.
- Payload field names come from community documentation; I could not retrieve Safaricom's own docs (community SDK docs only: PHP, Python). My vulnerable handler's phone match is illustrative, not a model of current Daraja behaviour (see §4).
I review tenant isolation for payment-taking Supabase apps (fixed price, written report). Details: https://cal-me-j.github.io/services/tenant-isolation-review/?utm_source=devto&utm_medium=article&utm_campaign=post01