How-to

Firebase Security Rules, explained

Published 21 October 2026 · 7 min read

Your Firebase config — the API key, project ID, and database URL baked into your app — is public by design. Anyone can open your site, view the source, and read it. That is normal and expected. The only thing that actually protects your users' data is a separate layer called Security Rules. If your rules are open, your database is open, no matter how clever your app looks.

In plain terms: Firebase does not decide who can read or write your data based on your app's code. It decides based on the rules you write and publish. Get the rules right and a stranger with your config can do nothing. Get them wrong and that same stranger can download or delete everything.

Why the Firebase config being public is not the problem

Founders often panic when they see their Firebase API key sitting in the browser. It is fine. A Firebase "API key" is really just a project identifier — it is not a secret and was never meant to be hidden. Google's own documentation says so.

The real control is Security Rules. They sit on Google's servers and are evaluated on every single read and write before it is allowed. Think of your config as the building address (public) and your rules as the locks on the doors (the thing that matters).

The "test mode" trap

When you create a Firestore or Realtime Database, Firebase offers to start in test mode. This is where most no-code and AI-builder apps get burned. Test mode publishes rules that look like this:

Test mode is meant for a weekend prototype, not a live app. Two things go wrong: either you launch while if true is still in place (fully exposed), or the expiry date passes and your app suddenly breaks for real users because everything is now denied. Neither is a good place to be. You need rules you actually chose.

Firestore rules: require login, then require ownership

Firestore uses a rules language where you match document paths and set conditions. The first thing almost every app needs is: you must be signed in.

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null;
    }
  }
}

request.auth != null means the request came from a logged-in user. That blocks anonymous strangers — but note it still lets any logged-in user read and write everyone's data. That is rarely what you want.

The next step is ownership: a user should only touch their own records. If each document stores the owner's ID in a userId field, you check it:

match /notes/{noteId} {
  allow read, write: if request.auth != null
    && request.auth.uid == resource.data.userId;
}

Here resource.data.userId is the owner stored on the existing document, and request.auth.uid is the person making the request. They must match. For creating new documents the existing record does not exist yet, so you check the incoming data instead with request.resource.data.userId == request.auth.uid.

Realtime Database rules: a different syntax, same idea

The older Realtime Database uses JSON-shaped rules with .read and .write keys. The concepts are identical — require auth, then scope by path.

{
  "rules": {
    ".read": false,
    ".write": false,
    "users": {
      "$uid": {
        ".read": "auth != null && auth.uid === $uid",
        ".write": "auth != null && auth.uid === $uid"
      }
    }
  }
}

The top-level false denies everything by default, then each path opens access only to the matching user. The $uid wildcard captures the path segment so you can compare it to auth.uid. A common mistake is setting .read: "auth != null" at the root, which lets any logged-in user read the entire database.

Don't forget Storage

Firebase Storage (for uploaded files and images) has its own, separate set of rules — securing Firestore does nothing for your files. The pattern is the same: require request.auth != null and scope paths to the owner, for example storing uploads under a /users/{userId}/ folder and allowing access only when request.auth.uid == userId. Leaving Storage in test mode is how private documents and user photos end up publicly downloadable.

How to test your rules before you trust them

Always default to deny and open up deliberately. A rule that grants access is far safer than one that forgets to block it.

Scan your Firebase app — free

Faultline deep-scans Firebase apps for exactly these issues — a free surface scan plus an analyst-reviewed deep audit.

Run a free scan

Security Rules are not an advanced, optional extra — for a Firebase app they are your security. If you only do one thing after reading this, open your rules in the Firebase console and make sure they do not say if true.

Related: Is Firebase secure? · Is FlutterFlow secure?