Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Firestore returns permission-denied: Missing or insufficient permissions

ErrorUncaught Error in snapshot listener: FirebaseError: [code=permission-denied]: Missing or insufficient permissions.

Short answer

Firestore rejected the request because your security rules, or App Check enforcement, did not allow it. Check four things: the rules in production mode, whether the user is signed in, whether the query matches the rule's constraints, and whether App Check is enforced without the client set up.

  • Applies to Cloud Firestore, Firebase CLI, Flutter 3.3x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the Rules tab of Firestore in the Firebase console and read the rule for the path you are touching. Then compare it with what the request sends: user, path, and query filters.

    firebase deploy --only firestore  # deploys the rules file from the project; this overwrites the rules in the console
  2. 2Match what you see, then jump to its fix

    1. 1Every client read and write fails, even for signed-in usersThe database is in production mode and denies clientsFix
    2. 2The rule contains request.auth != null and the app calls Firestore before sign-in finishesThe rule needs a signed-in user and there is noneFix
    3. 3A single document read works but a collection query failsThe query could return documents the rules do not allowFix
    4. 4The error started after you enabled enforcement for Cloud FirestoreApp Check enforcement is on but the app sends no tokenFix

What does the error look like?

  • Uncaught Error in snapshot listener: FirebaseError: [code=permission-denied]: Missing or insufficient permissions.
  • FirebaseError: [code=permission-denied]: Missing or insufficient permissions.

Cause 1 of 4

The database is in production mode and denies clients

1Check

Production mode blocks all reads and writes from mobile and web clients; server libraries are not affected. The rules would read allow read, write: if false.

cat firestore.rules

2Fix

Write rules that allow what the app needs, for example documents a user owns. Do not use allow read, write: if true, which lets anyone overwrite the whole database.

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId} {
      allow read, write: if request.auth != null && request.auth.uid == userId;
    }
  }
}

3Verify

Publish the rules and repeat the request. Updates can take up to a minute to reach new queries.

firebase deploy --only firestore

Confirmed by firebase.google.com

Did this fix it?

Cause 2 of 4

The rule needs a signed-in user and there is none

1Check

Rules that check request.auth deny any request that arrives without a signed-in user.

grep -n "request.auth" firestore.rules

2Fix

Wait until sign-in completes before the first Firestore call, for example by listening to auth state.

FirebaseAuth.instance.authStateChanges().listen((user) {
  if (user != null) {
    // safe to read or write now
  }
});

3Verify

Sign in, then repeat the request.

print(FirebaseAuth.instance.currentUser?.uid);

Confirmed by firebase.google.com

Did this fix it?

Cause 3 of 4

The query could return documents the rules do not allow

1Check

Rules are not filters: Firestore checks the whole query against the rules, and if any possible result is not allowed the entire request fails. A rule on resource.data.author fails a query with no author filter.

grep -n "resource.data" firestore.rules

2Fix

Add a where clause that matches the rule's condition.

final snap = await FirebaseFirestore.instance
    .collection('stories')
    .where('author', isEqualTo: FirebaseAuth.instance.currentUser!.uid)
    .get();

3Verify

The query returns documents without the error.

print(snap.docs.length);

Confirmed by firebase.google.com

Did this fix it?

Cause 4 of 4

App Check enforcement is on but the app sends no token

1Check

With App Check enforced, requests without a valid attestation token are blocked and show the same permission-denied message.

# Firebase console: App Check, Cloud Firestore, check whether Enforce is on

2Fix

Turn enforcement off while you finish setting App Check up in the client, then turn it back on and check the verified-request metrics.

# In the App Check section of the Firebase console, switch enforcement for Cloud Firestore off, finish the client setup, then switch it on again

3Verify

Repeat the request while enforcement is off, then again after setup with it on.

# watch App Check metrics in the Firebase console

Confirmed by firebase.google.com

Did this fix it?

How do I stop it coming back?

  • Keep firestore.rules in the repository and deploy it from there, since a CLI deploy overwrites console edits.
  • Write queries with the same filters your rules require.
  • Never use allow read, write: if true outside a throwaway project.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Mobile apps

Flutter Android: Failed to load FirebaseOptions from resource

Failed to load FirebaseOptions from resource. Check that you have defined values.xml correctly.

On Android, Firebase reads its settings from a values.xml file that the Google services Gradle plugin builds from google-services.json.

4 causes, each with a fix

Mobile apps

Xcode cannot find GoogleService-Info.plist for the Runner target

error: Build input file cannot be found: '/Users/GoogleService-Info.plist' (in target 'Runner' from project 'Runner')

Xcode's project file lists GoogleService-Info.plist as an input, but nothing exists at the path it records.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.