Short answer
A paywall that only exists in the frontend is a suggestion: anyone can call your Supabase API directly, skip the check and even write their own plan or credit balance. Enforce it in the database instead. Keep plan, credits and payment status in columns or tables users can’t write, let only your server (payment webhook, Edge Function) change them, gate premium rows with RLS policies that check an active subscription, and spend credits in a function only the server may call.
Ask an AI about this page:ChatGPTClaude
On this page
Key takeaways
- A paywall in React code hides a button. It doesn’t stop anyone from calling your API with the public key.
- An owner-only update policy still lets the owner change every column of their row, including
planandcredits. - Plan, credits, prices and payment status must be written only by your server: payment webhook, Edge Function, database function.
- Premium rows need an RLS policy that checks an active subscription, not just a hidden page.
- Credits are spent in one conditional update on the server, never by the browser.
Why a frontend paywall doesn’t hold
A typical AI-built SaaS checks the plan like this:
if (profile.plan !== "pro") return <UpgradeBanner />;
const { data } = await supabase.from("premium_reports").select("*");It looks like a paywall, and for honest users it works. But the check runs in the visitor’s browser, on code they can read and change. Your Supabase URL and public key are in the same bundle, so anyone can skip the page and send select * from premium_reports themselves. MITRE calls this client-side enforcement of server-side security (CWE-602 (opens in a new tab)), and OWASP lists bypassing checks by tampering with requests under broken access control, the first entry of its Top 10. Its advice is that access control only works in trusted server-side code.
In a Supabase app, “server-side” mostly means the database itself: row level security policies, table and column grants, and functions. Here are the four places paywalls leak, and the fix for each.
Leak 1: users can upgrade themselves
Plan and credits often live on the user’s profile, next to the display name, with the usual owner policies:
create table public.profiles (
id uuid primary key references auth.users (id) on delete cascade,
display_name text,
plan text not null default 'free',
credits integer not null default 10
);
alter table public.profiles enable row level security;
create policy "Users can read their profile"
on public.profiles for select
to authenticated
using ((select auth.uid()) = id);
create policy "Users can update their profile"
on public.profiles for update
to authenticated
using ((select auth.uid()) = id)
with check ((select auth.uid()) = id);The policies are correct in the usual sense: nobody can touch someone else’s profile. But a policy decides which rows, not which columns. The owner can run this, from the browser console or any HTTP client:
update public.profiles
set plan = 'pro', credits = 999999
where id = auth.uid()
returning plan, credits;In our test it returned pro and 999999. The quickest fix keeps the table and restricts which columns the browser may update:
revoke update on table public.profiles from authenticated;
grant update (display_name) on table public.profiles to authenticated;After this, the same update fails with permission denied for table profiles, changing display_name still works, and your server with the secret key can still set the plan. Supabase calls column privileges an advanced feature and recommends RLS together with a dedicated table for most cases. That cleaner long-term design is the next one: billing data in its own table that users can only read.
Leak 2: premium data anyone can read
If premium content sits in a table with using (true) or a policy for every signed-in user, the paywall is decorative. Gate the rows themselves. Keep the subscription in a table only your server writes, and check it with a helper:
create table public.subscriptions (
user_id uuid primary key references auth.users (id) on delete cascade,
plan text not null default 'free',
status text not null default 'inactive',
current_period_end timestamptz
);
alter table public.subscriptions enable row level security;
revoke all on table public.subscriptions from anon, authenticated;
grant select on table public.subscriptions to authenticated;
create policy "Users can read their subscription"
on public.subscriptions for select
to authenticated
using ((select auth.uid()) = user_id);
create schema if not exists private;
create or replace function private.has_active_plan(_plan text)
returns boolean
language sql
stable
security definer
set search_path = ''
as $$
select exists (
select 1 from public.subscriptions s
where s.user_id = (select auth.uid())
and s.plan = _plan
and s.status = 'active'
and s.current_period_end > now()
);
$$;
create policy "Pro subscribers can read premium reports"
on public.premium_reports for select
to authenticated
using ((select private.has_active_plan('pro')));In our test, a free user got zero premium rows, an attempt to insert their own “active” subscription failed with permission denied, a user whose subscription the server had recorded saw the report, and the same user lost access once current_period_end passed. The current_period_end check means a missed cancellation webhook doesn’t grant access forever.
Who writes subscriptions? Your payment webhook, running on the server with the secret key, after it has verified the provider’s signature. Our Stripe security guide covers that part; the fix for a subscription check that only lives in the frontend shows the full move from UI check to policy.
Leak 3: credits spent by the browser
Credit systems built by AI tools often look like this: the browser calls the AI feature, then runs update profiles set credits = credits - 1. If the browser can subtract, it can also not subtract, or add. Move the balance to a table users can only read, and spend credits in a function that only the server may call:
create table public.credit_balances (
user_id uuid primary key references auth.users (id) on delete cascade,
balance integer not null default 0 check (balance >= 0)
);
alter table public.credit_balances enable row level security;
revoke all on table public.credit_balances from anon, authenticated;
grant select on table public.credit_balances to authenticated;
create policy "Users can see their balance"
on public.credit_balances for select
to authenticated
using ((select auth.uid()) = user_id);
create or replace function public.spend_credits(_user_id uuid, _cost integer)
returns integer
language plpgsql
security definer
set search_path = ''
as $$
declare
remaining integer;
begin
if _cost <= 0 then
raise exception 'cost must be positive';
end if;
update public.credit_balances
set balance = balance - _cost
where user_id = _user_id and balance >= _cost
returning balance into remaining;
if remaining is null then
raise exception 'not enough credits';
end if;
return remaining;
end;
$$;
revoke execute on function public.spend_credits(uuid, integer) from public, anon, authenticated;
grant execute on function public.spend_credits(uuid, integer) to service_role;The important lines:
- One conditional update (
where … balance >= _cost) checks and deducts in a single statement, instead of reading the balance first and writing it later, which is how two parallel requests end up spending the same last credit. - A cost of zero or less is rejected. Without the check, a negative cost would add credits.
- Execute is revoked from
anonandauthenticatedtoo, not only frompublic. Supabase grants new functions inpublicto the API roles by default, so revoking frompublicalone is not enough. In our test, a signed-in user calling it gotpermission denied for function spend_credits.
Your Edge Function reads the user from the verified session, calls spend_credits with the secret key, and only then does the paid work. The fix for credits granted on the client walks through that function.
Leak 4: inserts that set trusted columns
The last pattern is the subtlest, because it involves no update at all. An orders table lets customers place orders with this policy:
create policy "Customers can place orders"
on public.orders for insert
to authenticated
with check (true);The frontend always sends payment_status: 'pending' and the right total. Someone calling the API directly can send 'paid', a total of one cent and another customer’s user_id, and the policy accepts it. In our test, exactly that row was stored. Anything downstream that trusts payment_status, such as a fulfilment job, now ships unpaid orders. This is one of the deliberately broken test apps we use to measure ShipRely’s audit engine.
Pin what the client may set in the policy:
drop policy "Customers can place orders" on public.orders;
create policy "Customers can place their own pending orders"
on public.orders for insert
to authenticated
with check (
(select auth.uid()) = user_id
and payment_status = 'pending'
);Our test also showed what this doesn’t fix: the client still chooses total_cents. Compute prices on the server, in a database function or trigger that reads the product price, or in the Edge Function that creates the checkout session. Never trust a price the browser sends. The using (true) fix covers open policies in general.
A fix prompt for your AI builder
Review how this app decides who has paid. List every place that reads or writes a plan, subscription status, credit balance, price, total or payment status, in frontend code, Edge Functions, RLS policies and database functions.
Then fix it with a migration:
- Users must not be able to update plan, credits or payment fields. Either revoke update on those tables from authenticated and grant update only on harmless columns, or move billing data to tables where authenticated has select only.
- Premium tables get an RLS select policy that calls a security definer helper with set search_path = ‘’ checking an active subscription (status active and current_period_end > now()).
- Credits are spent only in a function that does one conditional update (balance >= cost), rejects a cost of zero or less, and is executable only by service_role (revoke from public, anon and authenticated).
- Insert policies must pin user_id to (select auth.uid()) and any status column to its initial value; prices and totals are computed on the server.
Only the payment webhook and server code with the secret key may write subscriptions, credits and payment status. Show me the migration and the list before applying anything.
Frequently asked questions
Can users really change their own plan in a Supabase app?
If the plan is a column in a table they may update, yes. An update policy that checks only (select auth.uid()) = id lets the owner change every column of their row, including plan and credits. Restrict updates to harmless columns with column grants, or move billing data to a table users can only read.
Where should the subscription status come from?
From your payment provider, through a server: typically a webhook that verifies the provider’s signature and writes the status with the secret key. The browser may read the status but never write it.
Is it enough to check the plan in an Edge Function?
It is enough for what that function does, if it reads the plan from the database rather than from the request. But premium data that sits in a table the browser can query directly needs an RLS policy too, or users simply skip the function.
How do I stop users from spending credits they don’t have?
Don’t let the browser touch the balance. Deduct credits on the server, in the same step that does the paid work, with a single conditional update such as balance = balance - cost where balance >= cost, and make the function callable only by the service role.
Sources
- 1.Row Level Security — Supabase Docs
- 2.Column Level Security — Supabase Docs
- 3.CWE-602: Client-Side Enforcement of Server-Side Security — MITRE
- 4.A01 Broken Access Control — OWASP Top 10