>_ last-mile
guide

Firebase "insecure rules" email: fix your Firestore rules before launch

Firebase emails you when anyone can read or write your whole database. Here is what the warning means, the two rule patterns AI builders leave behind, and a safe fix you can test in ten minutes.

The email reads something like: "[Firebase] Your Cloud Firestore database has insecure rules. We've detected the following issue(s) with your security rules: any user can read your entire database, any user can write to your entire database." If your app is live, take it seriously. Your Firebase web config, including the API key, ships to every browser by design, so the only thing standing between a stranger and your data is your Security Rules.

What triggers it

Almost always one of these, usually left over from development or written by an AI builder to make an error go away:

// Open access: anyone, signed in or not
match /{document=**} {
  allow read, write: if true;
}

// Any signed-in user can read and write everything
match /{document=**} {
  allow read, write: if request.auth != null;
}

Test-mode rules (allow read, write: if request.time < timestamp.date(...)) are the same as the first one until the date passes. Then every client request starts failing, which is when people paste in if true to "fix" it.

The second pattern feels safe because it checks auth, but anyone can create an account in your app. Per Firebase's own guide, if a rule includes auth != null, confirm you really want every logged-in user to have that access.

The fix: rules per collection, scoped to the owner

Drop the global /{document=**} wildcard. Name each collection and say who can do what. A typical shape for user-owned data plus a public catalog:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {

    // Each user reads and writes only their own profile
    match /users/{userId} {
      allow read, write: if request.auth != null && request.auth.uid == userId;
    }

    // Owned documents: owner only, and ownership can't be changed
    match /notes/{noteId} {
      allow read, delete: if request.auth != null && request.auth.uid == resource.data.owner;
      allow create: if request.auth != null && request.auth.uid == request.resource.data.owner;
      allow update: if request.auth != null
                    && request.auth.uid == resource.data.owner
                    && request.resource.data.owner == resource.data.owner;
    }

    // Public catalog: anyone reads, nobody writes from the client
    match /products/{productId} {
      allow read: if true;
      allow write: if false;
    }
  }
}

Anything not matched is denied. That's what you want: new collections stay closed until you write rules for them.

Three things the rules must not trust

  • Paid status or roles written by the client. If the browser can set isPro: true or role: "admin" on its own user document, it will. Write those fields only from server code (Cloud Functions or a webhook using the Admin SDK) and block the client from changing them, for example with request.resource.data.diff(resource.data).affectedKeys().hasOnly(['displayName', 'photoURL']) on update.
  • Email domains without verification. request.auth.token.email.endsWith('@yourco.com') lets anyone sign up with that address unverified. Also require request.auth.token.email_verified == true.
  • Your server code being covered by rules. The Admin SDK and server client libraries bypass Security Rules entirely. That's fine for trusted code, but it means an unprotected API route or callable function that writes to Firestore is a hole your rules can't close. Check auth in the function itself.

Don't forget Storage

Cloud Storage for Firebase has its own rules file with the same failure modes. Uploaded avatars, invoices and exports are often world-readable by path. Scope them the same way, for example match /users/{userId}/{allPaths=**} with request.auth.uid == userId.

Test before you deploy

  1. Rules Playground. In the Firebase console, open Firestore, then Rules, then Rules playground. Simulate a read on another user's document as an unauthenticated user, then as a signed-in user with a different UID. Both should be denied.
  2. Emulator tests. If your project uses the Firebase CLI, run the Firestore emulator and write a few tests with @firebase/rules-unit-testing: owner can read, stranger can't, client can't set isPro.
  3. Two real accounts. In the live app, sign in as user A, copy a document ID, sign in as user B in a private window, and try to load it. You should get a permission error, not the data.
  4. Watch your app after deploying. Tighter rules will break any screen that relied on open access. Fix those queries by adding a where('owner', '==', uid) filter that matches the rule, not by loosening the rule. Firestore rejects a query if it could return documents the rule wouldn't allow.

Prompt your AI builder safely

A prompt that works in Cursor, Claude Code, Lovable or Bolt: "Firebase says my Firestore rules are insecure. Remove the global /{document=**} rule. List every collection the app uses and write per-collection rules: owner-only by request.auth.uid for user data, read-only for public data, deny everything else. Clients must not be able to change ownership, roles or paid status. Do not use allow read, write: if true or if request.auth != null on its own. Show me the rules and explain each match before deploying."

Then run the playground and two-account tests above. If the agent's answer to a later permission error is to loosen a rule, push back.

Bottom line

The insecure-rules email means the database is effectively public. Replace the wildcard with per-collection, owner-scoped rules, keep money and role fields server-only, and prove it with a stranger account before launch.

Owner-scoped access is LM-06 and LM-09 in the free Last Mile Rules, drop-in rules that stop Cursor, Claude Code or Codex loosening your database rules to make an error go away, each with a verify step. Run your live URL through the free check, or get all 110 rules.

Related guides


Make your coding agent enforce this

The 10 core Last Mile Rules are free: drop-in AGENTS.md, CLAUDE.md and Cursor rules that stop your agent shipping these mistakes, each with a Verify step.

Get launch notes

~/newsletter

$ subscribe --launch-notes # new checks, prompts and shipping tips for vibe coders. No spam.