Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 31 ยท Security Rules and Row Level Security

Security Pitfalls in Both Platforms

Avoid the classic Firebase and Supabase security mistakes: exposed keys, open rules and policies, missing validation, App Check, rate limits and an audit checklist.

Backend-as-a-service platforms make it easy to build quickly, and that same speed makes it easy to ship an app with the front door open. Most real-world incidents on Firebase and Supabase are not clever hacks. They are a test-mode database left public, a secret key inside a JavaScript bundle, or a policy that says true.

This lesson collects the pitfalls in one place and ends with a checklist you can run before every launch. The platform code is read-only. The runnable part is a small linter stand-in that reads a description of rules and policies and reports mistakes; it is ours, it covers a handful of patterns, and it is no replacement for the real tools. It builds on Firestore Security Rules and Row Level Security Policies.

Where apps usually leak

Secret keys Service keys inside client code.
Open rules Rules with if true, policies with using (true).
No validation The client decides prices and roles.
No limits Bots run up the bill.

Assume every client is hostile.

1 / 2

Know which keys are public

Both platforms hand you values that you put in the browser. Knowing which ones are meant to be public is the first skill.

Value Public? Notes
Firebase web config (apiKey, projectId...) Yes Identifies the project. Protection comes from rules and App Check, not secrecy.
Supabase URL and anon / publishable key Yes Acts as the "guest or user" role. Protected by RLS.
Supabase service_role / secret key No Bypasses RLS. Server only.
Firebase Admin SDK service account JSON No Full admin access. Server only.
Third-party API keys (Stripe secret, OpenAI...) No Keep in server secrets, call from a function.
JavaScript
// read-only: what a browser bundle may contain
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);    // fine

// never do this in client code
const admin = createClient(SUPABASE_URL, SERVICE_ROLE_KEY);         // leaks everything

Environment variables do not make a value private. Tools such as Vite and Next.js copy variables with prefixes like VITE_ or NEXT_PUBLIC_ into the bundle on purpose, so a secret with that prefix is published. Search your build output for the strings service_role, private_key and sk_live before you deploy. If a secret ever leaks, rotate it immediately; deleting the commit is not enough.

Open rules and policies

The most common mistakes are one-liners:

Text
// Firestore (read-only): wide open
match /{document=**} {
  allow read, write: if true;
}
SQL
-- Postgres (read-only): public to the world
create policy "all" on public.profiles for all using (true);
-- and a table with no "enable row level security" at all

Firebase's "test mode" sets a date-limited open rule, and some tutorials leave it in place. Supabase tables made with SQL are not protected until you enable RLS. Treat any if true, using (true), or missing RLS as a bug unless the data is truly public, and even then restrict writes.

A second kind of open door is a rule that only checks sign-in: request.auth != null or to authenticated using (true). Since anyone can register, it admits everybody. Tie access to the owner's uid, a team membership or an admin claim.

Validate on the server, not only in the form

Your form can limit input lengths and hide the "role" field, but an attacker never uses your form. They call the API directly with any values they like. So every important check must live where the user cannot edit it:

  • Rules and constraints for shape: required fields, types, lengths, allowed values (check (price >= 0), hasOnly([...])).
  • Functions for logic that needs trusted data: the price comes from your catalog, not from the request body; a coupon is verified, then used once; stock is checked before an order is created.
  • Immutable fields: the owner, role, createdAt and balance must not be writable from the client. In Firestore compare request.resource.data with resource.data; in Postgres use column privileges, triggers, or move the write behind a function.
SQL
-- read-only: the browser may only change two columns
revoke update on public.profiles from authenticated;
grant update (display_name, bio) on public.profiles to authenticated;

App Check and abuse protection

Rules decide who may do something. They do not stop a script that signs up a thousand accounts, or copies your frontend to hammer the API. Layers help:

  • Firebase App Check verifies that requests come from your real app, using providers such as reCAPTCHA for the web or device attestation on mobile. Turn on enforcement for Firestore, Storage and Functions after your clients send tokens. It lowers abuse, but it is not a login system.
  • Supabase offers CAPTCHA (hCaptcha or Cloudflare Turnstile) on sign-up and sign-in, and rate limits on auth endpoints. Add limits in Edge Functions for expensive actions. Attach per-IP or per-user counters.
  • Budgets and alerts. Set billing alerts in Google Cloud or the Supabase spend cap. A runaway loop of listeners or an abused function can produce a large bill overnight.
  • Quotas in your own data. Limit documents or rows per user, upload sizes and request rates, and enforce them server-side.

Other pitfalls worth knowing

  • Trusting user_metadata or client-written profile fields for roles. Use custom claims or app_metadata. See Users, Roles and Custom Claims.
  • Public storage buckets for private files. See File Storage and Uploads.
  • Overly broad CORS and redirect URLs. Restrict authorized domains and OAuth redirect allow-lists to the sites you own.
  • Logging secrets or tokens in console output or error reports.
  • Never testing as a stranger. Open a private window, sign out, and try your API calls by hand.

A policy linter stand-in

The linter below receives a plain description of your rules and policies and applies five checks from this lesson. Each finding says what is wrong and why. Real tools (the Supabase security advisor, the rules emulator, secret scanners) do far more, but writing a linter is a good way to learn what to look for.

JS Stand-in rules and policy linter

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice what passes: the owner check on /profiles/{uid}, and the recursive write rule set to false, which is a valid way to deny everything else. A linter like this finds patterns, but it cannot know your business logic, so human review and tests remain essential.

A second stand-in estimates what an unprotected endpoint could cost, which makes the case for budgets and rate limits concrete.

JS Abuse budget: what could a bot cost?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The example numbers are invented, but the shape is real: a limit turns an open-ended bill into a small, predictable one.

Pre-launch audit checklist

  1. Keys: only public keys in the client; secrets in server environment variables; secret scanning enabled in the repository.
  2. Firestore: no if true, no leftover test mode, nested paths covered, rules tests that must fail.
  3. Postgres: RLS enabled on every table in an exposed schema, with check on update and insert, views use security_invoker, security definer functions reviewed.
  4. Validation: constraints and rules for shape, functions for logic, immutable fields protected.
  5. Auth: email verification where it matters, CAPTCHA or App Check, OAuth redirect allow-list, roles in app_metadata or claims.
  6. Storage: private buckets by default, size and type limits.
  7. Limits: budget alerts, rate limits on functions, per-user quotas.
  8. Test as a stranger and as another user before every release.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which of these may safely be included in browser code?

    The anon key is designed to be public because RLS protects the data. The others grant full or privileged access.
  2. 2. Why is VITE_SERVICE_ROLE_KEY a serious mistake?

    Variables with the public prefix are copied into the client code, so anyone can read them.
  3. 3. A rule says allow write: if request.auth != null. What is the problem?

    Signed in only proves identity. Compare uid with an owner or membership.
  4. 4. What does App Check mainly do?

    App Check reduces abuse from other clients. It complements rules and does not replace them.

Summary

  • Know which keys are public (Firebase config, anon key) and which are secret (service role, admin SDK, third-party secrets).
  • Never leave if true, using (true), test mode or disabled RLS on private data.
  • Validate on the server side with rules, constraints and functions; protect immutable fields.
  • Add App Check or CAPTCHA, rate limits, quotas and billing alerts against abuse.
  • Run the audit checklist and test as a stranger before each release.

Next: File Storage and Uploads