The Firebase Security Rules mistakes that expose data are usually test-mode rules left in production, rules that let any signed-in user read everything, roles stored in documents users can edit, and writes that never check the incoming data. Each is visible in your rules file, and each fix can be proven with an emulator test.
Firebase apps talk to Firestore, Realtime Database and Cloud Storage directly from the browser or phone. No server sits in between, so the rules alone decide what each request may do. AI coding tools write rules that make a screen work. They are less careful about rules that stop the wrong user.
How Firebase Security Rules protect your data
Every client request is checked against your rules. If no allow statement matches and evaluates to true, the request is denied.
First, the Firebase API key in your front end is meant to be public. Firebase's API key guide says data in Firestore, Realtime Database and Cloud Storage is protected by Security Rules, and covered APIs by App Check, not by keeping the key secret.
Second, server code skips the rules entirely.
| Access path | Typical use | Rules apply? |
|---|---|---|
| Web, iOS, Android and Flutter SDKs | Your app's front end | Yes |
| Admin SDK and server client libraries | Cloud Functions, backend jobs | No, access is controlled by IAM |
The rules protect only the client path, which is exactly the path an attacker controls.
Seven Firebase Security Rules mistakes to look for
1. Test mode left on
Firebase's setup flow warns that test mode allows anyone to read and overwrite your data. AI tools often pick it to get past permission errors. The result is a rule like allow read, write: if true on /{document=**}. Firebase's guide to avoiding insecure rules notes that anyone who guesses your project ID can then steal, modify or delete the data.
Search your rules for if true and for bare allow read, write; in Storage. Each match needs a reason, such as genuinely public content.
2. Any signed-in user can do anything
allow read, write: if request.auth != null looks secure. It means any account can read and change every document in that path. With open sign-up, that is anyone who registers.
Tie each document to its owner instead: compare request.auth.uid with an owner field on the document, or with the user ID in the path.
3. Roles stored where users can write
A common pattern checks get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == "admin". That is fine only if users can't write their own role field. If the users/{uid} rule lets owners update their whole document, any user can promote themselves.
Keep roles in custom claims, set from server code and read in rules as request.auth.token.admin, or restrict which fields owners may change (see the baseline below).
4. Writes that don't check the incoming data
resource.data is the document as stored. request.resource.data is the document as it would be after the write. Rules that check only the first let users rewrite any field, including the owner. On updates, check both so ownership can't move to another user. On create, validate types and allowed fields.
5. Overlapping and recursive matches
When several match blocks cover the same document, access is allowed if any of them allows it, as the rules behavior guide explains. A broad rule added to fix one screen widens access everywhere it matches. Recursive wildcards like {document=**} also carry a permissive rule into every subcollection, including ones created later.
In Realtime Database the cascade runs the other way: a read or write granted at a parent path can't be revoked at a child.
6. Storage rules that only check sign-in
match /{allPaths=**} { allow read, write: if request.auth != null; } lets any user read or overwrite every file. Put files under a path containing the owner's user ID and compare it with request.auth.uid. Also limit request.resource.size and request.resource.contentType on writes. Our guide to securing file uploads covers the server-side checks rules can't do.
7. Cloud Functions that trust the caller
Cloud Functions usually access data through the Admin SDK, so your rules don't protect them. A callable function that updates whatever document ID the request names, without checking the caller's identity and permissions, bypasses your rules. Review every function that writes data as if it were an API endpoint.
A safer baseline for Firestore rules
This example uses hypothetical users and projects collections. Any path without a matching rule is denied.
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
function isUser(uid) {
return request.auth != null && request.auth.uid == uid;
}
match /users/{uid} {
allow get: if isUser(uid);
allow create: if isUser(uid)
&& !request.resource.data.keys().hasAny(['role', 'plan']);
allow update: if isUser(uid)
&& request.resource.data.diff(resource.data).affectedKeys()
.hasOnly(['displayName', 'photoURL']);
}
match /projects/{projectId} {
allow read, delete: if isUser(resource.data.ownerId);
allow create: if isUser(request.resource.data.ownerId);
allow update: if isUser(resource.data.ownerId)
&& request.resource.data.ownerId == resource.data.ownerId;
}
}
}Rules are not filters. A query for projects must include where("ownerId", "==", uid), or Firestore rejects the whole query, since it could return documents the user can't read.
How to test Firebase Security Rules before you deploy
The console's Rules Playground handles one-off checks. For lasting coverage, write tests against the Firestore emulator with @firebase/rules-unit-testing. Each test seeds data with rules disabled, then acts as a specific user:
const testEnv = await initializeTestEnvironment({
projectId: "demo-rules-test",
firestore: { rules: readFileSync("firestore.rules", "utf8") },
});
await testEnv.withSecurityRulesDisabled(async (ctx) => {
await setDoc(doc(ctx.firestore(), "projects/p1"), { ownerId: "alice" });
});
const alice = testEnv.authenticatedContext("alice").firestore();
const bob = testEnv.authenticatedContext("bob").firestore();
await assertSucceeds(getDoc(doc(alice, "projects/p1")));
await assertFails(getDoc(doc(bob, "projects/p1")));
await assertFails(updateDoc(doc(alice, "projects/p1"), { ownerId: "bob" }));
await assertFails(updateDoc(doc(alice, "users/alice"), { role: "admin" }));Seed real data before asserting that it's hidden, because a failed read on an empty database proves little. Run the suite in CI with firebase emulators:exec --only firestore "npm test", next to the other gates in our guide to taking an AI-built prototype to production.
How to audit a live Firebase project
- Open the Rules tab for Firestore, Realtime Database and every Storage bucket. The console shows what is deployed, which may differ from your repository.
- Search for
if true,request.auth != nullwith no further condition, and{document=**}. - Confirm every collection the app uses, including subcollections, has its own rule.
- Find every rule that reads a role or plan, and confirm users can't write that field.
- Sign in as two test users and try to read and change the other user's documents and files from the browser's developer tools. Every attempt should fail.
- Read each Cloud Function that writes data and confirm it checks who is calling.
- Turn on App Check enforcement once your clients send tokens. Firebase says it prevents some abuse, not all, so it adds to your rules rather than replacing them.
Common mistakes when fixing Firebase rules
- Editing rules in the console and the CLI. A
firebase deployoverwrites console edits, so a console fix can disappear on the next release. Keep rules in the repository and deploy them withfirebase deploy --only firestore:rulesand--only storage. - Locking everything with
if false, watching the app break, then reopening it withif true. Fix one collection at a time, with a test for each. - Hiding data in the UI. If the rules allow a read, anyone can make it without your interface.
- Forgetting a product. A project with secure Firestore rules can still have an open Realtime Database or Storage bucket from an early prototype.
- Writing
get()lookups for everything. Each one is a billed read, and requests are limited to 10 document lookups (20 for batched writes and transactions). Custom claims avoid both.
When to get an outside review
These checks find configuration errors. They don't find logic flaws, such as an invite flow that lets a user join any team. If your app holds customer, payment or health data, or a launch or investor review is close, an independent pass is worth it. Our Security Audit for AI-Built Apps covers database access rules such as Firebase rules, alongside secrets, authentication, storage and hosting, with a prioritized fix list. Teams on Supabase can start with our Supabase row-level security guide instead.
Frequently asked questions
Is it safe to put the Firebase config in my front-end code?
Yes. The API key and project ID identify your project, and every Firebase web app ships them. That holds only if your rules are correct, because they are what limits the key.
Is test mode safe if nobody knows the app exists?
No. A deployed app is on the internet, and its project ID ships in the front-end code. Replace test-mode rules with real ones before the first deploy, not before the launch.
Can I let an AI tool write my Firebase rules?
It can draft them. Treat the result like authorization code: read every rule, check it against the seven mistakes above, and keep emulator tests in the repository so a later prompt can't reopen access.
Do Security Rules protect data my Cloud Functions read?
No. Functions that use the Admin SDK bypass rules, so each function has to check the caller's identity and permissions itself.