Firebase Firestore Security: Production Security Rules Guide

Firebase Firestore Security: Production Security Rules Guide

The Risks of Default Firestore Rules

When starting a new Firebase project, developers often toggle Test Mode (allow read, write: if true;). Leaving this rule active in production opens your database to data leaks and unauthorized mutations.


Core Security Rules Principles

  1. Default Deny: Block all access by default.
  2. Authentication Checks: Verify user credentials via request.auth.
  3. Ownership Control: Restrict document writes to document owners.
  4. Schema Validation: Enforce required fields and strict data types.

Production-Ready Rules Snippet

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    
    function isAuthenticated() {
      return request.auth != null;
    }
    
    function isOwner(userId) {
      return isAuthenticated() && request.auth.uid == userId;
    }

    match /blog/{postId} {
      allow read: if resource.data.published == true;
      allow write: if request.auth.token.admin == true;
    }
  }
}

Conclusion

Firestore Security Rules serve as your primary backend firewall. Always test security rules with the Firebase Local Emulator Suite before deploying.