Firebase Security Rules: A Practical Guide to Testing Them
Security TestingHetz Team

Firebase Security Rules: A Practical Guide to Testing Them

How Firebase security rules work for Firestore, Realtime Database and Storage, the mistakes that expose data, and how to test rules with two accounts before launch.

Firebase lets a web or mobile app read and write data directly from the browser, with no server in between. That is what makes it fast to build with. It also means the security of your data depends on a set of rules that run on Firebase's side for every request. If those rules are wrong, the app does not need an attacker to leak data. Anyone with the app's configuration can ask for it.

This guide explains how Firebase security rules work for Firestore, Realtime Database and Cloud Storage, the mistakes we see most often, and how to test your rules before real users arrive. For the wider category of generated apps, read our guide to AI app builder security. For how Supabase handles the same problem with row level security, see Supabase security and RLS.

What Firebase Rules Decide

Firebase security rules are checked on every request from a client. Each rule describes when a read, a write, a create, an update or a delete is allowed. The rule can look at the signed-in user, the data being sent, and in some cases the data already stored. If no rule allows the operation, it is refused.

The official Firebase security rules documentation covers all three products. Firestore uses its own security rules guide, Realtime Database uses its own JSON-based rules, and Cloud Storage uses rules for files and paths. The core idea is the same in each: a request is allowed only when a rule says so.

Firebase Config Is Not a Secret

Every Firebase web app includes a configuration object with an API key, a project identifier and other values. The Firebase documentation on API keys explains that these keys identify your project and are not used to control access. Anyone who loads your app can read them, and that is expected.

This has a practical consequence. Hiding the key does nothing, and worrying about it wastes time. Your protection comes from the rules and from the server. If a stranger can read or write data with only the public configuration, the problem is the rules, and rotating the key will not fix it.

The Most Common Rule Mistakes

Test mode left on in production

Firebase offers a test mode for early development. Those rules allow all reads and writes for a limited period, so that a prototype works without configuration. When a project moves to production and the test rules stay in place, the database is effectively public. Check the rules of every project before launch, and make sure no test rule with an open date is still active.

Any signed-in user can read everything

A rule such as "allow read if the user is signed in" protects against anonymous visitors, but not against customers reading each other's data. Each collection that holds personal or business data needs a condition that compares the document's owner with the signed-in user's identifier.

Rules that trust fields the client sets

A rule that checks a field such as "owner" only when the document is created can be bypassed on update, if the update rule does not check the same field. Each operation needs its own rule, and each rule should check the existing owner, the new owner or both, depending on the action.

Queries the rules cannot guarantee

Firestore rules are not filters. A query must be written so that every document it can return is allowed by the rules. A query that lists every document in a collection and relies on the client to ignore the rest will be refused, or, if the rule is broader than intended, will expose everything. Build queries with the same owner condition the rule uses.

Storage paths without ownership

Cloud Storage rules that allow any signed-in user to read a whole bucket expose every uploaded file. A path such as a user folder should be protected by a rule that compares the path with the user's identifier, and sensitive files should not be readable by anyone who has a guessed link.

Server code that skips the rules

Requests made with the Firebase Admin SDK bypass security rules, because they run on a trusted server. That is correct for backend jobs. It becomes a problem when an endpoint uses admin access and does not check which user is asking. In that case, the rules are never consulted, and the endpoint decides what is returned.

A Quick Checklist

CheckWhat to look forPriority
No test mode in productionRules that allow everything, or an open expiry dateCritical
Per-user access on personal dataA second account reads the first account's documentsCritical
Every operation has its own ruleCreate, update and delete checked as well as readHigh
Queries constrained to allowed dataLists that return documents the rules would refuseHigh
Storage paths tied to ownershipFiles readable through a guessed pathHigh
Admin endpoints check the userServer routes that return data without a user checkHigh

Testing Rules Locally and Live

Start locally. The Firebase Emulator Suite runs Firestore, Realtime Database and Storage on your machine, and it can load your rules. Write tests that sign in as two users, then try allowed and forbidden operations. A test should fail when a user reads another user's document. Keep these tests in your repository so they run again after every rule change.

Emulator tests are necessary but not enough. The live project can have different configuration, extra collections created outside the code, and rules that were edited in the console and never committed. Test the live app the way an attacker would: sign in with two accounts, load pages, watch the network calls, and replay requests across accounts. Any document one account can read from another is a finding.

Where a Penetration Test Adds Value

A rule review finds mistakes in the rules. A penetration test finds what the rules let through in practice. It maps the app's calls to Firestore, Storage and any server endpoints, signs in as each role, and tries to cross every boundary. It also tests the combinations: a function that reads a collection on behalf of the user, a webhook that writes data without checking its source, or an old collection that the current rules forgot.

Hetz tests the live app on your verified domain. Findings are kept only when proven, each comes with the exact request and response, and each includes a fix for your stack. For methods, the NIST SP 800-115 guide to technical security testing describes the phases that a professional test follows. Our overview of automated vs manual penetration testing compares the options.

A Sample Rule Pattern

The pattern below allows a user to read and update only their own documents in a collection. It shows the idea, not a complete configuration, and it should be adapted and tested for your data model:

match /orders/{orderId} {
  allow read, update, delete: if request.auth != null
    && resource.data.ownerId == request.auth.uid;
  allow create: if request.auth != null
    && request.resource.data.ownerId == request.auth.uid;
}

Note that the create rule checks the new owner, while the update and delete rules check the existing owner. Each operation needs its own check, which is why a single broad rule is hard to get right.

Writing Tests That Check What Is Forbidden

Most rule tests check that the right user can do the right thing. That is necessary, but the more useful tests check what must fail. For each collection with personal data, write at least four cases: a signed-out visitor cannot read, a user cannot read another user's document, a user cannot change the owner of their own document, and a user cannot create a document on behalf of someone else. If any of these succeeds, the rule is too broad, and the test should be kept as a permanent regression check.

Write the same cases for Cloud Storage paths. A file upload into another user's folder should be refused, and a download through a guessed path should fail. Storage is easy to forget because it sits outside the database, yet many apps keep invoices, contracts and identity documents there.

Tests also help with rule changes that look harmless. Renaming a field, adding a status or moving a document to a subcollection can change which rule applies. A short test suite catches these changes before they reach users, while a review of the rule file alone often does not.

Keeping Rules Safe Over Time

Rules change as an app grows, and the person who edits them is rarely the person who wrote the first version. Keep the rules in version control, review changes the way you review code, and run the emulator tests in your deployment pipeline. When you add a feature that reads an existing collection in a new way, update the rule and the test together. A good habit is to record, next to each collection, which roles should read it and which should write it. That short table is the reference your tests and your reviewers can check against, and it makes an unexpected access path easy to spot.

Next steps

Before launch, remove test mode, write two-account tests for every collection with personal data, and check the storage paths and admin endpoints. To see what a full test covers and what it costs, read our guide to penetration testing cost, or compare options on pricing. When you are ready, start a scan.

FAQ

Frequently asked questions

Keep reading