Securing a Firebase application
A Firebase application reads and writes Cloud Firestore, the Realtime Database and Cloud Storage from the browser or the phone through Firebase's client libraries. What a request may do is decided by Firebase Security Rules, which Firebase evaluates for each request, and by App Check, which checks that the request came from the real app.5 The API key in the app's configuration names the project for quota and billing, and once it is restricted to Firebase services it does not have to be kept secret.5
When a database or bucket is created, the console offers locked mode, which refuses every request, or test mode, which in Google's words "allows anyone to read and overwrite your data".8
Procedure
- Replace test mode rules before launch. Open Rules for each of Cloud Firestore, Realtime Database and Storage. A rule that allows every path with no condition, such as
allow read, write: if trueundermatch /{document=**}, or".read": truein the Realtime Database, gives that access to anyone who knows the project ID.1Prevents: Publicly exposed database - Tie each rule to the owner of the data. Compare the signed-in user's ID with an owner field. On update, check both the stored document and the new one, so a user cannot hand a document to someone else:1
Prevents: Insecure direct object referenceservice cloud.firestore { match /databases/{database}/documents { match /orders/{orderId} { 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; } } } - Exclude anonymous accounts where a rule needs a real user. A rule that asks only for
request.auth != nulladmits every signed-in user. Anonymous sign-in gives a temporary account to any visitor who has not signed up, so such a rule admits everyone.2 Where a rule must exclude anonymous users, testrequest.auth.token.firebase.sign_in_provider != 'anonymous', oremail_verifiedfor a confirmed address.3Prevents: Authorization - Give each user a folder in Storage. Store uploads under a path that contains the user's ID and match the path in the Storage rules. An open Storage rule also makes files uploaded through App Engine public, because the bucket is shared.1
Prevents: Public cloud storage bucketservice firebase.storage { match /b/{bucket}/o { match /user/{userId}/{fileName} { allow read, write: if request.auth.uid == userId; } } } - Write Realtime Database rules from the top down. Realtime Database rules cascade: a rule on a parent path applies to every child, and a rule on a child can add access but cannot remove it.1 Grant access at the deepest path that needs it, for example under
"$uid"with"auth.uid === $uid".Prevents: Insecure direct object reference - Keep the rules in one place and test them. A deploy from the Firebase CLI replaces the rules in the console with the files in the project folder.4 Edit them in one of the two only, keep the files in the repository, and run each change through the Rules Simulator in the console or the Local Emulator Suite before publishing.
- Check permissions in server code. The Admin SDK, Cloud Functions and the Firestore server client libraries are not bound by Security Rules; access from them is governed by Google Cloud IAM.14 A function that reads data for a user must check that user's rights in its own code.Prevents: Insecure direct object reference
- Restrict API keys to the APIs they serve. In the Google Cloud console under APIs & Services, Credentials, the keys named Browser key, Android key and iOS key (auto created by Firebase) should list only Firebase APIs. Use a separate, restricted key for Maps or Places, and never add the Generative Language API to a key that ships in the app.5Prevents: Denial of wallet
- Enforce App Check. Register an attestation provider (reCAPTCHA Enterprise on the web, Play Integrity on Android, App Attest or DeviceCheck on Apple platforms), watch the metrics, then open Security, App Check, expand each product and click Enforce. Enforcement takes up to 15 minutes to apply.6Prevents: Abuse of leaked API keys
- Turn on email enumeration protection. Under Authentication, Settings, User actions, select Email enumeration protection (recommended) and save. Projects created before 15 September 2023 have it off.7Prevents: Brute-force attack
- Lower the sign-in quota. Set the quota of the
identitytoolkit.googleapis.comendpoints in the Google Cloud console close to the app's normal traffic. Password guessing through the public key then hits the limit sooner; a sudden growth in users can cause sign-in errors until the quota is raised.5Prevents: Brute-force attack
Prompt for a coding agent
Read firestore.rules, database.rules.json and storage.rules in this project, and every place the app reads or writes Firestore, the Realtime Database or Cloud Storage. Replace any rule that allows access with no condition or with only request.auth != null. Write rules so that each user can read and change only documents and files whose owner field or path contains their uid, and keep fields that set ownership from changing on update. Add tests for the rules that run in the Firebase Local Emulator Suite. List every rule you changed and any collection whose owner you could not determine.
Notable incident
In July 2025, a post on 4chan gave access to images of users of Tea, a dating safety app, and 4chan users said the data came from a database hosted on Firebase; 404 Media found that the exposed Storage address matched one in Tea's Android app.9 Tea said a legacy storage system with data from before February 2024 had been accessed: about 72,000 images, including about 13,000 selfies and photo IDs from account checks.10 A second exposure, of more than one million direct messages, was reported days later.10
In SecFlash
On the Cloud page, SecFlash reports rules that let anyone read or write everything as SF-CLD-051, expired test mode rules as SF-CLD-052, anonymous sign-in as SF-CLD-053 and email enumeration protection turned off as SF-CLD-054. The Exposure page lists a Realtime Database address found in the site's scripts (SF-EXP-212), public storage buckets (SF-EXP-214), and the Firebase configuration among the public keys in the page or in a mobile build (SF-EXP-306, SF-EXP-613). A Firebase key in google-services.json or GoogleService-Info.plist shows as SF-SEC-729 on the Security page, with a reminder to confirm its restriction in Google Cloud.
See also
- Public cloud storage bucket
- Insecure direct object reference
- Abuse of leaked API keys
- Denial of wallet
- Authorization
- Security checklist before launch
- Category: Guides
References
- a b c d e Google (n.d.). "Avoid insecure rules". Firebase Security Rules documentation.
- back Google (n.d.). "Authenticate with Firebase Anonymously". Firebase documentation.
- back Google (n.d.). "Security Rules and Firebase Authentication". Firebase documentation.
- a b Google (n.d.). "Get started with Cloud Firestore Security Rules". Firebase documentation.
- a b c d Google (n.d.). "Learn about using and managing API keys for Firebase". Firebase documentation.
- back Google (n.d.). "Enable App Check enforcement". Firebase documentation.
- back Google (n.d.). "Enable or disable email enumeration protection". Identity Platform documentation.
- back Google (n.d.). "Get started with Cloud Firestore". Firebase documentation.
- back Engadget (July 2025). "Tea app suffers breach, exposing thousands of user images". Engadget.
- a b Willison, S. (26 July 2025). "Official statement from Tea on their data leak". Simon Willison's Weblog.
External links
- Manage and deploy Firebase Security Rules, Firebase documentation