Deploying Firebase Security Rules โ
IMPORTANT: Security rules must be deployed to both DEV and PRODUCTION Firebase projects.
Prerequisites โ
Install Firebase CLI:
bashnpm install -g firebase-toolsLogin to Firebase:
bashfirebase login
Deploy to DEV Environment โ
bash
# Select DEV project
firebase use lantern-app-dev
# Deploy Firestore rules only
firebase deploy --only firestore:rules
# Deploy Storage rules only
firebase deploy --only storage:rules
# Deploy both at once
firebase deploy --only firestore:rules,storage:rulesVerify in Firebase Console:
- Go to https://console.firebase.google.com
- Select
lantern-app-devproject - Click "Firestore Database" โ "Rules"
- Verify rules are active
- Click "Storage" โ "Rules"
- Verify storage rules are active
Deploy to PRODUCTION Environment โ
โ ๏ธ WARNING: Production deployment affects live users!
bash
# Select PRODUCTION project
firebase use lantern-app-prod
# Deploy rules to production
firebase deploy --only firestore:rules,storage:rulesVerify in Firebase Console (same steps as DEV, but for lantern-app-prod).
Test Security Rules โ
Test Firestore Rules โ
- Go to Firebase Console โ Firestore Database โ Rules
- Click "Rules Playground"
- Test scenarios:
# Test: User can read their own profile Type: get Location: /users/{your-user-id} Auth: Authenticated as {your-user-id} Expected: ALLOW # Test: User cannot read another user's encrypted data Type: get Location: /users/{other-user-id} Auth: Authenticated as {your-user-id} Expected: ALLOW (but encrypted fields are gibberish) # Test: Unauthenticated users cannot read anything Type: get Location: /users/{any-user-id} Auth: Unauthenticated Expected: DENY # Test: User can only update their own profile Type: update Location: /users/{your-user-id} Auth: Authenticated as {your-user-id} Expected: ALLOW # Test: User cannot update another user's profile Type: update Location: /users/{other-user-id} Auth: Authenticated as {your-user-id} Expected: DENY
Test Storage Rules โ
- Go to Firebase Console โ Storage โ Rules
- Test upload scenarios:
# Test: Venue owner can upload venue photo Operation: create Path: /venues/venue-123/photo.jpg Auth: Authenticated Expected: ALLOW # Test: Cannot upload user profile photo Operation: create Path: /users/user-123/profile.jpg Auth: Authenticated Expected: DENY (privacy policy) # Test: File size limits enforced Operation: create Path: /venues/venue-123/big-photo.jpg File size: 10MB Expected: DENY (exceeds 5MB limit)
Firestore Rules Summary โ
| Collection | Read | Create | Update | Delete |
|---|---|---|---|---|
users | โ Authenticated (own + public fields) | โ Owner only | โ Owner only | โ Owner only |
checkins | โ Authenticated | โ Owner only | โ Owner only | โ Owner only |
waves | โ Sender or recipient | โ Authenticated | โ Sender/recipient (status only) | โ Sender or recipient |
chats | โ Participants only | โ Participants only | โ Participants (metadata only) | โ Never |
offers | โ Authenticated | โ Merchant only | โ Merchant only | โ Merchant only |
venues | โ Authenticated | โ Verified owners only | โ Owner only | โ Never |
Storage Rules Summary โ
| Path | Read | Write | Notes |
|---|---|---|---|
/venues/{venueId}/{fileName} | โ Anyone | โ Venue owner | Max 5MB, images only |
/offers/{offerId}/{fileName} | โ Anyone | โ Merchant | Max 2MB, images only |
/users/{userId}/{fileName} | โ NEVER | โ NEVER | No user photos allowed |
CI/CD Integration โ
To automatically deploy rules on merge to main:
Create GitHub Actions workflow:
yaml# .github/workflows/deploy-firebase-rules.yml name: Deploy Firebase Rules on: push: branches: - main paths: - 'firestore.rules' - 'storage.rules' jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 - run: npm install -g firebase-tools - run: firebase deploy --only firestore:rules,storage:rules --token ${{ secrets.FIREBASE_TOKEN }}Generate Firebase CI token:
bashfirebase login:ciAdd token to GitHub Secrets:
- Go to repo Settings โ Secrets
- Add
FIREBASE_TOKEN
Troubleshooting โ
Error: "Permission denied"
- Check that user is authenticated
- Verify user is accessing their own data
- Check Firestore Rules Playground for specific error
Error: "Missing required fields"
- Ensure all required fields are included in write operations
- Check field types match schema (e.g., timestamp vs string)
Error: "Resource already exists"
- User trying to create profile that already exists
- Use
set()withmerge: trueinstead ofcreate()
Next Steps โ
After deploying rules:
- โ Test auth flows (signup, login)
- โ Test profile creation and updates
- โ Verify encrypted data is stored correctly
- โ Test cross-device sync
- โ Security audit with Rules Playground
See also:
- Firestore Security Rules Reference
- Storage Security Rules Reference
- SECURITY.md for security policies