Case study: anyone could mark an order as paid (Supabase RPC)
#supabase#postgres#security-definer#auth#case-study
A small order site built on Supabase had three bugs that each worked with nothing more than the public anon key. A lookup function returned the full order, including the customer's name and phone number, for any order number, and the numbers were sequential. The function that marks an order as paid could be called by anyone, not only by the payment webhook. And the signup trigger copied the user's role from signup metadata, so signUp({ options: { data: { role: 'admin' } } }) created an admin. The developer confirmed all three and fixed them in production within four days.
We reported this privately on November 18, 2026. The developer fixed it, went further than the report, and agreed to this write-up on the condition that we don't name the project or the client. We've renamed the functions and columns below. The logic is unchanged.
What the app does
It's an order site for T-shirts for a one-off event. A customer fills in a form with their name, phone number and size, gets an order number, and pays by bank transfer or card. They can come back later, type in the order number, and see where their order is. Organisers sign in to a small admin area to approve payments and mark orders as collected.
The card payments go through a payment provider. When a payment succeeds, the provider calls a serverless webhook on the site, the webhook checks the payment with the provider's API, and only then marks the order as paid. That part was done carefully. The problem was one layer below it.
How did the bugs work?
1. The order lookup returned everything
The "check your order" page called a database function through /rest/v1/rpc:
create or replace function public.get_order_by_number(p_number text)
returns setof public.orders
language sql security definer set search_path = public as $$
select * from public.orders where order_number = p_number;
$$;
revoke all on function public.get_order_by_number(text) from public;
grant execute on function public.get_order_by_number(text) to anon, authenticated;
The orders table had RLS on and no public read policy, which is right. The function runs as SECURITY DEFINER, so it reads the table as its owner and skips RLS, and it returned the whole row: full name, phone number, which local group the customer belongs to, payment details. That's fine if only the customer knows the order number.
They didn't have to. Order numbers came from a sequence, a fixed prefix plus four digits, counting up from 0001. Anyone could call the function with 0001, 0002, 0003 and so on and collect the name and phone number of every customer.
2. Anyone could mark an order as paid
The webhook marked orders as paid through a second function:
create or replace function public.mark_order_paid(p_number text, p_transaction_id text, p_amount numeric, p_method text)
returns setof public.orders
language sql security definer set search_path = public as $$
update public.orders
set status = 'payment_approved', transaction_id = p_transaction_id,
amount_paid = p_amount, payment_method = p_method
where order_number = p_number and status = 'awaiting_payment'
returning *;
$$;
grant execute on function public.mark_order_paid(text, text, numeric, text) to anon, authenticated;
The webhook only called it after checking the payment with the provider. But the grant meant the browser could call it too. Anyone could place an order, skip paying, and call mark_order_paid with their own order number, any transaction id and card as the method. The order showed up in the admin area as paid by card.
The webhook's checks only protect the path that goes through the webhook. A function that the API roles can execute is a second, public path to the same update.
3. Signing up as admin
New accounts got a profile row from a trigger:
create or replace function public.handle_new_user()
returns trigger language plpgsql security definer set search_path = public as $$
begin
insert into public.profiles (id, name, role)
values (new.id,
coalesce(new.raw_user_meta_data->>'name', new.email),
coalesce(new.raw_user_meta_data->>'role', 'user'));
return new;
end; $$;
raw_user_meta_data is whatever the client passes in supabase.auth.signUp({ options: { data } }). The site never sends a role, so the default 'user' applied to real users. But anyone could call signUp themselves with data: { role: 'admin' }, and the admin policies on orders checked profiles.role. That gave them the admin area: every order and every payment receipt.
What did it expose?
- Every customer's name and phone number, by counting through order numbers.
- Free orders. Any order could be marked as paid by card without a payment.
- Full admin access to orders and uploaded payment receipts, for anyone who signed up through the API with a role in the metadata.
We tested none of this against the live project. Each finding came from reading the SQL in the repository and tracing how the site's code calls it, and the developer confirmed each one.
How was it fixed?
The fix shipped as one migration, four days after the report:
- The trigger no longer reads the role. New profiles get the column default, and promoting someone is a manual step in the SQL Editor. The insert and update policies on
profileswere dropped, andinsert, update, deleterevoked fromanonandauthenticated, so nobody can write a role through the API either. Public sign-ups were turned off, because only organisers need accounts. - The lookup returns only what the tracking page shows: order number, status, first name, shirt model, size, quantity, amount, payment method and order date. No phone number, no full name, no group.
- Marking an order as paid is server-only:
revoke all on function public.mark_order_paid(text, text, numeric, text)
from public, anon, authenticated;
grant execute on function public.mark_order_paid(text, text, numeric, text) to service_role;
The webhook now calls it with the service role key, which stays on the server. 4. An unused receipt-upload function was closed too. The site had stopped using it, but it was still executable and let anyone replace the receipt on any order. 5. Receipts in Storage are limited to admin and moderator accounts. Before, any signed-in account could read them.
The migration also ends with a read-only check for signs that the bugs were used: admin and moderator accounts nobody recognises, and card-paid orders whose transaction id doesn't appear in the payment provider's statement. That's the step most fixes skip. Closing a hole doesn't tell you whether someone already used it.
Does my app have the same problem?
Three checks, each in the SQL Editor.
SECURITY DEFINER functions the API roles can call. For each one, ask: if a stranger calls this with any arguments, what happens?
select p.oid::regprocedure as function,
has_function_privilege('anon', p.oid, 'execute') as anon_can_run,
has_function_privilege('authenticated', p.oid, 'execute') as authenticated_can_run
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where n.nspname = 'public' and p.prosecdef;
Functions that read user metadata, usually the signup trigger. A role, plan or tenant read from raw_user_meta_data is chosen by the user:
select p.oid::regprocedure as function
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where n.nspname = 'public'
and p.prosrc ilike '%raw_user_meta_data%';
Lookups by a guessable value. If a public function or page finds a record by an order number, invoice number or short code, check whether that value counts up. If it does, either return only what's safe to show a stranger, or give each order a random token (gen_random_uuid() or a long random string) and look it up by that.
Does GuardLayer catch this?
Two of the three. The code scan flags mark_order_paid as a SECURITY DEFINER function that anyone can call and that never checks the caller (supabase/security-definer-no-caller-check), and the signup trigger for copying role from user metadata (supabase/signup-trigger-trusts-user-metadata), each with the file, the line and the fix. It doesn't flag the order lookup: returning an order for its number is what that function is for, and whether the number can be guessed depends on how it's generated, which the scanner doesn't connect to the lookup.
FAQ
The webhook checked the payment. Why wasn't that enough?
Because the webhook was only one way to reach the update. Any function the anon role can execute is reachable directly at /rest/v1/rpc/<name> with the public key. Checks belong inside the function, or the function shouldn't be executable by the API roles at all.
Is revoke all ... from public enough to close a function?
Not on Supabase. anon and authenticated get their own execute grant on new functions in public, so revoke from public, anon, authenticated and grant back only to the role that needs it. The Kagelin case study covers this in detail.
Is user metadata ever safe to use in a trigger?
For things the user is allowed to choose, like a display name. Never for a role, plan, tenant or anything an RLS policy checks. Put those in app_metadata or a table that only your server writes. See app_metadata vs user_metadata.
Should public lookups use UUIDs instead of order numbers? Look the order up by something only the customer has, such as a random token in their confirmation link, and keep the readable order number for display. Even then, return only the fields the page needs.
Check your Supabase Auth settings and tables, free
Connect your Supabase project read-only. GuardLayer checks your Auth settings (leaked passwords, email confirmation, secure email change, token rotation, CAPTCHA, MFA) and your RLS, policies and grants, and says how to fix each problem. It never reads your rows and can't change anything.
Check my Supabase projectPrefer to check code instead? Scan your repo, no signup.