AleannLab

Firebase Rules Left Open: How to Check and Fix Firestore and Storage Rules

AleannLab team3 min read

In Firebase the browser talks to the database directly, and security rules are the only thing deciding what each request may do. Rules like allow read, write: if true make the whole database public. Rules like if request.auth != null make it public to anyone who signs up. Real protection compares the document's owner to request.auth.uid, per collection, per operation.

On this page
  1. What open rules look like
  2. The Firebase config is not the secret
  3. Rules that check ownership
  4. Do not let users edit fields that grant power
  5. Storage has its own rules
  6. Test before you deploy
  7. Keep rules in the repo

Firebase projects start in one of two modes. "Test mode" opens everything for 30 days so you can build quickly. AI tools and tutorials start there, and then one of two things happens: the app launches with open rules, or the 30 days run out, the app breaks, and someone pastes the first rule that makes it work again.

What open rules look like

Fully open:

code
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

Open with an expiry date, the test-mode default:

code
allow read, write: if request.time < timestamp.date(2027, 1, 15);

When the date passes, a common "fix" is to move the date forward by a year.

Open to any account:

code
match /{document=**} {
  allow read, write: if request.auth != null;
}

This one looks secure and passes a quick review. But if sign-up is open, anyone can create an account in ten seconds and then read and write every document of every user. Anonymous auth makes it even easier.

The Firebase config is not the secret

People see apiKey in the Firebase config in their frontend and assume that is the leak. It is not. That config identifies the project and is meant to be public. With it, anyone can call your database from their own script, and that is by design. What they can do after that is decided only by your rules.

Rules that check ownership

Write a rule per collection. Start from "deny" (which is what happens when no rule matches) and add what the app needs:

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

    match /users/{userId} {
      allow read, update: if request.auth != null && request.auth.uid == userId;
      allow create: if request.auth != null && request.auth.uid == userId;
    }

    match /orders/{orderId} {
      allow read: if request.auth != null
                  && resource.data.userId == request.auth.uid;
      allow create: if request.auth != null
                    && request.resource.data.userId == request.auth.uid;
      // no update or delete from the client
    }
  }
}

Two variables are easy to mix up. resource.data is the document as it is stored now. request.resource.data is the document the client wants to write. A create rule that does not check request.resource.data.userId lets a user create orders in someone else's name.

Do not let users edit fields that grant power

If users/{userId} contains role: "admin" or plan: "pro" and the user may update their own document, they can promote themselves. Restrict which fields an update may touch:

code
allow update: if request.auth.uid == userId
  && request.resource.data.diff(resource.data)
       .affectedKeys().hasOnly(['displayName', 'photoURL']);

Roles are safer in custom claims, which only the Admin SDK on a server can set, and which rules read as request.auth.token.admin == true.

Storage has its own rules

Cloud Storage for Firebase uses a separate rules file with the same failure modes:

code
rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /users/{userId}/{allPaths=**} {
      allow read: if request.auth != null && request.auth.uid == userId;
      allow write: if request.auth != null && request.auth.uid == userId
                   && request.resource.size < 10 * 1024 * 1024
                   && request.resource.contentType.matches('image/.*');
    }
  }
}

Realtime Database has a third format (JSON). If the project uses it, check .read and .write there too. A project can have locked-down Firestore rules and a wide-open Realtime Database nobody remembers creating.

Test before you deploy

The Rules Playground in the Firebase console lets you simulate a request as a signed-out user, or as a specific UID, against the rules you are editing. Use it for a quick check.

For anything you plan to maintain, write tests against the local emulator:

JavaScript
import { initializeTestEnvironment, assertFails, assertSucceeds }
  from '@firebase/rules-unit-testing';
import { doc, getDoc } from 'firebase/firestore';
import { readFileSync } from 'node:fs';

const env = await initializeTestEnvironment({
  projectId: 'demo-app',
  firestore: { rules: readFileSync('firestore.rules', 'utf8') },
});

const alice = env.authenticatedContext('alice').firestore();
const anon = env.unauthenticatedContext().firestore();

await assertSucceeds(getDoc(doc(alice, 'users/alice')));
await assertFails(getDoc(doc(alice, 'users/bob')));
await assertFails(getDoc(doc(anon, 'users/alice')));

Run them in CI. Rules are code, and an agent editing them next month should not be able to reopen the database without a failing test.

Keep rules in the repo

Rules edited in the console have no review and no history you will look at. Keep firestore.rules and storage.rules in git and deploy with firebase deploy --only firestore:rules,storage.

If you are on Supabase instead, the same class of problem is covered in our post on Supabase RLS mistakes. Database rules are check 2 in our security checklist for vibe-coded apps, and reviewing them is part of our app rescue and security audit.

All articles →

Ready to discuss your project with us?

idea
*
*
*
I'm interested in...
Web Development
App Development
UX/UI Design
DevOps
Artificial Intelligence And Machine Learning
QA Testing
Mobile Development
Consultation
OutSource
Project budget (USD)
≤ $5K
$10K-$20K
$30K-$40K
$50K-$60K
>$100K
Upload file