Airtight RLS, Wide-Open RPC: Minting Unlimited Credits Using Only a Public Supabase Anon Key
In a 58-minute autonomous Breachger Assault run against a metered SaaS API, every database table had properly configured Row-Level Security – yet two unscoped Postgres RPC functions let any unauthenticated caller read balances and mint unlimited credits using only the browser's public anon key.
Every table in the database had Row-Level Security enabled. Every table returned zero rows to anonymous callers. And in under an hour, an unauthenticated POST request carrying only the browser's public API key could read any customer's balance and mint unlimited paid credits onto any account.
During a 58-minute autonomous Breachger Assault engagement against a metered SaaS API platform, our platform evaluated authentication, cross-account isolation, billing logic, and domain mail controls. Nine unverified leads were dropped automatically by Breachger's deterministic proof engine. Four findings independently reproduced against the live target – reducing to two root defects: a pair of unscoped PostgreSQL RPC functions left executable by Supabase's anon role, and an unenforced SPF/DMARC perimeter posture. Client identity, domains, project references, user UUIDs, mail providers, and exact balance values have been anonymized throughout this write-up.
Summary
- Why RLS did not save the ledger: Row-Level Security governs table queries. It does not stop a
SECURITY DEFINERPostgreSQL function from running with owner privileges whenEXECUTEremains granted toPUBLIC– which in Supabase includes the browser-visibleanonrole.- Unauthenticated credit minting (
F-1): SendingPOST /rest/v1/rpc/add_creditswith only the publicanonapikeyheader – noAuthorizationbearer token and no user session – returned the target account's live credit balance onp_amount: 0and minted arbitrary credits on non-zero amounts.- Systemic billing pattern (
F-2): The write-side sibling function (deduct_credits) carried the identical missing authorization check, proving the defect was shared across the credit helpers rather than an isolated mistake.- Proof over volume: 4 findings were independently reproduced (1 High, 3 Medium); 9 speculative candidates were excluded rather than padded into the report.
- The fix: Revoke
EXECUTEon billing RPCs fromPUBLIC,anon, andauthenticated, grant exclusively toservice_role(or bind user-facing RPCs strictly toauth.uid()), tighten SPF to-all, and publish DMARC.
What 58 minutes of proof-gated testing produced
After DNS ownership verification, Breachger Assault ran a non-destructive external assessment across 3 in-scope hosts. Every finding below was independently replayed against the live target by an isolated verification engine before entering the signed report.
| ID | Finding | Severity | CVSS v3.1 | CWE |
|---|---|---|---|---|
F-1 | Unauthenticated add_credits RPC returns any user's credit balance and mints credits with the public anon key | High | 8.2 | CWE-639 |
F-2 | Unauthenticated deduct_credits RPC discloses any user's credit balance through a second unscoped function | Medium | 5.3 | CWE-639 |
F-3 | Root domain SPF record ends in ?all, leaving unlisted senders unrejected | Medium | 5.3 | CWE-290 |
F-4 | No DMARC record published for the root domain | Medium | 5.3 | CWE-345 |
Equally important is what held up – and what was left out:
- Controls that held up. No leaked private keys or hardcoded secrets appeared in client bundles. No database, cache, or internal management ports were exposed to the internet. TLS/HSTS was clean, no dangling DNS records existed, and direct anonymous queries against application, billing, cache, and usage tables all returned empty sets.
- Unverified noise excluded. Nine possible issues were automatically omitted. Framework CVE version banners and generic response-header observations remained inventory rather than findings. One cross-account API-key update lead hit upstream egress throttling before a clean two-account session proof could complete; instead of claiming an unverified IDOR, the report explicitly recorded it as an incomplete coverage item to retest with a stored session.
The Supabase and PostgreSQL trap: RLS on tables, PUBLIC on functions
This failure mode shows up repeatedly across modern SaaS and AI applications built on Supabase. It happens to careful teams who genuinely do enable Row-Level Security.
Supabase's anon key is designed to live inside client-side browser code; it is a publishable routing credential, not a secret. Developers lock down every table with RLS policies, verify that anonymous SELECT queries return [], and consider the database perimeter closed.
Then metered billing gets added. Updating a credit ledger atomically from a webhook or server route is cleanly implemented as a PostgreSQL stored function – add_credits(p_user_id uuid, p_amount integer) and deduct_credits(p_user_id uuid, p_amount integer) – typically declared SECURITY DEFINER so it can update the protected ledger table without opening direct table writes.
Here is the trap baked into PostgreSQL itself:
- Default function grants are wide open. Unless a migration explicitly revokes privileges, PostgreSQL grants
EXECUTEon every newly created function toPUBLIC. anoninheritsPUBLIC. In Supabase, bothanonandauthenticatedinheritPUBLIC, and PostgREST automatically mounts every function in the exposed schema at/rest/v1/rpc/<function_name>.SECURITY DEFINERbypasses table RLS. When an anonymous caller invokes that function over HTTP using the browser'sanonkey, the function executes with its owner's privileges – sailing straight past every RLS policy on the underlying tables.
If the function accepts p_user_id as an argument instead of deriving the caller from auth.uid(), there is no authentication boundary left.
Reproducing the billing bypass with zero session
Verification required no user account, no session cookie, and no Authorization header – only the project's public browser anon key and a target user UUID (host, key, UUID, and balance anonymized):
curl -s -X POST \
-H 'apikey: <PUBLIC_ANON_KEY>' \
-H 'Content-Type: application/json' \
-d '{"p_user_id":"00000000-0000-4000-8000-000000000042","p_amount":0}' \
https://project-ref.supabase.example/rest/v1/rpc/add_creditsThe PostgREST endpoint returned HTTP/2 200 (application/json) with the bare integer balance of that user:
420Passing p_amount: 0 proved the cross-account read without mutating customer state, while testing on a controlled account confirmed that a non-zero p_amount minted credits directly onto the target balance. Repeating the same unauthenticated call against the write-side sibling (F-2) produced the identical result:
curl -s -X POST \
-H 'apikey: <PUBLIC_ANON_KEY>' \
-H 'Content-Type: application/json' \
-d '{"p_user_id":"00000000-0000-4000-8000-000000000042","p_amount":0}' \
https://project-ref.supabase.example/rest/v1/rpc/deduct_creditsPatching add_credits alone would have left deduct_credits open on the same public interface.
Honest blast-radius scoping
Two scoping facts mattered when pricing the severity:
- Targeted by UUID, not enumerable. Because user identifiers were random
UUIDv4values rather than sequential integers, an external attacker could not walk the entire user base sequentially without first obtaining a user UUID. - No PII returned. Every proven database response returned only a bare integer credit balance – zero e-mail addresses, phone numbers, password hashes, session tokens, or payment details appeared in any response.
That is why F-1 scored 8.2 High (AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:H/A:N) rather than being inflated to a 10.0 Critical data breach. Even without PII leakage, unauthenticated credit minting breaks the core revenue gate of a metered API: any caller with their own or another user's UUID can grant themselves unlimited paid calls without paying a cent, making this an immediate 0–7 day fix.
The perimeter pair: neutral SPF and missing DMARC
Alongside the RPC findings, DNS inspection surfaced two paired e-mail authentication gaps on the root domain (F-3 and F-4):
SPF (TXT target.example): v=spf1 include:_spf.mail-provider.example ?all
DMARC (TXT _dmarc.target.example): (none published)The ?all qualifier tells receiving mail servers to treat unlisted sending hosts as neutral – neither pass nor fail. Combined with the complete absence of a _dmarc TXT record, receiving systems get no alignment enforcement policy to quarantine or reject forged @target.example mail, and the company receives no rua aggregate reports revealing spoofing attempts. For a B2B SaaS platform sending invoices, API alerts, and account notifications, closing both DNS records takes minutes and removes an easy pretexting vector.
Closing both classes permanently
1. Audit every function currently executable by anon
Before patching individual endpoints, run this query against your PostgreSQL database to see every function in public that the anon role can currently invoke over PostgREST:
select n.nspname as schema_name,
p.proname as function_name,
pg_get_function_identity_arguments(p.oid) as args,
p.prosecdef as security_definer
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where n.nspname = 'public'
and has_function_privilege('anon', p.oid, 'execute')
order by p.prosecdef desc, p.proname;Any billing, entitlement, credit, quota, or administrative helper showing security_definer = true in that result set is an immediate red flag.
2. Revoke default EXECUTE grants in a single migration
Billing functions invoked only by backend routes or payment webhooks should never be callable by PUBLIC, anon, or authenticated:
revoke all on function public.add_credits(uuid, integer) from public, anon, authenticated;
grant execute on function public.add_credits(uuid, integer) to service_role;
revoke all on function public.deduct_credits(uuid, integer) from public, anon, authenticated;
grant execute on function public.deduct_credits(uuid, integer) to service_role;If a user-facing RPC genuinely must be called by signed-in clients (authenticated), do not accept p_user_id as an authority parameter – resolve the acting user exclusively from auth.uid(), enforce server-side bounds on amounts, and still revoke EXECUTE from PUBLIC and anon.
To keep future migrations from reintroducing the same trap, strip default function privileges at the schema level (alter default privileges in schema public revoke execute on functions from public, anon;) and explicitly grant only what each new RPC requires.
3. Priority remediation and verification matrix
| Ref | Timeframe | Action | Deterministic retest check |
|---|---|---|---|
F-1 | Immediate (0–7 days) | Revoke EXECUTE on add_credits from PUBLIC, anon, and authenticated; grant only to service_role (or bind strictly to auth.uid()) | Replay the exact unauthenticated POST with the public anon key – must return 401/403 permission denied, never a balance |
F-2 | Immediate (same migration) | Apply the identical REVOKE/GRANT lock to deduct_credits and add a CI regression test calling both RPCs with only the anon key | Replay the unauthenticated deduct_credits POST – must fail closed identically |
F-3 | Short term (7–30 days) | Inventory all legitimate sending providers (keeping under 10 DNS lookups) and change ?all to -all | dig +short TXT target.example terminates in -all |
F-4 | Short term (7–30 days) | Publish _dmarc starting at p=quarantine; adkim=s; aspf=s with a monitored rua inbox, then move to p=reject; sp=reject once aligned | dig +short TXT _dmarc.target.example returns active v=DMARC1 policy |
Takeaways for teams building on Supabase and PostgreSQL
- RLS only guards tables – check your RPC grants. PostgreSQL grants
EXECUTEon new functions toPUBLICby default, which includes Supabase's browser-visibleanonrole. - Treat
SECURITY DEFINERfunctions like privileged API routes. PairingSECURITY DEFINERwith defaultPUBLICexecute privileges creates a direct bypass around table RLS. - Never trust
p_user_idfrom client-callable functions. Derive the subject fromauth.uid()inside the function, or restrict server-only billing helpers exclusively toservice_role. - Audit helper functions in pairs. Credit, quota, and entitlement RPCs almost always ship in sibling sets (
add_*/deduct_*,grant_*/revoke_*). Fixing one without auditing the schema leaves the pattern live. - Demand proof-gated reporting. Four independently reproduced findings with explicit coverage boundaries beat forty speculative alerts every time.
This entire engagement – from DNS authorisation through autonomous discovery, deterministic replay verification, and a cryptographically sealed report – completed in 58 minutes on Breachger Assault. If your team builds on Supabase, PostgreSQL RPCs, or metered APIs and wants to verify your real authorization boundaries before your next launch, you can start with a Single-App Proven Audit – one application and its APIs tested in 24 hours, with 100% of the $2,500 fee credited toward Continuous Assault within 30 days.