HIGH cryptographic failuresfirestore

Cryptographic Failures in Firestore

How Cryptographic Failures Manifests in Firestore

Cryptographic failures in Firestore typically stem from misconfigurations in security rules, client SDK usage, and misunderstanding Google's shared responsibility model. Firestore encrypts data at rest by default using Google-managed keys, and in transit via TLS. However, developers often introduce vulnerabilities by:

  • Overly permissive security rules: Rules that allow unauthenticated reads or writes to sensitive collections, effectively bypassing application-layer access control. For example, a rule like match /users/{userId} { allow read: if true; } exposes all user documents publicly.
  • Missing transport encryption enforcement: While Firestore requires TLS, custom backend proxies or misconfigured client SDKs might fall back to unencrypted connections if not properly validated. Some developers disable SSL verification in development, then inadvertently deploy those settings.
  • Storing sensitive data without additional encryption: Relying solely on Google's at-rest encryption may not satisfy compliance regimes (e.g., PCI-DSS, HIPAA) that require customer-managed keys (CMK) or field-level encryption for highly sensitive data like SSNs or credit card numbers.
  • Improper handling of Firestore security rule context: Rules that fail to validate request.auth or use it incorrectly can lead to horizontal privilege escalation (BOLA/IDOR), a cryptographic-adjacent failure where cryptographic identity tokens are not properly enforced.

A common attack pattern involves enumerating document IDs in a predictable path (e.g., /users/123, /users/124) when rules only check existence but not ownership. Another is using Firestore's list operations on subcollections when rules grant collection-level access without per-document checks.

Firestore-Specific Detection

Detecting cryptographic failures in Firestore requires analyzing both the security rule configuration and runtime behavior. middleBrick's scan assesses this through its Encryption and Data Exposure checks:

  • Security Rule Analysis: The scanner fetches Firestore rules (if exposed via .settings/rules or similar endpoints) and looks for patterns like allow read: if false; (overly restrictive) or allow read: if true; (overly permissive). It also checks for missing request.auth != null checks on sensitive paths.
  • TLS Enforcement: middleBrick tests whether the endpoint enforces HTTPS and validates certificates. It attempts connections without TLS to see if the server redirects or rejects.
  • Data Exposure Testing: By sending unauthenticated requests to likely collection paths (e.g., /users, /payments), the scanner checks if data is returned. It then scans responses for PII patterns (SSNs, credit card numbers) and API keys, flagging unencrypted sensitive data exposure.
  • OpenAPI/Swagger Correlation: If an OpenAPI spec is provided, middleBrick cross-references defined schemas with actual responses. A field marked as type: string, format: password returned in clear text indicates a failure.

Example scan command using the CLI:

middlebrick scan https://your-app.firebaseio.com

The report will highlight cryptographic issues under the Encryption and Data Exposure categories, with severity based on data sensitivity and exposure scope.

Firestore-Specific Remediation

Remediation focuses on tightening security rules, enforcing encryption in transit, and applying field-level encryption for regulated data.

1. Harden Security Rules

Replace permissive rules with least-privilege checks. Ensure every read/write operation validates user identity and ownership:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    // User can only access their own document
    match /users/{userId} {
      allow read, write: if request.auth != null && request.auth.uid == userId;
    }
    // Subcollections inherit parent rules unless overridden
    match /users/{userId}/orders/{orderId} {
      allow read, write: if request.auth != null && request.auth.uid == userId;
    }
    // Public data only
    match /public/{document=**} {
      allow read: if true;
      allow write: if false;
    }
  }
}

Test rules locally with the Firebase Emulator Suite before deploying.

2. Enforce TLS and Certificate Validation

In client SDKs, ensure SSL verification is enabled. For Node.js:

const admin = require('firebase-admin');
admin.initializeApp({
  credential: admin.credential.applicationDefault(),
  databaseURL: 'https://your-app.firebaseio.com'
});
// The SDK enforces TLS by default; do not set {
//   ssl: false
// } in any custom agent configuration.

3. Apply Field-Level Encryption for Sensitive Data

For data requiring customer-managed encryption, encrypt on the client before storage. Use a library like node-forge or Google Cloud KMS:

const { encrypt } = require('./crypto-utils'); // Your AES-GCM helper

async function saveSensitiveData(userId, ssn) {
  const encrypted = await encrypt(ssn, process.env.DATA_KEY);
  await admin.firestore().collection('users').doc(userId).update({
    ssn_encrypted: encrypted
  });
}

Store only the ciphertext and initialization vector (IV) in Firestore. Decryption occurs client-side after retrieval.

4. Enable App Check

To prevent unauthorized API access (e.g., from compromised apps), enable Firebase App Check with a provider like reCAPTCHA Enterprise or DeviceCheck. This adds a per-device attestation layer before Firestore requests are accepted.

Compliance and Continuous Monitoring

Cryptographic failures directly impact compliance with frameworks like PCI-DSS (requiring encryption of cardholder data), HIPAA (protecting PHI), and GDPR (mandating appropriate security for personal data). middleBrick maps findings to these frameworks, helping prioritize remediation.

To maintain ongoing security:

  • Use middleBrick's Pro plan continuous monitoring to scan APIs on a schedule (e.g., daily) and receive alerts via Slack or email if new cryptographic issues appear.
  • Integrate the GitHub Action into CI/CD to scan staging APIs before production deploy, failing the build if the Encryption score drops below a threshold (e.g., B).
  • Track score trends in the Web Dashboard to ensure improvements are sustained over time.

Example GitHub Action workflow snippet:

name: API Security Scan
on: [pull_request]
jobs:
  middlebrick:
    runs-on: ubuntu-latest
    steps:
      - uses: middlebrick/github-action@v1
        with:
          api_url: ${{ secrets.STAGING_API_URL }}
          fail_below: 'B'

This ensures cryptographic configurations are validated automatically with every change.

FAQ

Q: Does Firestore's default at-rest encryption satisfy all compliance requirements?
A: No. While Firestore encrypts data at rest with Google-managed keys, regulations like PCI-DSS often require customer-managed keys (CMK) or field-level encryption for certain data types. You must assess your specific compliance obligations and implement additional encryption if needed.

Q: How often should I scan my Firestore APIs for cryptographic failures?
A: Scan after any rule change, deployment, or quarterly at minimum. middleBrick's continuous monitoring (Pro plan) automates this, scanning on a configurable schedule and alerting on new exposures. For CI/CD-integrated workflows, scan on every PR that touches security rules or data models.