How to Harden a Supabase or Firebase Backend Before Launch

Supabase and Firebase let you ship a working backend in an afternoon, and AI app builders lean on them heavily. The catch is that the browser talks to the database directly. There is no server of yours in between to say "no", so the only thing standing between a stranger and your data is the access rules you configured. If those rules are missing or too generous, the app still looks fine in a demo.
This guide covers the checks we would run on a Supabase or Firebase backend before launch: row-level security, storage buckets, service keys, Firebase security rules, and a short routine for testing it all the way an attacker would. It is part of the wider production-readiness checklist for vibe-coded apps, going deeper on items 1 to 4.
All snippets below are examples to adapt. Table names, column names and roles will differ in your project, so read each one, understand it, and test it before relying on it.
Why This Backend Model Is Easy to Get Wrong
Both platforms expose your data over an HTTP API that the front end calls using a key shipped in the browser. That key is designed to be public. What makes it safe is not secrecy, it is the rules attached to your data:
- In Supabase, those rules are Postgres row-level security (RLS) policies.
- In Firebase, they are security rules for Firestore, Realtime Database and Cloud Storage.
No rules, or "allow everything" rules, mean the public key is effectively an admin key. Generated code often optimises for "the query works", and the quickest way to make a blocked query work is to loosen the rule. That is the pattern to look for.
Supabase: Turn On Row-Level Security
Tables in the exposed public schema are reachable through the auto-generated REST API. Depending on how a table was created (dashboard or SQL migration), RLS may be off, so check every table rather than assuming.
-- Example: enable RLS on a table (adapt the name)
alter table public.notes enable row level security;
Once RLS is on and no policy exists, the API returns no rows to the anon and authenticated roles. That is the safe starting point: you then add the narrowest policies that make the app work.
A per-user policy with auth.uid()
For data that belongs to one user, with a user_id column that stores the owner:
-- Example: owners can read their own rows
create policy "notes_select_own"
on public.notes for select
to authenticated
using ( (select auth.uid()) = user_id );
-- Example: owners can insert rows only for themselves
create policy "notes_insert_own"
on public.notes for insert
to authenticated
with check ( (select auth.uid()) = user_id );
Two details matter. using filters which existing rows a request can see or change; with check validates the new row being written, which stops a user inserting a row with someone else's user_id. An update policy needs both. Wrapping auth.uid() in (select ...) is a documented way to let Postgres evaluate it once per query rather than once per row.
Why using (true) Is Dangerous
-- Anti-pattern: do not ship this on a table with user data
create policy "allow all" on public.notes
for select using ( true );
using (true) means "every row passes". Without a to clause the policy applies to every role, including anon, so the public key in your front end can read the whole table. It is also the most common "fix" for a query that returns empty results under RLS. If a query returns nothing, the correct response is to fix the condition, not to open the table.
Other places where RLS is easy to bypass unintentionally, worth checking in review:
- Views. In Postgres, a view normally runs with its owner's rights and can skip RLS on the underlying tables. On recent Postgres versions you can set
security_invoker = trueon the view; confirm the option for your version. - Functions marked
security definer. They run with the creator's rights. Review each one and what arguments it trusts. - Policies built on
user_metadata. Users can edit that part of their own token. Authorisation data belongs inapp_metadataor in your own tables.
Supabase: Storage Buckets and Service Keys
Public versus private buckets
A public bucket serves its files to anyone who has the URL, with no login and no policy check on reads. That is right for logos and marketing images. It is wrong for invoices, IDs, contracts or user uploads. Keep those in a private bucket and control access with policies on storage.objects:
-- Example: private bucket "user-files", each user limited to a folder named after their id
create policy "user_files_select_own"
on storage.objects for select
to authenticated
using (
bucket_id = 'user-files'
and (storage.foldername(name))[1] = (select auth.uid())::text
);
create policy "user_files_insert_own"
on storage.objects for insert
to authenticated
with check (
bucket_id = 'user-files'
and (storage.foldername(name))[1] = (select auth.uid())::text
);
To share a private file for a limited time, generate a signed URL on the server instead of switching the bucket to public.
The service_role key never goes in client code
Supabase gives you two keys. The anon key is meant for the browser and is only as powerful as your RLS allows. The service_role key bypasses RLS entirely. Treat it like a database root password:
- It must never appear in front-end code, in a mobile app bundle, or in any variable the build exposes to the browser (for example variables with a
NEXT_PUBLIC_orVITE_prefix). - Use it only in server-side code you control, such as an edge function or backend job, read from an environment variable.
- Search your repository history too. A key removed from the latest commit is still in earlier ones, and if it ever shipped, rotate it.
AI-generated code sometimes reaches for the service key because "it fixes the permission error". That is the same mistake as using (true), with more power.
Firebase: Deny by Default and Match request.auth.uid
Firestore and Cloud Storage reject anything your rules do not explicitly allow. The risk is the rules that say "allow", usually written once to unblock development. Two to look for: rules left in test mode (open to everyone, sometimes with an expiry date nobody tracks) and a blanket allow read, write: if true.
// Example Firestore rules: adapt collection and field names
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /notes/{noteId} {
allow read, delete: if request.auth != null
&& resource.data.userId == request.auth.uid;
allow create: if request.auth != null
&& request.resource.data.userId == request.auth.uid;
allow update: if request.auth != null
&& resource.data.userId == request.auth.uid
&& request.resource.data.userId == resource.data.userId;
}
// Everything not matched above is denied.
}
}
Points to check:
- Rules are not filters. A query for all notes fails if the rules would deny any document it could return. Your app must query
where("userId", "==", uid), and the rule must still enforce it. - Stop ownership changes on update. The last line of the update rule prevents a user rewriting
userIdto hand a document to someone else. - Storage rules need the same care. For example,
match /users/{uid}/{allPaths=**} { allow read, write: if request.auth != null && request.auth.uid == uid; }. - The web config is not a secret. The Firebase API key in your client config identifies the project. The credential to protect is the Admin SDK service-account key, which bypasses all rules and must stay on the server.
Test It Like an Attacker
Reading rules is not enough. Policies interact in ways that surprise their authors, so prove the behaviour from the outside. Use a throwaway test account and a staging project where you can.
Supabase
-
Find your project URL and the
anonkey (visible in your front-end bundle, which is the point of this test). Call a table with no session:# Example: replace the project URL, table name and key curl "https://YOUR-PROJECT.supabase.co/rest/v1/notes?select=*" \ -H "apikey: YOUR_ANON_KEY" \ -H "Authorization: Bearer YOUR_ANON_KEY"Expect an empty list or a permission error, never user rows. Repeat for every table, not only the main one.
-
Sign in as test user A, copy the session token, and request user B's rows by adding a filter such as
?user_id=eq.<B's id>. Expect an empty result. -
As user A, try to insert or update a row with
user_idset to B's id. Expect a row-level security violation. -
As an unauthenticated visitor, request a private-bucket file URL and try to list the bucket. Expect a refusal.
-
Do not test from the SQL editor. It runs with elevated rights and will make every policy look correct.
Firebase
- Request a document through the Firestore REST endpoint without an ID token. Expect a permission-denied response.
- Sign in as user A and read, list and write documents owned by user B. Expect denial for each.
- Use the Firebase Emulator Suite to write automated tests for your rules, so a later edit cannot quietly reopen a collection.
Write down what you tested and what came back. A short table of "request, expected, actual" is also what a developer or auditor will want to see.
A Pre-Launch Checklist for the Backend
- RLS enabled on every table in an exposed schema, with explicit policies.
- No
using (true)orallow ...: if trueon tables holding user data. - Policies use
auth.uid()orrequest.auth.uid, and write rules validate the new row, not just the old one. - Sensitive files are in private buckets with access rules.
- No service key or admin credential in the browser, the mobile bundle or the repository history.
- Views and
security definerfunctions reviewed. - Attacker-style tests run and recorded, and rerun after each schema change.
When a Checklist Is Not Enough
A self-run test catches the obvious holes. It does not replace someone attacking the app with a plan, especially where payments, health data or multi-tenant separation are involved. A web penetration test does that, and our guide to API security best practices covers the layer above the database. If you already suspect your AI-built app has gaps, our rescue service starts with an audit and ends in a phased fix plan.
FAQ
Is the Supabase anon key safe to expose in my front end?
Yes, it is designed to be public, but only if row-level security is enabled and the policies are correct. The anon key grants whatever your policies allow to the anon role. With no RLS or a using (true) policy, it can expose the whole table. The service_role key is different and must never leave the server.
Do I still need security rules if my app has its own login screen?
Yes. A login screen is only a user interface; anyone can skip it and call the API directly with your public key. The rules on the database and storage are what enforce who can read and write. Treat the front end as untrusted.
What is the fastest way to check whether my backend is exposed?
Call your REST or Firestore endpoint with only the public key and no user session, for every table or collection, then repeat while logged in as one user and asking for another user's records. If any of those return data they should not, stop and fix the rules before launch.
Have a project idea?
Get a rough estimate in 5 minutes with our Project Scoper.
Scope Your ProjectReady to discuss?
30 minutes. No pitch deck. Just an honest conversation about your project.
Book a Free Call





