Guides / Firebase
Firebase Security Rules - checklist for AI-built apps
Firebase apps read and write data straight from the browser, so Security Rules are your real security. How to spot test-mode rules and lock down Firestore, Realtime Database and Storage.
In a Firebase app, the browser talks directly to Firestore, the Realtime Database and Cloud Storage. Your Firebase config (API key, project ID) is public by design: it's in every visitor's browser, and that's fine. What protects your data is Firebase Security Rules. AI tools and quick-start guides often leave those rules wide open.
1. You're not still in test mode
When you create a database in test mode, Firebase uses rules like this:
allow read, write: if request.time < timestamp.date(2026, 12, 18);
That means anyone can read and write everything until the date passes, and after that your app suddenly stops working. AI tools sometimes "fix" the breakage by extending the date or switching to if true. Neither is a fix.
Search your rules for if true and request.time <, and replace them with real conditions.
2. Firestore: scope each collection to its owner
A common safe pattern is that users can only read and write their own documents:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow read, write: if request.auth != null && request.auth.uid == userId;
}
match /orders/{orderId} {
allow read: if request.auth != null && resource.data.ownerId == request.auth.uid;
allow create: if request.auth != null && request.resource.data.ownerId == request.auth.uid;
}
}
}
Watch out for allow read: if request.auth != null on its own. It lets any signed-in user read every document in the collection.
3. Realtime Database: no public root
Realtime Database rules apply to everything below them. A rule like this at the top makes the whole database public:
{ "rules": { ".read": true, ".write": true } }
Start locked and open only what's needed, per user:
{
"rules": {
"users": {
"$uid": {
".read": "auth != null && auth.uid == $uid",
".write": "auth != null && auth.uid == $uid"
}
}
}
}
4. Storage: private files stay private
Cloud Storage has its own rules. Uploaded files such as invoices, IDs and profile documents should only be readable by their owner. Don't leave allow read, write: if true; in production.
5. Validate data, not just access
Rules can also check what's written: required fields, types, sizes. That stops users from giving themselves an isAdmin: true field, a common mistake when admin status is stored in a user's own document.
6. Admin actions go through the server
Anything an admin does (refunds, changing other users' roles) should run in a Cloud Function using the Admin SDK, which ignores rules, never from the browser with loosened rules.
7. App Check and API key restrictions
Restrict your Firebase API key to your domains in the Google Cloud console, and consider App Check to make it harder for scripts to call your backend directly. These help, but they don't replace good rules.
8. Test as a stranger
Use the Rules Playground in the Firebase console, or try reading your data while logged out. If anything private comes back, your rules need work.
Quick checklist
- No
if trueand no test-moderequest.timerules - Each collection or path scoped to
request.auth.uid - Signed-in doesn't mean allowed to read everything
- Storage rules lock private files to their owner
- Rules validate fields like roles and prices
- Admin actions run in Cloud Functions
- Tested as a logged-out visitor
Check your site in one minute
Scanora runs these checks for you: leaked keys, open files, headers, SSL, speed and AI-search readiness. Free, no signup.