← All posts
firebaseapi keysecurityvibe codingapp checkfirestore security rules

How to Fix an Exposed Firebase API Key: Step-by-Step Guide

O

OverMCP Team

If you've accidentally exposed your Firebase API key in client-side code, a public repository, or an environment file, here's the short answer: revoke the key immediately, restrict it using Firebase Console, and implement security measures like Firebase Security Rules and App Check. Firebase API keys are not secret by design — they are meant to be public — but without proper restrictions, they can lead to unauthorized database access, cost spikes, and data breaches.

What Is an Exposed Firebase API Key?

A Firebase API key (often labeled apiKey in your config) is a simple identifier that routes requests to your Firebase project. Unlike a secret key, it's not encrypted and is visible in client-side code by design. However, if an attacker obtains it, they can interact with your Firebase services — like Firestore, Realtime Database, or Authentication — unless you have security rules in place. An exposed Firebase API key in a public GitHub repo, a .env file that got committed, or even in your browser's network tab can be abused for:

  • Reading/writing to your Firestore or Realtime Database
  • Spamming your Authentication endpoints
  • Incurring unexpected billing costs
  • Exfiltrating user data
  • Step 1: Revoke the Exposed API Key

    If you've confirmed your key is exposed (e.g., in a public repo or a leak scanner), immediately revoke it:

  • Go to the Google Cloud Console.
  • Under API Keys, find the key used by Firebase (usually named "Firebase API Key" or "Web API key").
  • Click the key, then click DELETE KEY.
  • Create a new API key by clicking + CREATE CREDENTIALS > API Key.
  • Update your app with the new key (store it in a .env file for build time, but remember it will still be visible in the client).
  • Note: Revoking the key will break any existing app instances until they update to the new key. Plan accordingly.

    Step 2: Restrict the API Key

    Never leave an API key unrestricted. In the Google Cloud Console, edit your key and apply these restrictions:

  • Application restrictions: Choose "HTTP referrers (web sites)" and add your domain(s). For mobile apps, use "IP addresses" or "Android/iOS apps" with package names and SHA-1 fingerprints.
  • API restrictions: Click "Restrict key" and select only the Firebase APIs you use (e.g., Firestore, Authentication). Do not leave it unrestricted.
  • Example for a web app:

    Allowed referrers:
    https://yourdomain.com/*
    https://*.yourdomain.com/*

    Step 3: Enforce Firebase Security Rules

    Your security rules are the real gatekeepers. Even with a valid API key, an attacker cannot access your data if rules are set correctly. Update your Firestore and Realtime Database rules to require authentication and validate data structure.

    Firestore rules example:

    rules_version = '2';
    service cloud.firestore {
      match /databases/{database}/documents {
        // Only authenticated users can read/write
        match /users/{userId} {
          allow read, write: if request.auth != null && request.auth.uid == userId;
        }
        // Public data can be read by anyone, but only admins write
        match /public/{document} {
          allow read: if true;
          allow write: if request.auth != null && request.auth.token.isAdmin == true;
        }
      }
    }

    Realtime Database rules example:

    {
      "rules": {
        "users": {
          "$uid": {
            ".read": "$uid === auth.uid",
            ".write": "$uid === auth.uid"
          }
        },
        "public": {
          ".read": true,
          ".write": "auth.uid !== null && root.child('admins').child(auth.uid).exists()"
        }
      }
    }

    Step 4: Use Firebase App Check

    App Check adds an additional layer of security by verifying that requests come from your app. It prevents API key abuse even if the key is exposed.

  • In Firebase Console, go to App Check.
  • Register your app (web, Android, iOS).
  • For web, use reCAPTCHA v3 or reCAPTCHA Enterprise.
  • Enable enforcement: In App Check > APIs, toggle on enforcement for Firestore, Realtime Database, etc.
  • Now, only requests with a valid App Check token (generated by your app) will be accepted.

    Step 5: Remove the Exposed Key from Public Sources

    If your key was leaked in a public GitHub repo:

  • Delete the file from the repo history. Simply removing it in a new commit is not enough — attackers can still find it in git history.
  • Use a tool like git filter-branch or BFG Repo-Cleaner to purge the key from all commits.
  • Rotate any other secrets that were in the same file.
  • For .env files:

  • Immediately add .env to .gitignore.
  • If it was committed, use the same history-rewriting techniques.
  • Step 6: Monitor and Audit

    After fixing the exposure, monitor your Firebase project:

  • Check Usage and Billing for unusual spikes.
  • Review Authentication logs for suspicious sign-in attempts.
  • Use Security Rules Monitor (Firebase Console) to see if rules are too permissive.
  • For ongoing scanning, consider a tool like OverMCP that automatically scans your GitHub repos and deployed apps for leaked Firebase API keys and other secrets.

    Code Example: Safe Firebase Config Setup

    Instead of hardcoding your config in JavaScript, use environment variables (but remember, they still end up in the client bundle):

    // .env.local (never commit this)
    NEXT_PUBLIC_FIREBASE_API_KEY=AIzaSy...
    NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=your-project.firebaseapp.com
    
    // firebase.js (Next.js example)
    import { initializeApp } from 'firebase/app';
    
    const firebaseConfig = {
      apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
      authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
      // ...
    };
    
    const app = initializeApp(firebaseConfig);

    Even with env vars, the key is visible in the browser. That's why App Check and security rules are non-negotiable.

    FAQ

    What happens if my Firebase API key is exposed?

    An exposed Firebase API key alone is not a direct security risk if you have proper security rules and App Check enabled. However, without restrictions, attackers can access your Firebase services, leading to data theft, unauthorized operations, and unexpected bills.

    Can I keep using the same Firebase API key after exposure?

    No. You should revoke the exposed key and create a new one immediately. Then apply API key restrictions and enforce security rules and App Check to prevent future abuse.

    How do I know if my Firebase API key is exposed?

    You can search for your API key in public GitHub repositories using GitHub's code search, or use a secret scanning tool like OverMCP that automatically detects exposed keys in codebases and deployments.

    Is your app secure?

    Free scan in 30 seconds. No signup needed.

    Scan My App Free