Skip to content
ShipSafeStack

Checklist

Firebase Security Rules examples

Short answer. Firestore and Storage start from rules you deploy, not from the screens in the app. Open access and Test mode let anyone who can reach the project read and write. The examples below are the shapes Firebase documents: deny by default, signed-in only, owner-only, public read, custom claims, field checks, and per-user Storage folders. The Admin SDK bypasses them.

Updated 25 Nov 2026

This is a practical checklist, not a guarantee. It is not a penetration test, a compliance audit, or proof that an app is safe to launch.

Firebase Security Rules are the check on client reads and writes. They are not a filter the database applies after the query, and they do not run for the Admin SDK. This page is a copy-paste reference for Firestore and Cloud Storage. The Supabase equivalent is Supabase RLS policy examples. How the two products differ is Supabase vs Firebase and Supabase vs Firebase for vibe-coded apps. Firebase is the directory entry.

Official pages: Firebase Security Rules, Firestore Security, insecure rules, and Storage Security.

Why open rules are a problem

Firebase's insecure-rules page says that creating a database or a Storage bucket offers Locked mode, which denies all users, or Test mode, which grants access to all users. The Firestore page says the default rules for an instance created in the console deny everyone, and that people then open them while developing. If the app is deployed, it is on the internet. Anyone who can reach the project can read, change, or delete data when the rules allow it.

The open-access sample they say never to ship is:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

A generated client often deploys with whatever rules were in the starter. Open the Rules tab, or the file named in firebase.json, and read what is actually published. A condition that only checks request.time against a future date is still open until that date. Test mode is for a local emulator, not for a project whose URL other people can open.

The closed-access sample is the opposite, and it is what Firebase shows for a server-only database:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if false;
    }
  }
}

The Admin SDK and Cloud Functions can still read and write. Client apps cannot, until you add a narrower allow.

Signed-in only

Firebase's conditions page shows this for data any signed-in user may touch:

service cloud.firestore {
  match /databases/{database}/documents {
    match /cities/{city} {
      allow read, write: if request.auth != null;
    }
  }
}

When to use it: every signed-in user is allowed to see and change every document in that collection. The insecure-rules page says to confirm that is what you want. request.auth != null does not mean "this user." It means any account, including one someone else just created.

Owner-only documents

Use a path that is the user's id when the document is that user's own record. From the conditions page:

service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId} {
      allow read, update, delete: if request.auth != null && request.auth.uid == userId;
      allow create: if request.auth != null;
    }
  }
}

When the owner is a field, not the path, Firebase's insecure-rules page uses author_uid. resource is the document already stored. request.resource is the document after the write. Create checks the incoming field. Update checks both, so the caller cannot hand the document to someone else.

service cloud.firestore {
  match /databases/{database}/documents {
    match /posts/{postId} {
      allow read, delete: if request.auth.uid == resource.data.author_uid;
      allow create: if request.auth.uid == request.resource.data.author_uid;
      allow update: if request.auth.uid == request.resource.data.author_uid
                    && request.auth.uid == resource.data.author_uid;
    }
  }
}

Common mistake: an update rule that only checks request.resource.data.author_uid. The caller can set that field to their own id and take a document they did not own. The documented update checks the stored field and the incoming field.

Public read, owner write

Firebase's mixed example allows anyone to read, and only the owner to write:

service cloud.firestore {
  match /databases/{database}/documents {
    match /posts/{postId} {
      allow read: if true;
      allow create: if request.auth.uid == request.resource.data.author_uid;
      allow update: if request.auth.uid == request.resource.data.author_uid
                    && request.auth.uid == resource.data.author_uid;
      allow delete: if request.auth.uid == resource.data.author_uid;
    }
  }
}

When to use it: the documents are meant to be public, such as a published post. allow read: if true is every visitor. Do not copy it onto a collection of private rows.

Custom claims

Role checks that must not be editable by the client belong in custom claims. Firebase's custom claims page says the Admin SDK sets them from a privileged server, with setCustomUserClaims, and that the claims show up in the ID token the next time one is issued. The client can refresh with getIdToken(true). Rules read them on request.auth.token. The rules and auth page shows:

service cloud.firestore {
  match /databases/{database}/documents {
    match /adminContent/{docId} {
      allow write: if request.auth.token.admin == true;
    }
  }
}

Do not store that flag in a field the user can write, and do not trust a role the client sends in the request body. Claims are for access control. Firebase says not to use them as a profile store, and the payload must stay within 1000 bytes. A claim you set is not on the current token until a new token is issued.

Looking up a role document with get() also works. Firebase notes that each get() is a billed read. A claim does not add that read.

Field checks

Rules do not give the document a schema. Firebase's rules fields page uses request.resource.data.keys() to limit what a create may contain, and is to check types. hasOnly allows only the listed keys. Valid is types include string, int, bool, and timestamp.

service cloud.firestore {
  match /databases/{database}/documents {
    match /posts/{postId} {
      allow create: if request.auth.uid == request.resource.data.author_uid
        && request.resource.data.keys().hasOnly(['title', 'body', 'author_uid'])
        && request.resource.data.title is string
        && request.resource.data.title.size() < 120
        && request.resource.data.body is string;
    }
  }
}

size() is the length of a rules string. Calling a missing field, such as request.resource.data.title when title was omitted, denies the write. On update, keys() is the whole document after the write, not the fields that changed. Firebase says to use request.resource.data.diff(resource.data).affectedKeys() when you need the changed set. A rule allows the whole write or rejects it. It cannot accept some fields and drop others in the same operation.

Storage: per-user folders, type, and size

Storage rules match a path. Firebase's insecure-rules page shows files laid out as user/<uid>/file.txt:

service firebase.storage {
  match /b/{bucket}/o {
    match /user/{userId}/{fileName} {
      allow read, write: if request.auth.uid == userId;
    }
  }
}

The public-read variant allows anyone to read and only that user to write:

service firebase.storage {
  match /b/{bucket}/o {
    match /user/{userId}/{fileName} {
      allow read;
      allow write: if request.auth.uid == userId;
    }
  }
}

allow read with no condition is public. Use it only for files you mean to publish, such as a profile image. Private files keep the request.auth.uid == userId check on read as well.

The Storage security page limits type and size on request.resource, which is the file being written. size is bytes. This is their example for an image under 5 MB:

service firebase.storage {
  match /b/{bucket}/o {
    match /user/{userId}/{fileName} {
      allow read: if request.auth.uid == userId;
      allow write: if request.auth.uid == userId
                   && request.resource.size < 5 * 1024 * 1024
                   && request.resource.contentType.matches('image/.*');
    }
  }
}

A rule of allow read, write with no condition, matching /{allPaths=**}, is the Storage open-access sample. Firebase says it lets anyone overwrite the files.

Rules are not filters, and the Admin SDK skips them

Firebase's rules conditions page says rules are not filters. A query for every document in a collection fails if it could return a document the caller may not read, even when every stored document would have been allowed. The query has to include the same constraint as the rule. Their query page shows where("author", "==", user.uid) next to a rule that checks author. A single-document get is evaluated against that document. A list is evaluated against what the query could return.

The same insecure-rules page says the server client libraries bypass Cloud Firestore Security Rules and authenticate with Google Application Default Credentials. The Admin SDK and Cloud Functions still reach data when client rules are if false. Use the Admin SDK on a server you control. Do not ship its credentials in the client. The browser apiKey is a different value. Is the Firebase API key safe to expose? is that question. How to tell a publishable key from a secret is API keys in the frontend.

How to test

Firebase's insecure-rules page says to use the emulator before you deploy, and the Rules Playground for a quick check in the console.

The Playground is on the Rules tab. Choose read or write, a document path, and an auth state: signed out, anonymous, or a specific user id. If the rule reads a field, supply the document the request would write. Run it. The result is for the rules in the editor, not a certificate that the deployed set matches.

Automated tests use the Emulator Suite and @firebase/rules-unit-testing. The Firestore emulator testing page shows authenticatedContext for a signed-in user, including custom claims, and unauthenticatedContext for a signed-out one. withSecurityRulesDisabled loads fixtures without running rules. Assert both the allowed write and the denied one. A test that only checks the happy path misses the open rule.

Checklist

  • Open the published rules. Replace if true, and any Test mode grant, before the project is reachable.
  • Signed-in only is every account. Owner data needs request.auth.uid against the path or against both resource and request.resource.
  • Admin flags belong in custom claims set by the Admin SDK, not in a field the client writes.
  • Constrain creates with keys().hasOnly and is. On updates, use diff if you need the fields that changed.
  • Storage paths should include the uid. Cap request.resource.size and contentType.
  • Queries must match the rules. List queries are all or nothing.
  • Run the Playground, then emulator tests, as a second user and as signed out.
  • Then run a free passive check on the public URL. It does not read your Firebase rules. The worksheets for this pass are the checklist pack and the pre-launch checklist.

FAQ

Does request.auth != null mean only this user can read the row?

No. Firebase's insecure-rules page says a check that the user is logged in still allows every logged-in user. Compare request.auth.uid to the document path or to an owner field, and on update compare both the stored document and the incoming one.

Can I set a custom claim from the client?

No. Firebase says custom claims are set from a privileged server with the Admin SDK, and that they appear on the ID token the next time one is issued. Rules see them as request.auth.token. A field the client can edit is not that claim.

Why does a query fail when the user owns every document?

Rules are not filters. Firebase evaluates a list query against what it could return. If the query omits the constraint in the rule, the whole request fails even when every stored document would have been allowed. Add the same where the rule checks. Reading one document by id is evaluated against that document.

Sources

Checked 16 Nov 2026.

Informational only. This page describes checks and vendor documentation. It is not a security audit, a penetration test, or a certification, and it is not a guarantee.

Newsletter

A short weekly note on new tools and security checklists. No spam, unsubscribe anytime.