Anh Tran dev notes

Production bugs from mobile and backend work, written up with the fix and how it was checked.

Note 02 / Supabase / Security / Postgres

Supabase RPC rewards that clients can forge: dedup keys, premium flags and a function that listed no one as Pro

How check-in and quest reward functions let a signed-in user mint unlimited in-app currency, why the server could not be trusted to spot paying users, and the rule that fixed both: the server builds every key and reads every entitlement itself.

By
Trần Thế Anh, Software Engineer
Published
Work done
7 October 2026, Yuuki (Supabase Postgres)
Stack
Supabase Postgres, SECURITY DEFINER functions, row-level security
Reading
6 min, 1,349 words

In a Supabase app the client can call any Postgres function it has EXECUTE on, using the anon or authenticated key that ships inside the app. If a function adds currency to a balance and takes its own safety checks as parameters, those checks are advice. This note covers four holes of that kind found in the reward functions of the Yuuki app on 7 October 2026, the rule that closed them, and the awkward cleanup of keeping old app versions working while the server stopped trusting them.

The setup

The apps give in-app currency (the code calls it hearts) for a daily check-in and for finishing daily quests. Premium subscribers earn double. Each grant has to happen once per day per user, so the client calls a function with a dedup key, the function checks whether that key was already used, and inserts a ledger row if not.

Two functions did this, grant_checkin_reward and claim_quest_reward. A trigger on a user_streaks table added a third path. All three were readable with pg_get_functiondef on production, which is how the problems were confirmed: by reading what the database actually ran, not what the repository said it should.

Hole 1: the key came from the client

grant_checkin_reward(p_dedup_key text, p_premium boolean) treated the key as a unique token. A legitimate client sent checkin:<user id>:<local date>. Nothing forced that shape. Call the function again with checkin:<user id>:anything-else and each call is a “new” check-in with a fresh payout, unlimited.

Hole 2: the day was free text

claim_quest_reward(p_quest, p_day, p_premium) built its key from p_day, a text parameter. Every invented day string was a new key, so a finished quest could be claimed again for each day you could type. Real dates are not required to look like dates when the parameter is text.

Hole 3: the client declared itself premium

Both functions took p_premium boolean and doubled the reward when it was true. The app computed that value from RevenueCat on the device. A caller with the public key can pass true without owning anything.

Hole 4 (the odd one): paying users never matched the server’s own premium check

Why had the client been trusted for premium at all? Because the server’s own function, is_user_premium(), could not be relied on. It required the string pro to be in the user’s entitlement list. The webhook (see the first note in this series) wrote RevenueCat’s internal entitlement id, a generated string, not the name pro. When the function body was read on production on 7 October 2026, the active paid rows carried the internal id and none carried pro. So the server’s check answered “not premium” for people who were paying, and the client-supplied flag existed to work around it.

That detail is why a security fix here could not be done by deleting the parameter alone. Deleting p_premium while is_user_premium() still returned false for payers would have taken double rewards away from the people who bought them.

The rule

A function that adds value must not accept a dedup key, a date or an entitlement flag from the client. The server derives the key from auth.uid() plus data it controls, and reads the entitlement from a table only the server writes.

Everything else was an application of that sentence.

What changed

Premium comes from the subscriptions table. is_user_premium(uid) became a thin wrapper over a function that asks “does this user have a currently valid row in subscriptions”, which only the webhook, running with the service role, writes. It no longer cares what the entitlement is called, so RevenueCat’s generated id and a Stripe-style pro both count.

create or replace function public.is_user_premium(p_uid uuid)
returns boolean
language sql stable security definer
set search_path = public
as $$ select public._has_active_subscription(p_uid) $$;

revoke all on function public.is_user_premium(uuid) from public, anon, authenticated;

The revoke is not decoration. Once the function returned the truth, leaving it callable would let any signed-in user ask whether another user, identified by a uuid, is a paying customer. Clients never called it directly (a search of the app source, the edge functions and the admin site found no RPC call), so removing the grant broke nothing.

The reward functions stopped trusting their parameters. p_premium is still in both signatures (p_premium boolean default false) so that older app builds keep calling them without an error, but the body ignores it and asks is_user_premium instead. The check-in key always has the shape checkin:<user id>:<day>, and the day is parsed out of what the client sent and must fall inside a window around the server’s UTC date (the next note covers why the window is one day either side). p_day for quests must match YYYY-MM-DD exactly and sit inside the same window, then the key is rebuilt from the parsed date, so a differently written spelling of the same day cannot become a second key.

Old overloads were patched too. Older signatures such as grant_checkin_reward(text) were not called by any shipped build, but they were still executable by anon and authenticated. An unused function with a public grant is an open door, so they got the same guards.

Keeping old app versions alive

The server could change in one deploy; the apps in people’s pockets could not. Before editing a signature, the audit listed every function the apps call, and which build each call came from, by cross-referencing the push-token table (it records the app version per device) with the git history of each release. The findings shaped the patch:

  • One older React Native build called the check-in function after saving the streak but did not check whether the save had worked. It had to keep getting valid replies even when its save failed.
  • Newer builds retried a failed grant and wrote an unsent grant to a local queue for up to seven days.
  • Every client sent the device’s local date.

So refusals keep the old JSON shape. A denied call returns { ok: false, reason: 'no_amount', denied: '<why>' }, and no_amount is on the client’s list of permanent failures, so it does not retry or queue the grant for seven days. A duplicate returns { ok: true, duplicate: true, granted: 0 }, which is what clients already treated as “done”. The extra denied field is for logs. A server change that changes the response vocabulary breaks old clients; one that reuses it leaves them working.

What was left out on purpose

Quest completion is still claimed by the client: progress lives on the device, and the server only sees a request that says “I finished quest X”. The fix bounds the damage (once per quest per valid day, never doubled by a lie about premium) but does not eliminate it. The change notes list it as out of scope and record it in the report: the reward is limited to what an honest completion would earn, and a determined user can claim a quest they did not finish. Pretending otherwise would have been less useful than naming it.

How it was checked

The SQL file is idempotent (create or replace, drop trigger if exists, revoke and grant repeated safely), so it can be re-run. It also opens with a dependency guard: if the helper _has_active_subscription is missing, the script stops with an error instead of half-applying. That is a small habit worth copying. Make a migration refuse to run out of order.

The holes were confirmed by reading the live function bodies with pg_get_functiondef. Checks that would show the fix working, none of which I re-ran while writing this note:

  • Call the check-in function twice with different invented keys. Before the change the balance rises twice; after it, the second call should come back as a duplicate for the same day, and a day outside the window should be refused.
  • Call the quest function with p_day set to 'not-a-day'. The reply should be no_amount with a denied reason.
  • Ask is_user_premium for a paying test user from a service-role session (true), then from an authenticated session (a permission error).