Overview
Firestore Security Rules control read/write access to all collections. The rules are defined infirestore.rules and deployed alongside the database schema.
File: firestore.rules
Helper Functions
Three utility functions are defined at the root level for reusability (firestore.rules:7-19):
isAdmin()
Checks if the authenticated user has admin role.- User must be authenticated (
request.auth != null) - User document must exist in
userscollection - User’s
rolefield must equal'admin'
isOwner(userId)
Checks if the authenticated user matches the specified user ID.userId: The user ID to compare against (usually from document path or data)
isAuthenticated()
Checks if any user is authenticated.Collection Rules
Users
Path:users/{userId} (firestore.rules:24-30)
- Read: User can read their own profile OR admin
- Write: User can update their own profile OR admin
- Addresses subcollection: Same rules as parent
- Customer updates profile information
- Customer manages delivery addresses
- Admin views/edits any user account
Products
Path:products/{productId} (firestore.rules:33-50)
- Read: Public (anyone can view products)
- Create/Delete: Admin only
- Update: Two rules (evaluated with OR logic):
- Admin: Can change any field (prices, descriptions, images, etc.)
- Authenticated Users: Can ONLY modify
stock,combinations, andupdatedAt
diff() method compares the incoming document with the existing one:
.hasOnly(['stock', 'combinations', 'updatedAt']) ensures no other fields were modified.
Categories & Brands
Paths:categories/{catId}, brands/{brandId} (firestore.rules:53-61)
- Read: Public
- Write: Admin only
Orders
Path:orders/{orderId} (firestore.rules:64-72)
- Must have
total > 0(prevents empty orders) - Must have
itemsarray (prevents malformed orders) - Anyone can create (Cloud Functions create orders on behalf of users)
- Admin can read all orders
- Authenticated users can read their own orders (
userId == request.auth.uid) - Guest checkout orders are readable by anyone if
userId == 'GUEST_MP'
- Admin only (prevents customers from modifying order status)
-
Guest Orders: The
userId == 'GUEST_MP'rule allows unauthenticated checkout via MercadoPago. This is acceptable because:- Order details are sent via email
- Payment is verified server-side
- Sensitive operations (status changes) require admin
-
Creation Validation: The
total > 0check prevents spam:
Remissions
Path:remissions/{remissionId} (firestore.rules:75-80)
- All operations: Admin only
functions/mercadopago.js:286)
Warranties
Path:warranties/{warrantyId} (firestore.rules:83-87)
- Must be authenticated
- Can only create warranty for yourself (
userId == request.auth.uid)
- User can read their own warranties
- Admin can read all warranties
- Admin only (prevents customers from changing status/resolution)
Internal Operations
Paths:warranty_inventory, suppliers, purchases (firestore.rules:90-92)
Cart (Legacy)
Path:carts/{cartItemId} (firestore.rules:95-98)
- Users can only manage their own cart items
- Must be authenticated
Configuration
Path:config/{configDoc} (firestore.rules:101-104)
- Read: Public (needed for shipping calculator, site settings)
- Write: Admin only
config/shipping: Shipping rates and thresholdsconfig/merchant_feed_cache: Google feed cacheconfig/site_settings: General settings
Accounting & Treasury
Paths:expenses, expenses_trash, accounts, payables (firestore.rules:107-110)
expenses: Income and expense transactionsexpenses_trash: Soft-deleted expenses (audit trail)accounts: Treasury accounts (banks, payment gateways)payables: Accounts payable tracking
Chats (WhatsApp)
Path:chats/{chatId} (firestore.rules:113-118)
whatsappWebhook function with admin SDK (functions/whatsapp.js:158)
Security Patterns
Pattern 1: Public Read, Admin Write
products, categories, brands, config
Pattern 2: Owner Access
users, warranties, carts
Pattern 3: Admin Only
remissions, expenses, accounts, suppliers, chats
Pattern 4: Restricted Field Updates
products
Pattern 5: Creation Validation
orders
Data Validation
While security rules focus on access control, they can also validate data structure:Testing Rules
Firebase Emulator
Unit Tests
Createfirestore.rules.test.js:
Deployment
Best Practices
1. Principle of Least Privilege
2. Validate on Write
3. Use Helper Functions
4. Server-Side Validation
Rules are not a replacement for server-side validation:5. Monitor Rule Violations
Set up alerts in Firebase Console:- Alerts → Create Alert
- Metric:
firestore.googleapis.com/document/read_count - Filter:
response_code = PERMISSION_DENIED - Threshold: >10 per hour
Common Pitfalls
Pitfall 1: Forgetting Server SDK Bypass
Pitfall 2: Guest Checkout Security
Pitfall 3: Subcollection Access
Subcollections don’t inherit parent rules:Security Checklist
- Admin role checked via Firestore document (not custom claims)
- Financial collections restricted to admin only
- Users can only access their own orders/warranties
- Product prices protected from client manipulation
- Order creation validated (total > 0, items exist)
- Remissions can only be created server-side
- Public read access limited to catalog data
- WhatsApp chats restricted to admin panel