Nexus Void Research
Firebase, Firestore, Firebase Security Rules, App Check, Realtime Database, Cloud Storage, How-to
How to Secure Firebase: Security Rules, App Check and Keys
How to secure a Firebase database: replace test-mode rules, scope rules to request.auth.uid, validate writes, test in the emulator, enforce App Check.
To secure a Firebase database, replace test-mode rules with Firebase Security Rules that require authentication and tie each document to request.auth.uid, validate the data being written, and test the rules with the emulator before deploying. Your Firebase API key is not a secret, so security comes from rules, App Check, restricted API keys and keeping the Admin SDK service account on the server.
Firebase apps talk to Firestore, Realtime Database and Cloud Storage directly from the browser or the phone. That means the rules you deploy are your authorization layer: if they allow it, anyone with your project ID can do it, from a script, without your app. The Firebase Security Rules documentation is the reference; this guide covers the rules and settings that prevent the most common Firebase data leaks. The principle is the same one the OWASP Authorization Cheat Sheet sets out for any application: deny by default, and check on every request that this user may access this record. Rules that skip that check fall under CWE-862: Missing Authorization.
Is the Firebase API key a secret?
No. The apiKey in your Firebase config identifies your project to Google's services; it is designed to be included in client apps, and hiding it does not protect anything. What protects your data is:
Security Rules for Firestore, Realtime Database and Storage.
App Check, which helps ensure requests come from your genuine app.
API key restrictions in the Google Cloud console, so the key only works for the APIs and apps you intend.
The things that are secret: the Admin SDK service account key (a JSON file), any server-side tokens, and third-party API keys your Cloud Functions use.
Step 1: Get rid of test-mode rules
New projects often start with rules like these:
Both let anyone on the internet read and change your whole database. Replace them before you have real users.
Step 2: Write Firestore rules tied to the signed-in user
Lock everything by default, then open exactly what each collection needs:
Rules are not filters: a query must only ask for documents the rules allow, or the whole query is rejected. Query with where("ownerId", "==", uid) to match the rule above.
For shared data, check membership, for example with get() on a team document or a custom claim on the user's token. Missing ownership checks here are the Firebase version of IDOR.
Step 3: Validate what gets written
Authentication alone lets a user write anything into their own document, including fields like role: "admin" or credits: 999999. Validate the shape of every write:
affectedKeys().hasOnly([...]) blocks users from changing fields you did not list. Set sensitive fields like roles, balances and subscription status only from trusted server code.
Step 4: Secure Realtime Database and Storage
Realtime Database uses JSON rules with the same idea:
In Realtime Database, a .read or .write granted at a parent path cannot be revoked at a child path, so keep broad grants off the root.
Cloud Storage rules restrict files to their owner and validate uploads:
Step 5: Test rules before you deploy
Use the Firebase Emulator Suite and the @firebase/rules-unit-testing library to run tests that act as different users:
An unauthenticated client cannot read
users.User A cannot read or update user B's document.
A user cannot set
roleon their own profile.An oversized or non-image upload is rejected.
Run these in CI so a rules change cannot ship without passing.
Step 6: Turn on App Check and restrict API keys
App Check verifies requests come from your registered app, using Play Integrity on Android, App Attest or DeviceCheck on Apple platforms, and reCAPTCHA Enterprise on the web. Enforce it for Firestore, Storage, Realtime Database and Cloud Functions after monitoring for a while. It reduces abuse; it does not replace rules.
Restrict the API key in the Google Cloud console to the APIs your app uses, and to your app's bundle IDs, package names or website referrers.
Step 7: Keep the Admin SDK and Cloud Functions safe
The Admin SDK bypasses Security Rules. Use it only in trusted server code. Never ship the service account JSON in an app or commit it; on Google Cloud, prefer the runtime's default credentials over a downloaded key.
Check auth in every callable or HTTP function. In callable functions, read the caller from
request.authand reject unauthenticated calls; then check authorization for the specific resource.Validate input in functions exactly as in any API.
Store third-party keys in Secret Manager, not in function source. See how to store API keys securely.
Step 8: Harden Authentication and billing
Turn on email enumeration protection and keep authorized domains to your real domains.
Disable sign-in providers you do not use; consider MFA for sensitive apps.
Set budget alerts in Google Cloud, so abuse of an open endpoint shows up as a cost alert before a large bill.
Firebase security checklist
Check | Done when |
|---|---|
No test-mode rules | No |
Owner-scoped rules | Every collection checks |
Writes validated | Allowed fields listed with |
Sensitive fields server-only | Roles, balances, plans written only by trusted code |
Storage rules | Per-user paths; size and content-type limits |
Rules tested in CI | Emulator tests for cross-user access pass |
App Check enforced | Enabled for every Firebase product you use |
Admin credentials protected | Service account key never in apps or repositories |
Functions check auth | Every callable or HTTP function verifies the caller |
How does ARGUS help secure Firebase apps?
Secret scanning catches service account JSON files and third-party keys committed to your repository, filters out documentation placeholders, test fixtures and publishable keys, and marks real secrets Live, Rotated or Test when AI scanning is on.
AI business logic analysis looks for IDOR, privilege escalation and authentication bypass in your Cloud Functions and backend code written in JavaScript, TypeScript or Python.
Pull request review flags risky changes before merge, and Assign to Agent opens a fix pull request for you to review and merge.
Limits: ARGUS analyses the code in your repository; it does not evaluate deployed Firebase Security Rules or test your project's live endpoints, so keep emulator tests for your rules. See ARGUS secret scanning, or scan a repository free. For more on the risks of apps built quickly with AI tools on Firebase and Supabase, see vibe coding security.
Frequently asked questions
How do I secure my Firebase database?
Replace test-mode rules with Security Rules that require authentication and check request.auth.uid against the document's owner, validate every write's fields and types, deny everything else by default, test rules with the emulator in CI, enforce App Check, restrict your API key and keep the Admin SDK on the server.
Is it safe to expose the Firebase API key?
Yes. The Firebase API key identifies your project and is meant to be in client apps; it does not grant access by itself. Your data is protected by Security Rules and App Check. Do restrict the key to your APIs and apps in the Google Cloud console, and never expose service account keys.
What are Firebase test mode rules and why are they dangerous?
Test mode rules allow anyone to read and write your database until a set date, or forever with if true. Anyone who finds your project ID can then read, change or delete all data from a script. Replace them with authenticated, owner-scoped rules before real users arrive.
How do I stop users from changing their own role in Firestore?
Restrict which fields a user can update with request.resource.data.diff(resource.data).affectedKeys().hasOnly([...]), leaving out role, plan and balance fields. Set those fields only from trusted server code, such as Cloud Functions using the Admin SDK, or use custom claims on the user's token.
Does App Check replace Security Rules?
No. App Check helps ensure requests come from your genuine app, which cuts abuse from scripts. It does not decide which user may access which data. You still need Security Rules for authorization; App Check is an extra layer on top.
How do I test Firebase Security Rules?
Run the Firebase Emulator Suite and write tests with @firebase/rules-unit-testing that act as anonymous, owner and non-owner users, asserting which reads and writes succeed or fail. Run them in CI so any rules change has to pass before deployment.
Does the Firebase Admin SDK bypass Security Rules?
Yes. The Admin SDK has full access and ignores Security Rules, which is why it must only run in trusted server environments such as Cloud Functions or your backend. Never put its service account key in a client app or a repository.
How do I secure Firebase Storage uploads?
Write Storage rules that restrict each user to their own path, require authentication, and validate uploads by size and content type with request.resource.size and request.resource.contentType. Serve private files only to their owner and avoid public buckets for user data.
DATA SOURCES
Firebase Security Rules: https://firebase.google.com/docs/rules ; OWASP Authorization Cheat Sheet ; CWE-862