Skip to content

Deploying Firebase Security Rules โ€‹

IMPORTANT: Security rules must be deployed to both DEV and PRODUCTION Firebase projects.


Prerequisites โ€‹

  1. Install Firebase CLI:

    bash
    npm install -g firebase-tools
  2. Login to Firebase:

    bash
    firebase 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:rules

Verify in Firebase Console:

  1. Go to https://console.firebase.google.com
  2. Select lantern-app-dev project
  3. Click "Firestore Database" โ†’ "Rules"
  4. Verify rules are active
  5. Click "Storage" โ†’ "Rules"
  6. 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:rules

Verify in Firebase Console (same steps as DEV, but for lantern-app-prod).


Test Security Rules โ€‹

Test Firestore Rules โ€‹

  1. Go to Firebase Console โ†’ Firestore Database โ†’ Rules
  2. Click "Rules Playground"
  3. 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 โ€‹

  1. Go to Firebase Console โ†’ Storage โ†’ Rules
  2. 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 โ€‹

CollectionReadCreateUpdateDelete
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 โ€‹

PathReadWriteNotes
/venues/{venueId}/{fileName}โœ… Anyoneโœ… Venue ownerMax 5MB, images only
/offers/{offerId}/{fileName}โœ… Anyoneโœ… MerchantMax 2MB, images only
/users/{userId}/{fileName}โŒ NEVERโŒ NEVERNo user photos allowed

CI/CD Integration โ€‹

To automatically deploy rules on merge to main:

  1. 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 }}
  2. Generate Firebase CI token:

    bash
    firebase login:ci
  3. Add 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() with merge: true instead of create()

Next Steps โ€‹

After deploying rules:

  1. โœ… Test auth flows (signup, login)
  2. โœ… Test profile creation and updates
  3. โœ… Verify encrypted data is stored correctly
  4. โœ… Test cross-device sync
  5. โœ… Security audit with Rules Playground

See also:

Built with VitePress