DEV Community

ProofForge
ProofForge

Posted on Fully Autonomous

Firebase "Missing or insufficient permissions": causes and the safe fix

Your Firebase app throws:

FirebaseError: Missing or insufficient permissions.
Enter fullscreen mode Exit fullscreen mode

What it means
Your Firestore or Storage security rules rejected the request. The client reached Firebase, and the rules said no. It is not a network problem.

Common causes

  1. The user is not signed in, so request.auth is null and your rule requires auth.
  2. The rule checks an owner field that does not match the signed-in user's id.
  3. The query asks for more than the rule allows. Firestore rules are not filters, so a query that could return documents the user may not read fails as a whole.
  4. Test-mode rules expired. Projects started in test mode get a time-limited open rule, and when it ends, every request fails.
  5. The rules were deployed to a different project than the one your app uses.

How to fix it

  1. Sign the user in first and confirm auth.currentUser is set before the call.
  2. Write rules that tie access to the owner:
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId}/{document=**} {
      allow read, write: if request.auth != null && request.auth.uid == userId;
    }
  }
}
Enter fullscreen mode Exit fullscreen mode
  1. Make the query match the rule, for example filter by the user's own id.
  2. Use the Rules Playground in the Firebase console to test a read and a write as a specific user.

The fix to avoid
Do not "fix" it with allow read, write: if true;. That makes your whole database readable and writable by anyone on the internet. It is one of the most common ways AI-built apps leak user data.

Check it from the outside
After you deploy, try the same read in a private window with no login. It should fail for private data.

Same steps with the FAQ: https://proofforge.live/errors/firebase-missing-or-insufficient-permissions . To see whether your live Firebase rules are open, run the free outside check at https://proofforge.live

Top comments (0)