Firebase security rules mistakes in AI-built apps (and how to fix them)
The most common Firebase mistake in AI-built apps is rules that check whether someone is signed in instead of whether they own the data: allow read, write: if request.auth != null lets any user (including anonymous sign-ins) read and overwrite everyone's documents. The other usual suspects are test-mode rules that stay open until a date, if true, users being able to write their own role field, and open Storage buckets. Your Firebase API key being public is normal. Security rules are what protect the data, so they have to be specific.
Firebase lets the browser talk to your database directly, and its docs are blunt about what that means: "Firebase Security Rules are the only safeguard blocking access for malicious users" (Firebase: Basic security rules). The web apiKey in your config isn't a secret, because it identifies your project rather than authorising access (Firebase: API keys). So everything depends on the rules.
Mistake 1: test-mode rules left in place
When you start a database in test mode, Firebase writes a rule that opens everything until a date about 30 days out. AI tools (and tired humans) often "fix" the expiry warning by pushing the date forward.
Until that date, anyone on the internet can read, change and delete all your data. Firebase lists open access as the first insecure pattern to avoid (Firebase: Avoid insecure rules).
Mistake 2: "signed in" instead of "owner"
This looks safe but isn't. Anyone can create an account in your app (or sign in anonymously, if that's enabled) and then read every user's documents. Firebase calls this out as "any authenticated user" access in its insecure rules guide.
Mistake 3: users can make themselves admin
If your app stores role: "admin" or plan: "pro" on the user's own document, and the rules let users update their own document freely, a user can just write role: "admin". Lock role fields so clients can't set them, and change roles from a trusted server (Admin SDK or Cloud Functions) or with custom claims.
Mistake 4: Storage and Realtime Database forgotten
Cloud Storage and Realtime Database have separate rules from Firestore. A tight Firestore setup doesn't help if Storage allows public reads of uploaded files, or Realtime Database has ".read": true at the root.
Mistake 5: no validation on writes
Even with ownership checks, rules that don't validate data let users write huge documents, unexpected fields or someone else's ownerId. Check request.resource.data on create and update.
A safer starting point (Firestore)
Adapt collection and field names to your app, then test with the Rules Playground in the console or the emulator before deploying (Firebase docs).
Realtime Database and Storage: safer starting points
Realtime Database rules are JSON. This example lets each signed-in user read and write only their own node under /users. Everything else stays denied because no rule grants it (Firebase: Realtime Database rules):
Cloud Storage rules look like Firestore's. This example keeps each user's uploads private, and limits writes to images under 5 MB so nobody can use your bucket as free file hosting (Firebase: Storage rules):
If some files really should be public (for example product images), put them under a separate path with its own read rule, and keep writes restricted.
Copy-paste fix prompts
Extra protection worth turning on
- App Check helps make sure requests come from your real app, which cuts down scripted abuse (Firebase App Check). It complements rules, it doesn't replace them.
- Restrict your API keys to the APIs they need, and don't enable billable non-Firebase APIs on the key that ships in your app (Firebase: API keys).
- Go through Firebase's own security checklist before launch.
FAQ
Is it safe that my Firebase apiKey is visible in my website code?
Yes. The Firebase web API key identifies your project and isn't used to authorise access to your data. Security Rules do that. Do restrict the key to the APIs you need, and don't put billable non-Firebase APIs on it.
Is 'allow read, write: if request.auth != null' secure?
Usually not. It lets any signed-in user, including anyone who just created an account or signed in anonymously, read and change every document. Rules should check that the user owns the specific document, for example request.auth.uid == userId.
My Firebase test mode is expiring. Should I extend the date?
No. Extending the date keeps your whole database open to the internet until then. Replace the test-mode rule with real rules based on ownership, and test them in the Rules Playground or emulator.
Do Firestore rules also protect Cloud Storage?
No. Cloud Storage and Realtime Database each have their own separate security rules. Review all three if your app uses them.
How do I test my Firebase security rules?
Use the Rules Playground in the Firebase console or the local Firebase emulator. Simulate a logged-out visitor, user A and user B, and confirm that each can only do what you intend.
Sources
- Firebase: Basic security rules
- Firebase: Realtime Database security rules
- Firebase: Cloud Storage security rules
- Firebase: Avoid insecure rules
- Firebase: API keys
- Firebase: App Check
- Firebase security checklist
This guide is general information, not a security audit. Platforms change, so check the linked docs for current details. Spotted something out of date? Email hello@milokits.com.