You commented AUDIT, so here is everything the carousel promised: the 12-point checklist, the Supabase Row Level Security (RLS) templates, and the Cloudflare rate-limiting rules.
AI coding tools are good at writing code that runs. They are less reliable at the parts nobody sees in a demo: who is allowed to read which row, where the secret keys live, and what stops a bot from calling your paid AI endpoint all night. Work through this once before you launch, and again after any big change.
What you need
Access to your app's code and its hosting dashboard.
Your Supabase project. The SQL Editor is enough. On another Postgres database, the same SQL works with small changes.
A Cloudflare account with your domain on it, for the rate-limiting part. The Free plan works.
About an hour.
The 12-point checklist
Secrets
No secret keys in the browser. Open your live site, open DevTools, and search all loaded files for
sk-,sb_secret_andservice_role. Anything you find there is public.Only the publishable key ships to the client. Supabase says its publishable key is safe to expose, but only because RLS limits what it can reach. The secret key, and the older
service_rolekey, bypasses RLS completely. Keep it in server-side environment variables only.Scan your git history, then rotate. Deleting a key from the code does not remove it from old commits. Run gitleaks (commands below). If it finds a key, rotate that key in the provider's dashboard.
Database
RLS is on for every table. Run the first query below. It should return zero rows.
Every table has a policy for each action. Write separate policies for select, insert, update and delete, each tied to the signed-in user. Be suspicious of any
using (true)on a table that holds private data.Close the side doors. Remove the default grants for the
anonrole on private tables. Never useuser_metadatain a policy, because users can edit it themselves. Setsecurity_invokeron views, because views bypass RLS by default.
Object access (IDOR)
Test with two accounts. Sign in as user A and request a record that belongs to user B, by changing the ID in the URL or the request. You should get nothing back. Server code that uses the secret key must check ownership itself, because RLS does not apply there.
Use random IDs. Move public-facing IDs from 1, 2, 3 to UUIDs. OWASP recommends this, but treat it as an extra layer. The ownership check in point 7 is the real fix.
Cost and abuse
AI calls go through your server. The browser calls your endpoint. Your endpoint calls the model, with the key stored as a server secret.
Rate limit the AI endpoints. Limit by IP at the edge, and by signed-in user inside the function. Both rules are below.
Set hard caps. Limit input length and output tokens on every model call, and set a spending limit with your AI provider. If it has no spending limit, set a billing alert.
Before every deploy
Automate the checks. Run gitleaks and Semgrep on every push, and open the Security Advisor in your Supabase dashboard, which flags tables with RLS turned off.
Supabase RLS templates
Run these in the SQL Editor, on a test project first. Replace the table names (notes, profiles, usage_log) and the owner column (user_id) with your own.
Find tables with RLS off
select schemaname, tablename
from pg_tables
where schemaname = 'public'
and rowsecurity = false;Template 1: private rows, owner only. For notes, orders, chats, anything that belongs to one user.
alter table public.notes enable row level security;
revoke all on table public.notes from anon, authenticated;
grant select, insert, update, delete on table public.notes to authenticated;
create policy "Owners can read their rows"
on public.notes for select
to authenticated
using ( (select auth.uid()) = user_id );
create policy "Owners can insert their rows"
on public.notes for insert
to authenticated
with check ( (select auth.uid()) = user_id );
create policy "Owners can update their rows"
on public.notes for update
to authenticated
using ( (select auth.uid()) = user_id )
with check ( (select auth.uid()) = user_id );
create policy "Owners can delete their rows"
on public.notes for delete
to authenticated
using ( (select auth.uid()) = user_id );
create index notes_user_id_idx on public.notes using btree (user_id);Template 2: public read, owner write. For public profiles or listings.
alter table public.profiles enable row level security;
revoke all on table public.profiles from anon, authenticated;
grant select on table public.profiles to anon, authenticated;
grant insert, update on table public.profiles to authenticated;
create policy "Anyone can read profiles"
on public.profiles for select
to anon, authenticated
using ( true );
create policy "Users can create their own profile"
on public.profiles for insert
to authenticated
with check ( (select auth.uid()) = id );
create policy "Users can update their own profile"
on public.profiles for update
to authenticated
using ( (select auth.uid()) = id )
with check ( (select auth.uid()) = id );Template 3: server only. For usage logs, billing records and anything the browser should never touch.
alter table public.usage_log enable row level security;
revoke all on table public.usage_log from anon, authenticated;
-- No policies on purpose. Only your server, using the secret key, can read or write.Fix a view (Postgres 15 and later)
alter view public.my_view set (security_invoker = true);Cloudflare rate-limiting rules
In the Cloudflare dashboard, go to Security rules, select Create rule, then Rate limiting rules.
Rule 1: all API routes (works on the Free plan)
Field: URI Path. Operator: starts with. Value:
/api/With the same characteristics: IP
When rate exceeds: 20 requests per 10 seconds
Then take action: Block. Duration: 10 seconds
The same rule as an expression:
(starts_with(http.request.uri.path, "/api/"))Tune the number to your app. The Free plan gives you one rule, counted by IP, with a fixed 10-second window and a 10-second block. It stops simple loops, not patient ones.
Rule 2: the AI route (Pro plan and above)
URI Path equals
/api/chat(use your own AI route)When rate exceeds: 10 requests per 1 minute
Then take action: Block. Duration: 1 hour
Pro allows two rules, windows up to 1 minute and blocks up to 1 hour.
Rule 3: per-user limit inside a Worker
IP limits are blunt, because many people on a mobile network share one IP. Cloudflare's guidance for its Workers rate limit binding is to count by a stable ID, such as the user ID or API key. Add this to wrangler.toml:
[[ratelimits]]
name = "AI_LIMITER"
namespace_id = "1001"
[ratelimits.simple]
limit = 10
period = 60Then check the limit before you call the model:
export default {
async fetch(request, env) {
// 1. Verify the session token and get the user's ID
const userId = await getUserId(request);
if (!userId) {
return new Response("Sign in first", { status: 401 });
}
// 2. Count this request against that user
const { success } = await env.AI_LIMITER.limit({ key: userId });
if (!success) {
return new Response("Too many requests", { status: 429 });
}
// 3. Only now call the model, with the API key stored as a Worker secret
return callModel(request, env);
}
};getUserId and callModel are yours to write. The first verifies the session token. The second calls your AI provider. You need Wrangler 4.36.0 or later, and the period must be 10 or 60 seconds.
Run the scans
brew install gitleaks
gitleaks git -v .
gitleaks dir -v .
pipx install semgrep
semgrep scangitleaks git scans your commit history and gitleaks dir scans the files as they are now. semgrep scan runs Semgrep's open-source community rules on your code. On Windows or Linux, see each tool's install page in the sources.
Good to know
This is a starting checklist for the four leaks in the carousel, not a full security review. If you handle payments or health data, get a professional audit.
Turning on RLS and revoking grants can break screens that relied on open access. Test on a staging project, and fix it by adding the right policy, not by turning RLS back off.
Supabase turns RLS on by default for tables created in its dashboard. Don't assume the same for tables created by SQL migrations, which is how most AI tools create them. That is why check 4 matters.
Supabase has said it plans to deprecate the older
anonandservice_rolekeys by the end of 2026. Moving to the new publishable and secret keys now saves you a migration later.The Workers rate limit is approximate. Counts are kept per Cloudflare location and can run slightly over. For a strict global cap, Cloudflare points to Durable Objects.
Run the two-account test only on apps you own or have permission to test.
Sources
Found this useful? Follow @kevinsingh.me on Instagram for a new AI tool every day, and subscribe so you don't miss the next one.

